Remove unneeded SVG code, including most isSupportedAttribute functions
[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 "CSSPropertyNames.h"
27 #include "RenderImageResource.h"
28 #include "RenderSVGImage.h"
29 #include "RenderSVGResource.h"
30 #include "SVGNames.h"
31 #include "XLinkNames.h"
32 #include <wtf/NeverDestroyed.h>
33
34 namespace WebCore {
35
36 // Animated property definitions
37 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::xAttr, X, x)
38 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::yAttr, Y, y)
39 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::widthAttr, Width, width)
40 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::heightAttr, Height, height)
41 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
42 DEFINE_ANIMATED_STRING(SVGImageElement, XLinkNames::hrefAttr, Href, href)
43 DEFINE_ANIMATED_BOOLEAN(SVGImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
44
45 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGImageElement)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
51     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
52     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
53     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
54 END_REGISTER_ANIMATED_PROPERTIES
55
56 inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document& document)
57     : SVGGraphicsElement(tagName, document)
58     , m_x(LengthModeWidth)
59     , m_y(LengthModeHeight)
60     , m_width(LengthModeWidth)
61     , m_height(LengthModeHeight)
62     , m_imageLoader(*this)
63 {
64     registerAnimatedPropertiesForSVGImageElement();
65 }
66
67 Ref<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document& document)
68 {
69     return adoptRef(*new SVGImageElement(tagName, document));
70 }
71
72 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
73 {
74     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
75     if (supportedAttributes.get().isEmpty()) {
76         SVGLangSpace::addSupportedAttributes(supportedAttributes);
77         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
78         SVGURIReference::addSupportedAttributes(supportedAttributes);
79         supportedAttributes.get().add(SVGNames::xAttr);
80         supportedAttributes.get().add(SVGNames::yAttr);
81         supportedAttributes.get().add(SVGNames::widthAttr);
82         supportedAttributes.get().add(SVGNames::heightAttr);
83         supportedAttributes.get().add(SVGNames::preserveAspectRatioAttr);
84     }
85     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
86 }
87
88 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
89 {
90     if (name == SVGNames::preserveAspectRatioAttr) {
91         SVGPreserveAspectRatio preserveAspectRatio;
92         preserveAspectRatio.parse(value);
93         setPreserveAspectRatioBaseValue(preserveAspectRatio);
94         return;
95     }
96
97     SVGParsingError parseError = NoError;
98
99     if (name == SVGNames::xAttr)
100         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
101     else if (name == SVGNames::yAttr)
102         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
103     else if (name == SVGNames::widthAttr)
104         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
105     else if (name == SVGNames::heightAttr)
106         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
107
108     reportAttributeParsingError(parseError, name, value);
109
110     SVGGraphicsElement::parseAttribute(name, value);
111     SVGExternalResourcesRequired::parseAttribute(name, value);
112     SVGURIReference::parseAttribute(name, value);
113 }
114
115 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
116 {
117     if (!isSupportedAttribute(attrName)) {
118         SVGGraphicsElement::svgAttributeChanged(attrName);
119         return;
120     }
121
122     InstanceInvalidationGuard guard(*this);
123
124     if (attrName == SVGNames::widthAttr
125         || attrName == SVGNames::heightAttr) {
126         invalidateSVGPresentationAttributeStyle();
127         return;
128     }
129
130     bool isLengthAttribute = attrName == SVGNames::xAttr
131                           || attrName == SVGNames::yAttr
132                           || attrName == SVGNames::widthAttr
133                           || attrName == SVGNames::heightAttr;
134
135     if (isLengthAttribute)
136         updateRelativeLengthsInformation();
137
138     if (SVGURIReference::isKnownAttribute(attrName)) {
139         m_imageLoader.updateFromElementIgnoringPreviousError();
140         return;
141     }
142
143     auto* renderer = this->renderer();
144     if (!renderer)
145         return;
146
147     if (isLengthAttribute) {
148         if (downcast<RenderSVGImage>(*renderer).updateImageViewport())
149             RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
150         return;
151     }
152
153     if (attrName == SVGNames::preserveAspectRatioAttr
154         || SVGLangSpace::isKnownAttribute(attrName)
155         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
156         RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
157         return;
158     }
159
160     ASSERT_NOT_REACHED();
161 }
162
163 RenderPtr<RenderElement> SVGImageElement::createElementRenderer(Ref<RenderStyle>&& style)
164 {
165     return createRenderer<RenderSVGImage>(*this, WTF::move(style));
166 }
167
168 bool SVGImageElement::haveLoadedRequiredResources()
169 {
170     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
171 }
172
173 void SVGImageElement::didAttachRenderers()
174 {
175     if (auto* imageObj = downcast<RenderSVGImage>(renderer())) {
176         if (imageObj->imageResource().hasImage())
177             return;
178
179         imageObj->imageResource().setCachedImage(m_imageLoader.image());
180     }
181 }
182
183 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode& rootParent)
184 {
185     SVGGraphicsElement::insertedInto(rootParent);
186     if (!rootParent.inDocument())
187         return InsertionDone;
188     // Update image loader, as soon as we're living in the tree.
189     // We can only resolve base URIs properly, after that!
190     m_imageLoader.updateFromElement();
191     return InsertionDone;
192 }
193
194 const AtomicString& SVGImageElement::imageSourceURL() const
195 {
196     return getAttribute(XLinkNames::hrefAttr);
197 }
198
199 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
200 {
201     SVGGraphicsElement::addSubresourceAttributeURLs(urls);
202
203     addSubresourceURL(urls, document().completeURL(href()));
204 }
205
206 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
207 {
208     m_imageLoader.elementDidMoveToNewDocument();
209     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
210 }
211
212 }