Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLBodyElement.cpp
1 /*
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.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include "config.h"
25 #include "core/html/HTMLBodyElement.h"
26
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"
40
41 namespace blink {
42
43 using namespace HTMLNames;
44
45 inline HTMLBodyElement::HTMLBodyElement(Document& document)
46     : HTMLElement(bodyTag, document)
47 {
48     ScriptWrappable::init(this);
49 }
50
51 DEFINE_NODE_FACTORY(HTMLBodyElement)
52
53 HTMLBodyElement::~HTMLBodyElement()
54 {
55 }
56
57 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
58 {
59     if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
60         return true;
61     return HTMLElement::isPresentationAttribute(name);
62 }
63
64 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
65 {
66     if (name == backgroundAttr) {
67         String url = stripLeadingAndTrailingHTMLSpaces(value);
68         if (!url.isEmpty()) {
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()));
73         }
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);
88         }
89     } else
90         HTMLElement::collectStyleForPresentationAttribute(name, value, style);
91 }
92
93 void HTMLBodyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94 {
95     if (name == vlinkAttr || name == alinkAttr || name == linkAttr) {
96         if (value.isNull()) {
97             if (name == linkAttr)
98                 document().textLinkColors().resetLinkColor();
99             else if (name == vlinkAttr)
100                 document().textLinkColors().resetVisitedLinkColor();
101             else
102                 document().textLinkColors().resetActiveLinkColor();
103         } else {
104             RGBA32 color;
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);
110                 else
111                     document().textLinkColors().setActiveLinkColor(color);
112             }
113         }
114
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()));
154     else
155         HTMLElement::parseAttribute(name, value);
156 }
157
158 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
159 {
160     HTMLElement::insertedInto(insertionPoint);
161     return InsertionShouldCallDidNotifySubtreeInsertions;
162 }
163
164 void HTMLBodyElement::didNotifySubtreeInsertionsToDocument()
165 {
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))
171         return;
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);
179 }
180
181 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
182 {
183     return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
184 }
185
186 bool HTMLBodyElement::hasLegalLinkAttribute(const QualifiedName& name) const
187 {
188     return name == backgroundAttr || HTMLElement::hasLegalLinkAttribute(name);
189 }
190
191 const QualifiedName& HTMLBodyElement::subResourceAttributeName() const
192 {
193     return backgroundAttr;
194 }
195
196 bool HTMLBodyElement::supportsFocus() const
197 {
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();
201 }
202
203 static int adjustForZoom(int value, Document* document)
204 {
205     LocalFrame* frame = document->frame();
206     float zoomFactor = frame->pageZoomFactor();
207     if (zoomFactor == 1)
208         return value;
209     // Needed because of truncation (rather than rounding) when scaling up.
210     if (zoomFactor > 1)
211         value++;
212     return static_cast<int>(value / zoomFactor);
213 }
214
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,
222 // auto or scroll.
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
225 // or quirks modes.
226 int HTMLBodyElement::scrollLeft()
227 {
228     Document& document = this->document();
229     document.updateLayoutIgnorePendingStylesheets();
230
231     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
232         RenderBox* render = renderBox();
233         if (!render)
234             return 0;
235         if (render->hasOverflowClip())
236             return adjustForAbsoluteZoom(render->scrollLeft(), render);
237         if (!document.inQuirksMode())
238             return 0;
239     }
240
241     FrameView* view = document.view();
242     return view ? adjustForZoom(view->scrollX(), &document) : 0;
243 }
244
245 void HTMLBodyElement::setScrollLeft(int scrollLeft)
246 {
247     Document& document = this->document();
248     document.updateLayoutIgnorePendingStylesheets();
249
250     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
251         RenderBox* render = renderBox();
252         if (!render)
253             return;
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()));
257             return;
258         }
259         if (!document.inQuirksMode())
260             return;
261     }
262
263     LocalFrame* frame = document.frame();
264     if (!frame)
265         return;
266     FrameView* view = frame->view();
267     if (!view)
268         return;
269     view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor()), view->scrollY()));
270 }
271
272 int HTMLBodyElement::scrollTop()
273 {
274     Document& document = this->document();
275     document.updateLayoutIgnorePendingStylesheets();
276
277     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
278         RenderBox* render = renderBox();
279         if (!render)
280             return 0;
281         if (render->hasOverflowClip())
282             return adjustForAbsoluteZoom(render->scrollTop(), render);
283         if (!document.inQuirksMode())
284             return 0;
285     }
286
287     FrameView* view = document.view();
288     return view ? adjustForZoom(view->scrollY(), &document) : 0;
289 }
290
291 void HTMLBodyElement::setScrollTop(int scrollTop)
292 {
293     Document& document = this->document();
294     document.updateLayoutIgnorePendingStylesheets();
295
296     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
297         RenderBox* render = renderBox();
298         if (!render)
299             return;
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()));
303             return;
304         }
305         if (!document.inQuirksMode())
306             return;
307     }
308
309     LocalFrame* frame = document.frame();
310     if (!frame)
311         return;
312     FrameView* view = frame->view();
313     if (!view)
314         return;
315     view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor())));
316 }
317
318 int HTMLBodyElement::scrollHeight()
319 {
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;
325 }
326
327 int HTMLBodyElement::scrollWidth()
328 {
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;
334 }
335
336 } // namespace blink