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