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 "CSSStyleSelector.h"
32 #include "DOMImplementation.h"
35 #include "EventListener.h"
36 #include "EventNames.h"
37 #include "FrameView.h"
38 #include "HTMLNames.h"
39 #include "RegisteredEventListener.h"
40 #include "RenderObject.h"
41 #include "SVGCursorElement.h"
42 #include "SVGDocumentExtensions.h"
43 #include "SVGElementInstance.h"
44 #include "SVGElementRareData.h"
46 #include "SVGSVGElement.h"
47 #include "SVGStyledLocatableElement.h"
48 #include "SVGTextElement.h"
49 #include "SVGURIReference.h"
50 #include "SVGUseElement.h"
51 #include "ScriptEventListener.h"
56 using namespace HTMLNames;
58 SVGElement::SVGElement(const QualifiedName& tagName, Document* document, ConstructionType constructionType)
59 : StyledElement(tagName, document, constructionType)
61 setHasCustomStyleForRenderer();
64 PassRefPtr<SVGElement> SVGElement::create(const QualifiedName& tagName, Document* document)
66 return adoptRef(new SVGElement(tagName, document));
69 SVGElement::~SVGElement()
71 if (!hasRareSVGData())
72 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
74 SVGElementRareData::SVGElementRareDataMap& rareDataMap = SVGElementRareData::rareDataMap();
75 SVGElementRareData::SVGElementRareDataMap::iterator it = rareDataMap.find(this);
76 ASSERT(it != rareDataMap.end());
78 SVGElementRareData* rareData = it->second;
79 if (SVGCursorElement* cursorElement = rareData->cursorElement())
80 cursorElement->removeClient(this);
81 if (CSSCursorImageValue* cursorImageValue = rareData->cursorImageValue())
82 cursorImageValue->removeReferencedElement(this);
85 rareDataMap.remove(it);
87 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
90 SVGElementRareData* SVGElement::rareSVGData() const
92 ASSERT(hasRareSVGData());
93 return SVGElementRareData::rareDataFromMap(this);
96 SVGElementRareData* SVGElement::ensureRareSVGData()
101 ASSERT(!SVGElementRareData::rareDataMap().contains(this));
102 SVGElementRareData* data = new SVGElementRareData;
103 SVGElementRareData::rareDataMap().set(this, data);
108 void SVGElement::reportAttributeParsingError(SVGParsingError error, Attribute* attribute)
110 if (error == NoError)
113 String errorString = "<" + tagName() + "> attribute " + attribute->name().toString() + "=\"" + attribute->value() + "\"";
114 SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
116 if (error == NegativeValueForbiddenError) {
117 extensions->reportError("Invalid negative value for " + errorString);
121 if (error == ParsingAttributeFailedError) {
122 extensions->reportError("Invalid value for " + errorString);
126 ASSERT_NOT_REACHED();
130 bool SVGElement::isSupported(StringImpl* feature, StringImpl* version) const
132 return DOMImplementation::hasFeature(feature, version);
135 String SVGElement::xmlbase() const
137 return fastGetAttribute(XMLNames::baseAttr);
140 void SVGElement::setXmlbase(const String& value, ExceptionCode&)
142 setAttribute(XMLNames::baseAttr, value);
145 void SVGElement::removedFromDocument()
147 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
148 StyledElement::removedFromDocument();
151 SVGSVGElement* SVGElement::ownerSVGElement() const
153 ContainerNode* n = parentOrHostNode();
155 if (n->hasTagName(SVGNames::svgTag))
156 return static_cast<SVGSVGElement*>(n);
158 n = n->parentOrHostNode();
164 SVGElement* SVGElement::viewportElement() const
166 // This function needs shadow tree support - as RenderSVGContainer uses this function
167 // to determine the "overflow" property. <use> on <symbol> wouldn't work otherwhise.
168 ContainerNode* n = parentOrHostNode();
170 if (n->hasTagName(SVGNames::svgTag) || n->hasTagName(SVGNames::imageTag) || n->hasTagName(SVGNames::symbolTag))
171 return static_cast<SVGElement*>(n);
173 n = n->parentOrHostNode();
179 SVGDocumentExtensions* SVGElement::accessDocumentSVGExtensions()
181 // This function is provided for use by SVGAnimatedProperty to avoid
182 // global inclusion of Document.h in SVG code.
183 return document() ? document()->accessSVGExtensions() : 0;
186 void SVGElement::mapInstanceToElement(SVGElementInstance* instance)
190 HashSet<SVGElementInstance*>& instances = ensureRareSVGData()->elementInstances();
191 ASSERT(!instances.contains(instance));
193 instances.add(instance);
196 void SVGElement::removeInstanceMapping(SVGElementInstance* instance)
199 ASSERT(hasRareSVGData());
201 HashSet<SVGElementInstance*>& instances = rareSVGData()->elementInstances();
202 ASSERT(instances.contains(instance));
204 instances.remove(instance);
207 const HashSet<SVGElementInstance*>& SVGElement::instancesForElement() const
209 if (!hasRareSVGData()) {
210 DEFINE_STATIC_LOCAL(HashSet<SVGElementInstance*>, emptyInstances, ());
211 return emptyInstances;
213 return rareSVGData()->elementInstances();
216 bool SVGElement::boundingBox(FloatRect& rect, SVGLocatable::StyleUpdateStrategy styleUpdateStrategy)
218 if (isStyledLocatable()) {
219 rect = static_cast<SVGStyledLocatableElement*>(this)->getBBox(styleUpdateStrategy);
222 if (hasTagName(SVGNames::textTag)) {
223 rect = static_cast<SVGTextElement*>(this)->getBBox(styleUpdateStrategy);
229 void SVGElement::setCursorElement(SVGCursorElement* cursorElement)
231 SVGElementRareData* rareData = ensureRareSVGData();
232 if (SVGCursorElement* oldCursorElement = rareData->cursorElement()) {
233 if (cursorElement == oldCursorElement)
235 oldCursorElement->removeReferencedElement(this);
237 rareData->setCursorElement(cursorElement);
240 void SVGElement::cursorElementRemoved()
242 ASSERT(hasRareSVGData());
243 rareSVGData()->setCursorElement(0);
246 void SVGElement::setCursorImageValue(CSSCursorImageValue* cursorImageValue)
248 SVGElementRareData* rareData = ensureRareSVGData();
249 if (CSSCursorImageValue* oldCursorImageValue = rareData->cursorImageValue()) {
250 if (cursorImageValue == oldCursorImageValue)
252 oldCursorImageValue->removeReferencedElement(this);
254 rareData->setCursorImageValue(cursorImageValue);
257 void SVGElement::cursorImageValueRemoved()
259 ASSERT(hasRareSVGData());
260 rareSVGData()->setCursorImageValue(0);
263 SVGElement* SVGElement::correspondingElement()
265 ASSERT(!hasRareSVGData() || !rareSVGData()->correspondingElement() || shadowTreeRootNode());
266 return hasRareSVGData() ? rareSVGData()->correspondingElement() : 0;
269 void SVGElement::setCorrespondingElement(SVGElement* correspondingElement)
271 ensureRareSVGData()->setCorrespondingElement(correspondingElement);
274 void SVGElement::parseMappedAttribute(Attribute* attr)
277 if (attr->name() == onloadAttr)
278 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
279 else if (attr->name() == onclickAttr)
280 setAttributeEventListener(eventNames().clickEvent, createAttributeEventListener(this, attr));
281 else if (attr->name() == onmousedownAttr)
282 setAttributeEventListener(eventNames().mousedownEvent, createAttributeEventListener(this, attr));
283 else if (attr->name() == onmousemoveAttr)
284 setAttributeEventListener(eventNames().mousemoveEvent, createAttributeEventListener(this, attr));
285 else if (attr->name() == onmouseoutAttr)
286 setAttributeEventListener(eventNames().mouseoutEvent, createAttributeEventListener(this, attr));
287 else if (attr->name() == onmouseoverAttr)
288 setAttributeEventListener(eventNames().mouseoverEvent, createAttributeEventListener(this, attr));
289 else if (attr->name() == onmouseupAttr)
290 setAttributeEventListener(eventNames().mouseupEvent, createAttributeEventListener(this, attr));
291 else if (attr->name() == SVGNames::onfocusinAttr)
292 setAttributeEventListener(eventNames().focusinEvent, createAttributeEventListener(this, attr));
293 else if (attr->name() == SVGNames::onfocusoutAttr)
294 setAttributeEventListener(eventNames().focusoutEvent, createAttributeEventListener(this, attr));
295 else if (attr->name() == SVGNames::onactivateAttr)
296 setAttributeEventListener(eventNames().DOMActivateEvent, createAttributeEventListener(this, attr));
298 StyledElement::parseMappedAttribute(attr);
301 void SVGElement::animatedPropertyTypeForAttribute(const QualifiedName& attributeName, Vector<AnimatedPropertyType>& propertyTypes)
303 localAttributeToPropertyMap().animatedPropertyTypeForAttribute(attributeName, propertyTypes);
306 bool SVGElement::haveLoadedRequiredResources()
308 Node* child = firstChild();
310 if (child->isSVGElement() && !static_cast<SVGElement*>(child)->haveLoadedRequiredResources())
312 child = child->nextSibling();
317 static bool hasLoadListener(Node* node)
319 if (node->hasEventListeners(eventNames().loadEvent))
322 for (node = node->parentNode(); node && node->isElementNode(); node = node->parentNode()) {
323 const EventListenerVector& entry = node->getEventListeners(eventNames().loadEvent);
324 for (size_t i = 0; i < entry.size(); ++i) {
325 if (entry[i].useCapture)
333 void SVGElement::sendSVGLoadEventIfPossible(bool sendParentLoadEvents)
335 RefPtr<SVGElement> currentTarget = this;
336 while (currentTarget && currentTarget->haveLoadedRequiredResources()) {
338 if (sendParentLoadEvents)
339 parent = currentTarget->parentNode(); // save the next parent to dispatch too incase dispatching the event changes the tree
340 if (hasLoadListener(currentTarget.get()))
341 currentTarget->dispatchEvent(Event::create(eventNames().loadEvent, false, false));
342 currentTarget = (parent && parent->isSVGElement()) ? static_pointer_cast<SVGElement>(parent) : RefPtr<SVGElement>();
346 void SVGElement::finishParsingChildren()
348 StyledElement::finishParsingChildren();
350 // finishParsingChildren() is called when the close tag is reached for an element (e.g. </svg>)
351 // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
352 sendSVGLoadEventIfPossible();
355 bool SVGElement::childShouldCreateRenderer(Node* child) const
357 if (child->isSVGElement())
358 return static_cast<SVGElement*>(child)->isValid();
362 void SVGElement::attributeChanged(Attribute* attr, bool preserveDecls)
368 StyledElement::attributeChanged(attr, preserveDecls);
370 // When an animated SVG property changes through SVG DOM, svgAttributeChanged() is called, not attributeChanged().
371 // Next time someone tries to access the XML attributes, the synchronization code starts. During that synchronization
372 // SVGAnimatedPropertySynchronizer may call NamedNodeMap::removeAttribute(), which in turn calls attributeChanged().
373 // At this point we're not allowed to call svgAttributeChanged() again - it may lead to extra work being done, or crashes
374 // see bug https://bugs.webkit.org/show_bug.cgi?id=40994.
375 if (isSynchronizingSVGAttributes())
378 if (isIdAttributeName(attr->name()))
379 document()->accessSVGExtensions()->removeAllAnimationElementsFromTarget(this);
381 // Changes to the style attribute are processed lazily (see Element::getAttribute() and related methods),
382 // so we don't want changes to the style attribute to result in extra work here.
383 if (attr->name() != HTMLNames::styleAttr)
384 svgAttributeChanged(attr->name());
387 void SVGElement::updateAnimatedSVGAttribute(const QualifiedName& name) const
389 if (isSynchronizingSVGAttributes() || areSVGAttributesValid())
392 setIsSynchronizingSVGAttributes();
394 SVGElement* nonConstThis = const_cast<SVGElement*>(this);
395 if (name == anyQName()) {
396 nonConstThis->localAttributeToPropertyMap().synchronizeProperties(nonConstThis);
397 setAreSVGAttributesValid();
399 nonConstThis->localAttributeToPropertyMap().synchronizeProperty(nonConstThis, name);
401 clearIsSynchronizingSVGAttributes();
404 SVGAttributeToPropertyMap& SVGElement::localAttributeToPropertyMap()
406 ASSERT_NOT_REACHED();
408 DEFINE_STATIC_LOCAL(SVGAttributeToPropertyMap, dummyMap, ());
412 void SVGElement::synchronizeRequiredFeatures(void* contextElement)
414 ASSERT(contextElement);
415 static_cast<SVGElement*>(contextElement)->synchronizeRequiredFeatures();
418 void SVGElement::synchronizeRequiredExtensions(void* contextElement)
420 ASSERT(contextElement);
421 static_cast<SVGElement*>(contextElement)->synchronizeRequiredExtensions();
424 void SVGElement::synchronizeSystemLanguage(void* contextElement)
426 ASSERT(contextElement);
427 static_cast<SVGElement*>(contextElement)->synchronizeSystemLanguage();
430 PassRefPtr<RenderStyle> SVGElement::customStyleForRenderer()
432 if (correspondingElement())
433 return document()->styleSelector()->styleForElement(correspondingElement(), parentNode() ? parentNode()->renderer()->style() : 0, false/*allowSharing*/);
435 return document()->styleSelector()->styleForElement(static_cast<Element*>(this), 0, true);
439 bool SVGElement::isAnimatableAttribute(const QualifiedName& name)
441 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, animatableAttributes, ());
443 if (animatableAttributes.isEmpty()) {
444 animatableAttributes.add(HTMLNames::classAttr);
445 animatableAttributes.add(XLinkNames::hrefAttr);
446 animatableAttributes.add(SVGNames::amplitudeAttr);
447 animatableAttributes.add(SVGNames::azimuthAttr);
448 animatableAttributes.add(SVGNames::baseFrequencyAttr);
449 animatableAttributes.add(SVGNames::biasAttr);
450 animatableAttributes.add(SVGNames::clipPathUnitsAttr);
451 animatableAttributes.add(SVGNames::cxAttr);
452 animatableAttributes.add(SVGNames::cyAttr);
453 animatableAttributes.add(SVGNames::diffuseConstantAttr);
454 animatableAttributes.add(SVGNames::divisorAttr);
455 animatableAttributes.add(SVGNames::dxAttr);
456 animatableAttributes.add(SVGNames::dyAttr);
457 animatableAttributes.add(SVGNames::edgeModeAttr);
458 animatableAttributes.add(SVGNames::elevationAttr);
459 animatableAttributes.add(SVGNames::exponentAttr);
460 animatableAttributes.add(SVGNames::externalResourcesRequiredAttr);
461 animatableAttributes.add(SVGNames::filterResAttr);
462 animatableAttributes.add(SVGNames::filterUnitsAttr);
463 animatableAttributes.add(SVGNames::fxAttr);
464 animatableAttributes.add(SVGNames::fyAttr);
465 animatableAttributes.add(SVGNames::gradientTransformAttr);
466 animatableAttributes.add(SVGNames::gradientUnitsAttr);
467 animatableAttributes.add(SVGNames::heightAttr);
468 animatableAttributes.add(SVGNames::in2Attr);
469 animatableAttributes.add(SVGNames::inAttr);
470 animatableAttributes.add(SVGNames::interceptAttr);
471 animatableAttributes.add(SVGNames::k1Attr);
472 animatableAttributes.add(SVGNames::k2Attr);
473 animatableAttributes.add(SVGNames::k3Attr);
474 animatableAttributes.add(SVGNames::k4Attr);
475 animatableAttributes.add(SVGNames::kernelMatrixAttr);
476 animatableAttributes.add(SVGNames::kernelUnitLengthAttr);
477 animatableAttributes.add(SVGNames::lengthAdjustAttr);
478 animatableAttributes.add(SVGNames::limitingConeAngleAttr);
479 animatableAttributes.add(SVGNames::markerHeightAttr);
480 animatableAttributes.add(SVGNames::markerUnitsAttr);
481 animatableAttributes.add(SVGNames::markerWidthAttr);
482 animatableAttributes.add(SVGNames::maskContentUnitsAttr);
483 animatableAttributes.add(SVGNames::maskUnitsAttr);
484 animatableAttributes.add(SVGNames::methodAttr);
485 animatableAttributes.add(SVGNames::modeAttr);
486 animatableAttributes.add(SVGNames::numOctavesAttr);
487 animatableAttributes.add(SVGNames::offsetAttr);
488 animatableAttributes.add(SVGNames::operatorAttr);
489 animatableAttributes.add(SVGNames::orderAttr);
490 animatableAttributes.add(SVGNames::orientAttr);
491 animatableAttributes.add(SVGNames::pathLengthAttr);
492 animatableAttributes.add(SVGNames::patternContentUnitsAttr);
493 animatableAttributes.add(SVGNames::patternTransformAttr);
494 animatableAttributes.add(SVGNames::patternUnitsAttr);
495 animatableAttributes.add(SVGNames::pointsAtXAttr);
496 animatableAttributes.add(SVGNames::pointsAtYAttr);
497 animatableAttributes.add(SVGNames::pointsAtZAttr);
498 animatableAttributes.add(SVGNames::preserveAlphaAttr);
499 animatableAttributes.add(SVGNames::preserveAspectRatioAttr);
500 animatableAttributes.add(SVGNames::primitiveUnitsAttr);
501 animatableAttributes.add(SVGNames::radiusAttr);
502 animatableAttributes.add(SVGNames::rAttr);
503 animatableAttributes.add(SVGNames::refXAttr);
504 animatableAttributes.add(SVGNames::refYAttr);
505 animatableAttributes.add(SVGNames::resultAttr);
506 animatableAttributes.add(SVGNames::rotateAttr);
507 animatableAttributes.add(SVGNames::rxAttr);
508 animatableAttributes.add(SVGNames::ryAttr);
509 animatableAttributes.add(SVGNames::scaleAttr);
510 animatableAttributes.add(SVGNames::seedAttr);
511 animatableAttributes.add(SVGNames::slopeAttr);
512 animatableAttributes.add(SVGNames::spacingAttr);
513 animatableAttributes.add(SVGNames::specularConstantAttr);
514 animatableAttributes.add(SVGNames::specularExponentAttr);
515 animatableAttributes.add(SVGNames::spreadMethodAttr);
516 animatableAttributes.add(SVGNames::startOffsetAttr);
517 animatableAttributes.add(SVGNames::stdDeviationAttr);
518 animatableAttributes.add(SVGNames::stitchTilesAttr);
519 animatableAttributes.add(SVGNames::surfaceScaleAttr);
520 animatableAttributes.add(SVGNames::tableValuesAttr);
521 animatableAttributes.add(SVGNames::targetAttr);
522 animatableAttributes.add(SVGNames::targetXAttr);
523 animatableAttributes.add(SVGNames::targetYAttr);
524 animatableAttributes.add(SVGNames::transformAttr);
525 animatableAttributes.add(SVGNames::typeAttr);
526 animatableAttributes.add(SVGNames::valuesAttr);
527 animatableAttributes.add(SVGNames::viewBoxAttr);
528 animatableAttributes.add(SVGNames::widthAttr);
529 animatableAttributes.add(SVGNames::x1Attr);
530 animatableAttributes.add(SVGNames::x2Attr);
531 animatableAttributes.add(SVGNames::xAttr);
532 animatableAttributes.add(SVGNames::xChannelSelectorAttr);
533 animatableAttributes.add(SVGNames::y1Attr);
534 animatableAttributes.add(SVGNames::y2Attr);
535 animatableAttributes.add(SVGNames::yAttr);
536 animatableAttributes.add(SVGNames::yChannelSelectorAttr);
537 animatableAttributes.add(SVGNames::zAttr);
539 return animatableAttributes.contains(name);
545 #endif // ENABLE(SVG)