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/StylePropertySet.h"
32 #include "core/css/parser/CSSParser.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)
50 DEFINE_NODE_FACTORY(HTMLBodyElement)
52 HTMLBodyElement::~HTMLBodyElement()
56 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
58 if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
60 return HTMLElement::isPresentationAttribute(name);
63 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
65 if (name == backgroundAttr) {
66 String url = stripLeadingAndTrailingHTMLSpaces(value);
68 RefPtrWillBeRawPtr<CSSImageValue> imageValue = CSSImageValue::create(url, document().completeURL(url));
69 imageValue->setInitiator(localName());
70 imageValue->setReferrer(Referrer(document().outgoingReferrer(), document().referrerPolicy()));
71 style->setProperty(CSSProperty(CSSPropertyBackgroundImage, imageValue.release()));
73 } else if (name == marginwidthAttr || name == leftmarginAttr) {
74 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
75 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
76 } else if (name == marginheightAttr || name == topmarginAttr) {
77 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
78 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
79 } else if (name == bgcolorAttr) {
80 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
81 } else if (name == textAttr) {
82 addHTMLColorToStyle(style, CSSPropertyColor, value);
83 } else if (name == bgpropertiesAttr) {
84 if (equalIgnoringCase(value, "fixed")) {
85 UseCounter::count(document(), UseCounter::BgPropertiesFixed);
86 addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundAttachment, CSSValueFixed);
89 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
92 void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94 if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
97 document().textLinkColors().resetLinkColor();
98 else if (name == vlinkAttr)
99 document().textLinkColors().resetVisitedLinkColor();
101 document().textLinkColors().resetActiveLinkColor();
104 if (CSSParser::parseColor(color, value, !document().inQuirksMode())) {
105 if (name == linkAttr)
106 document().textLinkColors().setLinkColor(color);
107 else if (name == vlinkAttr)
108 document().textLinkColors().setVisitedLinkColor(color);
110 document().textLinkColors().setActiveLinkColor(color);
114 setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::LinkColorChange));
115 } else if (name == onloadAttr)
116 document().setWindowAttributeEventListener(EventTypeNames::load, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
117 else if (name == onbeforeunloadAttr)
118 document().setWindowAttributeEventListener(EventTypeNames::beforeunload, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
119 else if (name == onunloadAttr)
120 document().setWindowAttributeEventListener(EventTypeNames::unload, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
121 else if (name == onpagehideAttr)
122 document().setWindowAttributeEventListener(EventTypeNames::pagehide, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
123 else if (name == onpageshowAttr)
124 document().setWindowAttributeEventListener(EventTypeNames::pageshow, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
125 else if (name == onpopstateAttr)
126 document().setWindowAttributeEventListener(EventTypeNames::popstate, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
127 else if (name == onblurAttr)
128 document().setWindowAttributeEventListener(EventTypeNames::blur, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
129 else if (name == onerrorAttr)
130 document().setWindowAttributeEventListener(EventTypeNames::error, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
131 else if (name == onfocusAttr)
132 document().setWindowAttributeEventListener(EventTypeNames::focus, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
133 else if (RuntimeEnabledFeatures::orientationEventEnabled() && name == onorientationchangeAttr)
134 document().setWindowAttributeEventListener(EventTypeNames::orientationchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
135 else if (name == onhashchangeAttr)
136 document().setWindowAttributeEventListener(EventTypeNames::hashchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
137 else if (name == onmessageAttr)
138 document().setWindowAttributeEventListener(EventTypeNames::message, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
139 else if (name == onresizeAttr)
140 document().setWindowAttributeEventListener(EventTypeNames::resize, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
141 else if (name == onscrollAttr)
142 document().setWindowAttributeEventListener(EventTypeNames::scroll, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
143 else if (name == onselectionchangeAttr)
144 document().setAttributeEventListener(EventTypeNames::selectionchange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
145 else if (name == onstorageAttr)
146 document().setWindowAttributeEventListener(EventTypeNames::storage, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
147 else if (name == ononlineAttr)
148 document().setWindowAttributeEventListener(EventTypeNames::online, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
149 else if (name == onofflineAttr)
150 document().setWindowAttributeEventListener(EventTypeNames::offline, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
151 else if (name == onlanguagechangeAttr)
152 document().setWindowAttributeEventListener(EventTypeNames::languagechange, createAttributeEventListener(document().frame(), name, value, eventParameterName()));
154 HTMLElement::parseAttribute(name, value);
157 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
159 HTMLElement::insertedInto(insertionPoint);
160 return InsertionShouldCallDidNotifySubtreeInsertions;
163 void HTMLBodyElement::didNotifySubtreeInsertionsToDocument()
165 // FIXME: It's surprising this is web compatible since it means a
166 // marginwidth and marginheight attribute can magically appear on the <body>
167 // of all documents embedded through <iframe> or <frame>.
168 HTMLFrameOwnerElement* ownerElement = document().ownerElement();
169 if (!isHTMLFrameElementBase(ownerElement))
171 HTMLFrameElementBase& ownerFrameElement = toHTMLFrameElementBase(*ownerElement);
172 int marginWidth = ownerFrameElement.marginWidth();
173 int marginHeight = ownerFrameElement.marginHeight();
174 if (marginWidth != -1)
175 setIntegralAttribute(marginwidthAttr, marginWidth);
176 if (marginHeight != -1)
177 setIntegralAttribute(marginheightAttr, marginHeight);
180 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
182 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
185 bool HTMLBodyElement::hasLegalLinkAttribute(const QualifiedName& name) const
187 return name == backgroundAttr || HTMLElement::hasLegalLinkAttribute(name);
190 const QualifiedName& HTMLBodyElement::subResourceAttributeName() const
192 return backgroundAttr;
195 bool HTMLBodyElement::supportsFocus() const
197 // This override is needed because the inherited method bails if the parent is editable.
198 // The <body> should be focusable even if <html> is editable.
199 return hasEditableStyle() || HTMLElement::supportsFocus();
202 static int adjustForZoom(int value, Document* document)
204 LocalFrame* frame = document->frame();
205 float zoomFactor = frame->pageZoomFactor();
208 // Needed because of truncation (rather than rounding) when scaling up.
211 return static_cast<int>(value / zoomFactor);
214 // Blink, Gecko and Presto's quirks mode implementations of overflow set to the
215 // body element differ from IE's: the formers can create a scrollable area for the
216 // body element that is not the same as the root elements's one. On IE's quirks mode
217 // though, as body is the root element, body's and the root element's scrollable areas,
218 // if any, are the same.
219 // In order words, a <body> will only have an overflow clip (that differs from
220 // documentElement's) if both html and body nodes have its overflow set to either hidden,
222 // That said, Blink's {set}scroll{Top,Left} behaviors match Gecko's: even if there is a non-overflown
223 // scrollable area, scrolling should not get propagated to the viewport in neither strict
225 double HTMLBodyElement::scrollLeft()
227 Document& document = this->document();
228 document.updateLayoutIgnorePendingStylesheets();
230 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
231 RenderBox* render = renderBox();
234 if (render->hasOverflowClip())
235 return adjustScrollForAbsoluteZoom(render->scrollLeft(), *render);
236 if (!document.inQuirksMode())
240 if (FrameView* view = document.view())
241 return adjustScrollForAbsoluteZoom(view->scrollX(), document.frame()->pageZoomFactor());
245 void HTMLBodyElement::setScrollLeft(double scrollLeft)
247 Document& document = this->document();
248 document.updateLayoutIgnorePendingStylesheets();
250 if (std::isnan(scrollLeft))
253 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
254 RenderBox* render = renderBox();
257 if (render->hasOverflowClip()) {
258 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
259 render->setScrollLeft(static_cast<int>(scrollLeft * render->style()->effectiveZoom()));
262 if (!document.inQuirksMode())
266 LocalFrame* frame = document.frame();
269 FrameView* view = frame->view();
272 view->setScrollPosition(DoublePoint(scrollLeft * frame->pageZoomFactor(), view->scrollY()));
275 double HTMLBodyElement::scrollTop()
277 Document& document = this->document();
278 document.updateLayoutIgnorePendingStylesheets();
280 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
281 RenderBox* render = renderBox();
284 if (render->hasOverflowClip())
285 return adjustLayoutUnitForAbsoluteZoom(render->scrollTop(), *render);
286 if (!document.inQuirksMode())
290 if (FrameView* view = document.view())
291 return adjustScrollForAbsoluteZoom(view->scrollY(), document.frame()->pageZoomFactor());
295 void HTMLBodyElement::setScrollTop(double scrollTop)
297 Document& document = this->document();
298 document.updateLayoutIgnorePendingStylesheets();
300 if (std::isnan(scrollTop))
303 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
304 RenderBox* render = renderBox();
307 if (render->hasOverflowClip()) {
308 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
309 render->setScrollTop(static_cast<int>(scrollTop * render->style()->effectiveZoom()));
312 if (!document.inQuirksMode())
316 LocalFrame* frame = document.frame();
319 FrameView* view = frame->view();
322 view->setScrollPosition(DoublePoint(view->scrollX(), scrollTop * frame->pageZoomFactor()));
325 int HTMLBodyElement::scrollHeight()
327 // Update the document's layout.
328 Document& document = this->document();
329 document.updateLayoutIgnorePendingStylesheets();
330 FrameView* view = document.view();
331 return view ? adjustForZoom(view->contentsHeight(), &document) : 0;
334 int HTMLBodyElement::scrollWidth()
336 // Update the document's layout.
337 Document& document = this->document();
338 document.updateLayoutIgnorePendingStylesheets();
339 FrameView* view = document.view();
340 return view ? adjustForZoom(view->contentsWidth(), &document) : 0;