2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 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.
24 #include "SVGScriptElement.h"
26 #include "Attribute.h"
29 #include "EventNames.h"
34 // Animated property definitions
35 DEFINE_ANIMATED_STRING(SVGScriptElement, XLinkNames::hrefAttr, Href, href)
36 DEFINE_ANIMATED_BOOLEAN(SVGScriptElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
38 inline SVGScriptElement::SVGScriptElement(const QualifiedName& tagName, Document* document, bool wasInsertedByParser, bool wasAlreadyStarted)
39 : SVGElement(tagName, document)
40 , ScriptElement(this, wasInsertedByParser, wasAlreadyStarted)
44 PassRefPtr<SVGScriptElement> SVGScriptElement::create(const QualifiedName& tagName, Document* document, bool insertedByParser)
46 return adoptRef(new SVGScriptElement(tagName, document, insertedByParser, false));
49 void SVGScriptElement::parseMappedAttribute(Attribute* attr)
51 const QualifiedName& attrName = attr->name();
53 if (attrName == SVGNames::typeAttr)
54 setType(attr->value());
56 if (SVGURIReference::parseMappedAttribute(attr))
58 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
61 SVGElement::parseMappedAttribute(attr);
65 void SVGScriptElement::svgAttributeChanged(const QualifiedName& attrName)
67 SVGElement::svgAttributeChanged(attrName);
69 if (SVGURIReference::isKnownAttribute(attrName))
70 handleSourceAttribute(href());
71 else if (SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
72 // Handle dynamic updates of the 'externalResourcesRequired' attribute. Only possible case: changing from 'true' to 'false'
73 // causes an immediate dispatch of the SVGLoad event. If the attribute value was 'false' before inserting the script element
74 // in the document, the SVGLoad event has already been dispatched.
75 if (!externalResourcesRequiredBaseValue() && !haveFiredLoadEvent() && !wasInsertedByParser()) {
76 setHaveFiredLoadEvent(true);
77 ASSERT(haveLoadedRequiredResources());
79 sendSVGLoadEventIfPossible();
84 void SVGScriptElement::synchronizeProperty(const QualifiedName& attrName)
86 SVGElement::synchronizeProperty(attrName);
88 if (attrName == anyQName()) {
89 synchronizeExternalResourcesRequired();
94 if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
95 synchronizeExternalResourcesRequired();
96 else if (SVGURIReference::isKnownAttribute(attrName))
100 void SVGScriptElement::insertedIntoDocument()
102 SVGElement::insertedIntoDocument();
103 ScriptElement::insertedIntoDocument(sourceAttributeValue());
105 if (wasInsertedByParser())
108 // Eventually send SVGLoad event now for the dynamically inserted script element
109 if (!externalResourcesRequiredBaseValue()) {
110 setHaveFiredLoadEvent(true);
111 sendSVGLoadEventIfPossible();
115 void SVGScriptElement::removedFromDocument()
117 SVGElement::removedFromDocument();
118 ScriptElement::removedFromDocument();
121 void SVGScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
123 ScriptElement::childrenChanged();
124 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
127 bool SVGScriptElement::isURLAttribute(Attribute* attr) const
129 return attr->name() == sourceAttributeValue();
132 void SVGScriptElement::finishParsingChildren()
134 ScriptElement::finishParsingChildren(sourceAttributeValue());
135 SVGElement::finishParsingChildren();
137 // A SVGLoad event has been fired by SVGElement::finishParsingChildren.
138 if (!externalResourcesRequiredBaseValue())
139 setHaveFiredLoadEvent(true);
142 String SVGScriptElement::type() const
147 void SVGScriptElement::setType(const String& type)
152 void SVGScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
154 SVGElement::addSubresourceAttributeURLs(urls);
156 addSubresourceURL(urls, document()->completeURL(href()));
159 bool SVGScriptElement::haveLoadedRequiredResources()
161 return !externalResourcesRequiredBaseValue() || haveFiredLoadEvent();
164 String SVGScriptElement::sourceAttributeValue() const
169 String SVGScriptElement::charsetAttributeValue() const
174 String SVGScriptElement::typeAttributeValue() const
179 String SVGScriptElement::languageAttributeValue() const
184 String SVGScriptElement::forAttributeValue() const
189 String SVGScriptElement::eventAttributeValue() const
194 bool SVGScriptElement::asyncAttributeValue() const
199 bool SVGScriptElement::deferAttributeValue() const
204 void SVGScriptElement::dispatchLoadEvent()
206 bool externalResourcesRequired = externalResourcesRequiredBaseValue();
208 if (wasInsertedByParser())
209 ASSERT(externalResourcesRequired != haveFiredLoadEvent());
210 else if (haveFiredLoadEvent()) {
211 // If we've already fired an load event and externalResourcesRequired is set to 'true'
212 // externalResourcesRequired has been modified while loading the <script>. Don't dispatch twice.
213 if (externalResourcesRequired)
217 // HTML and SVG differ completly in the 'onload' event handling of <script> elements.
218 // HTML fires the 'load' event after it sucessfully loaded a remote resource, otherwhise an error event.
219 // SVG fires the SVGLoad event immediately after parsing the <script> element, if externalResourcesRequired
220 // is set to 'false', otherwhise it dispatches the 'SVGLoad' event just after loading the remote resource.
221 if (externalResourcesRequired) {
222 ASSERT(!haveFiredLoadEvent());
224 // Dispatch SVGLoad event
225 setHaveFiredLoadEvent(true);
226 ASSERT(haveLoadedRequiredResources());
228 sendSVGLoadEventIfPossible();
232 void SVGScriptElement::dispatchErrorEvent()
234 dispatchEvent(Event::create(eventNames().errorEvent, true, false));
237 PassRefPtr<Element> SVGScriptElement::cloneElementWithoutAttributesAndChildren() const
239 return adoptRef(new SVGScriptElement(tagQName(), document(), false, wasAlreadyStarted()));
244 #endif // ENABLE(SVG)