1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2018 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "SVGTRefElement.h"
#include "AddEventListenerOptions.h"
#include "ElementRareData.h"
#include "EventListener.h"
#include "EventNames.h"
#include "LegacyRenderSVGResource.h"
#include "MutationEvent.h"
#include "RenderSVGInline.h"
#include "RenderSVGInlineText.h"
#include "SVGDocumentExtensions.h"
#include "SVGElementInlines.h"
#include "SVGNames.h"
#include "ScriptDisallowedScope.h"
#include "ShadowRoot.h"
#include "StyleInheritedData.h"
#include "Text.h"
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(SVGTRefElement);
Ref<SVGTRefElement> SVGTRefElement::create(const QualifiedName& tagName, Document& document)
{
Ref element = adoptRef(*new SVGTRefElement(tagName, document));
element->ensureUserAgentShadowRoot();
return element;
}
class SVGTRefTargetEventListener final : public EventListener {
public:
static Ref<SVGTRefTargetEventListener> create(SVGTRefElement& trefElement)
{
return adoptRef(*new SVGTRefTargetEventListener(trefElement));
}
static const SVGTRefTargetEventListener* cast(const EventListener* listener)
{
return listener->type() == SVGTRefTargetEventListenerType ? static_cast<const SVGTRefTargetEventListener*>(listener) : nullptr;
}
void attach(RefPtr<Element>&& target);
void detach();
bool isAttached() const { return m_target.get(); }
private:
explicit SVGTRefTargetEventListener(SVGTRefElement& trefElement);
Ref<SVGTRefElement> protectedTRefElement() const { return m_trefElement.get(); }
RefPtr<Element> protectedTarget() const { return m_target; }
void handleEvent(ScriptExecutionContext&, Event&) final;
bool operator==(const EventListener&) const final;
WeakRef<SVGTRefElement, WeakPtrImplWithEventTargetData> m_trefElement;
RefPtr<Element> m_target;
};
SVGTRefTargetEventListener::SVGTRefTargetEventListener(SVGTRefElement& trefElement)
: EventListener(SVGTRefTargetEventListenerType)
, m_trefElement(trefElement)
{
}
void SVGTRefTargetEventListener::attach(RefPtr<Element>&& target)
{
ASSERT(!isAttached());
ASSERT(target.get());
ASSERT(target->isConnected());
target->addEventListener(eventNames().DOMSubtreeModifiedEvent, *this, false);
target->addEventListener(eventNames().DOMNodeRemovedFromDocumentEvent, *this, false);
m_target = WTFMove(target);
}
void SVGTRefTargetEventListener::detach()
{
if (!isAttached())
return;
RefPtr target = m_target;
target->removeEventListener(eventNames().DOMSubtreeModifiedEvent, *this, false);
target->removeEventListener(eventNames().DOMNodeRemovedFromDocumentEvent, *this, false);
m_target = nullptr;
}
bool SVGTRefTargetEventListener::operator==(const EventListener& listener) const
{
if (const SVGTRefTargetEventListener* targetListener = SVGTRefTargetEventListener::cast(&listener))
return &m_trefElement == &targetListener->m_trefElement;
return false;
}
void SVGTRefTargetEventListener::handleEvent(ScriptExecutionContext&, Event& event)
{
if (!isAttached())
return;
if (event.type() == eventNames().DOMSubtreeModifiedEvent && m_trefElement.ptr() != event.target())
protectedTRefElement()->updateReferencedText(protectedTarget().get());
else if (event.type() == eventNames().DOMNodeRemovedFromDocumentEvent)
protectedTRefElement()->detachTarget();
}
inline SVGTRefElement::SVGTRefElement(const QualifiedName& tagName, Document& document)
: SVGTextPositioningElement(tagName, document, makeUniqueRef<PropertyRegistry>(*this))
, SVGURIReference(this)
, m_targetListener(SVGTRefTargetEventListener::create(*this))
{
ASSERT(hasTagName(SVGNames::trefTag));
}
SVGTRefElement::~SVGTRefElement()
{
protectedTargetListener()->detach();
}
Ref<SVGTRefTargetEventListener> SVGTRefElement::protectedTargetListener() const
{
return m_targetListener;
}
void SVGTRefElement::updateReferencedText(Element* target)
{
String textContent;
if (target)
textContent = target->textContent();
RefPtr root = userAgentShadowRoot();
ASSERT(root);
ScriptDisallowedScope::EventAllowedScope allowedScope(*root);
if (!root->firstChild())
root->appendChild(Text::create(protectedDocument(), WTFMove(textContent)));
else {
ASSERT(root->firstChild()->isTextNode());
root->protectedFirstChild()->setTextContent(WTFMove(textContent));
}
}
void SVGTRefElement::detachTarget()
{
// Remove active listeners and clear the text content.
protectedTargetListener()->detach();
ASSERT(shadowRoot());
RefPtr container = shadowRoot()->firstChild();
if (container)
container->setTextContent(String { });
if (!isConnected())
return;
// Mark the referenced ID as pending.
auto target = SVGURIReference::targetElementFromIRIString(href(), protectedDocument());
if (!target.identifier.isEmpty())
treeScopeForSVGReferences().addPendingSVGResource(target.identifier, *this);
}
void SVGTRefElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
SVGURIReference::parseAttribute(name, newValue);
SVGTextPositioningElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
}
void SVGTRefElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGTextPositioningElement::svgAttributeChanged(attrName);
if (SVGURIReference::isKnownAttribute(attrName)) {
InstanceInvalidationGuard guard(*this);
buildPendingResource();
updateSVGRendererForElementChange();
}
}
RenderPtr<RenderElement> SVGTRefElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderSVGInline>(RenderObject::Type::SVGInline, *this, WTFMove(style));
}
bool SVGTRefElement::childShouldCreateRenderer(const Node& child) const
{
return child.isInShadowTree();
}
bool SVGTRefElement::rendererIsNeeded(const RenderStyle& style)
{
if (parentNode()
&& (parentNode()->hasTagName(SVGNames::aTag)
|| parentNode()->hasTagName(SVGNames::altGlyphTag)
|| parentNode()->hasTagName(SVGNames::textTag)
|| parentNode()->hasTagName(SVGNames::textPathTag)
|| parentNode()->hasTagName(SVGNames::tspanTag)))
return StyledElement::rendererIsNeeded(style);
return false;
}
void SVGTRefElement::clearTarget()
{
protectedTargetListener()->detach();
}
void SVGTRefElement::buildPendingResource()
{
// Remove any existing event listener.
protectedTargetListener()->detach();
// If we're not yet in a document, this function will be called again from insertedIntoAncestor().
if (!isConnected())
return;
auto target = SVGURIReference::targetElementFromIRIString(href(), treeScopeForSVGReferences());
if (!target.element) {
if (target.identifier.isEmpty())
return;
treeScopeForSVGReferences().addPendingSVGResource(target.identifier, *this);
ASSERT(hasPendingResources());
return;
}
// Don't set up event listeners if this is a shadow tree node.
// SVGUseElement::transferEventListenersToShadowTree() handles this task, and addEventListener()
// expects every element instance to have an associated shadow tree element - which is not the
// case when we land here from SVGUseElement::buildShadowTree().
if (!isInShadowTree())
protectedTargetListener()->attach(target.element.copyRef());
updateReferencedText(target.element.get());
}
Node::InsertedIntoAncestorResult SVGTRefElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
auto result = SVGElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (insertionType.connectedToDocument)
return InsertedIntoAncestorResult::NeedsPostInsertionCallback;
return result;
}
void SVGTRefElement::didFinishInsertingNode()
{
SVGTextPositioningElement::didFinishInsertingNode();
buildPendingResource();
}
void SVGTRefElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
SVGElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
if (removalType.disconnectedFromDocument)
protectedTargetListener()->detach();
}
}
|