[SVG] Start moving special casing of SVG out of the bindings - SVGPreserveAspectRatio
[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::hasSingleSecurityOrigin() const
73 {
74     auto* renderer = downcast<RenderSVGImage>(this->renderer());
75     if (!renderer || !renderer->imageResource().hasImage())
76         return true;
77     auto* image = renderer->imageResource().cachedImage()->image();
78     return !image || image->hasSingleSecurityOrigin();
79 }
80
81 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
82 {
83     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
84     if (supportedAttributes.get().isEmpty()) {
85         SVGLangSpace::addSupportedAttributes(supportedAttributes);
86         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
87         SVGURIReference::addSupportedAttributes(supportedAttributes);
88         supportedAttributes.get().add(SVGNames::xAttr);
89         supportedAttributes.get().add(SVGNames::yAttr);
90         supportedAttributes.get().add(SVGNames::widthAttr);
91         supportedAttributes.get().add(SVGNames::heightAttr);
92         supportedAttributes.get().add(SVGNames::preserveAspectRatioAttr);
93     }
94     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
95 }
96
97 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
98 {
99     if (name == SVGNames::preserveAspectRatioAttr) {
100         SVGPreserveAspectRatioValue preserveAspectRatio;
101         preserveAspectRatio.parse(value);
102         setPreserveAspectRatioBaseValue(preserveAspectRatio);
103         return;
104     }
105
106     SVGParsingError parseError = NoError;
107
108     if (name == SVGNames::xAttr)
109         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
110     else if (name == SVGNames::yAttr)
111         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
112     else if (name == SVGNames::widthAttr)
113         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
114     else if (name == SVGNames::heightAttr)
115         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
116
117     reportAttributeParsingError(parseError, name, value);
118
119     SVGGraphicsElement::parseAttribute(name, value);
120     SVGExternalResourcesRequired::parseAttribute(name, value);
121     SVGURIReference::parseAttribute(name, value);
122 }
123
124 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
125 {
126     if (!isSupportedAttribute(attrName)) {
127         SVGGraphicsElement::svgAttributeChanged(attrName);
128         return;
129     }
130
131     InstanceInvalidationGuard guard(*this);
132
133     if (attrName == SVGNames::widthAttr
134         || attrName == SVGNames::heightAttr) {
135         invalidateSVGPresentationAttributeStyle();
136         return;
137     }
138
139     bool isLengthAttribute = attrName == SVGNames::xAttr
140                           || attrName == SVGNames::yAttr
141                           || attrName == SVGNames::widthAttr
142                           || attrName == SVGNames::heightAttr;
143
144     if (isLengthAttribute)
145         updateRelativeLengthsInformation();
146
147     if (SVGURIReference::isKnownAttribute(attrName)) {
148         m_imageLoader.updateFromElementIgnoringPreviousError();
149         return;
150     }
151
152     auto* renderer = this->renderer();
153     if (!renderer)
154         return;
155
156     if (isLengthAttribute) {
157         if (downcast<RenderSVGImage>(*renderer).updateImageViewport())
158             RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
159         return;
160     }
161
162     if (attrName == SVGNames::preserveAspectRatioAttr
163         || SVGLangSpace::isKnownAttribute(attrName)
164         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
165         RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
166         return;
167     }
168
169     ASSERT_NOT_REACHED();
170 }
171
172 RenderPtr<RenderElement> SVGImageElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
173 {
174     return createRenderer<RenderSVGImage>(*this, WTFMove(style));
175 }
176
177 bool SVGImageElement::haveLoadedRequiredResources()
178 {
179     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
180 }
181
182 void SVGImageElement::didAttachRenderers()
183 {
184     if (auto* imageObj = downcast<RenderSVGImage>(renderer())) {
185         if (imageObj->imageResource().hasImage())
186             return;
187
188         imageObj->imageResource().setCachedImage(m_imageLoader.image());
189     }
190 }
191
192 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode& rootParent)
193 {
194     SVGGraphicsElement::insertedInto(rootParent);
195     if (!rootParent.inDocument())
196         return InsertionDone;
197     // Update image loader, as soon as we're living in the tree.
198     // We can only resolve base URIs properly, after that!
199     m_imageLoader.updateFromElement();
200     return InsertionDone;
201 }
202
203 const AtomicString& SVGImageElement::imageSourceURL() const
204 {
205     return getAttribute(XLinkNames::hrefAttr);
206 }
207
208 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
209 {
210     SVGGraphicsElement::addSubresourceAttributeURLs(urls);
211
212     addSubresourceURL(urls, document().completeURL(href()));
213 }
214
215 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
216 {
217     m_imageLoader.elementDidMoveToNewDocument();
218     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
219 }
220
221 }