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