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>
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.
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.
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.
24 #include "core/svg/SVGImageElement.h"
26 #include "CSSPropertyNames.h"
28 #include "XLinkNames.h"
29 #include "core/rendering/RenderImageResource.h"
30 #include "core/rendering/svg/RenderSVGImage.h"
31 #include "core/rendering/svg/RenderSVGResource.h"
32 #include "core/svg/SVGElementInstance.h"
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)
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
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)
64 ASSERT(hasTagName(SVGNames::imageTag));
65 ScriptWrappable::init(this);
66 registerAnimatedPropertiesForSVGImageElement();
69 PassRefPtr<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document& document)
71 return adoptRef(new SVGImageElement(tagName, document));
74 bool SVGImageElement::currentFrameHasSingleSecurityOrigin() const
76 if (RenderSVGImage* renderSVGImage = toRenderSVGImage(renderer())) {
77 if (renderSVGImage->imageResource()->hasImage()) {
78 if (Image* image = renderSVGImage->imageResource()->cachedImage()->image())
79 return image->currentFrameHasSingleSecurityOrigin();
86 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
88 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
89 if (supportedAttributes.isEmpty()) {
90 SVGLangSpace::addSupportedAttributes(supportedAttributes);
91 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
92 SVGURIReference::addSupportedAttributes(supportedAttributes);
93 supportedAttributes.add(SVGNames::xAttr);
94 supportedAttributes.add(SVGNames::yAttr);
95 supportedAttributes.add(SVGNames::widthAttr);
96 supportedAttributes.add(SVGNames::heightAttr);
97 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
99 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
102 bool SVGImageElement::isPresentationAttribute(const QualifiedName& name) const
104 if (name == SVGNames::widthAttr || name == SVGNames::heightAttr)
106 return SVGGraphicsElement::isPresentationAttribute(name);
109 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
111 if (!isSupportedAttribute(name))
112 SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
113 else if (name == SVGNames::widthAttr)
114 addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, value);
115 else if (name == SVGNames::heightAttr)
116 addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, value);
119 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
121 SVGParsingError parseError = NoError;
123 if (!isSupportedAttribute(name))
124 SVGGraphicsElement::parseAttribute(name, value);
125 else if (name == SVGNames::xAttr)
126 setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
127 else if (name == SVGNames::yAttr)
128 setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
129 else if (name == SVGNames::preserveAspectRatioAttr) {
130 SVGPreserveAspectRatio preserveAspectRatio;
131 preserveAspectRatio.parse(value);
132 setPreserveAspectRatioBaseValue(preserveAspectRatio);
133 } else if (name == SVGNames::widthAttr)
134 setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
135 else if (name == SVGNames::heightAttr)
136 setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
137 else if (SVGLangSpace::parseAttribute(name, value)
138 || SVGExternalResourcesRequired::parseAttribute(name, value)
139 || SVGURIReference::parseAttribute(name, value)) {
141 ASSERT_NOT_REACHED();
143 reportAttributeParsingError(parseError, name, value);
146 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
148 if (!isSupportedAttribute(attrName)) {
149 SVGGraphicsElement::svgAttributeChanged(attrName);
153 SVGElementInstance::InvalidationGuard invalidationGuard(this);
155 bool isLengthAttribute = attrName == SVGNames::xAttr
156 || attrName == SVGNames::yAttr
157 || attrName == SVGNames::widthAttr
158 || attrName == SVGNames::heightAttr;
160 if (isLengthAttribute)
161 updateRelativeLengthsInformation();
163 if (SVGURIReference::isKnownAttribute(attrName)) {
164 m_imageLoader.updateFromElementIgnoringPreviousError();
168 RenderObject* renderer = this->renderer();
172 if (isLengthAttribute) {
173 if (toRenderSVGImage(renderer)->updateImageViewport())
174 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
178 if (attrName == SVGNames::preserveAspectRatioAttr
179 || SVGLangSpace::isKnownAttribute(attrName)
180 || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
181 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
185 ASSERT_NOT_REACHED();
188 bool SVGImageElement::selfHasRelativeLengths() const
190 return xCurrentValue().isRelative()
191 || yCurrentValue().isRelative()
192 || widthCurrentValue().isRelative()
193 || heightCurrentValue().isRelative();
196 RenderObject* SVGImageElement::createRenderer(RenderStyle*)
198 return new RenderSVGImage(this);
201 bool SVGImageElement::haveLoadedRequiredResources()
203 return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
206 void SVGImageElement::attach(const AttachContext& context)
208 SVGGraphicsElement::attach(context);
210 if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
211 if (imageObj->imageResource()->hasImage())
214 imageObj->imageResource()->setImageResource(m_imageLoader.image());
218 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
220 SVGGraphicsElement::insertedInto(rootParent);
221 if (!rootParent->inDocument())
222 return InsertionDone;
223 // Update image loader, as soon as we're living in the tree.
224 // We can only resolve base URIs properly, after that!
225 m_imageLoader.updateFromElement();
226 return InsertionDone;
229 const AtomicString SVGImageElement::imageSourceURL() const
231 return hrefCurrentValue();
234 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
236 SVGGraphicsElement::addSubresourceAttributeURLs(urls);
238 addSubresourceURL(urls, document().completeURL(hrefCurrentValue()));
241 void SVGImageElement::didMoveToNewDocument(Document& oldDocument)
243 m_imageLoader.elementDidMoveToNewDocument();
244 SVGGraphicsElement::didMoveToNewDocument(oldDocument);