#include "config.h"
#if ENABLE(SVG)
#include "SVGUseElement.h"
#include "CSSStyleSelector.h"
#include "Document.h"
#include "Event.h"
#include "EventListener.h"
#include "HTMLNames.h"
#include "MappedAttribute.h"
#include "NodeRenderStyle.h"
#include "RegisteredEventListener.h"
#include "RenderSVGShadowTreeRootContainer.h"
#include "SVGElementInstance.h"
#include "SVGElementInstanceList.h"
#include "SVGGElement.h"
#include "SVGLength.h"
#include "SVGPreserveAspectRatio.h"
#include "SVGShadowTreeElements.h"
#include "SVGSMILElement.h"
#include "SVGSVGElement.h"
#include "SVGSymbolElement.h"
#include "XLinkNames.h"
#include "XMLSerializer.h"
#include "XMLTokenizer.h"
namespace WebCore {
SVGUseElement::SVGUseElement(const QualifiedName& tagName, Document* doc)
: SVGStyledTransformableElement(tagName, doc)
, SVGTests()
, SVGLangSpace()
, SVGExternalResourcesRequired()
, SVGURIReference()
, m_x(LengthModeWidth)
, m_y(LengthModeHeight)
, m_width(LengthModeWidth)
, m_height(LengthModeHeight)
, m_isPendingResource(false)
, m_needsShadowTreeRecreation(false)
{
}
SVGUseElement::~SVGUseElement()
{
}
SVGElementInstance* SVGUseElement::instanceRoot() const
{
if (!m_targetElementInstance)
document()->updateLayoutIgnorePendingStylesheets();
return m_targetElementInstance.get();
}
SVGElementInstance* SVGUseElement::animatedInstanceRoot() const
{
return 0;
}
void SVGUseElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == SVGNames::xAttr)
setXBaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::yAttr)
setYBaseValue(SVGLength(LengthModeHeight, attr->value()));
else if (attr->name() == SVGNames::widthAttr) {
setWidthBaseValue(SVGLength(LengthModeWidth, attr->value()));
if (widthBaseValue().value(this) < 0.0)
document()->accessSVGExtensions()->reportError("A negative value for use attribute <width> is not allowed");
} else if (attr->name() == SVGNames::heightAttr) {
setHeightBaseValue(SVGLength(LengthModeHeight, attr->value()));
if (heightBaseValue().value(this) < 0.0)
document()->accessSVGExtensions()->reportError("A negative value for use attribute <height> is not allowed");
} else {
if (SVGTests::parseMappedAttribute(attr))
return;
if (SVGLangSpace::parseMappedAttribute(attr))
return;
if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
return;
if (SVGURIReference::parseMappedAttribute(attr))
return;
SVGStyledTransformableElement::parseMappedAttribute(attr);
}
}
void SVGUseElement::insertedIntoDocument()
{
SVGElement::insertedIntoDocument();
ASSERT(!m_targetElementInstance || ((document()->isSVGDocument() || document()->isXHTMLDocument()) && !static_cast<XMLTokenizer*>(document()->tokenizer())->wellFormed()));
ASSERT(!m_isPendingResource);
}
void SVGUseElement::removedFromDocument()
{
SVGElement::removedFromDocument();
detachInstance();
}
void SVGUseElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGStyledTransformableElement::svgAttributeChanged(attrName);
if (!renderer())
return;
if (SVGURIReference::isKnownAttribute(attrName)) {
if (m_isPendingResource) {
document()->accessSVGExtensions()->removePendingResource(m_resourceId);
m_resourceId = String();
m_isPendingResource = false;
}
invalidateShadowTree();
return;
}
if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr) {
updateContainerOffsets();
return;
}
if (attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr) {
updateContainerSizes();
return;
}
if (SVGStyledElement::isKnownAttribute(attrName)) {
setNeedsStyleRecalc();
return;
}
if (SVGStyledTransformableElement::isKnownAttribute(attrName)) {
renderer()->setNeedsTransformUpdate();
renderer()->setNeedsLayout(true);
return;
}
if (SVGTests::isKnownAttribute(attrName)
|| SVGLangSpace::isKnownAttribute(attrName)
|| SVGExternalResourcesRequired::isKnownAttribute(attrName))
invalidateShadowTree();
}
void SVGUseElement::synchronizeProperty(const QualifiedName& attrName)
{
SVGStyledTransformableElement::synchronizeProperty(attrName);
if (attrName == anyQName()) {
synchronizeX();
synchronizeY();
synchronizeWidth();
synchronizeHeight();
synchronizeExternalResourcesRequired();
synchronizeHref();
return;
}
if (attrName == SVGNames::xAttr)
synchronizeX();
else if (attrName == SVGNames::yAttr)
synchronizeY();
else if (attrName == SVGNames::widthAttr)
synchronizeWidth();
else if (attrName == SVGNames::heightAttr)
synchronizeHeight();
else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
synchronizeExternalResourcesRequired();
else if (SVGURIReference::isKnownAttribute(attrName))
synchronizeHref();
}
static void updateContainerSize(SVGUseElement* useElement, SVGElementInstance* targetInstance)
{
for (SVGElementInstance* instance = targetInstance->firstChild(); instance; instance = instance->nextSibling())
updateContainerSize(useElement, instance);
SVGElement* correspondingElement = targetInstance->correspondingElement();
ASSERT(correspondingElement);
bool isSymbolTag = correspondingElement->hasTagName(SVGNames::symbolTag);
if (!correspondingElement->hasTagName(SVGNames::svgTag) && !isSymbolTag)
return;
SVGElement* shadowTreeElement = targetInstance->shadowTreeElement();
ASSERT(shadowTreeElement);
ASSERT(shadowTreeElement->hasTagName(SVGNames::svgTag));
if (useElement->hasAttribute(SVGNames::widthAttr))
shadowTreeElement->setAttribute(SVGNames::widthAttr, useElement->getAttribute(SVGNames::widthAttr));
else if (isSymbolTag && shadowTreeElement->hasAttribute(SVGNames::widthAttr))
shadowTreeElement->setAttribute(SVGNames::widthAttr, "100%");
if (useElement->hasAttribute(SVGNames::heightAttr))
shadowTreeElement->setAttribute(SVGNames::heightAttr, useElement->getAttribute(SVGNames::heightAttr));
else if (isSymbolTag && shadowTreeElement->hasAttribute(SVGNames::heightAttr))
shadowTreeElement->setAttribute(SVGNames::heightAttr, "100%");
}
void SVGUseElement::updateContainerSizes()
{
if (!m_targetElementInstance)
return;
updateContainerSize(this, m_targetElementInstance.get());
if (renderer())
renderer()->setNeedsLayout(true);
}
static void updateContainerOffset(SVGElementInstance* targetInstance)
{
for (SVGElementInstance* instance = targetInstance->firstChild(); instance; instance = instance->nextSibling())
updateContainerOffset(instance);
SVGElement* correspondingElement = targetInstance->correspondingElement();
ASSERT(correspondingElement);
if (!correspondingElement->hasTagName(SVGNames::useTag))
return;
SVGElement* shadowTreeElement = targetInstance->shadowTreeElement();
ASSERT(shadowTreeElement);
ASSERT(shadowTreeElement->hasTagName(SVGNames::gTag));
if (!static_cast<SVGGElement*>(shadowTreeElement)->isShadowTreeContainerElement())
return;
SVGUseElement* useElement = static_cast<SVGUseElement*>(correspondingElement);
SVGShadowTreeContainerElement* containerElement = static_cast<SVGShadowTreeContainerElement*>(shadowTreeElement);
containerElement->setContainerOffset(useElement->x(), useElement->y());
}
void SVGUseElement::updateContainerOffsets()
{
if (!m_targetElementInstance)
return;
SVGElement* shadowRoot = m_targetElementInstance->shadowTreeElement();
ASSERT(shadowRoot);
Node* parentNode = shadowRoot->parentNode();
ASSERT(parentNode);
ASSERT(parentNode->isSVGElement());
ASSERT(parentNode->hasTagName(SVGNames::gTag));
ASSERT(static_cast<SVGGElement*>(parentNode)->isShadowTreeContainerElement());
SVGShadowTreeContainerElement* containerElement = static_cast<SVGShadowTreeContainerElement*>(parentNode);
containerElement->setContainerOffset(x(), y());
updateContainerOffset(m_targetElementInstance.get());
if (renderer())
renderer()->setNeedsLayout(true);
}
void SVGUseElement::recalcStyle(StyleChange change)
{
if (needsStyleRecalc() && m_targetElementInstance) {
if (SVGElement* shadowRoot = m_targetElementInstance->shadowTreeElement())
shadowRoot->setNeedsStyleRecalc();
}
SVGStyledTransformableElement::recalcStyle(change);
bool needsStyleUpdate = !m_needsShadowTreeRecreation;
if (m_needsShadowTreeRecreation) {
static_cast<RenderSVGShadowTreeRootContainer*>(renderer())->markShadowTreeForRecreation();
m_needsShadowTreeRecreation = false;
}
RenderSVGShadowTreeRootContainer* shadowRoot = static_cast<RenderSVGShadowTreeRootContainer*>(renderer());
if (!shadowRoot)
return;
shadowRoot->updateFromElement();
if (!needsStyleUpdate)
return;
shadowRoot->updateStyle(change);
}
#ifdef DUMP_INSTANCE_TREE
void dumpInstanceTree(unsigned int& depth, String& text, SVGElementInstance* targetInstance)
{
SVGElement* element = targetInstance->correspondingElement();
ASSERT(element);
SVGElement* shadowTreeElement = targetInstance->shadowTreeElement();
ASSERT(shadowTreeElement);
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), shadowTreeElement=%s (%p), id=%s)\n",
targetInstance, parentNodeName.latin1().data(), element->parentNode(), firstChildNodeName.latin1().data(), element->firstChild(),
elementNodeName.latin1().data(), element, shadowTreeElementNodeName.latin1().data(), shadowTreeElement, elementId.latin1().data());
for (unsigned int i = 0; i < depth; ++i)
text += " ";
const HashSet<SVGElementInstance*>& elementInstances = element->instancesForElement();
text += String::format("Corresponding element is associated with %i instance(s):\n", elementInstances.size());
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* element)
{
#if ENABLE(SVG_FOREIGN_OBJECT)
if (element->hasTagName(SVGNames::foreignObjectTag))
return true;
#endif
#if ENABLE(SVG_ANIMATION)
if (SVGSMILElement::isSMILElement(element))
return true;
#endif
return false;
}
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::buildPendingResource()
{
String id = SVGURIReference::getTarget(href());
Element* targetElement = document()->getElementById(id);
ASSERT(!m_targetElementInstance);
if (!targetElement) {
if (m_isPendingResource || id.isEmpty())
return;
m_isPendingResource = true;
m_resourceId = id;
document()->accessSVGExtensions()->addPendingResource(id, this);
return;
}
if (m_isPendingResource) {
ASSERT(!m_targetElementInstance);
m_isPendingResource = false;
invalidateShadowTree();
}
}
void SVGUseElement::buildShadowAndInstanceTree(SVGShadowTreeRootElement* shadowRoot)
{
String id = SVGURIReference::getTarget(href());
Element* targetElement = document()->getElementById(id);
if (!targetElement) {
ASSERT(m_resourceId.isEmpty());
return;
}
Node* parent = parentNode();
while (parent) {
if (parent->isShadowNode())
return;
parent = parent->parentNode();
}
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = static_cast<SVGElement*>(targetElement);
detachInstance();
if (!target || target == this)
return;
m_targetElementInstance = SVGElementInstance::create(this, target);
bool foundProblem = false;
buildInstanceTree(target, m_targetElementInstance.get(), foundProblem);
if (foundProblem) {
detachInstance();
return;
}
ASSERT(m_targetElementInstance);
ASSERT(!m_targetElementInstance->shadowTreeElement());
ASSERT(m_targetElementInstance->correspondingUseElement() == this);
ASSERT(m_targetElementInstance->correspondingElement() == target);
buildShadowTree(shadowRoot, target, m_targetElementInstance.get());
#if ENABLE(SVG) && ENABLE(SVG_USE)
expandUseElementsInShadowTree(shadowRoot, shadowRoot);
expandSymbolElementsInShadowTree(shadowRoot, shadowRoot);
#endif
associateInstancesWithShadowTreeElements(shadowRoot->firstChild(), m_targetElementInstance.get());
if (!m_targetElementInstance->shadowTreeElement()) {
shadowRoot->removeAllChildren();
detachInstance();
return;
}
ASSERT(m_targetElementInstance->shadowTreeElement()->parentNode() == shadowRoot);
#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
ExceptionCode ec = 0;
PassRefPtr<XMLSerializer> serializer = XMLSerializer::create();
String markup = serializer->serializeToString(shadowRoot, ec);
ASSERT(!ec);
fprintf(stderr, "Dumping <use> shadow tree markup:\n%s\n", markup.latin1().data());
#endif
transferEventListenersToShadowTree(m_targetElementInstance.get());
updateContainerOffsets();
updateContainerSizes();
}
void SVGUseElement::detachInstance()
{
if (!m_targetElementInstance)
return;
m_targetElementInstance->clearUseElement();
m_targetElementInstance = 0;
}
RenderObject* SVGUseElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderSVGShadowTreeRootContainer(this);
}
static void updateFromElementCallback(Node* node)
{
if (RenderObject* renderer = node->renderer())
renderer->updateFromElement();
}
void SVGUseElement::attach()
{
SVGStyledTransformableElement::attach();
if (renderer())
queuePostAttachCallback(updateFromElementCallback, this);
}
void SVGUseElement::detach()
{
SVGStyledTransformableElement::detach();
detachInstance();
}
static bool isDirectReference(Node* n)
{
return n->hasTagName(SVGNames::pathTag) ||
n->hasTagName(SVGNames::rectTag) ||
n->hasTagName(SVGNames::circleTag) ||
n->hasTagName(SVGNames::ellipseTag) ||
n->hasTagName(SVGNames::polygonTag) ||
n->hasTagName(SVGNames::polylineTag) ||
n->hasTagName(SVGNames::textTag);
}
Path SVGUseElement::toClipPath() const
{
Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
if (!n)
return Path();
if (n->isSVGElement() && static_cast<SVGElement*>(n)->isStyledTransformable()) {
if (!isDirectReference(n))
document()->accessSVGExtensions()->reportError("Not allowed to use indirect reference in <clip-path>");
else {
Path clipPath = static_cast<SVGStyledTransformableElement*>(n)->toClipPath();
clipPath.translate(FloatSize(x().value(this), y().value(this)));
clipPath.transform(animatedLocalTransform());
return clipPath;
}
}
return Path();
}
RenderObject* SVGUseElement::rendererClipChild() const
{
Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
if (!n)
return 0;
if (n->isSVGElement() && isDirectReference(n))
return static_cast<SVGElement*>(n)->renderer();
return 0;
}
void SVGUseElement::buildInstanceTree(SVGElement* target, SVGElementInstance* targetInstance, bool& foundProblem)
{
ASSERT(target);
ASSERT(targetInstance);
for (Node* node = target->firstChild(); node; node = node->nextSibling()) {
SVGElement* element = 0;
if (node->isSVGElement())
element = static_cast<SVGElement*>(node);
if (!element || isDisallowedElement(element))
continue;
RefPtr<SVGElementInstance> instance = SVGElementInstance::create(this, element);
SVGElementInstance* instancePtr = instance.get();
targetInstance->appendChild(instance.release());
buildInstanceTree(element, instancePtr, foundProblem);
}
if (target->hasTagName(SVGNames::useTag))
handleDeepUseReferencing(static_cast<SVGUseElement*>(target), targetInstance, foundProblem);
}
void SVGUseElement::handleDeepUseReferencing(SVGUseElement* use, SVGElementInstance* targetInstance, bool& foundProblem)
{
String id = SVGURIReference::getTarget(use->href());
Element* targetElement = document()->getElementById(id);
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = static_cast<SVGElement*>(targetElement);
if (!target)
return;
foundProblem = (target == this);
if (foundProblem)
return;
SVGElementInstance* instance = targetInstance->parentNode();
while (instance) {
SVGElement* element = instance->correspondingElement();
if (element->getIDAttribute() == id) {
foundProblem = true;
return;
}
instance = instance->parentNode();
}
RefPtr<SVGElementInstance> newInstance = SVGElementInstance::create(this, target);
SVGElementInstance* newInstancePtr = newInstance.get();
targetInstance->appendChild(newInstance.release());
buildInstanceTree(target, newInstancePtr, foundProblem);
}
void SVGUseElement::removeDisallowedElementsFromSubtree(Node* subtree)
{
ASSERT(!subtree->inDocument());
ExceptionCode ec;
Node* node = subtree->firstChild();
while (node) {
if (isDisallowedElement(node)) {
Node* next = node->traverseNextSibling(subtree);
node->parent()->removeChild(node, ec);
node = next;
} else
node = node->traverseNextNode(subtree);
}
}
void SVGUseElement::buildShadowTree(SVGShadowTreeRootElement* shadowRoot, SVGElement* target, SVGElementInstance* targetInstance)
{
if (isDisallowedElement(target))
return;
RefPtr<Element> newChild = targetInstance->correspondingElement()->cloneElementWithChildren();
if (subtreeContainsDisallowedElement(newChild.get()))
removeDisallowedElementsFromSubtree(newChild.get());
SVGElement* newChildPtr = 0;
if (newChild->isSVGElement())
newChildPtr = static_cast<SVGElement*>(newChild.get());
ASSERT(newChildPtr);
ExceptionCode ec = 0;
shadowRoot->appendChild(newChild.release(), ec);
ASSERT(!ec);
}
#if ENABLE(SVG) && ENABLE(SVG_USE)
void SVGUseElement::expandUseElementsInShadowTree(SVGShadowTreeRootElement* shadowRoot, Node* element)
{
if (element->hasTagName(SVGNames::useTag)) {
SVGUseElement* use = static_cast<SVGUseElement*>(element);
String id = SVGURIReference::getTarget(use->href());
Element* targetElement = document()->getElementById(id);
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = static_cast<SVGElement*>(targetElement);
RefPtr<SVGShadowTreeContainerElement> cloneParent = new SVGShadowTreeContainerElement(document());
use->cloneChildNodes(cloneParent.get());
transferUseAttributesToReplacedElement(use, cloneParent.get());
ExceptionCode ec = 0;
if (target && !isDisallowedElement(target)) {
RefPtr<Element> newChild = target->cloneElementWithChildren();
SVGElement* newChildPtr = 0;
if (newChild->isSVGElement())
newChildPtr = static_cast<SVGElement*>(newChild.get());
ASSERT(newChildPtr);
cloneParent->appendChild(newChild.release(), ec);
ASSERT(!ec);
}
if (subtreeContainsDisallowedElement(cloneParent.get()))
removeDisallowedElementsFromSubtree(cloneParent.get());
ASSERT(use->parentNode());
use->parentNode()->replaceChild(cloneParent.release(), use, ec);
ASSERT(!ec);
expandUseElementsInShadowTree(shadowRoot, shadowRoot);
return;
}
for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
expandUseElementsInShadowTree(shadowRoot, child.get());
}
void SVGUseElement::expandSymbolElementsInShadowTree(SVGShadowTreeRootElement* shadowRoot, Node* element)
{
if (element->hasTagName(SVGNames::symbolTag)) {
RefPtr<SVGSVGElement> svgElement = new SVGSVGElement(SVGNames::svgTag, document());
svgElement->attributes()->setAttributes(*element->attributes());
ExceptionCode ec = 0;
for (Node* child = element->firstChild(); child; child = child->nextSibling()) {
RefPtr<Node> newChild = child->cloneNode(true);
svgElement->appendChild(newChild.release(), ec);
ASSERT(!ec);
}
if (subtreeContainsDisallowedElement(svgElement.get()))
removeDisallowedElementsFromSubtree(svgElement.get());
ASSERT(element->parentNode());
element->parentNode()->replaceChild(svgElement.release(), element, ec);
ASSERT(!ec);
expandSymbolElementsInShadowTree(shadowRoot, shadowRoot);
return;
}
for (RefPtr<Node> child = element->firstChild(); child; child = child->nextSibling())
expandSymbolElementsInShadowTree(shadowRoot, child.get());
}
#endif
void SVGUseElement::transferEventListenersToShadowTree(SVGElementInstance* target)
{
if (!target)
return;
SVGElement* originalElement = target->correspondingElement();
ASSERT(originalElement);
if (SVGElement* shadowTreeElement = target->shadowTreeElement()) {
if (EventTargetData* d = originalElement->eventTargetData()) {
EventListenerMap& map = d->eventListenerMap;
EventListenerMap::iterator end = map.end();
for (EventListenerMap::iterator it = map.begin(); it != end; ++it) {
EventListenerVector& entry = *it->second;
for (size_t i = 0; i < entry.size(); ++i) {
if (entry[i].listener->wasCreatedFromMarkup())
continue;
shadowTreeElement->addEventListener(it->first, entry[i].listener, entry[i].useCapture);
}
}
}
}
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)) {
#if ENABLE(SVG) && ENABLE(SVG_USE)
ASSERT(target->nodeName() == SVGNames::gTag);
#else
ASSERT(target->nodeName() == SVGNames::gTag || target->nodeName() == SVGNames::useTag);
#endif
} else if (originalElement->hasTagName(SVGNames::symbolTag)) {
#if ENABLE(SVG) && ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT)
ASSERT(target->nodeName() == SVGNames::svgTag);
#endif
} else
ASSERT(target->nodeName() == originalElement->nodeName());
SVGElement* element = 0;
if (target->isSVGElement())
element = static_cast<SVGElement*>(target);
ASSERT(!targetInstance->shadowTreeElement());
targetInstance->setShadowTreeElement(element);
Node* node = target->firstChild();
for (SVGElementInstance* instance = targetInstance->firstChild(); node && instance; instance = instance->nextSibling()) {
while (node && !node->isSVGElement())
node = node->nextSibling();
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()
{
m_needsShadowTreeRecreation = true;
setNeedsStyleRecalc();
}
void SVGUseElement::transferUseAttributesToReplacedElement(SVGElement* from, SVGElement* to) const
{
ASSERT(from);
ASSERT(to);
to->attributes()->setAttributes(*from->attributes());
ExceptionCode ec = 0;
to->removeAttribute(SVGNames::xAttr, ec);
ASSERT(!ec);
to->removeAttribute(SVGNames::yAttr, ec);
ASSERT(!ec);
to->removeAttribute(SVGNames::widthAttr, ec);
ASSERT(!ec);
to->removeAttribute(SVGNames::heightAttr, ec);
ASSERT(!ec);
to->removeAttribute(XLinkNames::hrefAttr, ec);
ASSERT(!ec);
}
bool SVGUseElement::hasRelativeValues() const
{
return x().isRelative() || y().isRelative() || width().isRelative() || height().isRelative();
}
}
#endif // ENABLE(SVG)