Remove stylesheet pointer from StylePropertySet
[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(SVGStyledTransformableElement)
57     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
58 END_REGISTER_ANIMATED_PROPERTIES
59
60 inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document* document)
61     : SVGStyledTransformableElement(tagName, document)
62     , m_x(LengthModeWidth)
63     , m_y(LengthModeHeight)
64     , m_width(LengthModeWidth)
65     , m_height(LengthModeHeight)
66     , m_imageLoader(this)
67 {
68     ASSERT(hasTagName(SVGNames::imageTag));
69     registerAnimatedPropertiesForSVGImageElement();
70 }
71
72 PassRefPtr<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document* document)
73 {
74     return adoptRef(new SVGImageElement(tagName, document));
75 }
76
77 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
78 {
79     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
80     if (supportedAttributes.isEmpty()) {
81         SVGTests::addSupportedAttributes(supportedAttributes);
82         SVGLangSpace::addSupportedAttributes(supportedAttributes);
83         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
84         SVGURIReference::addSupportedAttributes(supportedAttributes);
85         supportedAttributes.add(SVGNames::xAttr);
86         supportedAttributes.add(SVGNames::yAttr);
87         supportedAttributes.add(SVGNames::widthAttr);
88         supportedAttributes.add(SVGNames::heightAttr);
89         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
90     }
91     return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
92 }
93
94 bool SVGImageElement::isPresentationAttribute(Attribute* attr) const
95 {
96     if (attr->name() == SVGNames::widthAttr || attr->name() == SVGNames::heightAttr)
97         return true;
98     return SVGStyledTransformableElement::isPresentationAttribute(attr);
99 }
100
101 void SVGImageElement::collectStyleForAttribute(Attribute* attr, StylePropertySet* style)
102 {
103     if (!isSupportedAttribute(attr->name()))
104         SVGStyledTransformableElement::collectStyleForAttribute(attr, style);
105     else if (attr->name() == SVGNames::widthAttr)
106         addPropertyToAttributeStyle(style, CSSPropertyWidth, attr->value());
107     else if (attr->name() == SVGNames::heightAttr)
108         addPropertyToAttributeStyle(style, CSSPropertyHeight, attr->value());
109 }
110
111 void SVGImageElement::parseAttribute(Attribute* attr)
112 {
113     SVGParsingError parseError = NoError;
114
115     if (!isSupportedAttribute(attr->name()))
116         SVGStyledTransformableElement::parseAttribute(attr);
117     else if (attr->name() == SVGNames::xAttr)
118         setXBaseValue(SVGLength::construct(LengthModeWidth, attr->value(), parseError));
119     else if (attr->name() == SVGNames::yAttr)
120         setYBaseValue(SVGLength::construct(LengthModeHeight, attr->value(), parseError));
121     else if (attr->name() == SVGNames::preserveAspectRatioAttr)
122         SVGPreserveAspectRatio::parsePreserveAspectRatio(this, attr->value());
123     else if (attr->name() == SVGNames::widthAttr)
124         setWidthBaseValue(SVGLength::construct(LengthModeWidth, attr->value(), parseError, ForbidNegativeLengths));
125     else if (attr->name() == SVGNames::heightAttr)
126         setHeightBaseValue(SVGLength::construct(LengthModeHeight, attr->value(), parseError, ForbidNegativeLengths));
127     else if (SVGTests::parseAttribute(attr)
128              || SVGLangSpace::parseAttribute(attr)
129              || SVGExternalResourcesRequired::parseAttribute(attr)
130              || SVGURIReference::parseAttribute(attr)) {
131     } else
132         ASSERT_NOT_REACHED();
133
134     reportAttributeParsingError(parseError, attr);
135 }
136
137 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
138 {
139     if (!isSupportedAttribute(attrName)) {
140         SVGStyledTransformableElement::svgAttributeChanged(attrName);
141         return;
142     }
143
144     SVGElementInstance::InvalidationGuard invalidationGuard(this);
145     
146     bool isLengthAttribute = attrName == SVGNames::xAttr
147                           || attrName == SVGNames::yAttr
148                           || attrName == SVGNames::widthAttr
149                           || attrName == SVGNames::heightAttr;
150
151     if (isLengthAttribute)
152         updateRelativeLengthsInformation();
153
154     if (SVGTests::handleAttributeChange(this, attrName))
155         return;
156
157     if (SVGURIReference::isKnownAttribute(attrName)) {
158         m_imageLoader.updateFromElementIgnoringPreviousError();
159         return;
160     }
161
162     RenderObject* renderer = this->renderer();
163     if (!renderer)
164         return;
165
166     if (isLengthAttribute) {
167         if (toRenderSVGImage(renderer)->updateImageViewport())
168             RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
169         return;
170     }
171
172     if (attrName == SVGNames::preserveAspectRatioAttr
173         || SVGLangSpace::isKnownAttribute(attrName)
174         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
175         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
176         return;
177     }
178
179     ASSERT_NOT_REACHED();
180 }
181
182 bool SVGImageElement::selfHasRelativeLengths() const
183 {
184     return x().isRelative()
185         || y().isRelative()
186         || width().isRelative()
187         || height().isRelative();
188 }
189
190 RenderObject* SVGImageElement::createRenderer(RenderArena* arena, RenderStyle*)
191 {
192     return new (arena) RenderSVGImage(this);
193 }
194
195 bool SVGImageElement::haveLoadedRequiredResources()
196 {
197     return !externalResourcesRequiredBaseValue() || m_imageLoader.haveFiredLoadEvent();
198 }
199
200 void SVGImageElement::attach()
201 {
202     SVGStyledTransformableElement::attach();
203
204     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
205         if (imageObj->imageResource()->hasImage())
206             return;
207
208         imageObj->imageResource()->setCachedImage(m_imageLoader.image());
209     }
210 }
211
212 void SVGImageElement::insertedIntoDocument()
213 {
214     SVGStyledTransformableElement::insertedIntoDocument();
215
216     // Update image loader, as soon as we're living in the tree.
217     // We can only resolve base URIs properly, after that!
218     m_imageLoader.updateFromElement();
219 }
220
221 const QualifiedName& SVGImageElement::imageSourceAttributeName() const
222 {
223     return XLinkNames::hrefAttr;
224 }
225
226 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
227 {
228     SVGStyledTransformableElement::addSubresourceAttributeURLs(urls);
229
230     addSubresourceURL(urls, document()->completeURL(href()));
231 }
232
233 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
234 {
235     m_imageLoader.elementDidMoveToNewDocument();
236     SVGStyledTransformableElement::didMoveToNewDocument(oldDocument);
237 }
238
239 }
240
241 #endif // ENABLE(SVG)