Add the default video poster if it doesn't exist in video tag
[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(const QualifiedName& name) const
95 {
96     if (name == SVGNames::widthAttr || name == SVGNames::heightAttr)
97         return true;
98     return SVGStyledTransformableElement::isPresentationAttribute(name);
99 }
100
101 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
102 {
103     if (!isSupportedAttribute(name))
104         SVGStyledTransformableElement::collectStyleForPresentationAttribute(name, value, style);
105     else if (name == SVGNames::widthAttr)
106         addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, value);
107     else if (name == SVGNames::heightAttr)
108         addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, value);
109 }
110
111 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
112 {
113     SVGParsingError parseError = NoError;
114
115     if (!isSupportedAttribute(name))
116         SVGStyledTransformableElement::parseAttribute(name, value);
117     else if (name == SVGNames::xAttr)
118         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
119     else if (name == SVGNames::yAttr)
120         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
121     else if (name == SVGNames::preserveAspectRatioAttr) {
122         SVGPreserveAspectRatio preserveAspectRatio;
123         preserveAspectRatio.parse(value);
124         setPreserveAspectRatioBaseValue(preserveAspectRatio);
125     } else if (name == SVGNames::widthAttr)
126         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
127     else if (name == SVGNames::heightAttr)
128         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
129     else if (SVGTests::parseAttribute(name, value)
130              || SVGLangSpace::parseAttribute(name, value)
131              || SVGExternalResourcesRequired::parseAttribute(name, value)
132              || SVGURIReference::parseAttribute(name, value)) {
133     } else
134         ASSERT_NOT_REACHED();
135
136     reportAttributeParsingError(parseError, name, value);
137 }
138
139 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
140 {
141     if (!isSupportedAttribute(attrName)) {
142         SVGStyledTransformableElement::svgAttributeChanged(attrName);
143         return;
144     }
145
146     SVGElementInstance::InvalidationGuard invalidationGuard(this);
147     
148     bool isLengthAttribute = attrName == SVGNames::xAttr
149                           || attrName == SVGNames::yAttr
150                           || attrName == SVGNames::widthAttr
151                           || attrName == SVGNames::heightAttr;
152
153     if (isLengthAttribute)
154         updateRelativeLengthsInformation();
155
156     if (SVGTests::handleAttributeChange(this, attrName))
157         return;
158
159     if (SVGURIReference::isKnownAttribute(attrName)) {
160         m_imageLoader.updateFromElementIgnoringPreviousError();
161         return;
162     }
163
164     RenderObject* renderer = this->renderer();
165     if (!renderer)
166         return;
167
168     if (isLengthAttribute) {
169         if (toRenderSVGImage(renderer)->updateImageViewport())
170             RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
171         return;
172     }
173
174     if (attrName == SVGNames::preserveAspectRatioAttr
175         || SVGLangSpace::isKnownAttribute(attrName)
176         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
177         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
178         return;
179     }
180
181     ASSERT_NOT_REACHED();
182 }
183
184 bool SVGImageElement::selfHasRelativeLengths() const
185 {
186     return x().isRelative()
187         || y().isRelative()
188         || width().isRelative()
189         || height().isRelative();
190 }
191
192 RenderObject* SVGImageElement::createRenderer(RenderArena* arena, RenderStyle*)
193 {
194     return new (arena) RenderSVGImage(this);
195 }
196
197 bool SVGImageElement::haveLoadedRequiredResources()
198 {
199     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
200 }
201
202 void SVGImageElement::attach()
203 {
204     SVGStyledTransformableElement::attach();
205
206     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
207         if (imageObj->imageResource()->hasImage())
208             return;
209
210         imageObj->imageResource()->setCachedImage(m_imageLoader.image());
211     }
212 }
213
214 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
215 {
216     SVGStyledTransformableElement::insertedInto(rootParent);
217     if (!rootParent->inDocument())
218         return InsertionDone;
219     // Update image loader, as soon as we're living in the tree.
220     // We can only resolve base URIs properly, after that!
221     m_imageLoader.updateFromElement();
222     return InsertionDone;
223 }
224
225 const AtomicString& SVGImageElement::imageSourceURL() const
226 {
227     return getAttribute(XLinkNames::hrefAttr);
228 }
229
230 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
231 {
232     SVGStyledTransformableElement::addSubresourceAttributeURLs(urls);
233
234     addSubresourceURL(urls, document()->completeURL(href()));
235 }
236
237 void SVGImageElement::didMoveToNewDocument(Document* oldDocument)
238 {
239     m_imageLoader.elementDidMoveToNewDocument();
240     SVGStyledTransformableElement::didMoveToNewDocument(oldDocument);
241 }
242
243 }
244
245 #endif // ENABLE(SVG)