Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "core/svg/SVGImageElement.h"
25
26 #include "CSSPropertyNames.h"
27 #include "XLinkNames.h"
28 #include "core/rendering/RenderImageResource.h"
29 #include "core/rendering/svg/RenderSVGImage.h"
30 #include "core/rendering/svg/RenderSVGResource.h"
31 #include "core/svg/SVGElementInstance.h"
32
33 namespace WebCore {
34
35 inline SVGImageElement::SVGImageElement(Document& document)
36     : SVGGraphicsElement(SVGNames::imageTag, document)
37     , SVGURIReference(this)
38     , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
39     , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
40     , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
41     , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
42     , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
43     , m_imageLoader(this)
44     , m_needsLoaderURIUpdate(true)
45 {
46     ScriptWrappable::init(this);
47
48     addToPropertyMap(m_x);
49     addToPropertyMap(m_y);
50     addToPropertyMap(m_width);
51     addToPropertyMap(m_height);
52     addToPropertyMap(m_preserveAspectRatio);
53 }
54
55 PassRefPtr<SVGImageElement> SVGImageElement::create(Document& document)
56 {
57     return adoptRef(new SVGImageElement(document));
58 }
59
60 bool SVGImageElement::currentFrameHasSingleSecurityOrigin() const
61 {
62     if (RenderSVGImage* renderSVGImage = toRenderSVGImage(renderer())) {
63         if (renderSVGImage->imageResource()->hasImage()) {
64             if (Image* image = renderSVGImage->imageResource()->cachedImage()->image())
65                 return image->currentFrameHasSingleSecurityOrigin();
66         }
67     }
68
69     return true;
70 }
71
72 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
73 {
74     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
75     if (supportedAttributes.isEmpty()) {
76         SVGURIReference::addSupportedAttributes(supportedAttributes);
77         supportedAttributes.add(SVGNames::xAttr);
78         supportedAttributes.add(SVGNames::yAttr);
79         supportedAttributes.add(SVGNames::widthAttr);
80         supportedAttributes.add(SVGNames::heightAttr);
81         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
82     }
83     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
84 }
85
86 bool SVGImageElement::isPresentationAttribute(const QualifiedName& name) const
87 {
88     if (name == SVGNames::widthAttr || name == SVGNames::heightAttr)
89         return true;
90     return SVGGraphicsElement::isPresentationAttribute(name);
91 }
92
93 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
94 {
95     if (!isSupportedAttribute(name))
96         SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
97     else if (name == SVGNames::widthAttr)
98         addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, value);
99     else if (name == SVGNames::heightAttr)
100         addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, value);
101 }
102
103 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
104 {
105     SVGParsingError parseError = NoError;
106
107     if (!isSupportedAttribute(name)) {
108         SVGGraphicsElement::parseAttribute(name, value);
109     } else if (name == SVGNames::xAttr) {
110         m_x->setBaseValueAsString(value, parseError);
111     } else if (name == SVGNames::yAttr) {
112         m_y->setBaseValueAsString(value, parseError);
113     } else if (name == SVGNames::widthAttr) {
114         m_width->setBaseValueAsString(value, parseError);
115     } else if (name == SVGNames::heightAttr) {
116         m_height->setBaseValueAsString(value, parseError);
117     } else if (name == SVGNames::preserveAspectRatioAttr) {
118         m_preserveAspectRatio->setBaseValueAsString(value, parseError);
119     } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
120     } else {
121         ASSERT_NOT_REACHED();
122     }
123
124     reportAttributeParsingError(parseError, name, value);
125 }
126
127 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
128 {
129     if (!isSupportedAttribute(attrName)) {
130         SVGGraphicsElement::svgAttributeChanged(attrName);
131         return;
132     }
133
134     SVGElementInstance::InvalidationGuard invalidationGuard(this);
135
136     bool isLengthAttribute = attrName == SVGNames::xAttr
137                           || attrName == SVGNames::yAttr
138                           || attrName == SVGNames::widthAttr
139                           || attrName == SVGNames::heightAttr;
140
141     if (isLengthAttribute)
142         updateRelativeLengthsInformation();
143
144     if (SVGURIReference::isKnownAttribute(attrName)) {
145         if (inDocument())
146             m_imageLoader.updateFromElementIgnoringPreviousError();
147         else
148             m_needsLoaderURIUpdate = true;
149         return;
150     }
151
152     RenderObject* renderer = this->renderer();
153     if (!renderer)
154         return;
155
156     if (isLengthAttribute) {
157         if (toRenderSVGImage(renderer)->updateImageViewport())
158             RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
159         return;
160     }
161
162     if (attrName == SVGNames::preserveAspectRatioAttr) {
163         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
164         return;
165     }
166
167     ASSERT_NOT_REACHED();
168 }
169
170 bool SVGImageElement::selfHasRelativeLengths() const
171 {
172     return m_x->currentValue()->isRelative()
173         || m_y->currentValue()->isRelative()
174         || m_width->currentValue()->isRelative()
175         || m_height->currentValue()->isRelative();
176 }
177
178 RenderObject* SVGImageElement::createRenderer(RenderStyle*)
179 {
180     return new RenderSVGImage(this);
181 }
182
183 bool SVGImageElement::haveLoadedRequiredResources()
184 {
185     return !m_needsLoaderURIUpdate && !m_imageLoader.hasPendingActivity();
186 }
187
188 void SVGImageElement::attach(const AttachContext& context)
189 {
190     SVGGraphicsElement::attach(context);
191
192     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
193         if (imageObj->imageResource()->hasImage())
194             return;
195
196         imageObj->imageResource()->setImageResource(m_imageLoader.image());
197     }
198 }
199
200 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
201 {
202     SVGGraphicsElement::insertedInto(rootParent);
203     if (!rootParent->inDocument())
204         return InsertionDone;
205
206     // We can only resolve base URIs properly after tree insertion - hence, URI mutations while
207     // detached are deferred until this point.
208     if (m_needsLoaderURIUpdate) {
209         m_imageLoader.updateFromElementIgnoringPreviousError();
210         m_needsLoaderURIUpdate = false;
211     } else {
212         // A previous loader update may have failed to actually fetch the image if the document
213         // was inactive. In that case, force a re-update (but don't clear previous errors).
214         if (!m_imageLoader.image())
215             m_imageLoader.updateFromElement();
216     }
217
218     return InsertionDone;
219 }
220
221 const AtomicString SVGImageElement::imageSourceURL() const
222 {
223     return AtomicString(hrefString());
224 }
225
226 void SVGImageElement::didMoveToNewDocument(Document& oldDocument)
227 {
228     m_imageLoader.elementDidMoveToNewDocument();
229     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
230 }
231
232 }