Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLMarqueeElement.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2007, 2010 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "core/html/HTMLMarqueeElement.h"
25
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "HTMLNames.h"
29 #include "bindings/v8/ExceptionState.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "core/rendering/RenderMarquee.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
37 inline HTMLMarqueeElement::HTMLMarqueeElement(Document& document)
38     : HTMLElement(marqueeTag, document)
39     , ActiveDOMObject(&document)
40 {
41     ScriptWrappable::init(this);
42 }
43
44 PassRefPtrWillBeRawPtr<HTMLMarqueeElement> HTMLMarqueeElement::create(Document& document)
45 {
46     RefPtrWillBeRawPtr<HTMLMarqueeElement> marqueeElement(adoptRefWillBeRefCountedGarbageCollected(new HTMLMarqueeElement(document)));
47     marqueeElement->suspendIfNeeded();
48     return marqueeElement.release();
49 }
50
51 int HTMLMarqueeElement::minimumDelay() const
52 {
53     if (fastGetAttribute(truespeedAttr).isEmpty()) {
54         // WinIE uses 60ms as the minimum delay by default.
55         return 60;
56     }
57     return 0;
58 }
59
60 void HTMLMarqueeElement::didMoveToNewDocument(Document& oldDocument)
61 {
62     ActiveDOMObject::didMoveToNewExecutionContext(&document());
63     HTMLElement::didMoveToNewDocument(oldDocument);
64 }
65
66 bool HTMLMarqueeElement::isPresentationAttribute(const QualifiedName& name) const
67 {
68     if (name == widthAttr || name == heightAttr || name == bgcolorAttr || name == vspaceAttr || name == hspaceAttr || name == scrollamountAttr || name == scrolldelayAttr || name == loopAttr || name == behaviorAttr || name == directionAttr)
69         return true;
70     return HTMLElement::isPresentationAttribute(name);
71 }
72
73 void HTMLMarqueeElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
74 {
75     if (name == widthAttr) {
76         if (!value.isEmpty())
77             addHTMLLengthToStyle(style, CSSPropertyWidth, value);
78     } else if (name == heightAttr) {
79         if (!value.isEmpty())
80             addHTMLLengthToStyle(style, CSSPropertyHeight, value);
81     } else if (name == bgcolorAttr) {
82         if (!value.isEmpty())
83             addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
84     } else if (name == vspaceAttr) {
85         if (!value.isEmpty()) {
86             addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
87             addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
88         }
89     } else if (name == hspaceAttr) {
90         if (!value.isEmpty()) {
91             addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
92             addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
93         }
94     } else if (name == scrollamountAttr) {
95         if (!value.isEmpty())
96             addHTMLLengthToStyle(style, CSSPropertyInternalMarqueeIncrement, value);
97     } else if (name == scrolldelayAttr) {
98         if (!value.isEmpty())
99             addHTMLLengthToStyle(style, CSSPropertyInternalMarqueeSpeed, value);
100     } else if (name == loopAttr) {
101         if (!value.isEmpty()) {
102             if (value == "-1" || equalIgnoringCase(value, "infinite"))
103                 addPropertyToPresentationAttributeStyle(style, CSSPropertyInternalMarqueeRepetition, CSSValueInfinite);
104             else
105                 addHTMLLengthToStyle(style, CSSPropertyInternalMarqueeRepetition, value);
106         }
107     } else if (name == behaviorAttr) {
108         if (!value.isEmpty())
109             addPropertyToPresentationAttributeStyle(style, CSSPropertyInternalMarqueeStyle, value);
110     } else if (name == directionAttr) {
111         if (!value.isEmpty())
112             addPropertyToPresentationAttributeStyle(style, CSSPropertyInternalMarqueeDirection, value);
113     } else
114         HTMLElement::collectStyleForPresentationAttribute(name, value, style);
115 }
116
117 void HTMLMarqueeElement::start()
118 {
119     if (RenderMarquee* marqueeRenderer = renderMarquee())
120         marqueeRenderer->start();
121 }
122
123 void HTMLMarqueeElement::stop()
124 {
125     if (RenderMarquee* marqueeRenderer = renderMarquee())
126         marqueeRenderer->stop();
127 }
128
129 int HTMLMarqueeElement::scrollAmount() const
130 {
131     bool ok;
132     int scrollAmount = fastGetAttribute(scrollamountAttr).toInt(&ok);
133     return ok && scrollAmount >= 0 ? scrollAmount : RenderStyle::initialMarqueeIncrement().intValue();
134 }
135
136 void HTMLMarqueeElement::setScrollAmount(int scrollAmount, ExceptionState& exceptionState)
137 {
138     if (scrollAmount < 0)
139         exceptionState.throwDOMException(IndexSizeError, "The provided value (" + String::number(scrollAmount) + ") is negative.");
140     else
141         setIntegralAttribute(scrollamountAttr, scrollAmount);
142 }
143
144 int HTMLMarqueeElement::scrollDelay() const
145 {
146     bool ok;
147     int scrollDelay = fastGetAttribute(scrolldelayAttr).toInt(&ok);
148     return ok && scrollDelay >= 0 ? scrollDelay : RenderStyle::initialMarqueeSpeed();
149 }
150
151 void HTMLMarqueeElement::setScrollDelay(int scrollDelay, ExceptionState& exceptionState)
152 {
153     if (scrollDelay < 0)
154         exceptionState.throwDOMException(IndexSizeError, "The provided value (" + String::number(scrollDelay) + ") is negative.");
155     else
156         setIntegralAttribute(scrolldelayAttr, scrollDelay);
157 }
158
159 int HTMLMarqueeElement::loop() const
160 {
161     bool ok;
162     int loopValue = fastGetAttribute(loopAttr).toInt(&ok);
163     return ok && loopValue > 0 ? loopValue : -1;
164 }
165
166 void HTMLMarqueeElement::setLoop(int loop, ExceptionState& exceptionState)
167 {
168     if (loop <= 0 && loop != -1)
169         exceptionState.throwDOMException(IndexSizeError, "The provided value (" + String::number(loop) + ") is neither positive nor -1.");
170     else
171         setIntegralAttribute(loopAttr, loop);
172 }
173
174 void HTMLMarqueeElement::suspend()
175 {
176     if (RenderMarquee* marqueeRenderer = renderMarquee())
177         marqueeRenderer->suspend();
178 }
179
180 void HTMLMarqueeElement::resume()
181 {
182     if (RenderMarquee* marqueeRenderer = renderMarquee())
183         marqueeRenderer->updateMarqueePosition();
184 }
185
186 RenderMarquee* HTMLMarqueeElement::renderMarquee() const
187 {
188     if (renderer() && renderer()->isMarquee())
189         return toRenderMarquee(renderer());
190     return 0;
191 }
192
193 RenderObject* HTMLMarqueeElement::createRenderer(RenderStyle*)
194 {
195     return new RenderMarquee(this);
196 }
197
198 void HTMLMarqueeElement::timerFired(Timer<HTMLMarqueeElement>*)
199 {
200     if (!renderer())
201         return;
202
203     document().updateLayout();
204
205     // The updateLayout() could have destroyed renderer(), so this re-check is very important.
206     if (renderer())
207         toRenderMarquee(renderer())->timerFired();
208 }
209
210 } // namespace WebCore