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 "SVGDocumentExtensions.h"
42 #include "SVGElementInstance.h"
43 #include "SVGElementRareData.h"
45 #include "SVGSVGElement.h"
46 #include "SVGStyledLocatableElement.h"
47 #include "SVGTextElement.h"
48 #include "SVGURIReference.h"
49 #include "SVGUseElement.h"
50 #include "ScriptEventListener.h"
55 using namespace HTMLNames;
57 SVGElement::SVGElement(const QualifiedName& tagName, Document* document)
58 : StyledElement(tagName, document, CreateSVGElement)
62 PassRefPtr<SVGElement> SVGElement::create(const QualifiedName& tagName, Document* document)
64 return adoptRef(new SVGElement(tagName, document));
67 SVGElement::~SVGElement()
69 if (!hasRareSVGData())
70 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
72 SVGElementRareData::SVGElementRareDataMap& rareDataMap = SVGElementRareData::rareDataMap();
73 SVGElementRareData::SVGElementRareDataMap::iterator it = rareDataMap.find(this);
74 ASSERT(it != rareDataMap.end());
76 SVGElementRareData* rareData = it->second;
77 if (SVGCursorElement* cursorElement = rareData->cursorElement())
78 cursorElement->removeClient(this);
79 if (CSSCursorImageValue* cursorImageValue = rareData->cursorImageValue())
80 cursorImageValue->removeReferencedElement(this);
83 rareDataMap.remove(it);
85 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
88 SVGElementRareData* SVGElement::rareSVGData() const
90 ASSERT(hasRareSVGData());
91 return SVGElementRareData::rareDataFromMap(this);
94 SVGElementRareData* SVGElement::ensureRareSVGData()
99 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
100 SVGElementRareData* data = new SVGElementRareData;
101 SVGElementRareData::rareDataMap().set(this, data);
106 bool SVGElement::isSupported(StringImpl* feature, StringImpl* version) const
108 return DOMImplementation::hasFeature(feature, version);
111 String SVGElement::xmlbase() const
113 return getAttribute(XMLNames::baseAttr);
116 void SVGElement::setXmlbase(const String& value, ExceptionCode&)
118 setAttribute(XMLNames::baseAttr, value);
121 void SVGElement::removedFromDocument()
123 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
124 StyledElement::removedFromDocument();
127 SVGSVGElement* SVGElement::ownerSVGElement() const
129 ContainerNode* n = parentOrHostNode();
131 if (n->hasTagName(SVGNames::svgTag))
132 return static_cast<SVGSVGElement*>(n);
134 n = n->parentOrHostNode();
140 SVGElement* SVGElement::viewportElement() const
142 // This function needs shadow tree support - as RenderSVGContainer uses this function
143 // to determine the "overflow" property. <use> on <symbol> wouldn't work otherwhise.
144 ContainerNode* n = parentOrHostNode();
146 if (n->hasTagName(SVGNames::svgTag) || n->hasTagName(SVGNames::imageTag) || n->hasTagName(SVGNames::symbolTag))
147 return static_cast<SVGElement*>(n);
149 n = n->parentOrHostNode();
155 SVGDocumentExtensions* SVGElement::accessDocumentSVGExtensions() const
157 // This function is provided for use by SVGAnimatedProperty to avoid
158 // global inclusion of Document.h in SVG code.
159 return document() ? document()->accessSVGExtensions() : 0;
162 void SVGElement::mapInstanceToElement(SVGElementInstance* instance)
166 HashSet<SVGElementInstance*>& instances = ensureRareSVGData()->elementInstances();
167 ASSERT(!instances.contains(instance));
169 instances.add(instance);
172 void SVGElement::removeInstanceMapping(SVGElementInstance* instance)
175 ASSERT(hasRareSVGData());
177 HashSet<SVGElementInstance*>& instances = rareSVGData()->elementInstances();
178 ASSERT(instances.contains(instance));
180 instances.remove(instance);
183 const HashSet<SVGElementInstance*>& SVGElement::instancesForElement() const
185 if (!hasRareSVGData()) {
186 DEFINE_STATIC_LOCAL(HashSet<SVGElementInstance*>, emptyInstances, ());
187 return emptyInstances;
189 return rareSVGData()->elementInstances();
192 bool SVGElement::boundingBox(FloatRect& rect, SVGLocatable::StyleUpdateStrategy styleUpdateStrategy) const
194 if (isStyledLocatable()) {
195 rect = static_cast<const SVGStyledLocatableElement*>(this)->getBBox(styleUpdateStrategy);
198 if (hasTagName(SVGNames::textTag)) {
199 rect = static_cast<const SVGTextElement*>(this)->getBBox(styleUpdateStrategy);
205 void SVGElement::setCursorElement(SVGCursorElement* cursorElement)
207 SVGElementRareData* rareData = ensureRareSVGData();
208 if (SVGCursorElement* oldCursorElement = rareData->cursorElement()) {
209 if (cursorElement == oldCursorElement)
211 oldCursorElement->removeReferencedElement(this);
213 rareData->setCursorElement(cursorElement);
216 void SVGElement::cursorElementRemoved()
218 ASSERT(hasRareSVGData());
219 rareSVGData()->setCursorElement(0);
222 void SVGElement::setCursorImageValue(CSSCursorImageValue* cursorImageValue)
224 SVGElementRareData* rareData = ensureRareSVGData();
225 if (CSSCursorImageValue* oldCursorImageValue = rareData->cursorImageValue()) {
226 if (cursorImageValue == oldCursorImageValue)
228 oldCursorImageValue->removeReferencedElement(this);
230 rareData->setCursorImageValue(cursorImageValue);
233 void SVGElement::cursorImageValueRemoved()
235 ASSERT(hasRareSVGData());
236 rareSVGData()->setCursorImageValue(0);
239 void SVGElement::parseMappedAttribute(Attribute* attr)
242 if (attr->name() == onloadAttr)
243 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
244 else if (attr->name() == onclickAttr)
245 setAttributeEventListener(eventNames().clickEvent, createAttributeEventListener(this, attr));
246 else if (attr->name() == onmousedownAttr)
247 setAttributeEventListener(eventNames().mousedownEvent, createAttributeEventListener(this, attr));
248 else if (attr->name() == onmousemoveAttr)
249 setAttributeEventListener(eventNames().mousemoveEvent, createAttributeEventListener(this, attr));
250 else if (attr->name() == onmouseoutAttr)
251 setAttributeEventListener(eventNames().mouseoutEvent, createAttributeEventListener(this, attr));
252 else if (attr->name() == onmouseoverAttr)
253 setAttributeEventListener(eventNames().mouseoverEvent, createAttributeEventListener(this, attr));
254 else if (attr->name() == onmouseupAttr)
255 setAttributeEventListener(eventNames().mouseupEvent, createAttributeEventListener(this, attr));
256 else if (attr->name() == SVGNames::onfocusinAttr)
257 setAttributeEventListener(eventNames().focusinEvent, createAttributeEventListener(this, attr));
258 else if (attr->name() == SVGNames::onfocusoutAttr)
259 setAttributeEventListener(eventNames().focusoutEvent, createAttributeEventListener(this, attr));
260 else if (attr->name() == SVGNames::onactivateAttr)
261 setAttributeEventListener(eventNames().DOMActivateEvent, createAttributeEventListener(this, attr));
263 StyledElement::parseMappedAttribute(attr);
266 AttributeToPropertyTypeMap& SVGElement::attributeToPropertyTypeMap()
268 DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
269 return s_attributeToPropertyTypeMap;
272 AnimatedAttributeType SVGElement::animatedPropertyTypeForAttribute(const QualifiedName& attrName)
274 AttributeToPropertyTypeMap& animatedAttributeMap = attributeToPropertyTypeMap();
275 if (animatedAttributeMap.isEmpty())
276 fillAttributeToPropertyTypeMap();
277 if (animatedAttributeMap.contains(attrName))
278 return animatedAttributeMap.get(attrName);
280 return static_cast<SVGStyledElement*>(this)->animatedPropertyTypeForCSSProperty(attrName);
282 return AnimatedUnknown;
285 bool SVGElement::haveLoadedRequiredResources()
287 Node* child = firstChild();
289 if (child->isSVGElement() && !static_cast<SVGElement*>(child)->haveLoadedRequiredResources())
291 child = child->nextSibling();
296 static bool hasLoadListener(Node* node)
298 if (node->hasEventListeners(eventNames().loadEvent))
301 for (node = node->parentNode(); node && node->isElementNode(); node = node->parentNode()) {
302 const EventListenerVector& entry = node->getEventListeners(eventNames().loadEvent);
303 for (size_t i = 0; i < entry.size(); ++i) {
304 if (entry[i].useCapture)
312 void SVGElement::sendSVGLoadEventIfPossible(bool sendParentLoadEvents)
314 RefPtr<SVGElement> currentTarget = this;
315 while (currentTarget && currentTarget->haveLoadedRequiredResources()) {
317 if (sendParentLoadEvents)
318 parent = currentTarget->parentNode(); // save the next parent to dispatch too incase dispatching the event changes the tree
319 if (hasLoadListener(currentTarget.get()))
320 currentTarget->dispatchEvent(Event::create(eventNames().loadEvent, false, false));
321 currentTarget = (parent && parent->isSVGElement()) ? static_pointer_cast<SVGElement>(parent) : RefPtr<SVGElement>();
325 void SVGElement::finishParsingChildren()
327 StyledElement::finishParsingChildren();
329 // finishParsingChildren() is called when the close tag is reached for an element (e.g. </svg>)
330 // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
331 sendSVGLoadEventIfPossible();
334 bool SVGElement::childShouldCreateRenderer(Node* child) const
336 if (child->isSVGElement())
337 return static_cast<SVGElement*>(child)->isValid();
341 void SVGElement::attributeChanged(Attribute* attr, bool preserveDecls)
347 StyledElement::attributeChanged(attr, preserveDecls);
349 // When an animated SVG property changes through SVG DOM, svgAttributeChanged() is called, not attributeChanged().
350 // Next time someone tries to access the XML attributes, the synchronization code starts. During that synchronization
351 // SVGAnimatedPropertySynchronizer may call NamedNodeMap::removeAttribute(), which in turn calls attributeChanged().
352 // At this point we're not allowed to call svgAttributeChanged() again - it may lead to extra work being done, or crashes
353 // see bug https://bugs.webkit.org/show_bug.cgi?id=40994.
354 if (isSynchronizingSVGAttributes())
357 if (isIdAttributeName(attr->name()))
358 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
360 // Changes to the style attribute are processed lazily (see Element::getAttribute() and related methods),
361 // so we don't want changes to the style attribute to result in extra work here.
362 if (attr->name() != HTMLNames::styleAttr)
363 svgAttributeChanged(attr->name());
366 void SVGElement::updateAnimatedSVGAttribute(const QualifiedName& name) const
368 if (isSynchronizingSVGAttributes() || areSVGAttributesValid())
371 setIsSynchronizingSVGAttributes();
373 const_cast<SVGElement*>(this)->synchronizeProperty(name);
374 if (name == anyQName())
375 setAreSVGAttributesValid();
377 clearIsSynchronizingSVGAttributes();
382 #endif // ENABLE(SVG)