Remove the SVG instance tree
[WebKit-https.git] / Source / WebCore / svg / SVGImageElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Rob Buis <buis@kde.org>
4  * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org>
5  * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "config.h"
24 #include "SVGImageElement.h"
25
26 #include "Attribute.h"
27 #include "CSSPropertyNames.h"
28 #include "RenderImageResource.h"
29 #include "RenderSVGImage.h"
30 #include "RenderSVGResource.h"
31 #include "SVGNames.h"
32 #include "SVGSVGElement.h"
33 #include "XLinkNames.h"
34 #include <wtf/NeverDestroyed.h>
35
36 namespace WebCore {
37
38 // Animated property definitions
39 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::xAttr, X, x)
40 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::yAttr, Y, y)
41 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::widthAttr, Width, width)
42 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::heightAttr, Height, height)
43 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
44 DEFINE_ANIMATED_STRING(SVGImageElement, XLinkNames::hrefAttr, Href, href)
45 DEFINE_ANIMATED_BOOLEAN(SVGImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
46
47 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGImageElement)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
51     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
52     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
53     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
54     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
55     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
56 END_REGISTER_ANIMATED_PROPERTIES
57
58 inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document& document)
59     : SVGGraphicsElement(tagName, document)
60     , m_x(LengthModeWidth)
61     , m_y(LengthModeHeight)
62     , m_width(LengthModeWidth)
63     , m_height(LengthModeHeight)
64     , m_imageLoader(*this)
65 {
66     registerAnimatedPropertiesForSVGImageElement();
67 }
68
69 Ref<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document& document)
70 {
71     return adoptRef(*new SVGImageElement(tagName, document));
72 }
73
74 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
75 {
76     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
77     if (supportedAttributes.get().isEmpty()) {
78         SVGLangSpace::addSupportedAttributes(supportedAttributes);
79         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
80         SVGURIReference::addSupportedAttributes(supportedAttributes);
81         supportedAttributes.get().add(SVGNames::xAttr);
82         supportedAttributes.get().add(SVGNames::yAttr);
83         supportedAttributes.get().add(SVGNames::widthAttr);
84         supportedAttributes.get().add(SVGNames::heightAttr);
85         supportedAttributes.get().add(SVGNames::preserveAspectRatioAttr);
86     }
87     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
88 }
89
90 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
91 {
92     SVGParsingError parseError = NoError;
93
94     if (!isSupportedAttribute(name))
95         SVGGraphicsElement::parseAttribute(name, value);
96     else if (name == SVGNames::xAttr)
97         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
98     else if (name == SVGNames::yAttr)
99         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
100     else if (name == SVGNames::preserveAspectRatioAttr) {
101         SVGPreserveAspectRatio preserveAspectRatio;
102         preserveAspectRatio.parse(value);
103         setPreserveAspectRatioBaseValue(preserveAspectRatio);
104     } else if (name == SVGNames::widthAttr)
105         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
106     else if (name == SVGNames::heightAttr)
107         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
108     else if (SVGLangSpace::parseAttribute(name, value)
109              || SVGExternalResourcesRequired::parseAttribute(name, value)
110              || SVGURIReference::parseAttribute(name, value)) {
111     } else
112         ASSERT_NOT_REACHED();
113
114     reportAttributeParsingError(parseError, name, value);
115 }
116
117 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
118 {
119     if (!isSupportedAttribute(attrName)) {
120         SVGGraphicsElement::svgAttributeChanged(attrName);
121         return;
122     }
123
124     InstanceInvalidationGuard guard(*this);
125
126     if (attrName == SVGNames::widthAttr
127         || attrName == SVGNames::heightAttr) {
128         invalidateSVGPresentationAttributeStyle();
129         return;
130     }
131
132     bool isLengthAttribute = attrName == SVGNames::xAttr
133                           || attrName == SVGNames::yAttr
134                           || attrName == SVGNames::widthAttr
135                           || attrName == SVGNames::heightAttr;
136
137     if (isLengthAttribute)
138         updateRelativeLengthsInformation();
139
140     if (SVGURIReference::isKnownAttribute(attrName)) {
141         m_imageLoader.updateFromElementIgnoringPreviousError();
142         return;
143     }
144
145     auto* renderer = this->renderer();
146     if (!renderer)
147         return;
148
149     if (isLengthAttribute) {
150         if (downcast<RenderSVGImage>(*renderer).updateImageViewport())
151             RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
152         return;
153     }
154
155     if (attrName == SVGNames::preserveAspectRatioAttr
156         || SVGLangSpace::isKnownAttribute(attrName)
157         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
158         RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
159         return;
160     }
161
162     ASSERT_NOT_REACHED();
163 }
164
165 RenderPtr<RenderElement> SVGImageElement::createElementRenderer(Ref<RenderStyle>&& style)
166 {
167     return createRenderer<RenderSVGImage>(*this, WTF::move(style));
168 }
169
170 bool SVGImageElement::haveLoadedRequiredResources()
171 {
172     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
173 }
174
175 void SVGImageElement::didAttachRenderers()
176 {
177     if (auto* imageObj = downcast<RenderSVGImage>(renderer())) {
178         if (imageObj->imageResource().hasImage())
179             return;
180
181         imageObj->imageResource().setCachedImage(m_imageLoader.image());
182     }
183 }
184
185 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode& rootParent)
186 {
187     SVGGraphicsElement::insertedInto(rootParent);
188     if (!rootParent.inDocument())
189         return InsertionDone;
190     // Update image loader, as soon as we're living in the tree.
191     // We can only resolve base URIs properly, after that!
192     m_imageLoader.updateFromElement();
193     return InsertionDone;
194 }
195
196 const AtomicString& SVGImageElement::imageSourceURL() const
197 {
198     return getAttribute(XLinkNames::hrefAttr);
199 }
200
201 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
202 {
203     SVGGraphicsElement::addSubresourceAttributeURLs(urls);
204
205     addSubresourceURL(urls, document().completeURL(href()));
206 }
207
208 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
209 {
210     m_imageLoader.elementDidMoveToNewDocument();
211     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
212 }
213
214 }