2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
5 * Copyright (C) 2011 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
6 * Copyright (C) 2012 University of Szeged
7 * Copyright (C) 2012 Renata Hodovan <reni@webkit.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 #include "core/svg/SVGUseElement.h"
29 #include "XLinkNames.h"
30 #include "bindings/v8/ExceptionStatePlaceholder.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/ElementTraversal.h"
33 #include "core/events/Event.h"
34 #include "core/dom/shadow/ElementShadow.h"
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/fetch/FetchRequest.h"
37 #include "core/fetch/ResourceFetcher.h"
38 #include "core/rendering/svg/RenderSVGResource.h"
39 #include "core/rendering/svg/RenderSVGTransformableContainer.h"
40 #include "core/svg/SVGElementInstance.h"
41 #include "core/svg/SVGGElement.h"
42 #include "core/svg/SVGLengthContext.h"
43 #include "core/svg/SVGSVGElement.h"
44 #include "core/xml/parser/XMLDocumentParser.h"
46 // Dump SVGElementInstance object tree - useful to debug instanceRoot problems
47 // #define DUMP_INSTANCE_TREE
49 // Dump the deep-expanded shadow tree (where the renderers are built from)
50 // #define DUMP_SHADOW_TREE
54 // Animated property definitions
56 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGUseElement)
57 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
58 END_REGISTER_ANIMATED_PROPERTIES
60 inline SVGUseElement::SVGUseElement(Document& document, bool wasInsertedByParser)
61 : SVGGraphicsElement(SVGNames::useTag, document)
62 , SVGURIReference(this)
63 , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth)))
64 , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight)))
65 , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth)))
66 , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight)))
67 , m_wasInsertedByParser(wasInsertedByParser)
68 , m_haveFiredLoadEvent(false)
69 , m_needsShadowTreeRecreation(false)
70 , m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
72 ASSERT(hasCustomStyleCallbacks());
73 ScriptWrappable::init(this);
75 addToPropertyMap(m_x);
76 addToPropertyMap(m_y);
77 addToPropertyMap(m_width);
78 addToPropertyMap(m_height);
79 registerAnimatedPropertiesForSVGUseElement();
82 PassRefPtr<SVGUseElement> SVGUseElement::create(Document& document, bool wasInsertedByParser)
84 // Always build a #shadow-root for SVGUseElement.
85 RefPtr<SVGUseElement> use = adoptRef(new SVGUseElement(document, wasInsertedByParser));
86 use->ensureUserAgentShadowRoot();
90 SVGUseElement::~SVGUseElement()
92 setDocumentResource(0);
94 clearResourceReferences();
97 SVGElementInstance* SVGUseElement::instanceRoot()
99 // If there is no element instance tree, force immediate SVGElementInstance tree
100 // creation by asking the document to invoke our recalcStyle function - as we can't
101 // wait for the lazy creation to happen if e.g. JS wants to access the instanceRoot
102 // object right after creating the element on-the-fly
103 if (!m_targetElementInstance)
104 document().updateStyleIfNeeded();
106 return m_targetElementInstance.get();
109 SVGElementInstance* SVGUseElement::animatedInstanceRoot() const
111 // FIXME: Implement me.
115 bool SVGUseElement::isSupportedAttribute(const QualifiedName& attrName)
117 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
118 if (supportedAttributes.isEmpty()) {
119 SVGURIReference::addSupportedAttributes(supportedAttributes);
120 supportedAttributes.add(SVGNames::xAttr);
121 supportedAttributes.add(SVGNames::yAttr);
122 supportedAttributes.add(SVGNames::widthAttr);
123 supportedAttributes.add(SVGNames::heightAttr);
125 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
128 void SVGUseElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
130 SVGParsingError parseError = NoError;
132 if (!isSupportedAttribute(name)) {
133 SVGGraphicsElement::parseAttribute(name, value);
134 } else if (name == SVGNames::xAttr) {
135 m_x->setBaseValueAsString(value, AllowNegativeLengths, parseError);
136 } else if (name == SVGNames::yAttr) {
137 m_y->setBaseValueAsString(value, AllowNegativeLengths, parseError);
138 } else if (name == SVGNames::widthAttr) {
139 m_width->setBaseValueAsString(value, ForbidNegativeLengths, parseError);
140 } else if (name == SVGNames::heightAttr) {
141 m_height->setBaseValueAsString(value, ForbidNegativeLengths, parseError);
142 } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
144 ASSERT_NOT_REACHED();
147 reportAttributeParsingError(parseError, name, value);
151 static inline bool isWellFormedDocument(Document* document)
153 if (document->isXMLDocument())
154 return static_cast<XMLDocumentParser*>(document->parser())->wellFormed();
159 Node::InsertionNotificationRequest SVGUseElement::insertedInto(ContainerNode* rootParent)
161 // This functions exists to assure assumptions made in the code regarding SVGElementInstance creation/destruction are satisfied.
162 SVGGraphicsElement::insertedInto(rootParent);
163 if (!rootParent->inDocument())
164 return InsertionDone;
165 ASSERT(!m_targetElementInstance || !isWellFormedDocument(&document()));
166 ASSERT(!hasPendingResources() || !isWellFormedDocument(&document()));
167 if (!m_wasInsertedByParser) {
168 buildPendingResource();
170 if (!isStructurallyExternal()) {
171 sendSVGLoadEventIfPossibleAsynchronously();
174 return InsertionDone;
177 void SVGUseElement::removedFrom(ContainerNode* rootParent)
179 SVGGraphicsElement::removedFrom(rootParent);
180 if (rootParent->inDocument())
181 clearResourceReferences();
184 Document* SVGUseElement::referencedDocument() const
186 if (!isExternalURIReference(hrefString(), document()))
188 return externalDocument();
191 Document* SVGUseElement::externalDocument() const
193 if (m_resource && m_resource->isLoaded()) {
194 // Gracefully handle error condition.
195 if (m_resource->errorOccurred())
197 ASSERT(m_resource->document());
198 return m_resource->document();
203 void SVGUseElement::svgAttributeChanged(const QualifiedName& attrName)
205 if (!isSupportedAttribute(attrName)) {
206 SVGGraphicsElement::svgAttributeChanged(attrName);
210 SVGElementInstance::InvalidationGuard invalidationGuard(this);
212 RenderObject* renderer = this->renderer();
213 if (attrName == SVGNames::xAttr
214 || attrName == SVGNames::yAttr
215 || attrName == SVGNames::widthAttr
216 || attrName == SVGNames::heightAttr) {
217 updateRelativeLengthsInformation();
219 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
223 if (SVGURIReference::isKnownAttribute(attrName)) {
224 bool isExternalReference = isExternalURIReference(hrefString(), document());
225 if (isExternalReference) {
226 KURL url = document().completeURL(hrefString());
227 if (url.hasFragmentIdentifier()) {
228 FetchRequest request(ResourceRequest(url.string()), localName());
229 setDocumentResource(document().fetcher()->fetchSVGDocument(request));
232 setDocumentResource(0);
235 if (!m_wasInsertedByParser)
236 buildPendingResource();
244 ASSERT_NOT_REACHED();
247 #ifdef DUMP_INSTANCE_TREE
248 static void dumpInstanceTree(unsigned int& depth, String& text, SVGElementInstance* targetInstance)
250 SVGElement* element = targetInstance->correspondingElement();
253 if (element->hasTagName(SVGNames::useTag)) {
254 if (toSVGUseElement(element)->resourceIsStillLoading())
258 SVGElement* shadowTreeElement = targetInstance->shadowTreeElement();
259 ASSERT(shadowTreeElement);
261 SVGUseElement* directUseElement = targetInstance->directUseElement();
262 String directUseElementName = directUseElement ? directUseElement->nodeName() : "null";
264 String elementId = element->getIdAttribute();
265 String elementNodeName = element->nodeName();
266 String shadowTreeElementNodeName = shadowTreeElement->nodeName();
267 String parentNodeName = element->parentNode() ? element->parentNode()->nodeName() : "null";
268 String firstChildNodeName = element->firstChild() ? element->firstChild()->nodeName() : "null";
270 for (unsigned int i = 0; i < depth; ++i)
273 text += String::format("SVGElementInstance this=%p, (parentNode=%s (%p), firstChild=%s (%p), correspondingElement=%s (%p), directUseElement=%s (%p), shadowTreeElement=%s (%p), id=%s)\n",
274 targetInstance, parentNodeName.latin1().data(), element->parentNode(), firstChildNodeName.latin1().data(), element->firstChild(),
275 elementNodeName.latin1().data(), element, directUseElementName.latin1().data(), directUseElement, shadowTreeElementNodeName.latin1().data(), shadowTreeElement, elementId.latin1().data());
277 for (unsigned int i = 0; i < depth; ++i)
280 const HashSet<SVGElementInstance*>& elementInstances = element->instancesForElement();
281 text += "Corresponding element is associated with " + String::number(elementInstances.size()) + " instance(s):\n";
283 const HashSet<SVGElementInstance*>::const_iterator end = elementInstances.end();
284 for (HashSet<SVGElementInstance*>::const_iterator it = elementInstances.begin(); it != end; ++it) {
285 for (unsigned int i = 0; i < depth; ++i)
288 text += String::format(" -> SVGElementInstance this=%p, (refCount: %i, shadowTreeElement in document? %i)\n",
289 *it, (*it)->refCount(), (*it)->shadowTreeElement()->inDocument());
294 for (SVGElementInstance* instance = targetInstance->firstChild(); instance; instance = instance->nextSibling())
295 dumpInstanceTree(depth, text, instance);
301 static bool isDisallowedElement(Node* node)
303 // Spec: "Any 'svg', 'symbol', 'g', graphics element or other 'use' is potentially a template object that can be re-used
304 // (i.e., "instanced") in the SVG document via a 'use' element."
305 // "Graphics Element" is defined as 'circle', 'ellipse', 'image', 'line', 'path', 'polygon', 'polyline', 'rect', 'text'
306 // Excluded are anything that is used by reference or that only make sense to appear once in a document.
307 // We must also allow the shadow roots of other use elements.
308 if (node->isShadowRoot() || node->isTextNode())
311 if (!node->isSVGElement())
314 Element* element = toElement(node);
316 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, allowedElementTags, ());
317 if (allowedElementTags.isEmpty()) {
318 allowedElementTags.add(SVGNames::aTag);
319 allowedElementTags.add(SVGNames::circleTag);
320 allowedElementTags.add(SVGNames::descTag);
321 allowedElementTags.add(SVGNames::ellipseTag);
322 allowedElementTags.add(SVGNames::gTag);
323 allowedElementTags.add(SVGNames::imageTag);
324 allowedElementTags.add(SVGNames::lineTag);
325 allowedElementTags.add(SVGNames::metadataTag);
326 allowedElementTags.add(SVGNames::pathTag);
327 allowedElementTags.add(SVGNames::polygonTag);
328 allowedElementTags.add(SVGNames::polylineTag);
329 allowedElementTags.add(SVGNames::rectTag);
330 allowedElementTags.add(SVGNames::svgTag);
331 allowedElementTags.add(SVGNames::switchTag);
332 allowedElementTags.add(SVGNames::symbolTag);
333 allowedElementTags.add(SVGNames::textTag);
334 allowedElementTags.add(SVGNames::textPathTag);
335 allowedElementTags.add(SVGNames::titleTag);
336 allowedElementTags.add(SVGNames::tspanTag);
337 allowedElementTags.add(SVGNames::useTag);
339 return !allowedElementTags.contains<SVGAttributeHashTranslator>(element->tagQName());
342 static bool subtreeContainsDisallowedElement(Node* start)
344 if (isDisallowedElement(start))
347 for (Node* cur = start->firstChild(); cur; cur = cur->nextSibling()) {
348 if (subtreeContainsDisallowedElement(cur))
355 void SVGUseElement::scheduleShadowTreeRecreation()
357 if (!referencedDocument() || isInShadowTree())
359 m_needsShadowTreeRecreation = true;
360 document().scheduleUseShadowTreeUpdate(*this);
363 void SVGUseElement::clearResourceReferences()
365 // FIXME: We should try to optimize this, to at least allow partial reclones.
366 if (ShadowRoot* shadowTreeRootElement = userAgentShadowRoot())
367 shadowTreeRootElement->removeChildren();
369 if (m_targetElementInstance) {
370 m_targetElementInstance->detach();
371 m_targetElementInstance = 0;
374 m_needsShadowTreeRecreation = false;
375 document().unscheduleUseShadowTreeUpdate(*this);
377 document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
380 void SVGUseElement::buildPendingResource()
382 if (!referencedDocument() || isInShadowTree())
384 clearResourceReferences();
389 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), document(), &id, externalDocument());
390 if (!target || !target->inDocument()) {
391 // If we can't find the target of an external element, just give up.
392 // We can't observe if the target somewhen enters the external document, nor should we do it.
393 if (externalDocument())
398 referencedDocument()->accessSVGExtensions()->addPendingResource(id, this);
399 ASSERT(hasPendingResources());
403 if (target->isSVGElement()) {
404 buildShadowAndInstanceTree(toSVGElement(target));
405 invalidateDependentShadowTrees();
408 ASSERT(!m_needsShadowTreeRecreation);
411 void SVGUseElement::buildShadowAndInstanceTree(SVGElement* target)
413 ASSERT(!m_targetElementInstance);
415 // Do not build the shadow/instance tree for <use> elements living in a shadow tree.
416 // The will be expanded soon anyway - see expandUseElementsInShadowTree().
417 if (isInShadowTree())
420 // Do not allow self-referencing.
421 // 'target' may be null, if it's a non SVG namespaced element.
422 if (!target || target == this)
425 // Why a seperated instance/shadow tree? SVG demands it:
426 // The instance tree is accesable from JavaScript, and has to
427 // expose a 1:1 copy of the referenced tree, whereas internally we need
428 // to alter the tree for correct "use-on-symbol", "use-on-svg" support.
430 // Build instance tree. Create root SVGElementInstance object for the first sub-tree node.
432 // Spec: If the 'use' element references a simple graphics element such as a 'rect', then there is only a
433 // single SVGElementInstance object, and the correspondingElement attribute on this SVGElementInstance object
434 // is the SVGRectElement that corresponds to the referenced 'rect' element.
435 m_targetElementInstance = SVGElementInstance::create(this, this, target);
437 // Eventually enter recursion to build SVGElementInstance objects for the sub-tree children
438 bool foundProblem = false;
439 buildInstanceTree(target, m_targetElementInstance.get(), foundProblem, false);
441 if (instanceTreeIsLoading(m_targetElementInstance.get()))
444 // SVG specification does not say a word about <use> & cycles. My view on this is: just ignore it!
445 // Non-appearing <use> content is easier to debug, then half-appearing content.
447 clearResourceReferences();
451 // Assure instance tree building was successfull
452 ASSERT(m_targetElementInstance);
453 ASSERT(!m_targetElementInstance->shadowTreeElement());
454 ASSERT(m_targetElementInstance->correspondingUseElement() == this);
455 ASSERT(m_targetElementInstance->directUseElement() == this);
456 ASSERT(m_targetElementInstance->correspondingElement() == target);
458 ShadowRoot* shadowTreeRootElement = userAgentShadowRoot();
459 ASSERT(shadowTreeRootElement);
461 // Build shadow tree from instance tree
462 // This also handles the special cases: <use> on <symbol>, <use> on <svg>.
463 buildShadowTree(target, m_targetElementInstance.get());
465 // Expand all <use> elements in the shadow tree.
466 // Expand means: replace the actual <use> element by what it references.
467 expandUseElementsInShadowTree(shadowTreeRootElement);
469 // Expand all <symbol> elements in the shadow tree.
470 // Expand means: replace the actual <symbol> element by the <svg> element.
471 expandSymbolElementsInShadowTree(shadowTreeRootElement);
473 // Now that the shadow tree is completly expanded, we can associate
474 // shadow tree elements <-> instances in the instance tree.
475 associateInstancesWithShadowTreeElements(shadowTreeRootElement->firstChild(), m_targetElementInstance.get());
477 // If no shadow tree element is present, this means that the reference root
478 // element was removed, as it is disallowed (ie. <use> on <foreignObject>)
479 // Do NOT leave an inconsistent instance tree around, instead destruct it.
480 if (!m_targetElementInstance->shadowTreeElement()) {
481 clearResourceReferences();
485 ASSERT(m_targetElementInstance->shadowTreeElement()->parentNode() == shadowTreeRootElement);
487 // Transfer event listeners assigned to the referenced element to our shadow tree elements.
488 transferEventListenersToShadowTree(m_targetElementInstance.get());
490 // Update relative length information.
491 updateRelativeLengthsInformation();
493 // Eventually dump instance tree
494 #ifdef DUMP_INSTANCE_TREE
496 unsigned int depth = 0;
498 dumpInstanceTree(depth, text, m_targetElementInstance.get());
499 fprintf(stderr, "\nDumping <use> instance tree:\n%s\n", text.latin1().data());
502 // Eventually dump shadow tree
503 #ifdef DUMP_SHADOW_TREE
504 RefPtr<XMLSerializer> serializer = XMLSerializer::create();
505 String markup = serializer->serializeToString(shadowTreeRootElement, ASSERT_NO_EXCEPTION);
506 fprintf(stderr, "Dumping <use> shadow tree markup:\n%s\n", markup.latin1().data());
510 RenderObject* SVGUseElement::createRenderer(RenderStyle*)
512 return new RenderSVGTransformableContainer(this);
515 static bool isDirectReference(const Node* node)
517 return node->hasTagName(SVGNames::pathTag)
518 || node->hasTagName(SVGNames::rectTag)
519 || node->hasTagName(SVGNames::circleTag)
520 || node->hasTagName(SVGNames::ellipseTag)
521 || node->hasTagName(SVGNames::polygonTag)
522 || node->hasTagName(SVGNames::polylineTag)
523 || node->hasTagName(SVGNames::textTag);
526 void SVGUseElement::toClipPath(Path& path)
528 ASSERT(path.isEmpty());
530 Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
534 if (n->isSVGElement() && toSVGElement(n)->isSVGGraphicsElement()) {
535 if (!isDirectReference(n)) {
536 // Spec: Indirect references are an error (14.3.5)
537 document().accessSVGExtensions()->reportError("Not allowed to use indirect reference in <clip-path>");
539 toSVGGraphicsElement(n)->toClipPath(path);
540 // FIXME: Avoid manual resolution of x/y here. Its potentially harmful.
541 SVGLengthContext lengthContext(this);
542 path.translate(FloatSize(m_x->currentValue()->value(lengthContext), m_y->currentValue()->value(lengthContext)));
543 path.transform(animatedLocalTransform());
548 RenderObject* SVGUseElement::rendererClipChild() const
550 Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
554 if (n->isSVGElement() && isDirectReference(n))
555 return toSVGElement(n)->renderer();
560 void SVGUseElement::buildInstanceTree(SVGElement* target, SVGElementInstance* targetInstance, bool& foundProblem, bool foundUse)
563 ASSERT(targetInstance);
565 // Spec: If the referenced object is itself a 'use', or if there are 'use' subelements within the referenced
566 // object, the instance tree will contain recursive expansion of the indirect references to form a complete tree.
567 bool targetHasUseTag = target->hasTagName(SVGNames::useTag);
568 SVGElement* newTarget = 0;
569 if (targetHasUseTag) {
570 foundProblem = hasCycleUseReferencing(toSVGUseElement(target), targetInstance, newTarget);
574 // We only need to track first degree <use> dependencies. Indirect references are handled
575 // as the invalidation bubbles up the dependency chain.
577 document().accessSVGExtensions()->addElementReferencingTarget(this, target);
580 } else if (isDisallowedElement(target)) {
585 // A general description from the SVG spec, describing what buildInstanceTree() actually does.
587 // Spec: If the 'use' element references a 'g' which contains two 'rect' elements, then the instance tree
588 // contains three SVGElementInstance objects, a root SVGElementInstance object whose correspondingElement
589 // is the SVGGElement object for the 'g', and then two child SVGElementInstance objects, each of which has
590 // its correspondingElement that is an SVGRectElement object.
592 for (Node* node = target->firstChild(); node; node = node->nextSibling()) {
593 SVGElement* element = 0;
594 if (node->isSVGElement())
595 element = toSVGElement(node);
597 // Skip any non-svg nodes or any disallowed element.
598 if (!element || isDisallowedElement(element))
601 // Create SVGElementInstance object, for both container/non-container nodes.
602 RefPtr<SVGElementInstance> instance = SVGElementInstance::create(this, 0, element);
603 SVGElementInstance* instancePtr = instance.get();
604 targetInstance->appendChild(instance.release());
606 // Enter recursion, appending new instance tree nodes to the "instance" object.
607 buildInstanceTree(element, instancePtr, foundProblem, foundUse);
612 if (!targetHasUseTag || !newTarget)
615 RefPtr<SVGElementInstance> newInstance = SVGElementInstance::create(this, toSVGUseElement(target), newTarget);
616 SVGElementInstance* newInstancePtr = newInstance.get();
617 targetInstance->appendChild(newInstance.release());
618 buildInstanceTree(newTarget, newInstancePtr, foundProblem, foundUse);
621 bool SVGUseElement::hasCycleUseReferencing(SVGUseElement* use, SVGElementInstance* targetInstance, SVGElement*& newTarget)
623 ASSERT(referencedDocument());
624 Element* targetElement = SVGURIReference::targetElementFromIRIString(use->hrefString(), *referencedDocument());
626 if (targetElement && targetElement->isSVGElement())
627 newTarget = toSVGElement(targetElement);
632 // Shortcut for self-references
633 if (newTarget == this)
636 AtomicString targetId = newTarget->getIdAttribute();
637 SVGElementInstance* instance = targetInstance->parentNode();
639 SVGElement* element = instance->correspondingElement();
641 if (element->hasID() && element->getIdAttribute() == targetId && element->document() == newTarget->document())
644 instance = instance->parentNode();
649 static inline void removeDisallowedElementsFromSubtree(Element& subtree)
651 ASSERT(!subtree.inDocument());
652 Element* element = ElementTraversal::firstWithin(subtree);
654 if (isDisallowedElement(element)) {
655 Element* next = ElementTraversal::nextSkippingChildren(*element, &subtree);
656 // The subtree is not in document so this won't generate events that could mutate the tree.
657 element->parentNode()->removeChild(element);
660 element = ElementTraversal::next(*element, &subtree);
665 void SVGUseElement::buildShadowTree(SVGElement* target, SVGElementInstance* targetInstance)
667 // For instance <use> on <foreignObject> (direct case).
668 if (isDisallowedElement(target))
671 RefPtr<Element> newChild = targetInstance->correspondingElement()->cloneElementWithChildren();
673 // We don't walk the target tree element-by-element, and clone each element,
674 // but instead use cloneElementWithChildren(). This is an optimization for the common
675 // case where <use> doesn't contain disallowed elements (ie. <foreignObject>).
676 // Though if there are disallowed elements in the subtree, we have to remove them.
677 // For instance: <use> on <g> containing <foreignObject> (indirect case).
678 if (subtreeContainsDisallowedElement(newChild.get()))
679 removeDisallowedElementsFromSubtree(*newChild);
681 userAgentShadowRoot()->appendChild(newChild.release());
684 void SVGUseElement::expandUseElementsInShadowTree(Node* element)
686 // Why expand the <use> elements in the shadow tree here, and not just
687 // do this directly in buildShadowTree, if we encounter a <use> element?
689 // Short answer: Because we may miss to expand some elements. Ie. if a <symbol>
690 // contains <use> tags, we'd miss them. So once we're done with settin' up the
691 // actual shadow tree (after the special case modification for svg/symbol) we have
692 // to walk it completely and expand all <use> elements.
693 if (element->hasTagName(SVGNames::useTag)) {
694 SVGUseElement* use = toSVGUseElement(element);
695 ASSERT(!use->resourceIsStillLoading());
697 ASSERT(referencedDocument());
698 Element* targetElement = SVGURIReference::targetElementFromIRIString(use->hrefString(), *referencedDocument());
699 SVGElement* target = 0;
700 if (targetElement && targetElement->isSVGElement())
701 target = toSVGElement(targetElement);
703 // Don't ASSERT(target) here, it may be "pending", too.
704 // Setup sub-shadow tree root node
705 RefPtr<SVGGElement> cloneParent = SVGGElement::create(*referencedDocument());
706 use->cloneChildNodes(cloneParent.get());
708 // Spec: In the generated content, the 'use' will be replaced by 'g', where all attributes from the
709 // 'use' element except for x, y, width, height and xlink:href are transferred to the generated 'g' element.
710 transferUseAttributesToReplacedElement(use, cloneParent.get());
712 if (target && !isDisallowedElement(target)) {
713 RefPtr<Element> newChild = target->cloneElementWithChildren();
714 ASSERT(newChild->isSVGElement());
715 cloneParent->appendChild(newChild.release());
718 // We don't walk the target tree element-by-element, and clone each element,
719 // but instead use cloneElementWithChildren(). This is an optimization for the common
720 // case where <use> doesn't contain disallowed elements (ie. <foreignObject>).
721 // Though if there are disallowed elements in the subtree, we have to remove them.
722 // For instance: <use> on <g> containing <foreignObject> (indirect case).
723 if (subtreeContainsDisallowedElement(cloneParent.get()))
724 removeDisallowedElementsFromSubtree(*cloneParent);
726 RefPtr<Node> replacingElement(cloneParent.get());
728 // Replace <use> with referenced content.
729 ASSERT(use->parentNode());
730 use->parentNode()->replaceChild(cloneParent.release(), use);
732 // Expand the siblings because the *element* is replaced and we will
733 // lose the sibling chain when we are back from recursion.
734 element = replacingElement.get();
735 for (RefPtr<Node> sibling = element->nextSibling(); sibling; sibling = sibling->nextSibling())
736 expandUseElementsInShadowTree(sibling.get());
739 for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
740 expandUseElementsInShadowTree(child.get());
743 void SVGUseElement::expandSymbolElementsInShadowTree(Node* element)
745 if (element->hasTagName(SVGNames::symbolTag)) {
746 // Spec: The referenced 'symbol' and its contents are deep-cloned into the generated tree,
747 // with the exception that the 'symbol' is replaced by an 'svg'. This generated 'svg' will
748 // always have explicit values for attributes width and height. If attributes width and/or
749 // height are provided on the 'use' element, then these attributes will be transferred to
750 // the generated 'svg'. If attributes width and/or height are not specified, the generated
751 // 'svg' element will use values of 100% for these attributes.
752 ASSERT(referencedDocument());
753 RefPtr<SVGSVGElement> svgElement = SVGSVGElement::create(*referencedDocument());
755 // Transfer all data (attributes, etc.) from <symbol> to the new <svg> element.
756 svgElement->cloneDataFromElement(*toElement(element));
758 // Only clone symbol children, and add them to the new <svg> element
759 for (Node* child = element->firstChild(); child; child = child->nextSibling()) {
760 RefPtr<Node> newChild = child->cloneNode(true);
761 svgElement->appendChild(newChild.release());
764 // We don't walk the target tree element-by-element, and clone each element,
765 // but instead use cloneNode(deep=true). This is an optimization for the common
766 // case where <use> doesn't contain disallowed elements (ie. <foreignObject>).
767 // Though if there are disallowed elements in the subtree, we have to remove them.
768 // For instance: <use> on <g> containing <foreignObject> (indirect case).
769 if (subtreeContainsDisallowedElement(svgElement.get()))
770 removeDisallowedElementsFromSubtree(*svgElement);
772 RefPtr<Node> replacingElement(svgElement.get());
774 // Replace <symbol> with <svg>.
775 element->parentNode()->replaceChild(svgElement.release(), element);
777 // Expand the siblings because the *element* is replaced and we will
778 // lose the sibling chain when we are back from recursion.
779 element = replacingElement.get();
780 for (RefPtr<Node> sibling = element->nextSibling(); sibling; sibling = sibling->nextSibling())
781 expandSymbolElementsInShadowTree(sibling.get());
784 for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
785 expandSymbolElementsInShadowTree(child.get());
788 void SVGUseElement::transferEventListenersToShadowTree(SVGElementInstance* target)
793 SVGElement* originalElement = target->correspondingElement();
794 ASSERT(originalElement);
796 if (SVGElement* shadowTreeElement = target->shadowTreeElement()) {
797 if (EventTargetData* data = originalElement->eventTargetData())
798 data->eventListenerMap.copyEventListenersNotCreatedFromMarkupToTarget(shadowTreeElement);
801 for (SVGElementInstance* instance = target->firstChild(); instance; instance = instance->nextSibling())
802 transferEventListenersToShadowTree(instance);
805 void SVGUseElement::associateInstancesWithShadowTreeElements(Node* target, SVGElementInstance* targetInstance)
807 if (!target || !targetInstance)
810 SVGElement* originalElement = targetInstance->correspondingElement();
812 if (originalElement->hasTagName(SVGNames::useTag)) {
813 // <use> gets replaced by <g>
814 ASSERT(AtomicString(target->nodeName()) == SVGNames::gTag);
815 } else if (originalElement->hasTagName(SVGNames::symbolTag)) {
816 // <symbol> gets replaced by <svg>
817 ASSERT(AtomicString(target->nodeName()) == SVGNames::svgTag);
819 ASSERT(AtomicString(target->nodeName()) == originalElement->nodeName());
822 SVGElement* element = 0;
823 if (target->isSVGElement())
824 element = toSVGElement(target);
826 ASSERT(!targetInstance->shadowTreeElement());
827 targetInstance->setShadowTreeElement(element);
828 element->setCorrespondingElement(originalElement);
830 Node* node = target->firstChild();
831 for (SVGElementInstance* instance = targetInstance->firstChild(); node && instance; instance = instance->nextSibling()) {
832 // Skip any non-svg elements in shadow tree
833 while (node && !node->isSVGElement())
834 node = node->nextSibling();
839 associateInstancesWithShadowTreeElements(node, instance);
840 node = node->nextSibling();
844 SVGElementInstance* SVGUseElement::instanceForShadowTreeElement(Node* element) const
846 if (!m_targetElementInstance) {
847 ASSERT(!inDocument());
851 return instanceForShadowTreeElement(element, m_targetElementInstance.get());
854 SVGElementInstance* SVGUseElement::instanceForShadowTreeElement(Node* element, SVGElementInstance* instance) const
859 // We're dispatching a mutation event during shadow tree construction
860 // this instance hasn't yet been associated to a shadowTree element.
861 if (!instance->shadowTreeElement())
864 if (element == instance->shadowTreeElement())
867 for (SVGElementInstance* current = instance->firstChild(); current; current = current->nextSibling()) {
868 if (SVGElementInstance* search = instanceForShadowTreeElement(element, current))
875 void SVGUseElement::invalidateShadowTree()
877 if (!inActiveDocument() || m_needsShadowTreeRecreation)
879 scheduleShadowTreeRecreation();
880 invalidateDependentShadowTrees();
883 void SVGUseElement::invalidateDependentShadowTrees()
885 // Recursively invalidate dependent <use> shadow trees
886 const HashSet<SVGElementInstance*>& instances = instancesForElement();
887 const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
888 for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
889 if (SVGUseElement* element = (*it)->correspondingUseElement()) {
890 ASSERT(element->inDocument());
891 element->invalidateShadowTree();
896 void SVGUseElement::transferUseAttributesToReplacedElement(SVGElement* from, SVGElement* to) const
901 to->cloneDataFromElement(*from);
903 to->removeAttribute(SVGNames::xAttr);
904 to->removeAttribute(SVGNames::yAttr);
905 to->removeAttribute(SVGNames::widthAttr);
906 to->removeAttribute(SVGNames::heightAttr);
907 to->removeAttribute(XLinkNames::hrefAttr);
910 bool SVGUseElement::selfHasRelativeLengths() const
912 if (m_x->currentValue()->isRelative()
913 || m_y->currentValue()->isRelative()
914 || m_width->currentValue()->isRelative()
915 || m_height->currentValue()->isRelative())
918 if (!m_targetElementInstance)
921 SVGElement* element = m_targetElementInstance->correspondingElement();
925 return element->hasRelativeLengths();
928 void SVGUseElement::notifyFinished(Resource* resource)
933 invalidateShadowTree();
934 if (resource->errorOccurred())
935 dispatchEvent(Event::create(EventTypeNames::error));
936 else if (!resource->wasCanceled()) {
937 if (m_wasInsertedByParser && m_haveFiredLoadEvent)
939 if (!isStructurallyExternal())
942 ASSERT(!m_haveFiredLoadEvent);
943 m_haveFiredLoadEvent = true;
944 sendSVGLoadEventIfPossible();
948 bool SVGUseElement::resourceIsStillLoading()
950 if (m_resource && m_resource->isLoading())
955 bool SVGUseElement::instanceTreeIsLoading(SVGElementInstance* targetElementInstance)
957 for (SVGElementInstance* instance = targetElementInstance->firstChild(); instance; instance = instance->nextSibling()) {
958 if (SVGUseElement* use = instance->correspondingUseElement()) {
959 if (use->resourceIsStillLoading())
962 if (instance->hasChildNodes())
963 instanceTreeIsLoading(instance);
968 void SVGUseElement::finishParsingChildren()
970 SVGGraphicsElement::finishParsingChildren();
971 if (m_wasInsertedByParser) {
972 buildPendingResource();
973 m_wasInsertedByParser = false;
977 void SVGUseElement::setDocumentResource(ResourcePtr<DocumentResource> resource)
979 if (m_resource == resource)
983 m_resource->removeClient(this);
985 m_resource = resource;
987 m_resource->addClient(this);