#include "config.h"
#if ENABLE(SVG)
#include "SVGUseElement.h"
#include "Attribute.h"
#include "CachedResourceLoader.h"
#include "CachedResourceRequest.h"
#include "CachedSVGDocument.h"
#include "Document.h"
#include "ElementShadow.h"
#include "Event.h"
#include "EventListener.h"
#include "HTMLNames.h"
#include "NodeRenderStyle.h"
#include "NodeTraversal.h"
#include "RegisteredEventListener.h"
#include "RenderSVGResource.h"
#include "RenderSVGTransformableContainer.h"
#include "ShadowRoot.h"
#include "SVGElementInstance.h"
#include "SVGElementRareData.h"
#include "SVGElementInstanceList.h"
#include "SVGGElement.h"
#include "SVGLengthContext.h"
#include "SVGNames.h"
#include "SVGSMILElement.h"
#include "SVGSVGElement.h"
#include "SVGSymbolElement.h"
#include "StyleResolver.h"
#include "XLinkNames.h"
#include "XMLDocumentParser.h"
#include "XMLSerializer.h"
namespace WebCore {
DEFINE_ANIMATED_LENGTH(SVGUseElement, SVGNames::xAttr, X, x)
DEFINE_ANIMATED_LENGTH(SVGUseElement, SVGNames::yAttr, Y, y)
DEFINE_ANIMATED_LENGTH(SVGUseElement, SVGNames::widthAttr, Width, width)
DEFINE_ANIMATED_LENGTH(SVGUseElement, SVGNames::heightAttr, Height, height)
DEFINE_ANIMATED_STRING(SVGUseElement, XLinkNames::hrefAttr, Href, href)
DEFINE_ANIMATED_BOOLEAN(SVGUseElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGUseElement)
REGISTER_LOCAL_ANIMATED_PROPERTY(x)
REGISTER_LOCAL_ANIMATED_PROPERTY(y)
REGISTER_LOCAL_ANIMATED_PROPERTY(width)
REGISTER_LOCAL_ANIMATED_PROPERTY(height)
REGISTER_LOCAL_ANIMATED_PROPERTY(href)
REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
REGISTER_PARENT_ANIMATED_PROPERTIES(SVGStyledTransformableElement)
REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
END_REGISTER_ANIMATED_PROPERTIES
inline SVGUseElement::SVGUseElement(const QualifiedName& tagName, Document* document, bool wasInsertedByParser)
: SVGStyledTransformableElement(tagName, document)
, m_x(LengthModeWidth)
, m_y(LengthModeHeight)
, m_width(LengthModeWidth)
, m_height(LengthModeHeight)
, m_wasInsertedByParser(wasInsertedByParser)
, m_haveFiredLoadEvent(false)
, m_needsShadowTreeRecreation(false)
, m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
{
ASSERT(hasCustomStyleCallbacks());
ASSERT(hasTagName(SVGNames::useTag));
registerAnimatedPropertiesForSVGUseElement();
}
PassRefPtr<SVGUseElement> SVGUseElement::create(const QualifiedName& tagName, Document* document, bool wasInsertedByParser)
{
RefPtr<SVGUseElement> use = adoptRef(new SVGUseElement(tagName, document, wasInsertedByParser));
use->ensureUserAgentShadowRoot();
return use.release();
}
SVGUseElement::~SVGUseElement()
{
setCachedDocument(0);
clearResourceReferences();
}
SVGElementInstance* SVGUseElement::instanceRoot()
{
if (!m_targetElementInstance)
document()->updateLayoutIgnorePendingStylesheets();
return m_targetElementInstance.get();
}
SVGElementInstance* SVGUseElement::animatedInstanceRoot() const
{
return 0;
}
bool SVGUseElement::isSupportedAttribute(const QualifiedName& attrName)
{
DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
if (supportedAttributes.isEmpty()) {
SVGTests::addSupportedAttributes(supportedAttributes);
SVGLangSpace::addSupportedAttributes(supportedAttributes);
SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
SVGURIReference::addSupportedAttributes(supportedAttributes);
supportedAttributes.add(SVGNames::xAttr);
supportedAttributes.add(SVGNames::yAttr);
supportedAttributes.add(SVGNames::widthAttr);
supportedAttributes.add(SVGNames::heightAttr);
}
return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
}
void SVGUseElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
SVGParsingError parseError = NoError;
if (!isSupportedAttribute(name))
SVGStyledTransformableElement::parseAttribute(name, value);
else if (name == SVGNames::xAttr)
setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
else if (name == SVGNames::yAttr)
setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
else if (name == SVGNames::widthAttr)
setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
else if (name == SVGNames::heightAttr)
setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
else if (SVGTests::parseAttribute(name, value)
|| SVGLangSpace::parseAttribute(name, value)
|| SVGExternalResourcesRequired::parseAttribute(name, value)
|| SVGURIReference::parseAttribute(name, value)) {
} else
ASSERT_NOT_REACHED();
reportAttributeParsingError(parseError, name, value);
}
static inline bool isWellFormedDocument(Document* document)
{
if (document->isSVGDocument() || document->isXHTMLDocument())
return static_cast<XMLDocumentParser*>(document->parser())->wellFormed();
return true;
}
Node::InsertionNotificationRequest SVGUseElement::insertedInto(ContainerNode* rootParent)
{
SVGStyledTransformableElement::insertedInto(rootParent);
if (!rootParent->inDocument())
return InsertionDone;
ASSERT(!m_targetElementInstance || !isWellFormedDocument(document()));
ASSERT(!hasPendingResources() || !isWellFormedDocument(document()));
if (!m_wasInsertedByParser)
buildPendingResource();
SVGExternalResourcesRequired::insertedIntoDocument(this);
return InsertionDone;
}
void SVGUseElement::removedFrom(ContainerNode* rootParent)
{
SVGStyledTransformableElement::removedFrom(rootParent);
if (rootParent->inDocument())
clearResourceReferences();
}
Document* SVGUseElement::referencedDocument() const
{
if (!isExternalURIReference(href(), document()))
return document();
return externalDocument();
}
Document* SVGUseElement::externalDocument() const
{
if (m_cachedDocument && m_cachedDocument->isLoaded()) {
if (m_cachedDocument->errorOccurred())
return 0;
ASSERT(m_cachedDocument->document());
return m_cachedDocument->document();
}
return 0;
}
void SVGUseElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (!isSupportedAttribute(attrName)) {
SVGStyledTransformableElement::svgAttributeChanged(attrName);
return;
}
SVGElementInstance::InvalidationGuard invalidationGuard(this);
RenderObject* renderer = this->renderer();
if (attrName == SVGNames::xAttr
|| attrName == SVGNames::yAttr
|| attrName == SVGNames::widthAttr
|| attrName == SVGNames::heightAttr) {
updateRelativeLengthsInformation();
if (renderer)
RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
return;
}
if (SVGTests::handleAttributeChange(this, attrName))
return;
if (SVGExternalResourcesRequired::handleAttributeChange(this, attrName))
return;
if (SVGURIReference::isKnownAttribute(attrName)) {
bool isExternalReference = isExternalURIReference(href(), document());
if (isExternalReference) {
KURL url = document()->completeURL(href());
if (url.hasFragmentIdentifier()) {
CachedResourceRequest request(ResourceRequest(url.string()));
request.setInitiator(this);
setCachedDocument(document()->cachedResourceLoader()->requestSVGDocument(request));
}
} else
setCachedDocument(0);
if (!m_wasInsertedByParser)
buildPendingResource();
return;
}
if (!renderer)
return;
if (SVGLangSpace::isKnownAttribute(attrName)
|| SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
invalidateShadowTree();
return;
}
ASSERT_NOT_REACHED();
}
bool SVGUseElement::willRecalcStyle(StyleChange)
{
if (!m_wasInsertedByParser && m_needsShadowTreeRecreation && renderer() && needsStyleRecalc())
buildPendingResource();
return true;
}
#ifdef DUMP_INSTANCE_TREE
static void dumpInstanceTree(unsigned int& depth, String& text, SVGElementInstance* targetInstance)
{
SVGElement* element = targetInstance->correspondingElement();
ASSERT(element);
if (element->hasTagName(SVGNames::useTag)) {
if (static_cast<SVGUseElement*>(element)->cachedDocumentIsStillLoading())
return;
}
SVGElement* shadowTreeElement = targetInstance->shadowTreeElement();
ASSERT(shadowTreeElement);
SVGUseElement* directUseElement = targetInstance->directUseElement();
String directUseElementName = directUseElement ? directUseElement->nodeName() : "null";
String elementId = element->getIdAttribute();
String elementNodeName = element->nodeName();
String shadowTreeElementNodeName = shadowTreeElement->nodeName();
String parentNodeName = element->parentNode() ? element->parentNode()->nodeName() : "null";
String firstChildNodeName = element->firstChild() ? element->firstChild()->nodeName() : "null";
for (unsigned int i = 0; i < depth; ++i)
text += " ";
text += String::format("SVGElementInstance this=%p, (parentNode=%s (%p), firstChild=%s (%p), correspondingElement=%s (%p), directUseElement=%s (%p), shadowTreeElement=%s (%p), id=%s)\n",
targetInstance, parentNodeName.latin1().data(), element->parentNode(), firstChildNodeName.latin1().data(), element->firstChild(),
elementNodeName.latin1().data(), element, directUseElementName.latin1().data(), directUseElement, shadowTreeElementNodeName.latin1().data(), shadowTreeElement, elementId.latin1().data());
for (unsigned int i = 0; i < depth; ++i)
text += " ";
const HashSet<SVGElementInstance*>& elementInstances = element->instancesForElement();
text += "Corresponding element is associated with " + String::number(elementInstances.size()) + " instance(s):\n";
const HashSet<SVGElementInstance*>::const_iterator end = elementInstances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = elementInstances.begin(); it != end; ++it) {
for (unsigned int i = 0; i < depth; ++i)
text += " ";
text += String::format(" -> SVGElementInstance this=%p, (refCount: %i, shadowTreeElement in document? %i)\n",
*it, (*it)->refCount(), (*it)->shadowTreeElement()->inDocument());
}
++depth;
for (SVGElementInstance* instance = targetInstance->firstChild(); instance; instance = instance->nextSibling())
dumpInstanceTree(depth, text, instance);
--depth;
}
#endif
static bool isDisallowedElement(Node* node)
{
if (node->isShadowRoot() || node->isTextNode())
return false;
if (!node->isSVGElement())
return true;
Element* element = toElement(node);
DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, allowedElementTags, ());
if (allowedElementTags.isEmpty()) {
allowedElementTags.add(SVGNames::aTag);
allowedElementTags.add(SVGNames::circleTag);
allowedElementTags.add(SVGNames::descTag);
allowedElementTags.add(SVGNames::ellipseTag);
allowedElementTags.add(SVGNames::gTag);
allowedElementTags.add(SVGNames::imageTag);
allowedElementTags.add(SVGNames::lineTag);
allowedElementTags.add(SVGNames::metadataTag);
allowedElementTags.add(SVGNames::pathTag);
allowedElementTags.add(SVGNames::polygonTag);
allowedElementTags.add(SVGNames::polylineTag);
allowedElementTags.add(SVGNames::rectTag);
allowedElementTags.add(SVGNames::svgTag);
allowedElementTags.add(SVGNames::switchTag);
allowedElementTags.add(SVGNames::symbolTag);
allowedElementTags.add(SVGNames::textTag);
allowedElementTags.add(SVGNames::textPathTag);
allowedElementTags.add(SVGNames::titleTag);
allowedElementTags.add(SVGNames::trefTag);
allowedElementTags.add(SVGNames::tspanTag);
allowedElementTags.add(SVGNames::useTag);
}
return !allowedElementTags.contains<QualifiedName, SVGAttributeHashTranslator>(element->tagQName());
}
static bool subtreeContainsDisallowedElement(Node* start)
{
if (isDisallowedElement(start))
return true;
for (Node* cur = start->firstChild(); cur; cur = cur->nextSibling()) {
if (subtreeContainsDisallowedElement(cur))
return true;
}
return false;
}
void SVGUseElement::clearResourceReferences()
{
if (ShadowRoot* shadowTreeRootElement = shadow()->shadowRoot())
shadowTreeRootElement->removeChildren();
if (m_targetElementInstance) {
m_targetElementInstance->detach();
m_targetElementInstance = 0;
}
m_needsShadowTreeRecreation = false;
ASSERT(document());
document()->accessSVGExtensions()->removeAllTargetReferencesForElement(this);
}
void SVGUseElement::buildPendingResource()
{
if (!referencedDocument() || isInShadowTree())
return;
clearResourceReferences();
if (!inDocument())
return;
String id;
Element* target = SVGURIReference::targetElementFromIRIString(href(), document(), &id, externalDocument());
if (!target || !target->inDocument()) {
if (externalDocument())
return;
if (id.isEmpty())
return;
referencedDocument()->accessSVGExtensions()->addPendingResource(id, this);
ASSERT(hasPendingResources());
return;
}
if (target->isSVGElement()) {
buildShadowAndInstanceTree(toSVGElement(target));
invalidateDependentShadowTrees();
}
ASSERT(!m_needsShadowTreeRecreation);
}
void SVGUseElement::buildShadowAndInstanceTree(SVGElement* target)
{
ASSERT(!m_targetElementInstance);
if (isInShadowTree())
return;
if (!target || target == this)
return;
m_targetElementInstance = SVGElementInstance::create(this, this, target);
bool foundProblem = false;
buildInstanceTree(target, m_targetElementInstance.get(), foundProblem, false);
if (instanceTreeIsLoading(m_targetElementInstance.get()))
return;
if (foundProblem) {
clearResourceReferences();
return;
}
ASSERT(m_targetElementInstance);
ASSERT(!m_targetElementInstance->shadowTreeElement());
ASSERT(m_targetElementInstance->correspondingUseElement() == this);
ASSERT(m_targetElementInstance->directUseElement() == this);
ASSERT(m_targetElementInstance->correspondingElement() == target);
ShadowRoot* shadowTreeRootElement = shadow()->shadowRoot();
ASSERT(shadowTreeRootElement);
buildShadowTree(target, m_targetElementInstance.get());
expandUseElementsInShadowTree(shadowTreeRootElement);
expandSymbolElementsInShadowTree(shadowTreeRootElement);
associateInstancesWithShadowTreeElements(shadowTreeRootElement->firstChild(), m_targetElementInstance.get());
if (!m_targetElementInstance->shadowTreeElement()) {
clearResourceReferences();
return;
}
ASSERT(m_targetElementInstance->shadowTreeElement()->parentNode() == shadowTreeRootElement);
transferEventListenersToShadowTree(m_targetElementInstance.get());
updateRelativeLengthsInformation();
#ifdef DUMP_INSTANCE_TREE
String text;
unsigned int depth = 0;
dumpInstanceTree(depth, text, m_targetElementInstance.get());
fprintf(stderr, "\nDumping <use> instance tree:\n%s\n", text.latin1().data());
#endif
#ifdef DUMP_SHADOW_TREE
RefPtr<XMLSerializer> serializer = XMLSerializer::create();
String markup = serializer->serializeToString(shadowTreeRootElement, ASSERT_NO_EXCEPTION);
fprintf(stderr, "Dumping <use> shadow tree markup:\n%s\n", markup.latin1().data());
#endif
}
RenderObject* SVGUseElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderSVGTransformableContainer(this);
}
static bool isDirectReference(const Node* node)
{
return node->hasTagName(SVGNames::pathTag)
|| node->hasTagName(SVGNames::rectTag)
|| node->hasTagName(SVGNames::circleTag)
|| node->hasTagName(SVGNames::ellipseTag)
|| node->hasTagName(SVGNames::polygonTag)
|| node->hasTagName(SVGNames::polylineTag)
|| node->hasTagName(SVGNames::textTag);
}
void SVGUseElement::toClipPath(Path& path)
{
ASSERT(path.isEmpty());
Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
if (!n)
return;
if (n->isSVGElement() && toSVGElement(n)->isStyledTransformable()) {
if (!isDirectReference(n))
document()->accessSVGExtensions()->reportError("Not allowed to use indirect reference in <clip-path>");
else {
toSVGStyledTransformableElement(n)->toClipPath(path);
SVGLengthContext lengthContext(this);
path.translate(FloatSize(x().value(lengthContext), y().value(lengthContext)));
path.transform(animatedLocalTransform());
}
}
}
RenderObject* SVGUseElement::rendererClipChild() const
{
Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
if (!n)
return 0;
if (n->isSVGElement() && isDirectReference(n))
return toSVGElement(n)->renderer();
return 0;
}
void SVGUseElement::buildInstanceTree(SVGElement* target, SVGElementInstance* targetInstance, bool& foundProblem, bool foundUse)
{
ASSERT(target);
ASSERT(targetInstance);
bool targetHasUseTag = target->hasTagName(SVGNames::useTag);
SVGElement* newTarget = 0;
if (targetHasUseTag) {
foundProblem = hasCycleUseReferencing(static_cast<SVGUseElement*>(target), targetInstance, newTarget);
if (foundProblem)
return;
if (!foundUse) {
ASSERT(document());
document()->accessSVGExtensions()->addElementReferencingTarget(this, target);
foundUse = true;
}
} else if (isDisallowedElement(target)) {
foundProblem = true;
return;
}
for (Node* node = target->firstChild(); node; node = node->nextSibling()) {
SVGElement* element = 0;
if (node->isSVGElement())
element = toSVGElement(node);
if (!element || isDisallowedElement(element))
continue;
RefPtr<SVGElementInstance> instance = SVGElementInstance::create(this, 0, element);
SVGElementInstance* instancePtr = instance.get();
targetInstance->appendChild(instance.release());
buildInstanceTree(element, instancePtr, foundProblem, foundUse);
if (foundProblem)
return;
}
if (!targetHasUseTag || !newTarget)
return;
RefPtr<SVGElementInstance> newInstance = SVGElementInstance::create(this, static_cast<SVGUseElement*>(target), newTarget);
SVGElementInstance* newInstancePtr = newInstance.get();
targetInstance->appendChild(newInstance.release());
buildInstanceTree(newTarget, newInstancePtr, foundProblem, foundUse);
}
bool SVGUseElement::hasCycleUseReferencing(SVGUseElement* use, SVGElementInstance* targetInstance, SVGElement*& newTarget)
{
Element* targetElement = SVGURIReference::targetElementFromIRIString(use->href(), referencedDocument());
newTarget = 0;
if (targetElement && targetElement->isSVGElement())
newTarget = toSVGElement(targetElement);
if (!newTarget)
return false;
if (newTarget == this)
return true;
AtomicString targetId = newTarget->getIdAttribute();
SVGElementInstance* instance = targetInstance->parentNode();
while (instance) {
SVGElement* element = instance->correspondingElement();
if (element->hasID() && element->getIdAttribute() == targetId && element->document() == newTarget->document())
return true;
instance = instance->parentNode();
}
return false;
}
static inline void removeDisallowedElementsFromSubtree(Element* subtree)
{
ASSERT(!subtree->inDocument());
Element* element = ElementTraversal::firstWithin(subtree);
while (element) {
if (isDisallowedElement(element)) {
Element* next = ElementTraversal::nextSkippingChildren(element, subtree);
element->parentNode()->removeChild(element);
element = next;
} else
element = ElementTraversal::next(element, subtree);
}
}
void SVGUseElement::buildShadowTree(SVGElement* target, SVGElementInstance* targetInstance)
{
if (isDisallowedElement(target))
return;
RefPtr<Element> newChild = targetInstance->correspondingElement()->cloneElementWithChildren();
if (subtreeContainsDisallowedElement(newChild.get()))
removeDisallowedElementsFromSubtree(newChild.get());
shadow()->shadowRoot()->appendChild(newChild.release());
}
void SVGUseElement::expandUseElementsInShadowTree(Node* element)
{
if (element->hasTagName(SVGNames::useTag)) {
SVGUseElement* use = static_cast<SVGUseElement*>(element);
ASSERT(!use->cachedDocumentIsStillLoading());
Element* targetElement = SVGURIReference::targetElementFromIRIString(use->href(), referencedDocument());
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = toSVGElement(targetElement);
RefPtr<SVGGElement> cloneParent = SVGGElement::create(SVGNames::gTag, referencedDocument());
use->cloneChildNodes(cloneParent.get());
transferUseAttributesToReplacedElement(use, cloneParent.get());
if (target && !isDisallowedElement(target)) {
RefPtr<Element> newChild = target->cloneElementWithChildren();
ASSERT(newChild->isSVGElement());
cloneParent->appendChild(newChild.release());
}
if (subtreeContainsDisallowedElement(cloneParent.get()))
removeDisallowedElementsFromSubtree(cloneParent.get());
RefPtr<Node> replacingElement(cloneParent.get());
ASSERT(use->parentNode());
use->parentNode()->replaceChild(cloneParent.release(), use);
element = replacingElement.get();
for (RefPtr<Node> sibling = element->nextSibling(); sibling; sibling = sibling->nextSibling())
expandUseElementsInShadowTree(sibling.get());
}
for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
expandUseElementsInShadowTree(child.get());
}
void SVGUseElement::expandSymbolElementsInShadowTree(Node* element)
{
if (element->hasTagName(SVGNames::symbolTag)) {
RefPtr<SVGSVGElement> svgElement = SVGSVGElement::create(SVGNames::svgTag, referencedDocument());
svgElement->cloneDataFromElement(*toElement(element));
for (Node* child = element->firstChild(); child; child = child->nextSibling()) {
RefPtr<Node> newChild = child->cloneNode(true);
svgElement->appendChild(newChild.release());
}
if (subtreeContainsDisallowedElement(svgElement.get()))
removeDisallowedElementsFromSubtree(svgElement.get());
RefPtr<Node> replacingElement(svgElement.get());
element->parentNode()->replaceChild(svgElement.release(), element);
element = replacingElement.get();
for (RefPtr<Node> sibling = element->nextSibling(); sibling; sibling = sibling->nextSibling())
expandSymbolElementsInShadowTree(sibling.get());
}
for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
expandSymbolElementsInShadowTree(child.get());
}
void SVGUseElement::transferEventListenersToShadowTree(SVGElementInstance* target)
{
if (!target)
return;
SVGElement* originalElement = target->correspondingElement();
ASSERT(originalElement);
if (SVGElement* shadowTreeElement = target->shadowTreeElement()) {
if (EventTargetData* data = originalElement->eventTargetData())
data->eventListenerMap.copyEventListenersNotCreatedFromMarkupToTarget(shadowTreeElement);
}
for (SVGElementInstance* instance = target->firstChild(); instance; instance = instance->nextSibling())
transferEventListenersToShadowTree(instance);
}
void SVGUseElement::associateInstancesWithShadowTreeElements(Node* target, SVGElementInstance* targetInstance)
{
if (!target || !targetInstance)
return;
SVGElement* originalElement = targetInstance->correspondingElement();
if (originalElement->hasTagName(SVGNames::useTag)) {
ASSERT(target->nodeName() == SVGNames::gTag);
} else if (originalElement->hasTagName(SVGNames::symbolTag)) {
ASSERT(target->nodeName() == SVGNames::svgTag);
} else
ASSERT(target->nodeName() == originalElement->nodeName());
SVGElement* element = 0;
if (target->isSVGElement())
element = toSVGElement(target);
ASSERT(!targetInstance->shadowTreeElement());
targetInstance->setShadowTreeElement(element);
element->setCorrespondingElement(originalElement);
Node* node = target->firstChild();
for (SVGElementInstance* instance = targetInstance->firstChild(); node && instance; instance = instance->nextSibling()) {
while (node && !node->isSVGElement())
node = node->nextSibling();
if (!node)
break;
associateInstancesWithShadowTreeElements(node, instance);
node = node->nextSibling();
}
}
SVGElementInstance* SVGUseElement::instanceForShadowTreeElement(Node* element) const
{
if (!m_targetElementInstance) {
ASSERT(!inDocument());
return 0;
}
return instanceForShadowTreeElement(element, m_targetElementInstance.get());
}
SVGElementInstance* SVGUseElement::instanceForShadowTreeElement(Node* element, SVGElementInstance* instance) const
{
ASSERT(element);
ASSERT(instance);
if (!instance->shadowTreeElement())
return 0;
if (element == instance->shadowTreeElement())
return instance;
for (SVGElementInstance* current = instance->firstChild(); current; current = current->nextSibling()) {
if (SVGElementInstance* search = instanceForShadowTreeElement(element, current))
return search;
}
return 0;
}
void SVGUseElement::invalidateShadowTree()
{
if (!renderer() || m_needsShadowTreeRecreation)
return;
m_needsShadowTreeRecreation = true;
setNeedsStyleRecalc();
invalidateDependentShadowTrees();
}
void SVGUseElement::invalidateDependentShadowTrees()
{
const HashSet<SVGElementInstance*>& instances = instancesForElement();
const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
if (SVGUseElement* element = (*it)->correspondingUseElement()) {
ASSERT(element->inDocument());
element->invalidateShadowTree();
}
}
}
void SVGUseElement::transferUseAttributesToReplacedElement(SVGElement* from, SVGElement* to) const
{
ASSERT(from);
ASSERT(to);
to->cloneDataFromElement(*from);
to->removeAttribute(SVGNames::xAttr);
to->removeAttribute(SVGNames::yAttr);
to->removeAttribute(SVGNames::widthAttr);
to->removeAttribute(SVGNames::heightAttr);
to->removeAttribute(XLinkNames::hrefAttr);
}
bool SVGUseElement::selfHasRelativeLengths() const
{
if (x().isRelative()
|| y().isRelative()
|| width().isRelative()
|| height().isRelative())
return true;
if (!m_targetElementInstance)
return false;
SVGElement* element = m_targetElementInstance->correspondingElement();
if (!element || !element->isSVGStyledElement())
return false;
return toSVGStyledElement(element)->hasRelativeLengths();
}
void SVGUseElement::notifyFinished(CachedResource* resource)
{
if (!inDocument())
return;
invalidateShadowTree();
if (resource->errorOccurred())
dispatchEvent(Event::create(eventNames().errorEvent, false, false));
else if (!resource->wasCanceled())
SVGExternalResourcesRequired::dispatchLoadEvent(this);
}
bool SVGUseElement::cachedDocumentIsStillLoading()
{
if (m_cachedDocument && m_cachedDocument->isLoading())
return true;
return false;
}
bool SVGUseElement::instanceTreeIsLoading(SVGElementInstance* targetElementInstance)
{
for (SVGElementInstance* instance = targetElementInstance->firstChild(); instance; instance = instance->nextSibling()) {
if (SVGUseElement* use = instance->correspondingUseElement()) {
if (use->cachedDocumentIsStillLoading())
return true;
}
if (instance->hasChildNodes())
instanceTreeIsLoading(instance);
}
return false;
}
void SVGUseElement::finishParsingChildren()
{
SVGStyledTransformableElement::finishParsingChildren();
SVGExternalResourcesRequired::finishParsingChildren();
if (m_wasInsertedByParser) {
buildPendingResource();
m_wasInsertedByParser = false;
}
}
void SVGUseElement::setCachedDocument(CachedResourceHandle<CachedSVGDocument> cachedDocument)
{
if (m_cachedDocument == cachedDocument)
return;
if (m_cachedDocument)
m_cachedDocument->removeClient(this);
m_cachedDocument = cachedDocument;
if (m_cachedDocument)
m_cachedDocument->addClient(this);
}
}
#endif // ENABLE(SVG)