Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGElement.h
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifndef SVGElement_h
23 #define SVGElement_h
24
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"
33
34 namespace blink {
35
36 class AffineTransform;
37 class CSSCursorImageValue;
38 class Document;
39 class SVGAnimatedPropertyBase;
40 class SubtreeLayoutScope;
41 class SVGCursorElement;
42 class SVGDocumentExtensions;
43 class SVGElement;
44 class SVGElementRareData;
45 class SVGFitToViewBox;
46 class SVGSVGElement;
47 class SVGUseElement;
48
49 typedef WillBeHeapHashSet<RawPtrWillBeMember<SVGElement> > SVGElementSet;
50
51 class SVGElement : public Element {
52     DEFINE_WRAPPERTYPEINFO();
53 public:
54     virtual ~SVGElement();
55     virtual void attach(const AttachContext&) override;
56     virtual void detach(const AttachContext&) override;
57
58     virtual short tabIndex() const override;
59     virtual bool supportsFocus() const override { return false; }
60
61     bool isOutermostSVGSVGElement() const;
62
63     bool hasTagName(const SVGQualifiedName& name) const { return hasLocalName(name.localName()); }
64
65     virtual String title() const override;
66     bool hasRelativeLengths() const { return !m_elementsWithRelativeLengths.isEmpty(); }
67     static bool isAnimatableCSSProperty(const QualifiedName&);
68     enum CTMScope {
69         NearestViewportScope, // Used by SVGGraphicsElement::getCTM()
70         ScreenScope, // Used by SVGGraphicsElement::getScreenCTM()
71         AncestorScope // Used by SVGSVGElement::get{Enclosure|Intersection}List()
72     };
73     virtual AffineTransform localCoordinateSpaceTransform(CTMScope) const;
74     virtual bool needsPendingResourceHandling() const { return true; }
75
76     bool instanceUpdatesBlocked() const;
77     void setInstanceUpdatesBlocked(bool);
78
79     SVGSVGElement* ownerSVGElement() const;
80     SVGElement* viewportElement() const;
81
82     SVGDocumentExtensions& accessDocumentSVGExtensions();
83
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; }
89
90     // For SVGTests
91     virtual bool isValid() const { return true; }
92
93     virtual void svgAttributeChanged(const QualifiedName&);
94
95     PassRefPtr<SVGAnimatedPropertyBase> propertyFromAttribute(const QualifiedName& attributeName);
96     static AnimatedPropertyType animatedPropertyTypeForCSSAttribute(const QualifiedName& attributeName);
97
98     void sendSVGLoadEventToSelfAndAncestorChainIfPossible();
99     bool sendSVGLoadEventIfPossible();
100     void sendSVGLoadEventIfPossibleAsynchronously();
101     void svgLoadEventTimerFired(Timer<SVGElement>*);
102     virtual Timer<SVGElement>* svgLoadEventTimer();
103
104     virtual AffineTransform* animateMotionTransform() { return 0; }
105
106     void invalidateSVGAttributes() { ensureUniqueElementData().m_animatedSVGAttributesAreDirty = true; }
107     void invalidateSVGPresentationAttributeStyle() { ensureUniqueElementData().m_presentationAttributeStyleIsDirty = true; }
108
109     const WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> >& instancesForElement() const;
110     void mapInstanceToElement(SVGElement*);
111     void removeInstanceMapping(SVGElement*);
112
113     bool getBoundingBox(FloatRect&);
114
115     void setCursorElement(SVGCursorElement*);
116     void setCursorImageValue(CSSCursorImageValue*);
117
118 #if !ENABLE(OILPAN)
119     void cursorElementRemoved();
120     void cursorImageValueRemoved();
121 #endif
122
123     SVGElement* correspondingElement();
124     void setCorrespondingElement(SVGElement*);
125     SVGUseElement* correspondingUseElement() const;
126
127     void synchronizeAnimatedSVGAttribute(const QualifiedName&) const;
128
129     virtual PassRefPtr<RenderStyle> customStyleForRenderer() override final;
130
131     virtual void synchronizeRequiredFeatures() { }
132     virtual void synchronizeRequiredExtensions() { }
133     virtual void synchronizeSystemLanguage() { }
134
135 #if ENABLE(ASSERT)
136     virtual bool isAnimatableAttribute(const QualifiedName&) const;
137 #endif
138
139     MutableStylePropertySet* animatedSMILStyleProperties() const;
140     MutableStylePropertySet* ensureAnimatedSMILStyleProperties();
141     void setUseOverrideComputedStyle(bool);
142
143     virtual bool haveLoadedRequiredResources();
144
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;
147
148     void invalidateRelativeLengthClients(SubtreeLayoutScope* = 0);
149
150     void addToPropertyMap(PassRefPtr<SVGAnimatedPropertyBase>);
151
152     SVGAnimatedString* className() { return m_className.get(); }
153
154     bool inUseShadowTree() const;
155
156     SVGElementSet* setOfIncomingReferences() const;
157     void addReferenceTo(SVGElement*);
158     void rebuildAllIncomingReferences();
159     void removeAllIncomingReferences();
160     void removeAllOutgoingReferences();
161
162     class InvalidationGuard {
163         STACK_ALLOCATED();
164         WTF_MAKE_NONCOPYABLE(InvalidationGuard);
165     public:
166         InvalidationGuard(SVGElement* element) : m_element(element) { }
167         ~InvalidationGuard() { m_element->invalidateInstances(); }
168
169     private:
170         RawPtrWillBeMember<SVGElement> m_element;
171     };
172
173     class InstanceUpdateBlocker {
174         STACK_ALLOCATED();
175         WTF_MAKE_NONCOPYABLE(InstanceUpdateBlocker);
176     public:
177         InstanceUpdateBlocker(SVGElement* targetElement);
178         ~InstanceUpdateBlocker();
179
180     private:
181         RawPtrWillBeMember<SVGElement> m_targetElement;
182     };
183
184     void invalidateInstances();
185
186     virtual void trace(Visitor*) override;
187
188     static const AtomicString& eventParameterName();
189
190 protected:
191     SVGElement(const QualifiedName&, Document&, ConstructionType = CreateSVGElement);
192
193     virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
194
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&);
199
200     virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly) override;
201
202     virtual bool isPresentationAttribute(const QualifiedName&) const override;
203     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) override;
204
205     virtual InsertionNotificationRequest insertedInto(ContainerNode*) override;
206     virtual void removedFrom(ContainerNode*) override;
207     virtual void childrenChanged(const ChildrenChange&) override;
208
209     static CSSPropertyID cssPropertyIdForSVGAttributeName(const QualifiedName&);
210     void updateRelativeLengthsInformation() { updateRelativeLengthsInformation(selfHasRelativeLengths(), this); }
211     void updateRelativeLengthsInformation(bool hasRelativeLengths, SVGElement*);
212     static void markForLayoutAndParentResourceInvalidation(RenderObject*);
213
214     virtual bool selfHasRelativeLengths() const { return false; }
215
216     SVGElementRareData* ensureSVGRareData();
217     inline bool hasSVGRareData() const { return m_SVGRareData; }
218     inline SVGElementRareData* svgRareData() const
219     {
220         ASSERT(m_SVGRareData);
221         return m_SVGRareData.get();
222     }
223
224     // SVGFitToViewBox::parseAttribute uses reportAttributeParsingError.
225     friend class SVGFitToViewBox;
226     void reportAttributeParsingError(SVGParsingError, const QualifiedName&, const AtomicString&);
227     bool hasFocusEventListeners() const;
228
229 private:
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.
232
233     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
234     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) override final { return computedStyle(pseudoElementSpecifier); }
235     virtual void willRecalcStyle(StyleRecalcChange) override;
236
237     void buildPendingResourcesIfNeeded();
238
239     WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> > m_elementsWithRelativeLengths;
240
241     typedef HashMap<QualifiedName, RefPtr<SVGAnimatedPropertyBase> > AttributeToPropertyMap;
242     AttributeToPropertyMap m_attributeToPropertyMap;
243
244 #if ENABLE(ASSERT)
245     bool m_inRelativeLengthClientsInvalidation;
246 #endif
247
248     OwnPtrWillBeMember<SVGElementRareData> m_SVGRareData;
249     RefPtr<SVGAnimatedString> m_className;
250 };
251
252 struct SVGAttributeHashTranslator {
253     static unsigned hash(const QualifiedName& key)
254     {
255         if (key.hasPrefix()) {
256             QualifiedNameComponents components = { nullAtom.impl(), key.localName().impl(), key.namespaceURI().impl() };
257             return hashComponents(components);
258         }
259         return DefaultHash<QualifiedName>::Hash::hash(key);
260     }
261     static bool equal(const QualifiedName& a, const QualifiedName& b) { return a.matches(b); }
262 };
263
264 DEFINE_ELEMENT_TYPE_CASTS(SVGElement, isSVGElement());
265
266 template <typename T> bool isElementOfType(const SVGElement&);
267 template <> inline bool isElementOfType<const SVGElement>(const SVGElement&) { return true; }
268
269 inline bool Node::hasTagName(const SVGQualifiedName& name) const
270 {
271     return isSVGElement() && toSVGElement(*this).hasTagName(name);
272 }
273
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)
285
286 } // namespace blink
287
288 #include "core/SVGElementTypeHelpers.h"
289
290 #endif // SVGElement_h