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-2017 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 "HTMLBodyElement.h"
27 #include "CSSImageValue.h"
28 #include "CSSParser.h"
29 #include "CSSValueKeywords.h"
30 #include "DOMWindow.h"
31 #include "DOMWrapperWorld.h"
32 #include "EventNames.h"
34 #include "FrameView.h"
35 #include "HTMLFrameElement.h"
36 #include "HTMLIFrameElement.h"
37 #include "HTMLNames.h"
38 #include "HTMLParserIdioms.h"
39 #include "StyleProperties.h"
40 #include <wtf/NeverDestroyed.h>
44 using namespace HTMLNames;
46 HTMLBodyElement::HTMLBodyElement(const QualifiedName& tagName, Document& document)
47 : HTMLElement(tagName, document)
49 ASSERT(hasTagName(bodyTag));
52 bool HTMLBodyElement::isFirstBodyElementOfDocument() const
54 // By spec http://dev.w3.org/csswg/cssom-view/#the-html-body-element
55 // "The HTML body element is the first body HTML element child of the root HTML element html."
56 return document().body() == this;
59 Ref<HTMLBodyElement> HTMLBodyElement::create(Document& document)
61 return adoptRef(*new HTMLBodyElement(bodyTag, document));
64 Ref<HTMLBodyElement> HTMLBodyElement::create(const QualifiedName& tagName, Document& document)
66 return adoptRef(*new HTMLBodyElement(tagName, document));
69 HTMLBodyElement::~HTMLBodyElement()
73 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
75 if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
77 return HTMLElement::isPresentationAttribute(name);
80 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style)
82 if (name == backgroundAttr) {
83 String url = stripLeadingAndTrailingHTMLSpaces(value);
85 auto imageValue = CSSImageValue::create(document().completeURL(url));
86 imageValue.get().setInitiator(localName());
87 style.setProperty(CSSProperty(CSSPropertyBackgroundImage, WTFMove(imageValue)));
89 } else if (name == marginwidthAttr || name == leftmarginAttr) {
90 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
91 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
92 } else if (name == marginheightAttr || name == topmarginAttr) {
93 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
94 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
95 } else if (name == bgcolorAttr) {
96 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
97 } else if (name == textAttr) {
98 addHTMLColorToStyle(style, CSSPropertyColor, value);
99 } else if (name == bgpropertiesAttr) {
100 if (equalLettersIgnoringASCIICase(value, "fixed"))
101 addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundAttachment, CSSValueFixed);
103 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
106 HTMLElement::EventHandlerNameMap HTMLBodyElement::createWindowEventHandlerNameMap()
108 static const QualifiedName* const table[] = {
116 &onlanguagechangeAttr,
121 &onorientationchangeAttr,
129 &onwebkitmouseforcechangedAttr,
130 &onwebkitmouseforcedownAttr,
131 &onwebkitmouseforceupAttr,
132 &onwebkitmouseforcewillbeginAttr,
133 &onwebkitwillrevealbottomAttr,
134 &onwebkitwillrevealleftAttr,
135 &onwebkitwillrevealrightAttr,
136 &onwebkitwillrevealtopAttr,
139 EventHandlerNameMap map;
140 populateEventHandlerNameMap(map, table);
144 const AtomicString& HTMLBodyElement::eventNameForWindowEventHandlerAttribute(const QualifiedName& attributeName)
146 static NeverDestroyed<EventHandlerNameMap> map = createWindowEventHandlerNameMap();
147 return eventNameForEventHandlerAttribute(attributeName, map.get());
150 void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
152 if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
153 if (value.isNull()) {
154 if (name == linkAttr)
155 document().resetLinkColor();
156 else if (name == vlinkAttr)
157 document().resetVisitedLinkColor();
159 document().resetActiveLinkColor();
161 Color color = CSSParser::parseColor(value, !document().inQuirksMode());
162 if (color.isValid()) {
163 if (name == linkAttr)
164 document().setLinkColor(color);
165 else if (name == vlinkAttr)
166 document().setVisitedLinkColor(color);
168 document().setActiveLinkColor(color);
172 invalidateStyleForSubtree();
176 if (name == onselectionchangeAttr) {
177 document().setAttributeEventListener(eventNames().selectionchangeEvent, name, value, mainThreadNormalWorld());
181 auto& eventName = eventNameForWindowEventHandlerAttribute(name);
182 if (!eventName.isNull()) {
183 document().setWindowAttributeEventListener(eventName, name, value, mainThreadNormalWorld());
187 HTMLElement::parseAttribute(name, value);
190 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode& insertionPoint)
192 HTMLElement::insertedInto(insertionPoint);
193 if (!insertionPoint.isConnected())
194 return InsertionDone;
196 // FIXME: It's surprising this is web compatible since it means a marginwidth and marginheight attribute can
197 // magically appear on the <body> of all documents embedded through <iframe> or <frame>.
198 // FIXME: Perhaps this code should be in attach() instead of here.
199 auto* ownerElement = document().ownerElement();
200 if (!is<HTMLFrameElementBase>(ownerElement))
201 return InsertionDone;
203 return InsertionShouldCallFinishedInsertingSubtree;
206 void HTMLBodyElement::finishedInsertingSubtree()
208 auto* ownerElement = document().ownerElement();
209 RELEASE_ASSERT(is<HTMLFrameElementBase>(ownerElement));
210 auto& ownerFrameElement = downcast<HTMLFrameElementBase>(*ownerElement);
212 // Read values from the owner before setting any attributes, since setting an attribute can run arbitrary
213 // JavaScript, which might delete the owner element.
214 int marginWidth = ownerFrameElement.marginWidth();
215 int marginHeight = ownerFrameElement.marginHeight();
217 if (marginWidth != -1)
218 setIntegralAttribute(marginwidthAttr, marginWidth);
219 if (marginHeight != -1)
220 setIntegralAttribute(marginheightAttr, marginHeight);
223 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
225 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
228 bool HTMLBodyElement::supportsFocus() const
230 return hasEditableStyle() || HTMLElement::supportsFocus();
233 static int adjustForZoom(int value, const Frame& frame)
235 double zoomFactor = frame.pageZoomFactor() * frame.frameScaleFactor();
238 // Needed because of truncation (rather than rounding) when scaling up.
241 return static_cast<int>(value / zoomFactor);
244 int HTMLBodyElement::scrollLeft()
246 if (isFirstBodyElementOfDocument()) {
247 document().updateLayoutIgnorePendingStylesheets();
248 Frame* frame = document().frame();
251 FrameView* view = frame->view();
254 return adjustForZoom(view->contentsScrollPosition().x(), *frame);
256 return HTMLElement::scrollLeft();
259 void HTMLBodyElement::setScrollLeft(int scrollLeft)
261 if (isFirstBodyElementOfDocument()) {
262 document().updateLayoutIgnorePendingStylesheets();
263 Frame* frame = document().frame();
266 FrameView* view = frame->view();
269 view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor() * frame->frameScaleFactor()), view->scrollY()));
271 HTMLElement::setScrollLeft(scrollLeft);
274 int HTMLBodyElement::scrollTop()
276 if (isFirstBodyElementOfDocument()) {
277 document().updateLayoutIgnorePendingStylesheets();
278 Frame* frame = document().frame();
281 FrameView* view = frame->view();
284 return adjustForZoom(view->contentsScrollPosition().y(), *frame);
286 return HTMLElement::scrollTop();
289 void HTMLBodyElement::setScrollTop(int scrollTop)
291 if (isFirstBodyElementOfDocument()) {
292 document().updateLayoutIgnorePendingStylesheets();
293 Frame* frame = document().frame();
296 FrameView* view = frame->view();
299 view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor() * frame->frameScaleFactor())));
301 return HTMLElement::setScrollTop(scrollTop);
304 void HTMLBodyElement::scrollTo(const ScrollToOptions& options)
306 if (isFirstBodyElementOfDocument()) {
307 // If the element is the HTML body element, document is in quirks mode, and the element is not potentially scrollable,
308 // invoke scroll() on window with options as the only argument, and terminate these steps.
309 // Note that WebKit always uses quirks mode document scrolling behavior. See Document::scrollingElement().
310 // FIXME: Scrolling an independently scrollable body is broken: webkit.org/b/161612.
311 auto* window = document().domWindow();
315 window->scrollTo(options);
318 return HTMLElement::scrollTo(options);
321 int HTMLBodyElement::scrollHeight()
323 if (isFirstBodyElementOfDocument()) {
324 // Update the document's layout.
325 document().updateLayoutIgnorePendingStylesheets();
326 Frame* frame = document().frame();
329 FrameView* view = frame->view();
332 return adjustForZoom(view->contentsHeight(), *frame);
334 return HTMLElement::scrollHeight();
337 int HTMLBodyElement::scrollWidth()
339 if (isFirstBodyElementOfDocument()) {
340 // Update the document's layout.
341 document().updateLayoutIgnorePendingStylesheets();
342 Frame* frame = document().frame();
345 FrameView* view = frame->view();
348 return adjustForZoom(view->contentsWidth(), *frame);
350 return HTMLElement::scrollWidth();
353 void HTMLBodyElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
355 HTMLElement::addSubresourceAttributeURLs(urls);
357 addSubresourceURL(urls, document().completeURL(attributeWithoutSynchronization(backgroundAttr)));
360 } // namespace WebCore