1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Simon Hausmann (hausmann@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "HTMLBodyElement.h"
#include "Attribute.h"
#include "CSSImageValue.h"
#include "CSSParser.h"
#include "CSSValueKeywords.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameView.h"
#include "HTMLFrameElementBase.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "Page.h"
#include "ScriptEventListener.h"
#include "StylePropertySet.h"
namespace WebCore {
using namespace HTMLNames;
HTMLBodyElement::HTMLBodyElement(const QualifiedName& tagName, Document* document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(bodyTag));
}
PassRefPtr<HTMLBodyElement> HTMLBodyElement::create(Document* document)
{
return adoptRef(new HTMLBodyElement(bodyTag, document));
}
PassRefPtr<HTMLBodyElement> HTMLBodyElement::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new HTMLBodyElement(tagName, document));
}
HTMLBodyElement::~HTMLBodyElement()
{
}
bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
{
if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
return true;
return HTMLElement::isPresentationAttribute(name);
}
void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
{
if (name == backgroundAttr) {
String url = stripLeadingAndTrailingHTMLSpaces(value);
if (!url.isEmpty()) {
RefPtr<CSSImageValue> imageValue = CSSImageValue::create(document()->completeURL(url).string());
imageValue->setInitiator(localName());
style->setProperty(CSSProperty(CSSPropertyBackgroundImage, imageValue.release()));
}
} else if (name == marginwidthAttr || name == leftmarginAttr) {
addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
} else if (name == marginheightAttr || name == topmarginAttr) {
addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
} else if (name == bgcolorAttr) {
addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
} else if (name == textAttr) {
addHTMLColorToStyle(style, CSSPropertyColor, value);
} else if (name == bgpropertiesAttr) {
if (equalIgnoringCase(value, "fixed"))
addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundAttachment, CSSValueFixed);
} else
HTMLElement::collectStyleForPresentationAttribute(name, value, style);
}
void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
if (value.isNull()) {
if (name == linkAttr)
document()->resetLinkColor();
else if (name == vlinkAttr)
document()->resetVisitedLinkColor();
else
document()->resetActiveLinkColor();
} else {
RGBA32 color;
if (CSSParser::parseColor(color, value, !document()->inQuirksMode())) {
if (name == linkAttr)
document()->setLinkColor(color);
else if (name == vlinkAttr)
document()->setVisitedLinkColor(color);
else
document()->setActiveLinkColor(color);
}
}
setNeedsStyleRecalc();
} else if (name == onloadAttr)
document()->setWindowAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onbeforeunloadAttr)
document()->setWindowAttributeEventListener(eventNames().beforeunloadEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onunloadAttr)
document()->setWindowAttributeEventListener(eventNames().unloadEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onpagehideAttr)
document()->setWindowAttributeEventListener(eventNames().pagehideEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onpageshowAttr)
document()->setWindowAttributeEventListener(eventNames().pageshowEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onpopstateAttr)
document()->setWindowAttributeEventListener(eventNames().popstateEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onblurAttr)
document()->setWindowAttributeEventListener(eventNames().blurEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onfocusAttr)
document()->setWindowAttributeEventListener(eventNames().focusEvent, createAttributeEventListener(document()->frame(), name, value));
#if ENABLE(ORIENTATION_EVENTS)
else if (name == onorientationchangeAttr)
document()->setWindowAttributeEventListener(eventNames().orientationchangeEvent, createAttributeEventListener(document()->frame(), name, value));
#endif
else if (name == onhashchangeAttr)
document()->setWindowAttributeEventListener(eventNames().hashchangeEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onresizeAttr)
document()->setWindowAttributeEventListener(eventNames().resizeEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onscrollAttr)
document()->setWindowAttributeEventListener(eventNames().scrollEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onselectionchangeAttr)
document()->setAttributeEventListener(eventNames().selectionchangeEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onstorageAttr)
document()->setWindowAttributeEventListener(eventNames().storageEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == ononlineAttr)
document()->setWindowAttributeEventListener(eventNames().onlineEvent, createAttributeEventListener(document()->frame(), name, value));
else if (name == onofflineAttr)
document()->setWindowAttributeEventListener(eventNames().offlineEvent, createAttributeEventListener(document()->frame(), name, value));
else
HTMLElement::parseAttribute(name, value);
}
Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
{
HTMLElement::insertedInto(insertionPoint);
if (insertionPoint->inDocument())
return InsertionShouldCallDidNotifySubtreeInsertions;
return InsertionDone;
}
void HTMLBodyElement::didNotifySubtreeInsertions(ContainerNode* insertionPoint)
{
ASSERT_UNUSED(insertionPoint, insertionPoint->inDocument());
ASSERT(document());
// FIXME: Perhaps this code should be in attach() instead of here.
Element* ownerElement = document()->ownerElement();
if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
HTMLFrameElementBase* ownerFrameElement = toHTMLFrameElementBase(ownerElement);
int marginWidth = ownerFrameElement->marginWidth();
if (marginWidth != -1)
setAttribute(marginwidthAttr, String::number(marginWidth));
int marginHeight = ownerFrameElement->marginHeight();
if (marginHeight != -1)
setAttribute(marginheightAttr, String::number(marginHeight));
}
// FIXME: This call to scheduleRelayout should not be needed here.
// But without it we hang during WebKit tests; need to fix that and remove this.
if (FrameView* view = document()->view())
view->scheduleRelayout();
}
bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
}
bool HTMLBodyElement::supportsFocus() const
{
return rendererIsEditable() || HTMLElement::supportsFocus();
}
String HTMLBodyElement::aLink() const
{
return getAttribute(alinkAttr);
}
void HTMLBodyElement::setALink(const String& value)
{
setAttribute(alinkAttr, value);
}
String HTMLBodyElement::bgColor() const
{
return getAttribute(bgcolorAttr);
}
void HTMLBodyElement::setBgColor(const String& value)
{
setAttribute(bgcolorAttr, value);
}
String HTMLBodyElement::link() const
{
return getAttribute(linkAttr);
}
void HTMLBodyElement::setLink(const String& value)
{
setAttribute(linkAttr, value);
}
String HTMLBodyElement::text() const
{
return getAttribute(textAttr);
}
void HTMLBodyElement::setText(const String& value)
{
setAttribute(textAttr, value);
}
String HTMLBodyElement::vLink() const
{
return getAttribute(vlinkAttr);
}
void HTMLBodyElement::setVLink(const String& value)
{
setAttribute(vlinkAttr, value);
}
static int adjustForZoom(int value, Document* document)
{
Frame* frame = document->frame();
float zoomFactor = frame->pageZoomFactor() * frame->frameScaleFactor();
if (zoomFactor == 1)
return value;
// Needed because of truncation (rather than rounding) when scaling up.
if (zoomFactor > 1)
value++;
return static_cast<int>(value / zoomFactor);
}
int HTMLBodyElement::scrollLeft()
{
// Update the document's layout.
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
FrameView* view = document->view();
return view ? adjustForZoom(view->scrollX(), document) : 0;
}
void HTMLBodyElement::setScrollLeft(int scrollLeft)
{
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
Frame* frame = document->frame();
if (!frame)
return;
FrameView* view = frame->view();
if (!view)
return;
view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor() * frame->frameScaleFactor()), view->scrollY()));
}
int HTMLBodyElement::scrollTop()
{
// Update the document's layout.
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
FrameView* view = document->view();
return view ? adjustForZoom(view->scrollY(), document) : 0;
}
void HTMLBodyElement::setScrollTop(int scrollTop)
{
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
Frame* frame = document->frame();
if (!frame)
return;
FrameView* view = frame->view();
if (!view)
return;
view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor() * frame->frameScaleFactor())));
}
int HTMLBodyElement::scrollHeight()
{
// Update the document's layout.
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
FrameView* view = document->view();
return view ? adjustForZoom(view->contentsHeight(), document) : 0;
}
int HTMLBodyElement::scrollWidth()
{
// Update the document's layout.
Document* document = this->document();
document->updateLayoutIgnorePendingStylesheets();
FrameView* view = document->view();
return view ? adjustForZoom(view->contentsWidth(), document) : 0;
}
void HTMLBodyElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
{
HTMLElement::addSubresourceAttributeURLs(urls);
addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));
}
} // namespace WebCore
|