Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / svg / SVGScriptElement.cpp
blob7be72dcc9fa7a481b1c9771cb23a34c7c5301b50
1 /*
2 Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2007 Rob Buis <buis@kde.org>
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.
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.
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.
21 #include "config.h"
23 #if ENABLE(SVG)
24 #include "SVGScriptElement.h"
26 #include "Document.h"
27 #include "Event.h"
28 #include "EventNames.h"
29 #include "MappedAttribute.h"
30 #include "SVGNames.h"
32 namespace WebCore {
34 SVGScriptElement::SVGScriptElement(const QualifiedName& tagName, Document* doc, bool createdByParser)
35 : SVGElement(tagName, doc)
36 , SVGURIReference()
37 , SVGExternalResourcesRequired()
38 , m_href(this, XLinkNames::hrefAttr)
39 , m_externalResourcesRequired(this, SVGNames::externalResourcesRequiredAttr, false)
40 , m_data(this, this)
42 m_data.setCreatedByParser(createdByParser);
45 SVGScriptElement::~SVGScriptElement()
49 String SVGScriptElement::scriptContent() const
51 return m_data.scriptContent();
54 void SVGScriptElement::parseMappedAttribute(MappedAttribute* attr)
56 const QualifiedName& attrName = attr->name();
58 if (attrName == SVGNames::typeAttr)
59 setType(attr->value());
60 else {
61 if (SVGURIReference::parseMappedAttribute(attr))
62 return;
63 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
64 return;
66 SVGElement::parseMappedAttribute(attr);
70 void SVGScriptElement::svgAttributeChanged(const QualifiedName& attrName)
72 SVGElement::svgAttributeChanged(attrName);
74 if (SVGURIReference::isKnownAttribute(attrName))
75 handleSourceAttribute(m_data, href());
76 else if (SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
77 // Handle dynamic updates of the 'externalResourcesRequired' attribute. Only possible case: changing from 'true' to 'false'
78 // causes an immediate dispatch of the SVGLoad event. If the attribute value was 'false' before inserting the script element
79 // in the document, the SVGLoad event has already been dispatched.
80 if (!externalResourcesRequiredBaseValue() && !m_data.haveFiredLoadEvent() && !m_data.createdByParser()) {
81 m_data.setHaveFiredLoadEvent(true);
82 ASSERT(haveLoadedRequiredResources());
84 sendSVGLoadEventIfPossible();
89 void SVGScriptElement::insertedIntoDocument()
91 SVGElement::insertedIntoDocument();
92 ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue());
94 if (m_data.createdByParser())
95 return;
97 // Eventually send SVGLoad event now for the dynamically inserted script element
98 if (!externalResourcesRequiredBaseValue()) {
99 m_data.setHaveFiredLoadEvent(true);
100 sendSVGLoadEventIfPossible();
104 void SVGScriptElement::removedFromDocument()
106 SVGElement::removedFromDocument();
107 ScriptElement::removedFromDocument(m_data);
110 void SVGScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
112 ScriptElement::childrenChanged(m_data);
113 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
116 bool SVGScriptElement::isURLAttribute(Attribute* attr) const
118 return attr->name() == sourceAttributeValue();
121 void SVGScriptElement::finishParsingChildren()
123 ScriptElement::finishParsingChildren(m_data, sourceAttributeValue());
124 SVGElement::finishParsingChildren();
126 // A SVGLoad event has been fired by SVGElement::finishParsingChildren.
127 if (!externalResourcesRequiredBaseValue())
128 m_data.setHaveFiredLoadEvent(true);
131 String SVGScriptElement::type() const
133 return m_type;
136 void SVGScriptElement::setType(const String& type)
138 m_type = type;
141 String SVGScriptElement::scriptCharset() const
143 return m_data.scriptCharset();
146 void SVGScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
148 SVGElement::addSubresourceAttributeURLs(urls);
150 addSubresourceURL(urls, document()->completeURL(href()));
153 bool SVGScriptElement::haveLoadedRequiredResources()
155 return !externalResourcesRequiredBaseValue() || m_data.haveFiredLoadEvent();
158 String SVGScriptElement::sourceAttributeValue() const
160 return href();
163 String SVGScriptElement::charsetAttributeValue() const
165 return String();
168 String SVGScriptElement::typeAttributeValue() const
170 return type();
173 String SVGScriptElement::languageAttributeValue() const
175 return String();
178 String SVGScriptElement::forAttributeValue() const
180 return String();
183 void SVGScriptElement::dispatchLoadEvent()
185 bool externalResourcesRequired = externalResourcesRequiredBaseValue();
187 if (m_data.createdByParser())
188 ASSERT(externalResourcesRequired != m_data.haveFiredLoadEvent());
189 else if (m_data.haveFiredLoadEvent()) {
190 // If we've already fired an load event and externalResourcesRequired is set to 'true'
191 // externalResourcesRequired has been modified while loading the <script>. Don't dispatch twice.
192 if (externalResourcesRequired)
193 return;
196 // HTML and SVG differ completly in the 'onload' event handling of <script> elements.
197 // HTML fires the 'load' event after it sucessfully loaded a remote resource, otherwhise an error event.
198 // SVG fires the SVGLoad event immediately after parsing the <script> element, if externalResourcesRequired
199 // is set to 'false', otherwhise it dispatches the 'SVGLoad' event just after loading the remote resource.
200 if (externalResourcesRequired) {
201 ASSERT(!m_data.haveFiredLoadEvent());
203 // Dispatch SVGLoad event
204 m_data.setHaveFiredLoadEvent(true);
205 ASSERT(haveLoadedRequiredResources());
207 sendSVGLoadEventIfPossible();
211 void SVGScriptElement::dispatchErrorEvent()
213 dispatchEvent(Event::create(eventNames().errorEvent, true, false));
218 #endif // ENABLE(SVG)