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 "CSSValueKeywords.h"
28 #include "HTMLNames.h"
29 #include "bindings/v8/ScriptEventListener.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/html/HTMLFrameElementBase.h"
37 #include "core/html/parser/HTMLParserIdioms.h"
38 #include "core/rendering/RenderBox.h"
42 using namespace HTMLNames;
44 HTMLBodyElement::HTMLBodyElement(Document& document)
45 : HTMLElement(bodyTag, document)
47 ScriptWrappable::init(this);
50 PassRefPtrWillBeRawPtr<HTMLBodyElement> HTMLBodyElement::create(Document& document)
52 return adoptRefWillBeRefCountedGarbageCollected(new HTMLBodyElement(document));
55 HTMLBodyElement::~HTMLBodyElement()
59 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
61 if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
63 return HTMLElement::isPresentationAttribute(name);
66 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
68 if (name == backgroundAttr) {
69 String url = stripLeadingAndTrailingHTMLSpaces(value);
71 RefPtrWillBeRawPtr<CSSImageValue> imageValue = CSSImageValue::create(url, document().completeURL(url));
72 imageValue->setInitiator(localName());
73 imageValue->setReferrer(document().outgoingReferrer());
74 style->setProperty(CSSProperty(CSSPropertyBackgroundImage, imageValue.release()));
76 } else if (name == marginwidthAttr || name == leftmarginAttr) {
77 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
78 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
79 } else if (name == marginheightAttr || name == topmarginAttr) {
80 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
81 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
82 } else if (name == bgcolorAttr) {
83 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
84 } else if (name == textAttr) {
85 addHTMLColorToStyle(style, CSSPropertyColor, value);
86 } else if (name == bgpropertiesAttr) {
87 if (equalIgnoringCase(value, "fixed"))
88 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));
118 else if (name == onbeforeunloadAttr)
119 document().setWindowAttributeEventListener(EventTypeNames::beforeunload, createAttributeEventListener(document().frame(), name, value));
120 else if (name == onunloadAttr)
121 document().setWindowAttributeEventListener(EventTypeNames::unload, createAttributeEventListener(document().frame(), name, value));
122 else if (name == onpagehideAttr)
123 document().setWindowAttributeEventListener(EventTypeNames::pagehide, createAttributeEventListener(document().frame(), name, value));
124 else if (name == onpageshowAttr)
125 document().setWindowAttributeEventListener(EventTypeNames::pageshow, createAttributeEventListener(document().frame(), name, value));
126 else if (name == onpopstateAttr)
127 document().setWindowAttributeEventListener(EventTypeNames::popstate, createAttributeEventListener(document().frame(), name, value));
128 else if (name == onblurAttr)
129 document().setWindowAttributeEventListener(EventTypeNames::blur, createAttributeEventListener(document().frame(), name, value));
130 else if (name == onerrorAttr)
131 document().setWindowAttributeEventListener(EventTypeNames::error, createAttributeEventListener(document().frame(), name, value));
132 else if (name == onfocusAttr)
133 document().setWindowAttributeEventListener(EventTypeNames::focus, createAttributeEventListener(document().frame(), name, value));
134 else if (RuntimeEnabledFeatures::orientationEventEnabled() && name == onorientationchangeAttr)
135 document().setWindowAttributeEventListener(EventTypeNames::orientationchange, createAttributeEventListener(document().frame(), name, value));
136 else if (name == onhashchangeAttr)
137 document().setWindowAttributeEventListener(EventTypeNames::hashchange, createAttributeEventListener(document().frame(), name, value));
138 else if (name == onmessageAttr)
139 document().setWindowAttributeEventListener(EventTypeNames::message, createAttributeEventListener(document().frame(), name, value));
140 else if (name == onresizeAttr)
141 document().setWindowAttributeEventListener(EventTypeNames::resize, createAttributeEventListener(document().frame(), name, value));
142 else if (name == onscrollAttr)
143 document().setWindowAttributeEventListener(EventTypeNames::scroll, createAttributeEventListener(document().frame(), name, value));
144 else if (name == onselectionchangeAttr)
145 document().setAttributeEventListener(EventTypeNames::selectionchange, createAttributeEventListener(document().frame(), name, value));
146 else if (name == onstorageAttr)
147 document().setWindowAttributeEventListener(EventTypeNames::storage, createAttributeEventListener(document().frame(), name, value));
148 else if (name == ononlineAttr)
149 document().setWindowAttributeEventListener(EventTypeNames::online, createAttributeEventListener(document().frame(), name, value));
150 else if (name == onofflineAttr)
151 document().setWindowAttributeEventListener(EventTypeNames::offline, createAttributeEventListener(document().frame(), name, value));
153 HTMLElement::parseAttribute(name, value);
156 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
158 HTMLElement::insertedInto(insertionPoint);
159 return InsertionShouldCallDidNotifySubtreeInsertions;
162 void HTMLBodyElement::didNotifySubtreeInsertionsToDocument()
164 // FIXME: It's surprising this is web compatible since it means a
165 // marginwidth and marginheight attribute can magically appear on the <body>
166 // of all documents embedded through <iframe> or <frame>.
167 Element* ownerElement = document().ownerElement();
168 if (!isHTMLFrameElementBase(ownerElement))
170 HTMLFrameElementBase& ownerFrameElement = toHTMLFrameElementBase(*ownerElement);
171 int marginWidth = ownerFrameElement.marginWidth();
172 int marginHeight = ownerFrameElement.marginHeight();
173 if (marginWidth != -1)
174 setIntegralAttribute(marginwidthAttr, marginWidth);
175 if (marginHeight != -1)
176 setIntegralAttribute(marginheightAttr, marginHeight);
179 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
181 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
184 bool HTMLBodyElement::hasLegalLinkAttribute(const QualifiedName& name) const
186 return name == backgroundAttr || HTMLElement::hasLegalLinkAttribute(name);
189 const QualifiedName& HTMLBodyElement::subResourceAttributeName() const
191 return backgroundAttr;
194 bool HTMLBodyElement::supportsFocus() const
196 // This override is needed because the inherited method bails if the parent is editable.
197 // The <body> should be focusable even if <html> is editable.
198 return rendererIsEditable() || HTMLElement::supportsFocus();
201 static double adjustForZoom(int value, Document* document)
203 LocalFrame* frame = document->frame();
204 float zoomFactor = frame->pageZoomFactor();
206 return static_cast<double>(value);
207 return static_cast<double>(value) / zoomFactor;
210 // Blink, Gecko and Presto's quirks mode implementations of overflow set to the
211 // body element differ from IE's: the formers can create a scrollable area for the
212 // body element that is not the same as the root elements's one. On IE's quirks mode
213 // though, as body is the root element, body's and the root element's scrollable areas,
214 // if any, are the same.
215 // In order words, a <body> will only have an overflow clip (that differs from
216 // documentElement's) if both html and body nodes have its overflow set to either hidden,
218 // That said, Blink's {set}scroll{Top,Left} behaviors match Gecko's: even if there is a non-overflown
219 // scrollable area, scrolling should not get propagated to the viewport in neither strict
221 double HTMLBodyElement::scrollLeft()
223 Document& document = this->document();
224 document.updateLayoutIgnorePendingStylesheets();
226 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
227 RenderBox* render = renderBox();
230 if (render->hasOverflowClip())
231 return adjustForAbsoluteZoom(render->scrollLeft(), render);
232 if (!document.inQuirksMode())
236 FrameView* view = document.view();
237 return view ? adjustForZoom(view->scrollX(), &document) : 0;
240 void HTMLBodyElement::setScrollLeft(double scrollLeft)
242 Document& document = this->document();
243 document.updateLayoutIgnorePendingStylesheets();
245 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
246 RenderBox* render = renderBox();
249 if (render->hasOverflowClip()) {
250 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
251 render->setScrollLeft(static_cast<int>(scrollLeft * render->style()->effectiveZoom()));
254 if (!document.inQuirksMode())
258 LocalFrame* frame = document.frame();
261 FrameView* view = frame->view();
264 view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor()), view->scrollY()));
267 double HTMLBodyElement::scrollTop()
269 Document& document = this->document();
270 document.updateLayoutIgnorePendingStylesheets();
272 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
273 RenderBox* render = renderBox();
276 if (render->hasOverflowClip())
277 return adjustForAbsoluteZoom(render->scrollTop(), render);
278 if (!document.inQuirksMode())
282 FrameView* view = document.view();
283 return view ? adjustForZoom(view->scrollY(), &document) : 0;
286 void HTMLBodyElement::setScrollTop(double scrollTop)
288 Document& document = this->document();
289 document.updateLayoutIgnorePendingStylesheets();
291 if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
292 RenderBox* render = renderBox();
295 if (render->hasOverflowClip()) {
296 // FIXME: Investigate how are other browsers casting to int (rounding, ceiling, ...).
297 render->setScrollTop(static_cast<int>(scrollTop * render->style()->effectiveZoom()));
300 if (!document.inQuirksMode())
304 LocalFrame* frame = document.frame();
307 FrameView* view = frame->view();
310 view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor())));
313 double HTMLBodyElement::scrollHeight()
315 // Update the document's layout.
316 Document& document = this->document();
317 document.updateLayoutIgnorePendingStylesheets();
318 FrameView* view = document.view();
319 return view ? adjustForZoom(view->contentsHeight(), &document) : 0;
322 double HTMLBodyElement::scrollWidth()
324 // Update the document's layout.
325 Document& document = this->document();
326 document.updateLayoutIgnorePendingStylesheets();
327 FrameView* view = document.view();
328 return view ? adjustForZoom(view->contentsWidth(), &document) : 0;
331 } // namespace WebCore