<https://webkit.org/b/120115> SVG elements always have custom style resolve callbacks.
[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
24 #if ENABLE(SVG)
25 #include "SVGImageElement.h"
26
27 #include "Attribute.h"
28 #include "CSSPropertyNames.h"
29 #include "RenderImageResource.h"
30 #include "RenderSVGImage.h"
31 #include "RenderSVGResource.h"
32 #include "SVGElementInstance.h"
33 #include "SVGNames.h"
34 #include "SVGSVGElement.h"
35 #include "XLinkNames.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     ASSERT(isSVGImageElement(this));
68     registerAnimatedPropertiesForSVGImageElement();
69 }
70
71 PassRefPtr<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document* document)
72 {
73     return adoptRef(new SVGImageElement(tagName, document));
74 }
75
76 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
77 {
78     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
79     if (supportedAttributes.isEmpty()) {
80         SVGLangSpace::addSupportedAttributes(supportedAttributes);
81         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
82         SVGURIReference::addSupportedAttributes(supportedAttributes);
83         supportedAttributes.add(SVGNames::xAttr);
84         supportedAttributes.add(SVGNames::yAttr);
85         supportedAttributes.add(SVGNames::widthAttr);
86         supportedAttributes.add(SVGNames::heightAttr);
87         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
88     }
89     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
90 }
91
92 bool SVGImageElement::isPresentationAttribute(const QualifiedName& name) const
93 {
94     if (name == SVGNames::widthAttr || name == SVGNames::heightAttr)
95         return true;
96     return SVGGraphicsElement::isPresentationAttribute(name);
97 }
98
99 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
100 {
101     if (!isSupportedAttribute(name))
102         SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
103     else if (name == SVGNames::widthAttr)
104         addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, value);
105     else if (name == SVGNames::heightAttr)
106         addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, value);
107 }
108
109 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
110 {
111     SVGParsingError parseError = NoError;
112
113     if (!isSupportedAttribute(name))
114         SVGGraphicsElement::parseAttribute(name, value);
115     else if (name == SVGNames::xAttr)
116         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
117     else if (name == SVGNames::yAttr)
118         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
119     else if (name == SVGNames::preserveAspectRatioAttr) {
120         SVGPreserveAspectRatio preserveAspectRatio;
121         preserveAspectRatio.parse(value);
122         setPreserveAspectRatioBaseValue(preserveAspectRatio);
123     } else if (name == SVGNames::widthAttr)
124         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
125     else if (name == SVGNames::heightAttr)
126         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
127     else if (SVGLangSpace::parseAttribute(name, value)
128              || SVGExternalResourcesRequired::parseAttribute(name, value)
129              || SVGURIReference::parseAttribute(name, value)) {
130     } else
131         ASSERT_NOT_REACHED();
132
133     reportAttributeParsingError(parseError, name, value);
134 }
135
136 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
137 {
138     if (!isSupportedAttribute(attrName)) {
139         SVGGraphicsElement::svgAttributeChanged(attrName);
140         return;
141     }
142
143     SVGElementInstance::InvalidationGuard invalidationGuard(this);
144     
145     bool isLengthAttribute = attrName == SVGNames::xAttr
146                           || attrName == SVGNames::yAttr
147                           || attrName == SVGNames::widthAttr
148                           || attrName == SVGNames::heightAttr;
149
150     if (isLengthAttribute)
151         updateRelativeLengthsInformation();
152
153     if (SVGURIReference::isKnownAttribute(attrName)) {
154         m_imageLoader.updateFromElementIgnoringPreviousError();
155         return;
156     }
157
158     RenderObject* renderer = this->renderer();
159     if (!renderer)
160         return;
161
162     if (isLengthAttribute) {
163         if (toRenderSVGImage(renderer)->updateImageViewport())
164             RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
165         return;
166     }
167
168     if (attrName == SVGNames::preserveAspectRatioAttr
169         || SVGLangSpace::isKnownAttribute(attrName)
170         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
171         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
172         return;
173     }
174
175     ASSERT_NOT_REACHED();
176 }
177
178 bool SVGImageElement::selfHasRelativeLengths() const
179 {
180     return x().isRelative()
181         || y().isRelative()
182         || width().isRelative()
183         || height().isRelative();
184 }
185
186 RenderObject* SVGImageElement::createRenderer(RenderArena* arena, RenderStyle*)
187 {
188     return new (arena) RenderSVGImage(this);
189 }
190
191 bool SVGImageElement::haveLoadedRequiredResources()
192 {
193     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
194 }
195
196 void SVGImageElement::didAttachRenderers()
197 {
198     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
199         if (imageObj->imageResource()->hasImage())
200             return;
201
202         imageObj->imageResource()->setCachedImage(m_imageLoader.image());
203     }
204 }
205
206 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
207 {
208     SVGGraphicsElement::insertedInto(rootParent);
209     if (!rootParent->inDocument())
210         return InsertionDone;
211     // Update image loader, as soon as we're living in the tree.
212     // We can only resolve base URIs properly, after that!
213     m_imageLoader.updateFromElement();
214     return InsertionDone;
215 }
216
217 const AtomicString& SVGImageElement::imageSourceURL() const
218 {
219     return getAttribute(XLinkNames::hrefAttr);
220 }
221
222 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
223 {
224     SVGGraphicsElement::addSubresourceAttributeURLs(urls);
225
226     addSubresourceURL(urls, document()->completeURL(href()));
227 }
228
229 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
230 {
231     m_imageLoader.elementDidMoveToNewDocument();
232     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
233 }
234
235 }
236
237 #endif // ENABLE(SVG)