- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGScriptElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2007 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include "core/svg/SVGScriptElement.h"
24
25 #include "HTMLNames.h"
26 #include "XLinkNames.h"
27 #include "bindings/v8/ScriptEventListener.h"
28 #include "core/dom/Attribute.h"
29 #include "core/dom/Document.h"
30 #include "core/dom/ScriptLoader.h"
31 #include "core/events/ThreadLocalEventNames.h"
32 #include "core/svg/SVGElementInstance.h"
33 #include "core/svg/properties/SVGAnimatedStaticPropertyTearOff.h"
34
35 namespace WebCore {
36
37 // Animated property definitions
38 DEFINE_ANIMATED_STRING(SVGScriptElement, XLinkNames::hrefAttr, Href, href)
39 DEFINE_ANIMATED_BOOLEAN(SVGScriptElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
40
41 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGScriptElement)
42     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
43     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
44 END_REGISTER_ANIMATED_PROPERTIES
45
46 inline SVGScriptElement::SVGScriptElement(const QualifiedName& tagName, Document& document, bool wasInsertedByParser, bool alreadyStarted)
47     : SVGElement(tagName, document)
48     , m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
49     , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
50 {
51     ASSERT(hasTagName(SVGNames::scriptTag));
52     ScriptWrappable::init(this);
53     registerAnimatedPropertiesForSVGScriptElement();
54 }
55
56 PassRefPtr<SVGScriptElement> SVGScriptElement::create(const QualifiedName& tagName, Document& document, bool insertedByParser)
57 {
58     return adoptRef(new SVGScriptElement(tagName, document, insertedByParser, false));
59 }
60
61 bool SVGScriptElement::isSupportedAttribute(const QualifiedName& attrName)
62 {
63     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
64     if (supportedAttributes.isEmpty()) {
65         SVGURIReference::addSupportedAttributes(supportedAttributes);
66         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
67         supportedAttributes.add(SVGNames::typeAttr);
68         supportedAttributes.add(HTMLNames::onerrorAttr);
69     }
70     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
71 }
72
73 void SVGScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
74 {
75     if (!isSupportedAttribute(name)) {
76         SVGElement::parseAttribute(name, value);
77         return;
78     }
79
80     if (name == SVGNames::typeAttr) {
81         setType(value);
82         return;
83     }
84
85     if (name == HTMLNames::onerrorAttr) {
86         setAttributeEventListener(EventTypeNames::error, createAttributeEventListener(this, name, value));
87         return;
88     }
89
90     if (SVGURIReference::parseAttribute(name, value))
91         return;
92     if (SVGExternalResourcesRequired::parseAttribute(name, value))
93         return;
94
95     ASSERT_NOT_REACHED();
96 }
97
98 void SVGScriptElement::svgAttributeChanged(const QualifiedName& attrName)
99 {
100     if (!isSupportedAttribute(attrName)) {
101         SVGElement::svgAttributeChanged(attrName);
102         return;
103     }
104
105     SVGElementInstance::InvalidationGuard invalidationGuard(this);
106
107     if (attrName == SVGNames::typeAttr || attrName == HTMLNames::onerrorAttr)
108         return;
109
110     if (SVGURIReference::isKnownAttribute(attrName)) {
111         m_loader->handleSourceAttribute(hrefCurrentValue());
112         return;
113     }
114
115     if (SVGExternalResourcesRequired::handleAttributeChange(this, attrName))
116         return;
117
118     ASSERT_NOT_REACHED();
119 }
120
121 Node::InsertionNotificationRequest SVGScriptElement::insertedInto(ContainerNode* rootParent)
122 {
123     SVGElement::insertedInto(rootParent);
124     return InsertionShouldCallDidNotifySubtreeInsertions;
125 }
126
127 void SVGScriptElement::didNotifySubtreeInsertionsToDocument()
128 {
129     m_loader->didNotifySubtreeInsertionsToDocument();
130     SVGExternalResourcesRequired::insertedIntoDocument(this);
131 }
132
133 void SVGScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
134 {
135     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
136     m_loader->childrenChanged();
137 }
138
139 bool SVGScriptElement::isURLAttribute(const Attribute& attribute) const
140 {
141     return attribute.name() == sourceAttributeValue();
142 }
143
144 void SVGScriptElement::finishParsingChildren()
145 {
146     SVGElement::finishParsingChildren();
147     SVGExternalResourcesRequired::finishParsingChildren();
148 }
149
150 String SVGScriptElement::type() const
151 {
152     return m_type;
153 }
154
155 void SVGScriptElement::setType(const String& type)
156 {
157     m_type = type;
158 }
159
160 void SVGScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
161 {
162     SVGElement::addSubresourceAttributeURLs(urls);
163
164     addSubresourceURL(urls, document().completeURL(hrefCurrentValue()));
165 }
166
167 String SVGScriptElement::sourceAttributeValue() const
168 {
169     return hrefCurrentValue();
170 }
171
172 String SVGScriptElement::charsetAttributeValue() const
173 {
174     return String();
175 }
176
177 String SVGScriptElement::typeAttributeValue() const
178 {
179     return type();
180 }
181
182 String SVGScriptElement::languageAttributeValue() const
183 {
184     return String();
185 }
186
187 String SVGScriptElement::forAttributeValue() const
188 {
189     return String();
190 }
191
192 String SVGScriptElement::eventAttributeValue() const
193 {
194     return String();
195 }
196
197 bool SVGScriptElement::asyncAttributeValue() const
198 {
199     return false;
200 }
201
202 bool SVGScriptElement::deferAttributeValue() const
203 {
204     return false;
205 }
206
207 bool SVGScriptElement::hasSourceAttribute() const
208 {
209     return hasAttribute(XLinkNames::hrefAttr);
210 }
211
212 PassRefPtr<Element> SVGScriptElement::cloneElementWithoutAttributesAndChildren()
213 {
214     return adoptRef(new SVGScriptElement(tagQName(), document(), false, m_loader->alreadyStarted()));
215 }
216
217 void SVGScriptElement::setHaveFiredLoadEvent(bool haveFiredLoadEvent)
218 {
219     m_loader->setHaveFiredLoadEvent(haveFiredLoadEvent);
220 }
221
222 bool SVGScriptElement::isParserInserted() const
223 {
224     return m_loader->isParserInserted();
225 }
226
227 bool SVGScriptElement::haveFiredLoadEvent() const
228 {
229     return m_loader->haveFiredLoadEvent();
230 }
231
232 Timer<SVGElement>* SVGScriptElement::svgLoadEventTimer()
233 {
234     return &m_svgLoadEventTimer;
235 }
236
237 }