2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2009, 2014 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "core/SVGNames.h"
26 #include "core/dom/Element.h"
27 #include "core/svg/SVGAnimatedString.h"
28 #include "core/svg/SVGParsingError.h"
29 #include "core/svg/properties/SVGPropertyInfo.h"
30 #include "platform/Timer.h"
31 #include "wtf/HashMap.h"
32 #include "wtf/OwnPtr.h"
36 class AffineTransform;
37 class CSSCursorImageValue;
39 class SVGAnimatedPropertyBase;
40 class SubtreeLayoutScope;
41 class SVGCursorElement;
42 class SVGDocumentExtensions;
44 class SVGElementRareData;
45 class SVGFitToViewBox;
49 typedef WillBeHeapHashSet<RawPtrWillBeMember<SVGElement> > SVGElementSet;
51 class SVGElement : public Element {
52 DEFINE_WRAPPERTYPEINFO();
54 virtual ~SVGElement();
55 virtual void attach(const AttachContext&) override;
56 virtual void detach(const AttachContext&) override;
58 virtual short tabIndex() const override;
59 virtual bool supportsFocus() const override { return false; }
61 bool isOutermostSVGSVGElement() const;
63 bool hasTagName(const SVGQualifiedName& name) const { return hasLocalName(name.localName()); }
65 virtual String title() const override;
66 bool hasRelativeLengths() const { return !m_elementsWithRelativeLengths.isEmpty(); }
67 static bool isAnimatableCSSProperty(const QualifiedName&);
69 NearestViewportScope, // Used by SVGGraphicsElement::getCTM()
70 ScreenScope, // Used by SVGGraphicsElement::getScreenCTM()
71 AncestorScope // Used by SVGSVGElement::get{Enclosure|Intersection}List()
73 virtual AffineTransform localCoordinateSpaceTransform(CTMScope) const;
74 virtual bool needsPendingResourceHandling() const { return true; }
76 bool instanceUpdatesBlocked() const;
77 void setInstanceUpdatesBlocked(bool);
79 SVGSVGElement* ownerSVGElement() const;
80 SVGElement* viewportElement() const;
82 SVGDocumentExtensions& accessDocumentSVGExtensions();
84 virtual bool isSVGGraphicsElement() const { return false; }
85 virtual bool isFilterEffect() const { return false; }
86 virtual bool isTextContent() const { return false; }
87 virtual bool isTextPositioning() const { return false; }
88 virtual bool isStructurallyExternal() const { return false; }
91 virtual bool isValid() const { return true; }
93 virtual void svgAttributeChanged(const QualifiedName&);
95 PassRefPtr<SVGAnimatedPropertyBase> propertyFromAttribute(const QualifiedName& attributeName);
96 static AnimatedPropertyType animatedPropertyTypeForCSSAttribute(const QualifiedName& attributeName);
98 void sendSVGLoadEventToSelfAndAncestorChainIfPossible();
99 bool sendSVGLoadEventIfPossible();
100 void sendSVGLoadEventIfPossibleAsynchronously();
101 void svgLoadEventTimerFired(Timer<SVGElement>*);
102 virtual Timer<SVGElement>* svgLoadEventTimer();
104 virtual AffineTransform* animateMotionTransform() { return 0; }
106 void invalidateSVGAttributes() { ensureUniqueElementData().m_animatedSVGAttributesAreDirty = true; }
107 void invalidateSVGPresentationAttributeStyle() { ensureUniqueElementData().m_presentationAttributeStyleIsDirty = true; }
109 const WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> >& instancesForElement() const;
110 void mapInstanceToElement(SVGElement*);
111 void removeInstanceMapping(SVGElement*);
113 bool getBoundingBox(FloatRect&);
115 void setCursorElement(SVGCursorElement*);
116 void setCursorImageValue(CSSCursorImageValue*);
119 void cursorElementRemoved();
120 void cursorImageValueRemoved();
123 SVGElement* correspondingElement();
124 void setCorrespondingElement(SVGElement*);
125 SVGUseElement* correspondingUseElement() const;
127 void synchronizeAnimatedSVGAttribute(const QualifiedName&) const;
129 virtual PassRefPtr<RenderStyle> customStyleForRenderer() override final;
131 virtual void synchronizeRequiredFeatures() { }
132 virtual void synchronizeRequiredExtensions() { }
133 virtual void synchronizeSystemLanguage() { }
136 virtual bool isAnimatableAttribute(const QualifiedName&) const;
139 MutableStylePropertySet* animatedSMILStyleProperties() const;
140 MutableStylePropertySet* ensureAnimatedSMILStyleProperties();
141 void setUseOverrideComputedStyle(bool);
143 virtual bool haveLoadedRequiredResources();
145 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false) override final;
146 virtual bool removeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false) override final;
148 void invalidateRelativeLengthClients(SubtreeLayoutScope* = 0);
150 void addToPropertyMap(PassRefPtr<SVGAnimatedPropertyBase>);
152 SVGAnimatedString* className() { return m_className.get(); }
154 bool inUseShadowTree() const;
156 SVGElementSet* setOfIncomingReferences() const;
157 void addReferenceTo(SVGElement*);
158 void rebuildAllIncomingReferences();
159 void removeAllIncomingReferences();
160 void removeAllOutgoingReferences();
162 class InvalidationGuard {
164 WTF_MAKE_NONCOPYABLE(InvalidationGuard);
166 InvalidationGuard(SVGElement* element) : m_element(element) { }
167 ~InvalidationGuard() { m_element->invalidateInstances(); }
170 RawPtrWillBeMember<SVGElement> m_element;
173 class InstanceUpdateBlocker {
175 WTF_MAKE_NONCOPYABLE(InstanceUpdateBlocker);
177 InstanceUpdateBlocker(SVGElement* targetElement);
178 ~InstanceUpdateBlocker();
181 RawPtrWillBeMember<SVGElement> m_targetElement;
184 void invalidateInstances();
186 virtual void trace(Visitor*) override;
188 static const AtomicString& eventParameterName();
191 SVGElement(const QualifiedName&, Document&, ConstructionType = CreateSVGElement);
193 virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
195 // FIXME: |parseAttributeNew| is a new implementation of parseAttribute
196 // which maps attribute using |m_attributeToPropertyMap|.
197 // This is to replace |parseAttribute()| after all derived class switch to call this.
198 void parseAttributeNew(const QualifiedName&, const AtomicString&);
200 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly) override;
202 virtual bool isPresentationAttribute(const QualifiedName&) const override;
203 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) override;
205 virtual InsertionNotificationRequest insertedInto(ContainerNode*) override;
206 virtual void removedFrom(ContainerNode*) override;
207 virtual void childrenChanged(const ChildrenChange&) override;
209 static CSSPropertyID cssPropertyIdForSVGAttributeName(const QualifiedName&);
210 void updateRelativeLengthsInformation() { updateRelativeLengthsInformation(selfHasRelativeLengths(), this); }
211 void updateRelativeLengthsInformation(bool hasRelativeLengths, SVGElement*);
212 static void markForLayoutAndParentResourceInvalidation(RenderObject*);
214 virtual bool selfHasRelativeLengths() const { return false; }
216 SVGElementRareData* ensureSVGRareData();
217 inline bool hasSVGRareData() const { return m_SVGRareData; }
218 inline SVGElementRareData* svgRareData() const
220 ASSERT(m_SVGRareData);
221 return m_SVGRareData.get();
224 // SVGFitToViewBox::parseAttribute uses reportAttributeParsingError.
225 friend class SVGFitToViewBox;
226 void reportAttributeParsingError(SVGParsingError, const QualifiedName&, const AtomicString&);
227 bool hasFocusEventListeners() const;
230 bool isSVGElement() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
231 bool isStyledElement() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
233 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
234 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) override final { return computedStyle(pseudoElementSpecifier); }
235 virtual void willRecalcStyle(StyleRecalcChange) override;
237 void buildPendingResourcesIfNeeded();
239 WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> > m_elementsWithRelativeLengths;
241 typedef HashMap<QualifiedName, RefPtr<SVGAnimatedPropertyBase> > AttributeToPropertyMap;
242 AttributeToPropertyMap m_attributeToPropertyMap;
245 bool m_inRelativeLengthClientsInvalidation;
248 OwnPtrWillBeMember<SVGElementRareData> m_SVGRareData;
249 RefPtr<SVGAnimatedString> m_className;
252 struct SVGAttributeHashTranslator {
253 static unsigned hash(const QualifiedName& key)
255 if (key.hasPrefix()) {
256 QualifiedNameComponents components = { nullAtom.impl(), key.localName().impl(), key.namespaceURI().impl() };
257 return hashComponents(components);
259 return DefaultHash<QualifiedName>::Hash::hash(key);
261 static bool equal(const QualifiedName& a, const QualifiedName& b) { return a.matches(b); }
264 DEFINE_ELEMENT_TYPE_CASTS(SVGElement, isSVGElement());
266 template <typename T> bool isElementOfType(const SVGElement&);
267 template <> inline bool isElementOfType<const SVGElement>(const SVGElement&) { return true; }
269 inline bool Node::hasTagName(const SVGQualifiedName& name) const
271 return isSVGElement() && toSVGElement(*this).hasTagName(name);
274 // This requires isSVG*Element(const SVGElement&).
275 #define DEFINE_SVGELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
276 inline bool is##thisType(const thisType* element); \
277 inline bool is##thisType(const thisType& element); \
278 inline bool is##thisType(const SVGElement* element) { return element && is##thisType(*element); } \
279 inline bool is##thisType(const Node& node) { return node.isSVGElement() ? is##thisType(toSVGElement(node)) : false; } \
280 inline bool is##thisType(const Node* node) { return node && is##thisType(*node); } \
281 template<typename T> inline bool is##thisType(const PassRefPtr<T>& node) { return is##thisType(node.get()); } \
282 template<typename T> inline bool is##thisType(const RefPtr<T>& node) { return is##thisType(node.get()); } \
283 template <> inline bool isElementOfType<const thisType>(const SVGElement& element) { return is##thisType(element); } \
284 DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType)
288 #include "core/SVGElementTypeHelpers.h"
290 #endif // SVGElement_h