Upstream version 5.34.104.0
[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
39 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGScriptElement)
40 END_REGISTER_ANIMATED_PROPERTIES
41
42 inline SVGScriptElement::SVGScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
43     : SVGElement(SVGNames::scriptTag, document)
44     , SVGURIReference(this)
45     , m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
46     , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
47 {
48     ScriptWrappable::init(this);
49     registerAnimatedPropertiesForSVGScriptElement();
50 }
51
52 PassRefPtr<SVGScriptElement> SVGScriptElement::create(Document& document, bool insertedByParser)
53 {
54     return adoptRef(new SVGScriptElement(document, insertedByParser, false));
55 }
56
57 bool SVGScriptElement::isSupportedAttribute(const QualifiedName& attrName)
58 {
59     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
60     if (supportedAttributes.isEmpty()) {
61         SVGURIReference::addSupportedAttributes(supportedAttributes);
62         supportedAttributes.add(SVGNames::typeAttr);
63         supportedAttributes.add(HTMLNames::onerrorAttr);
64     }
65     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
66 }
67
68 void SVGScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
69 {
70     if (!isSupportedAttribute(name)) {
71         SVGElement::parseAttribute(name, value);
72         return;
73     }
74
75     SVGParsingError parseError = NoError;
76     if (name == SVGNames::typeAttr)
77         return;
78
79     if (name == HTMLNames::onerrorAttr) {
80         setAttributeEventListener(EventTypeNames::error, createAttributeEventListener(this, name, value));
81     } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
82     } else {
83         ASSERT_NOT_REACHED();
84     }
85
86     reportAttributeParsingError(parseError, name, value);
87 }
88
89 void SVGScriptElement::svgAttributeChanged(const QualifiedName& attrName)
90 {
91     if (!isSupportedAttribute(attrName)) {
92         SVGElement::svgAttributeChanged(attrName);
93         return;
94     }
95
96     SVGElementInstance::InvalidationGuard invalidationGuard(this);
97
98     if (attrName == SVGNames::typeAttr || attrName == HTMLNames::onerrorAttr)
99         return;
100
101     if (SVGURIReference::isKnownAttribute(attrName)) {
102         m_loader->handleSourceAttribute(hrefString());
103         return;
104     }
105
106     ASSERT_NOT_REACHED();
107 }
108
109 Node::InsertionNotificationRequest SVGScriptElement::insertedInto(ContainerNode* rootParent)
110 {
111     SVGElement::insertedInto(rootParent);
112     return InsertionShouldCallDidNotifySubtreeInsertions;
113 }
114
115 void SVGScriptElement::didNotifySubtreeInsertionsToDocument()
116 {
117     m_loader->didNotifySubtreeInsertionsToDocument();
118
119     if (!m_loader->isParserInserted()) {
120         m_loader->setHaveFiredLoadEvent(true);
121         sendSVGLoadEventIfPossibleAsynchronously();
122     }
123 }
124
125 void SVGScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
126 {
127     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
128     m_loader->childrenChanged();
129 }
130
131 bool SVGScriptElement::isURLAttribute(const Attribute& attribute) const
132 {
133     return attribute.name() == AtomicString(sourceAttributeValue());
134 }
135
136 void SVGScriptElement::finishParsingChildren()
137 {
138     SVGElement::finishParsingChildren();
139     m_loader->setHaveFiredLoadEvent(true);
140 }
141
142 bool SVGScriptElement::haveLoadedRequiredResources()
143 {
144     return m_loader->haveFiredLoadEvent();
145 }
146
147 String SVGScriptElement::sourceAttributeValue() const
148 {
149     return hrefString();
150 }
151
152 String SVGScriptElement::charsetAttributeValue() const
153 {
154     return String();
155 }
156
157 String SVGScriptElement::typeAttributeValue() const
158 {
159     return getAttribute(SVGNames::typeAttr).string();
160 }
161
162 String SVGScriptElement::languageAttributeValue() const
163 {
164     return String();
165 }
166
167 String SVGScriptElement::forAttributeValue() const
168 {
169     return String();
170 }
171
172 String SVGScriptElement::eventAttributeValue() const
173 {
174     return String();
175 }
176
177 bool SVGScriptElement::asyncAttributeValue() const
178 {
179     return false;
180 }
181
182 bool SVGScriptElement::deferAttributeValue() const
183 {
184     return false;
185 }
186
187 bool SVGScriptElement::hasSourceAttribute() const
188 {
189     return href()->isSpecified();
190 }
191
192 PassRefPtr<Element> SVGScriptElement::cloneElementWithoutAttributesAndChildren()
193 {
194     return adoptRef(new SVGScriptElement(document(), false, m_loader->alreadyStarted()));
195 }
196
197 void SVGScriptElement::dispatchLoadEvent()
198 {
199     dispatchEvent(Event::create(EventTypeNames::load));
200 }
201
202 #ifndef NDEBUG
203 bool SVGScriptElement::isAnimatableAttribute(const QualifiedName& name) const
204 {
205     if (name == SVGNames::typeAttr)
206         return false;
207
208     return SVGElement::isAnimatableAttribute(name);
209 }
210 #endif
211
212 }