2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2008 Rob Buis <buis@kde.org>
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
5 * Copyright (C) 2008 Alp Toker <alp@atoker.com>
6 * Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
27 #include "SVGElement.h"
29 #include "Attribute.h"
30 #include "CSSCursorImageValue.h"
31 #include "DOMImplementation.h"
34 #include "EventListener.h"
35 #include "EventNames.h"
36 #include "FrameView.h"
37 #include "HTMLNames.h"
38 #include "RegisteredEventListener.h"
39 #include "RenderObject.h"
40 #include "SVGCursorElement.h"
41 #include "SVGElementInstance.h"
42 #include "SVGElementRareData.h"
44 #include "SVGSVGElement.h"
45 #include "SVGStyledLocatableElement.h"
46 #include "SVGTextElement.h"
47 #include "SVGURIReference.h"
48 #include "SVGUseElement.h"
49 #include "ScriptEventListener.h"
54 using namespace HTMLNames;
56 SVGElement::SVGElement(const QualifiedName& tagName, Document* document)
57 : StyledElement(tagName, document, CreateSVGElement)
61 PassRefPtr<SVGElement> SVGElement::create(const QualifiedName& tagName, Document* document)
63 return adoptRef(new SVGElement(tagName, document));
66 SVGElement::~SVGElement()
68 if (!hasRareSVGData())
69 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
71 SVGElementRareData::SVGElementRareDataMap& rareDataMap = SVGElementRareData::rareDataMap();
72 SVGElementRareData::SVGElementRareDataMap::iterator it = rareDataMap.find(this);
73 ASSERT(it != rareDataMap.end());
75 SVGElementRareData* rareData = it->second;
76 if (SVGCursorElement* cursorElement = rareData->cursorElement())
77 cursorElement->removeClient(this);
78 if (CSSCursorImageValue* cursorImageValue = rareData->cursorImageValue())
79 cursorImageValue->removeReferencedElement(this);
82 rareDataMap.remove(it);
86 SVGElementRareData* SVGElement::rareSVGData() const
88 ASSERT(hasRareSVGData());
89 return SVGElementRareData::rareDataFromMap(this);
92 SVGElementRareData* SVGElement::ensureRareSVGData()
97 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
98 SVGElementRareData* data = new SVGElementRareData;
99 SVGElementRareData::rareDataMap().set(this, data);
104 bool SVGElement::isSupported(StringImpl* feature, StringImpl* version) const
106 return DOMImplementation::hasFeature(feature, version);
109 String SVGElement::xmlbase() const
111 return getAttribute(XMLNames::baseAttr);
114 void SVGElement::setXmlbase(const String& value, ExceptionCode&)
116 setAttribute(XMLNames::baseAttr, value);
119 SVGSVGElement* SVGElement::ownerSVGElement() const
121 ContainerNode* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
123 if (n->hasTagName(SVGNames::svgTag))
124 return static_cast<SVGSVGElement*>(n);
126 n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
132 SVGElement* SVGElement::viewportElement() const
134 // This function needs shadow tree support - as RenderSVGContainer uses this function
135 // to determine the "overflow" property. <use> on <symbol> wouldn't work otherwhise.
136 ContainerNode* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
138 if (n->hasTagName(SVGNames::svgTag) || n->hasTagName(SVGNames::imageTag) || n->hasTagName(SVGNames::symbolTag))
139 return static_cast<SVGElement*>(n);
141 n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
147 SVGDocumentExtensions* SVGElement::accessDocumentSVGExtensions() const
149 // This function is provided for use by SVGAnimatedProperty to avoid
150 // global inclusion of Document.h in SVG code.
151 return document() ? document()->accessSVGExtensions() : 0;
154 void SVGElement::mapInstanceToElement(SVGElementInstance* instance)
158 HashSet<SVGElementInstance*>& instances = ensureRareSVGData()->elementInstances();
159 ASSERT(!instances.contains(instance));
161 instances.add(instance);
164 void SVGElement::removeInstanceMapping(SVGElementInstance* instance)
167 ASSERT(hasRareSVGData());
169 HashSet<SVGElementInstance*>& instances = rareSVGData()->elementInstances();
170 ASSERT(instances.contains(instance));
172 instances.remove(instance);
175 const HashSet<SVGElementInstance*>& SVGElement::instancesForElement() const
177 if (!hasRareSVGData()) {
178 DEFINE_STATIC_LOCAL(HashSet<SVGElementInstance*>, emptyInstances, ());
179 return emptyInstances;
181 return rareSVGData()->elementInstances();
184 bool SVGElement::boundingBox(FloatRect& rect, SVGLocatable::StyleUpdateStrategy styleUpdateStrategy) const
186 if (isStyledLocatable()) {
187 rect = static_cast<const SVGStyledLocatableElement*>(this)->getBBox(styleUpdateStrategy);
190 if (hasTagName(SVGNames::textTag)) {
191 rect = static_cast<const SVGTextElement*>(this)->getBBox(styleUpdateStrategy);
197 void SVGElement::setCursorElement(SVGCursorElement* cursorElement)
199 ensureRareSVGData()->setCursorElement(cursorElement);
202 void SVGElement::setCursorImageValue(CSSCursorImageValue* cursorImageValue)
204 ensureRareSVGData()->setCursorImageValue(cursorImageValue);
207 void SVGElement::parseMappedAttribute(Attribute* attr)
210 if (attr->name() == onloadAttr)
211 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
212 else if (attr->name() == onclickAttr)
213 setAttributeEventListener(eventNames().clickEvent, createAttributeEventListener(this, attr));
214 else if (attr->name() == onmousedownAttr)
215 setAttributeEventListener(eventNames().mousedownEvent, createAttributeEventListener(this, attr));
216 else if (attr->name() == onmousemoveAttr)
217 setAttributeEventListener(eventNames().mousemoveEvent, createAttributeEventListener(this, attr));
218 else if (attr->name() == onmouseoutAttr)
219 setAttributeEventListener(eventNames().mouseoutEvent, createAttributeEventListener(this, attr));
220 else if (attr->name() == onmouseoverAttr)
221 setAttributeEventListener(eventNames().mouseoverEvent, createAttributeEventListener(this, attr));
222 else if (attr->name() == onmouseupAttr)
223 setAttributeEventListener(eventNames().mouseupEvent, createAttributeEventListener(this, attr));
224 else if (attr->name() == SVGNames::onfocusinAttr)
225 setAttributeEventListener(eventNames().focusinEvent, createAttributeEventListener(this, attr));
226 else if (attr->name() == SVGNames::onfocusoutAttr)
227 setAttributeEventListener(eventNames().focusoutEvent, createAttributeEventListener(this, attr));
228 else if (attr->name() == SVGNames::onactivateAttr)
229 setAttributeEventListener(eventNames().DOMActivateEvent, createAttributeEventListener(this, attr));
231 StyledElement::parseMappedAttribute(attr);
234 bool SVGElement::haveLoadedRequiredResources()
236 Node* child = firstChild();
238 if (child->isSVGElement() && !static_cast<SVGElement*>(child)->haveLoadedRequiredResources())
240 child = child->nextSibling();
245 static bool hasLoadListener(Node* node)
247 if (node->hasEventListeners(eventNames().loadEvent))
250 for (node = node->parentNode(); node && node->isElementNode(); node = node->parentNode()) {
251 const EventListenerVector& entry = node->getEventListeners(eventNames().loadEvent);
252 for (size_t i = 0; i < entry.size(); ++i) {
253 if (entry[i].useCapture)
261 void SVGElement::sendSVGLoadEventIfPossible(bool sendParentLoadEvents)
263 RefPtr<SVGElement> currentTarget = this;
264 while (currentTarget && currentTarget->haveLoadedRequiredResources()) {
266 if (sendParentLoadEvents)
267 parent = currentTarget->parentNode(); // save the next parent to dispatch too incase dispatching the event changes the tree
268 if (hasLoadListener(currentTarget.get())) {
269 RefPtr<Event> event = Event::create(eventNames().loadEvent, false, false);
270 event->setTarget(currentTarget);
271 currentTarget->dispatchGenericEvent(event.release());
273 currentTarget = (parent && parent->isSVGElement()) ? static_pointer_cast<SVGElement>(parent) : 0;
277 void SVGElement::finishParsingChildren()
279 StyledElement::finishParsingChildren();
281 // finishParsingChildren() is called when the close tag is reached for an element (e.g. </svg>)
282 // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
283 sendSVGLoadEventIfPossible();
286 bool SVGElement::childShouldCreateRenderer(Node* child) const
288 if (child->isSVGElement())
289 return static_cast<SVGElement*>(child)->isValid();
293 void SVGElement::insertedIntoDocument()
295 StyledElement::insertedIntoDocument();
297 if (!needsPendingResourceHandling())
300 SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
301 String resourceId = getIdAttribute();
302 if (!extensions->isPendingResource(resourceId))
305 OwnPtr<SVGDocumentExtensions::SVGPendingElements> clients(extensions->removePendingResource(resourceId));
306 if (clients->isEmpty())
309 const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
310 for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it)
311 (*it)->buildPendingResource();
314 void SVGElement::attributeChanged(Attribute* attr, bool preserveDecls)
320 StyledElement::attributeChanged(attr, preserveDecls);
322 // When an animated SVG property changes through SVG DOM, svgAttributeChanged() is called, not attributeChanged().
323 // Next time someone tries to access the XML attributes, the synchronization code starts. During that synchronization
324 // SVGAnimatedPropertySynchronizer may call NamedNodeMap::removeAttribute(), which in turn calls attributeChanged().
325 // At this point we're not allowed to call svgAttributeChanged() again - it may lead to extra work being done, or crashes
326 // see bug https://bugs.webkit.org/show_bug.cgi?id=40994.
327 if (isSynchronizingSVGAttributes())
330 // Changes to the style attribute are processed lazily (see Element::getAttribute() and related methods),
331 // so we don't want changes to the style attribute to result in extra work here.
332 if (attr->name() != HTMLNames::styleAttr)
333 svgAttributeChanged(attr->name());
336 void SVGElement::updateAnimatedSVGAttribute(const QualifiedName& name) const
338 if (isSynchronizingSVGAttributes() || areSVGAttributesValid())
341 setIsSynchronizingSVGAttributes();
343 const_cast<SVGElement*>(this)->synchronizeProperty(name);
344 if (name == anyQName())
345 setAreSVGAttributesValid();
347 clearIsSynchronizingSVGAttributes();
350 ContainerNode* SVGElement::eventParentNode()
352 if (ContainerNode* shadowParent = shadowParentNode())
354 return StyledElement::eventParentNode();
359 #endif // ENABLE(SVG)