Upstream version 7.36.149.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 "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"
39
40 namespace WebCore {
41
42 using namespace HTMLNames;
43
44 HTMLBodyElement::HTMLBodyElement(Document& document)
45     : HTMLElement(bodyTag, document)
46 {
47     ScriptWrappable::init(this);
48 }
49
50 PassRefPtrWillBeRawPtr<HTMLBodyElement> HTMLBodyElement::create(Document& document)
51 {
52     return adoptRefWillBeRefCountedGarbageCollected(new HTMLBodyElement(document));
53 }
54
55 HTMLBodyElement::~HTMLBodyElement()
56 {
57 }
58
59 bool HTMLBodyElement::isPresentationAttribute(const QualifiedName& name) const
60 {
61     if (name == backgroundAttr || name == marginwidthAttr || name == leftmarginAttr || name == marginheightAttr || name == topmarginAttr || name == bgcolorAttr || name == textAttr || name == bgpropertiesAttr)
62         return true;
63     return HTMLElement::isPresentationAttribute(name);
64 }
65
66 void HTMLBodyElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
67 {
68     if (name == backgroundAttr) {
69         String url = stripLeadingAndTrailingHTMLSpaces(value);
70         if (!url.isEmpty()) {
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()));
75         }
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);
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));
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));
152     else
153         HTMLElement::parseAttribute(name, value);
154 }
155
156 Node::InsertionNotificationRequest HTMLBodyElement::insertedInto(ContainerNode* insertionPoint)
157 {
158     HTMLElement::insertedInto(insertionPoint);
159     return InsertionShouldCallDidNotifySubtreeInsertions;
160 }
161
162 void HTMLBodyElement::didNotifySubtreeInsertionsToDocument()
163 {
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))
169         return;
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);
177 }
178
179 bool HTMLBodyElement::isURLAttribute(const Attribute& attribute) const
180 {
181     return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
182 }
183
184 bool HTMLBodyElement::hasLegalLinkAttribute(const QualifiedName& name) const
185 {
186     return name == backgroundAttr || HTMLElement::hasLegalLinkAttribute(name);
187 }
188
189 const QualifiedName& HTMLBodyElement::subResourceAttributeName() const
190 {
191     return backgroundAttr;
192 }
193
194 bool HTMLBodyElement::supportsFocus() const
195 {
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();
199 }
200
201 static double adjustForZoom(int value, Document* document)
202 {
203     LocalFrame* frame = document->frame();
204     float zoomFactor = frame->pageZoomFactor();
205     if (zoomFactor == 1)
206         return static_cast<double>(value);
207     return static_cast<double>(value) / zoomFactor;
208 }
209
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,
217 // auto or scroll.
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
220 // or quirks modes.
221 double HTMLBodyElement::scrollLeft()
222 {
223     Document& document = this->document();
224     document.updateLayoutIgnorePendingStylesheets();
225
226     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
227         RenderBox* render = renderBox();
228         if (!render)
229             return 0;
230         if (render->hasOverflowClip())
231             return adjustForAbsoluteZoom(render->scrollLeft(), render);
232         if (!document.inQuirksMode())
233             return 0;
234     }
235
236     FrameView* view = document.view();
237     return view ? adjustForZoom(view->scrollX(), &document) : 0;
238 }
239
240 void HTMLBodyElement::setScrollLeft(double scrollLeft)
241 {
242     Document& document = this->document();
243     document.updateLayoutIgnorePendingStylesheets();
244
245     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
246         RenderBox* render = renderBox();
247         if (!render)
248             return;
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()));
252             return;
253         }
254         if (!document.inQuirksMode())
255             return;
256     }
257
258     LocalFrame* frame = document.frame();
259     if (!frame)
260         return;
261     FrameView* view = frame->view();
262     if (!view)
263         return;
264     view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * frame->pageZoomFactor()), view->scrollY()));
265 }
266
267 double HTMLBodyElement::scrollTop()
268 {
269     Document& document = this->document();
270     document.updateLayoutIgnorePendingStylesheets();
271
272     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
273         RenderBox* render = renderBox();
274         if (!render)
275             return 0;
276         if (render->hasOverflowClip())
277             return adjustForAbsoluteZoom(render->scrollTop(), render);
278         if (!document.inQuirksMode())
279             return 0;
280     }
281
282     FrameView* view = document.view();
283     return view ? adjustForZoom(view->scrollY(), &document) : 0;
284 }
285
286 void HTMLBodyElement::setScrollTop(double scrollTop)
287 {
288     Document& document = this->document();
289     document.updateLayoutIgnorePendingStylesheets();
290
291     if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
292         RenderBox* render = renderBox();
293         if (!render)
294             return;
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()));
298             return;
299         }
300         if (!document.inQuirksMode())
301             return;
302     }
303
304     LocalFrame* frame = document.frame();
305     if (!frame)
306         return;
307     FrameView* view = frame->view();
308     if (!view)
309         return;
310     view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * frame->pageZoomFactor())));
311 }
312
313 double HTMLBodyElement::scrollHeight()
314 {
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;
320 }
321
322 double HTMLBodyElement::scrollWidth()
323 {
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;
329 }
330
331 } // namespace WebCore