#include "config.h"
#if ENABLE(SVG)
#include "SVGUseElement.h"
#include "Attribute.h"
#include "CSSStyleSelector.h"
#include "Document.h"
#include "Event.h"
#include "EventListener.h"
#include "HTMLNames.h"
#include "NodeRenderStyle.h"
#include "RegisteredEventListener.h"
#include "RenderSVGResource.h"
#include "RenderSVGShadowTreeRootContainer.h"
#include "SVGElementInstance.h"
#include "SVGElementInstanceList.h"
#include "SVGGElement.h"
#include "SVGNames.h"
#include "SVGSMILElement.h"
#include "SVGSVGElement.h"
#include "SVGShadowTreeElements.h"
#include "SVGSymbolElement.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)
inline SVGUseElement::SVGUseElement(const QualifiedName& tagName, Document* document)
: SVGStyledTransformableElement(tagName, document)
, m_x(LengthModeWidth)
, m_y(LengthModeHeight)
, m_width(LengthModeWidth)
, m_height(LengthModeHeight)
, m_updatesBlocked(false)
, m_needsShadowTreeRecreation(false)
{
ASSERT(hasTagName(SVGNames::useTag));
}
PassRefPtr<SVGUseElement> SVGUseElement::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new SVGUseElement(tagName, document));
}
SVGElementInstance* SVGUseElement::instanceRoot() const
{
if (!m_targetElementInstance)
document()->updateLayoutIgnorePendingStylesheets();
return m_targetElementInstance.get();
}
SVGElementInstance* SVGUseElement::animatedInstanceRoot() const
{
return 0;
}
void SVGUseElement::parseMappedAttribute(Attribute* 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);
}
}
static inline bool isWellFormedDocument(Document* document)
{
if (document->isSVGDocument() || document->isXHTMLDocument())
return static_cast<XMLDocumentParser*>(document->parser())->wellFormed();
return true;
}
void SVGUseElement::insertedIntoDocument()
{
SVGStyledTransformableElement::insertedIntoDocument();
ASSERT(!m_targetElementInstance || !isWellFormedDocument(document()));
ASSERT(!hasPendingResources() || !isWellFormedDocument(document()));
}
void SVGUseElement::removedFromDocument()
{
SVGStyledTransformableElement::removedFromDocument();
detachInstance();
}
void SVGUseElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGStyledTransformableElement::svgAttributeChanged(attrName);
bool isXYAttribute = attrName == SVGNames::xAttr || attrName == SVGNames::yAttr;
bool isWidthHeightAttribute = attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr;
if (isXYAttribute || isWidthHeightAttribute)
updateRelativeLengthsInformation();
if (SVGTests::handleAttributeChange(this, attrName))
return;
RenderObject* object = renderer();
if (!object)
return;
if (SVGURIReference::isKnownAttribute(attrName)) {
if (hasPendingResources()) {
OwnPtr<SVGDocumentExtensions::SVGPendingElements> clients(document()->accessSVGExtensions()->removePendingResource(m_resourceId));
ASSERT(!clients->isEmpty());
const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it) {
ASSERT((*it)->hasPendingResources());
(*it)->setHasPendingResources(false);
}
m_resourceId = String();
setHasPendingResources(false);
}
invalidateShadowTree();
return;
}
if (isXYAttribute) {
updateContainerOffsets();
return;
}
if (isWidthHeightAttribute) {
updateContainerSizes();
return;
}
if (SVGStyledElement::isKnownAttribute(attrName)) {
setNeedsStyleRecalc();
return;
}
if (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();
SVGTests::synchronizeProperties(this, attrName);
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();
else if (SVGTests::isKnownAttribute(attrName))
SVGTests::synchronizeProperties(this, attrName);
}
AttributeToPropertyTypeMap& SVGUseElement::attributeToPropertyTypeMap()
{
DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
return s_attributeToPropertyTypeMap;
}
void SVGUseElement::fillAttributeToPropertyTypeMap()
{
AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
SVGStyledTransformableElement::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
attributeToPropertyTypeMap.set(SVGNames::xAttr, AnimatedLength);
attributeToPropertyTypeMap.set(SVGNames::yAttr, AnimatedLength);
attributeToPropertyTypeMap.set(SVGNames::widthAttr, AnimatedLength);
attributeToPropertyTypeMap.set(SVGNames::heightAttr, AnimatedLength);
attributeToPropertyTypeMap.set(XLinkNames::hrefAttr, AnimatedString);
}
static void updateContainerSize(SVGElementInstance* targetInstance)
{
for (SVGElementInstance* instance = targetInstance->firstChild(); instance; instance = instance->nextSibling())
updateContainerSize(instance);
SVGUseElement* useElement = targetInstance->directUseElement();
if (!useElement)
return;
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;
ASSERT(m_targetElementInstance->directUseElement() == this);
updateContainerSize(m_targetElementInstance.get());
if (RenderObject* object = renderer())
RenderSVGResource::markForLayoutAndParentResourceInvalidation(object);
}
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);
ContainerNode* 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 (RenderObject* object = renderer())
RenderSVGResource::markForLayoutAndParentResourceInvalidation(object);
}
void SVGUseElement::recalcStyle(StyleChange change)
{
if ((change >= Inherit || needsStyleRecalc() || childNeedsStyleRecalc()) && m_targetElementInstance && !m_updatesBlocked) {
if (SVGElement* shadowRoot = m_targetElementInstance->shadowTreeElement())
shadowRoot->setNeedsStyleRecalc();
}
SVGStyledTransformableElement::recalcStyle(change);
if (m_updatesBlocked)
ASSERT(!m_needsShadowTreeRecreation);
RenderSVGShadowTreeRootContainer* shadowRoot = static_cast<RenderSVGShadowTreeRootContainer*>(renderer());
if (!shadowRoot)
return;
bool needsStyleUpdate = !m_needsShadowTreeRecreation;
if (m_needsShadowTreeRecreation) {
shadowRoot->markShadowTreeForRecreation();
m_needsShadowTreeRecreation = false;
}
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);
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* 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 = treeScope()->getElementById(id);
ASSERT(!m_targetElementInstance);
if (!targetElement) {
if (hasPendingResources() || id.isEmpty())
return;
m_resourceId = id;
ASSERT(!hasPendingResources());
document()->accessSVGExtensions()->addPendingResource(id, this);
ASSERT(hasPendingResources());
return;
}
if (hasPendingResources()) {
ASSERT(!m_targetElementInstance);
m_resourceId = String();
invalidateShadowTree();
}
}
void SVGUseElement::buildShadowAndInstanceTree(SVGShadowTreeRootElement* shadowRoot)
{
struct ShadowTreeUpdateBlocker {
ShadowTreeUpdateBlocker(SVGUseElement* currentUseElement)
: useElement(currentUseElement)
{
useElement->setUpdatesBlocked(true);
}
~ShadowTreeUpdateBlocker()
{
useElement->setUpdatesBlocked(false);
}
SVGUseElement* useElement;
};
ShadowTreeUpdateBlocker blocker(this);
String id = SVGURIReference::getTarget(href());
Element* targetElement = treeScope()->getElementById(id);
if (!targetElement) {
ASSERT(m_resourceId.isEmpty());
return;
}
ContainerNode* parent = parentNode();
while (parent) {
if (parent->isSVGShadowRoot())
return;
parent = parent->parentNodeGuaranteedHostFree();
}
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = static_cast<SVGElement*>(targetElement);
detachInstance();
if (!target || target == this)
return;
m_targetElementInstance = SVGElementInstance::create(this, 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->directUseElement() == this);
ASSERT(m_targetElementInstance->correspondingElement() == target);
buildShadowTree(shadowRoot, target, m_targetElementInstance.get());
#if ENABLE(SVG) && ENABLE(SVG_USE)
expandUseElementsInShadowTree(shadowRoot);
expandSymbolElementsInShadowTree(shadowRoot);
#endif
associateInstancesWithShadowTreeElements(shadowRoot->firstChild(), m_targetElementInstance.get());
if (!m_targetElementInstance->shadowTreeElement()) {
shadowRoot->removeAllChildren();
detachInstance();
return;
}
ASSERT(m_targetElementInstance->shadowTreeElement()->parentNode() == shadowRoot);
transferEventListenersToShadowTree(m_targetElementInstance.get());
updateContainerOffsets();
updateContainerSizes();
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
ExceptionCode ec = 0;
RefPtr<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
}
void SVGUseElement::detachInstance()
{
if (!m_targetElementInstance)
return;
m_targetElementInstance->clearUseElements();
m_targetElementInstance->clearChildren();
m_targetElementInstance = 0;
}
RenderObject* SVGUseElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderSVGShadowTreeRootContainer(this);
}
static void updateFromElementCallback(Node* node, unsigned)
{
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* 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) const
{
ASSERT(path.isEmpty());
Node* n = m_targetElementInstance ? m_targetElementInstance->shadowTreeElement() : 0;
if (!n)
return;
if (n->isSVGElement() && static_cast<SVGElement*>(n)->isStyledTransformable()) {
if (!isDirectReference(n))
document()->accessSVGExtensions()->reportError("Not allowed to use indirect reference in <clip-path>");
else {
static_cast<SVGStyledTransformableElement*>(n)->toClipPath(path);
path.translate(FloatSize(x().value(this), y().value(this)));
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 static_cast<SVGElement*>(n)->renderer();
return 0;
}
void SVGUseElement::buildInstanceTree(SVGElement* target, SVGElementInstance* targetInstance, bool& foundProblem)
{
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;
}
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, 0, element);
SVGElementInstance* instancePtr = instance.get();
targetInstance->appendChild(instance.release());
buildInstanceTree(element, instancePtr, foundProblem);
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);
}
bool SVGUseElement::hasCycleUseReferencing(SVGUseElement* use, SVGElementInstance* targetInstance, SVGElement*& newTarget)
{
String id = SVGURIReference::getTarget(use->href());
Element* targetElement = treeScope()->getElementById(id);
newTarget = 0;
if (targetElement && targetElement->isSVGElement())
newTarget = static_cast<SVGElement*>(targetElement);
if (!newTarget)
return false;
if (newTarget == this)
return true;
SVGElementInstance* instance = targetInstance->parentNode();
while (instance) {
SVGElement* element = instance->correspondingElement();
if (element->hasID() && element->idForStyleResolution() == id)
return true;
instance = instance->parentNode();
}
return false;
}
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->parentNode()->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_UNUSED(newChildPtr, newChildPtr);
ExceptionCode ec = 0;
shadowRoot->appendChild(newChild.release(), ec);
ASSERT(!ec);
}
#if ENABLE(SVG) && ENABLE(SVG_USE)
void SVGUseElement::expandUseElementsInShadowTree(Node* element)
{
if (element->hasTagName(SVGNames::useTag)) {
SVGUseElement* use = static_cast<SVGUseElement*>(element);
String id = SVGURIReference::getTarget(use->href());
Element* targetElement = treeScope()->getElementById(id);
SVGElement* target = 0;
if (targetElement && targetElement->isSVGElement())
target = static_cast<SVGElement*>(targetElement);
RefPtr<SVGShadowTreeContainerElement> cloneParent = SVGShadowTreeContainerElement::create(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_UNUSED(newChildPtr, newChildPtr);
cloneParent->appendChild(newChild.release(), ec);
ASSERT(!ec);
}
if (subtreeContainsDisallowedElement(cloneParent.get()))
removeDisallowedElementsFromSubtree(cloneParent.get());
RefPtr<Node> replacingElement(cloneParent.get());
ASSERT(use->parentNode());
use->parentNode()->replaceChild(cloneParent.release(), use, ec);
ASSERT(!ec);
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, 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());
RefPtr<Node> replacingElement(svgElement.get());
ASSERT(element->parentNode());
element->parentNode()->replaceChild(svgElement.release(), element, ec);
ASSERT(!ec);
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());
}
#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();
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 (m_updatesBlocked)
return;
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::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->isStyled())
return false;
return static_cast<SVGStyledElement*>(element)->hasRelativeLengths();
}
}
#endif // ENABLE(SVG)