2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Simon Hausmann (hausmann@kde.org)
5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #include "core/html/HTMLBodyElement.h"
27 #include "bindings/core/v8/ScriptEventListener.h"
28 #include "core/CSSValueKeywords.h"
29 #include "core/HTMLNames.h"
30 #include "core/css/CSSImageValue.h"
31 #include "core/css/parser/BisonCSSParser.h"
32 #include "core/css/StylePropertySet.h"
33 #include "core/dom/Attribute.h"
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/frame/UseCounter.h"
37 #include "core/html/HTMLFrameElementBase.h"
38 #include "core/html/parser/HTMLParserIdioms.h"
39 #include "core/rendering/RenderBox.h"
43 using namespace HTMLNames;
45 inline HTMLBodyElement::HTMLBodyElement(Document& document)
46 : HTMLElement(bodyTag, document)
48 ScriptWrappable::init(this);
51 DEFINE_NODE_FACTORY(HTMLBodyElement)
53 HTMLBodyElement::~HTMLBodyElement()
57 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
59 if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
61 return HTMLElement::isPresentationAttribute(name);
64 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
66 if (name == backgroundAttr) {
67 String url = stripLeadingAndTrailingHTMLSpaces(value);
69 RefPtrWillBeRawPtr<CSSImageValue> imageValue = CSSImageValue::create(url, document().completeURL(url));
70 imageValue->setInitiator(localName());
71 imageValue->setReferrer(Referrer(document().outgoingReferrer(), document().referrerPolicy()));
72 style->setProperty(CSSProperty(CSSPropertyBackgroundImage, imageValue.release()));
74 } else if (name == marginwidthAttr || name == leftmarginAttr) {
75 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
76 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
77 } else if (name == marginheightAttr || name == topmarginAttr) {
78 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
79 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
80 } else if (name == bgcolorAttr) {
81 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
82 } else if (name == textAttr) {
83 addHTMLColorToStyle(style, CSSPropertyColor, value);
84 } else if (name == bgpropertiesAttr) {
85 if (equalIgnoringCase(value, "fixed")) {
86 UseCounter::count(document(), UseCounter::BgPropertiesFixed);
87 addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundAttachment, CSSValueFixed);
90 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
93 void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
95 if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
98 document().textLinkColors().resetLinkColor();
99 else if (name == vlinkAttr)
100 document().textLinkColors().resetVisitedLinkColor();
102 document().textLinkColors().resetActiveLinkColor();
105 if (BisonCSSParser::parseColor(color, value, !document().inQuirksMode())) {
106 if (name == linkAttr)
107 document().textLinkColors().setLinkColor(color);
108 else if (name == vlinkAttr)
109 document().textLinkColors().setVisitedLinkColor(color);
111 document().textLinkColors().setActiveLinkColor(color);
115 setNeedsStyleRecalc(SubtreeStyleChange);
116 } else if (name == onloadAttr)
117 document().setWindowAttributeEventListener(EventTypeNames::load, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
118 else if (name == onbeforeunloadAttr)
119 document().setWindowAttributeEventListener(EventTypeNames::beforeunload, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
120 else if (name == onunloadAttr)
121 document().setWindowAttributeEventListener(EventTypeNames::unload, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
122 else if (name == onpagehideAttr)
123 document().setWindowAttributeEventListener(EventTypeNames::pagehide, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
124 else if (name == onpageshowAttr)
125 document().setWindowAttributeEventListener(EventTypeNames::pageshow, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
126 else if (name == onpopstateAttr)
127 document().setWindowAttributeEventListener(EventTypeNames::popstate, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
128 else if (name == onblurAttr)
129 document().setWindowAttributeEventListener(EventTypeNames::blur, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
130 else if (name == onerrorAttr)
131 document().setWindowAttributeEventListener(EventTypeNames::error, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
132 else if (name == onfocusAttr)
133 document().setWindowAttributeEventListener(EventTypeNames::focus, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
134 else if (RuntimeEnabledFeatures::orientationEventEnabled() && name == onorientationchangeAttr)
135 document().setWindowAttributeEventListener(EventTypeNames::orientationchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
136 else if (name == onhashchangeAttr)
137 document().setWindowAttributeEventListener(EventTypeNames::hashchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
138 else if (name == onmessageAttr)
139 document().setWindowAttributeEventListener(EventTypeNames::message, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
140 else if (name == onresizeAttr)
141 document().setWindowAttributeEventListener(EventTypeNames::resize, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
142 else if (name == onscrollAttr)
143 document().setWindowAttributeEventListener(EventTypeNames::scroll, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
144 else if (name == onselectionchangeAttr)
145 document().setAttributeEventListener(EventTypeNames::selectionchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
146 else if (name == onstorageAttr)
147 document().setWindowAttributeEventListener(EventTypeNames::storage, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
148 else if (name == ononlineAttr)
149 document().setWindowAttributeEventListener(EventTypeNames::online, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
150 else if (name == onofflineAttr)
151 document().setWindowAttributeEventListener(EventTypeNames::offline, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
152 else if (name == onlanguagechangeAttr)
153 document().setWindowAttributeEventListener(EventTypeNames::languagechange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
155 HTMLElement::parseAttribute(name, value);
158 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
160 HTMLElement::insertedInto(insertionPoint);
161 return InsertionShouldCallDidNotifySubtreeInsertions;
164 void HTMLBodyElement::didNotifySubtreeInsertionsToDocument()
166 // FIXME: It's surprising this is web compatible since it means a
167 // marginwidth and marginheight attribute can magically appear on the <body>
168 // of all documents embedded through <iframe> or <frame>.
169 Element* ownerElement = document().ownerElement();
170 if (!isHTMLFrameElementBase(ownerElement))
172 HTMLFrameElementBase& ownerFrameElement = toHTMLFrameElementBase(*ownerElement);
173 int marginWidth = ownerFrameElement.marginWidth();
174 int marginHeight = ownerFrameElement.marginHeight();
175 if (marginWidth != -1)
176 setIntegralAttribute(marginwidthAttr, marginWidth);
177 if (marginHeight != -1)
178 setIntegralAttribute(marginheightAttr, marginHeight);
181 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
183 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
186 bool HTMLBodyElement::hasLegalLinkAttribute(const QualifiedName& name) const
188 return name == backgroundAttr || HTMLElement::hasLegalLinkAttribute(name);
191 const QualifiedName& HTMLBodyElement::subResourceAttributeName() const
193 return backgroundAttr;
196 bool HTMLBodyElement::supportsFocus() const
198 // This override is needed because the inherited method bails if the parent is editable.
199 // The <body> should be focusable even if <html> is editable.
200 return hasEditableStyle() || HTMLElement::supportsFocus();
203 static int adjustForZoom(int value, Document* document)
205 LocalFrame* frame = document->frame();
206 float zoomFactor = frame->pageZoomFactor();
209 // Needed because of truncation (rather than rounding) when scaling up.
212 return static_cast<int>(value / zoomFactor);
215 // Blink, Gecko and Presto's quirks mode implementations of overflow set to the
216 // body element differ from IE's: the formers can create a scrollable area for the
217 // body element that is not the same as the root elements's one. On IE's quirks mode
218 // though, as body is the root element, body's and the root element's scrollable areas,
219 // if any, are the same.
220 // In order words, a <body> will only have an overflow clip (that differs from
221 // documentElement's) if both html and body nodes have its overflow set to either hidden,
223 // That said, Blink's {set}scroll{Top,Left} behaviors match Gecko's: even if there is a non-overflown
224 // scrollable area, scrolling should not get propagated to the viewport in neither strict
226 int HTMLBodyElement::scrollLeft()
228 Document& document = this->document();
229 document.updateLayoutIgnorePendingStylesheets();
231 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
232 RenderBox* render = renderBox();
235 if (render->hasOverflowClip())
236 return adjustForAbsoluteZoom(render->scrollLeft(), render);
237 if (!document.inQuirksMode())
241 FrameView* view = document.view();
242 return view ? adjustForZoom(view->scrollX(), &document) : 0;
245 void HTMLBodyElement::setScrollLeft(int scrollLeft)
247 Document& document = this->document();
248 document.updateLayoutIgnorePendingStylesheets();
250 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
251 RenderBox* render = renderBox();
254 if (render->hasOverflowClip()) {
255 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
256 render->setScrollLeft(static_cast<int>(scrollLeft * render->style()->effectiveZoom()));
259 if (!document.inQuirksMode())
263 LocalFrame* frame = document.frame();
266 FrameView* view = frame->view();
269 view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor()), view->scrollY()));
272 int HTMLBodyElement::scrollTop()
274 Document& document = this->document();
275 document.updateLayoutIgnorePendingStylesheets();
277 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
278 RenderBox* render = renderBox();
281 if (render->hasOverflowClip())
282 return adjustForAbsoluteZoom(render->scrollTop(), render);
283 if (!document.inQuirksMode())
287 FrameView* view = document.view();
288 return view ? adjustForZoom(view->scrollY(), &document) : 0;
291 void HTMLBodyElement::setScrollTop(int scrollTop)
293 Document& document = this->document();
294 document.updateLayoutIgnorePendingStylesheets();
296 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
297 RenderBox* render = renderBox();
300 if (render->hasOverflowClip()) {
301 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
302 render->setScrollTop(static_cast<int>(scrollTop * render->style()->effectiveZoom()));
305 if (!document.inQuirksMode())
309 LocalFrame* frame = document.frame();
312 FrameView* view = frame->view();
315 view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor())));
318 int HTMLBodyElement::scrollHeight()
320 // Update the document's layout.
321 Document& document = this->document();
322 document.updateLayoutIgnorePendingStylesheets();
323 FrameView* view = document.view();
324 return view ? adjustForZoom(view->contentsHeight(), &document) : 0;
327 int HTMLBodyElement::scrollWidth()
329 // Update the document's layout.
330 Document& document = this->document();
331 document.updateLayoutIgnorePendingStylesheets();
332 FrameView* view = document.view();
333 return view ? adjustForZoom(view->contentsWidth(), &document) : 0;