1/*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SVGUseElement_h
22#define SVGUseElement_h
23
24#include "CachedResourceHandle.h"
25#include "CachedSVGDocumentClient.h"
26#include "SVGAnimatedBoolean.h"
27#include "SVGAnimatedLength.h"
28#include "SVGExternalResourcesRequired.h"
29#include "SVGGraphicsElement.h"
30#include "SVGNames.h"
31#include "SVGURIReference.h"
32
33namespace WebCore {
34
35class CachedSVGDocument;
36class SVGElementInstance;
37
38class SVGUseElement final : public SVGGraphicsElement,
39                            public SVGExternalResourcesRequired,
40                            public SVGURIReference,
41                            public CachedSVGDocumentClient {
42public:
43    static PassRefPtr<SVGUseElement> create(const QualifiedName&, Document&, bool wasInsertedByParser);
44    virtual ~SVGUseElement();
45
46    SVGElementInstance* instanceRoot();
47    SVGElementInstance* animatedInstanceRoot() const;
48    SVGElementInstance* instanceForShadowTreeElement(Node*) const;
49    void invalidateShadowTree();
50    void invalidateDependentShadowTrees();
51
52    RenderElement* rendererClipChild() const;
53
54protected:
55    virtual void didNotifySubtreeInsertions(ContainerNode*) override;
56
57private:
58    SVGUseElement(const QualifiedName&, Document&, bool wasInsertedByParser);
59
60    virtual bool isValid() const override { return SVGTests::isValid(); }
61
62    virtual InsertionNotificationRequest insertedInto(ContainerNode&) override;
63    virtual void removedFrom(ContainerNode&) override;
64    virtual void buildPendingResource() override;
65
66    bool isSupportedAttribute(const QualifiedName&);
67    virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
68    virtual void svgAttributeChanged(const QualifiedName&) override;
69
70    virtual void willAttachRenderers() override;
71
72    virtual RenderPtr<RenderElement> createElementRenderer(PassRef<RenderStyle>) override;
73    virtual void toClipPath(Path&) override;
74
75    void clearResourceReferences();
76    void buildShadowAndInstanceTree(SVGElement* target);
77    void detachInstance();
78
79    virtual bool haveLoadedRequiredResources() override { return SVGExternalResourcesRequired::haveLoadedRequiredResources(); }
80
81    virtual void finishParsingChildren() override;
82    virtual bool selfHasRelativeLengths() const override;
83
84    // Instance tree handling
85    void buildInstanceTree(SVGElement* target, SVGElementInstance* targetInstance, bool& foundCycle, bool foundUse);
86    bool hasCycleUseReferencing(SVGUseElement*, SVGElementInstance* targetInstance, SVGElement*& newTarget);
87
88    // Shadow tree handling
89    void buildShadowTree(SVGElement* target, SVGElementInstance* targetInstance);
90
91    void expandUseElementsInShadowTree(Node* element);
92    void expandSymbolElementsInShadowTree(Node* element);
93
94    // "Tree connector"
95    void associateInstancesWithShadowTreeElements(Node* target, SVGElementInstance* targetInstance);
96    SVGElementInstance* instanceForShadowTreeElement(Node* element, SVGElementInstance* instance) const;
97
98    void transferUseAttributesToReplacedElement(SVGElement* from, SVGElement* to) const;
99    void transferEventListenersToShadowTree(SVGElementInstance* target);
100
101    BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGUseElement)
102        DECLARE_ANIMATED_LENGTH(X, x)
103        DECLARE_ANIMATED_LENGTH(Y, y)
104        DECLARE_ANIMATED_LENGTH(Width, width)
105        DECLARE_ANIMATED_LENGTH(Height, height)
106        DECLARE_ANIMATED_STRING(Href, href)
107        DECLARE_ANIMATED_BOOLEAN(ExternalResourcesRequired, externalResourcesRequired)
108    END_DECLARE_ANIMATED_PROPERTIES
109
110    bool cachedDocumentIsStillLoading();
111    Document* externalDocument() const;
112    bool instanceTreeIsLoading(SVGElementInstance*);
113    virtual void notifyFinished(CachedResource*) override;
114    Document* referencedDocument() const;
115    void setCachedDocument(CachedResourceHandle<CachedSVGDocument>);
116
117    // SVGExternalResourcesRequired
118    virtual void setHaveFiredLoadEvent(bool haveFiredLoadEvent) override { m_haveFiredLoadEvent = haveFiredLoadEvent; }
119    virtual bool isParserInserted() const override { return m_wasInsertedByParser; }
120    virtual bool haveFiredLoadEvent() const override { return m_haveFiredLoadEvent; }
121    virtual Timer<SVGElement>* svgLoadEventTimer() override { return &m_svgLoadEventTimer; }
122
123    bool m_wasInsertedByParser;
124    bool m_haveFiredLoadEvent;
125    bool m_needsShadowTreeRecreation;
126    RefPtr<SVGElementInstance> m_targetElementInstance;
127    CachedResourceHandle<CachedSVGDocument> m_cachedDocument;
128    Timer<SVGElement> m_svgLoadEventTimer;
129};
130
131NODE_TYPE_CASTS(SVGUseElement)
132
133}
134
135#endif
136