Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / svg / SVGImageElement.cpp
blob4e1d852d8775e4f590f50b1882d82aa06c3930b2
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>
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.
22 #include "config.h"
24 #include "core/svg/SVGImageElement.h"
26 #include "core/CSSPropertyNames.h"
27 #include "core/XLinkNames.h"
28 #include "core/layout/LayoutImageResource.h"
29 #include "core/layout/svg/LayoutSVGImage.h"
31 namespace blink {
33 inline SVGImageElement::SVGImageElement(Document& document)
34 : SVGGraphicsElement(SVGNames::imageTag, document)
35 , SVGURIReference(this)
36 , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(SVGLengthMode::Width), AllowNegativeLengths))
37 , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(SVGLengthMode::Height), AllowNegativeLengths))
38 , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(SVGLengthMode::Width), ForbidNegativeLengths))
39 , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(SVGLengthMode::Height), ForbidNegativeLengths))
40 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
41 , m_imageLoader(SVGImageLoader::create(this))
42 , m_needsLoaderURIUpdate(true)
44 addToPropertyMap(m_x);
45 addToPropertyMap(m_y);
46 addToPropertyMap(m_width);
47 addToPropertyMap(m_height);
48 addToPropertyMap(m_preserveAspectRatio);
51 DEFINE_NODE_FACTORY(SVGImageElement)
53 DEFINE_TRACE(SVGImageElement)
55 visitor->trace(m_x);
56 visitor->trace(m_y);
57 visitor->trace(m_width);
58 visitor->trace(m_height);
59 visitor->trace(m_preserveAspectRatio);
60 visitor->trace(m_imageLoader);
61 SVGGraphicsElement::trace(visitor);
62 SVGURIReference::trace(visitor);
65 bool SVGImageElement::currentFrameHasSingleSecurityOrigin() const
67 if (LayoutSVGImage* layoutSVGImage = toLayoutSVGImage(layoutObject())) {
68 if (layoutSVGImage->imageResource()->hasImage()) {
69 if (Image* image = layoutSVGImage->imageResource()->cachedImage()->image())
70 return image->currentFrameHasSingleSecurityOrigin();
74 return true;
77 bool SVGImageElement::isPresentationAttribute(const QualifiedName& attrName) const
79 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr
80 || attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr)
81 return true;
82 return SVGGraphicsElement::isPresentationAttribute(attrName);
85 bool SVGImageElement::isPresentationAttributeWithSVGDOM(const QualifiedName& attrName) const
87 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr
88 || attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr)
89 return true;
90 return SVGGraphicsElement::isPresentationAttributeWithSVGDOM(attrName);
93 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
95 RefPtrWillBeRawPtr<SVGAnimatedPropertyBase> property = propertyFromAttribute(name);
97 if (property == m_width)
98 addSVGLengthPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, *m_width->currentValue());
99 else if (property == m_height)
100 addSVGLengthPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, *m_height->currentValue());
101 else if (property == m_x)
102 addSVGLengthPropertyToPresentationAttributeStyle(style, CSSPropertyX, *m_x->currentValue());
103 else if (property == m_y)
104 addSVGLengthPropertyToPresentationAttributeStyle(style, CSSPropertyY, *m_y->currentValue());
105 else
106 SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
109 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
111 bool isLengthAttribute = attrName == SVGNames::xAttr
112 || attrName == SVGNames::yAttr
113 || attrName == SVGNames::widthAttr
114 || attrName == SVGNames::heightAttr;
116 if (isLengthAttribute || attrName == SVGNames::preserveAspectRatioAttr) {
117 SVGElement::InvalidationGuard invalidationGuard(this);
119 if (isLengthAttribute) {
120 invalidateSVGPresentationAttributeStyle();
121 setNeedsStyleRecalc(LocalStyleChange,
122 StyleChangeReasonForTracing::fromAttribute(attrName));
123 updateRelativeLengthsInformation();
126 LayoutObject* object = this->layoutObject();
127 if (!object)
128 return;
130 // FIXME: if isLengthAttribute then we should avoid this call if the
131 // viewport didn't change, however since we don't have the computed
132 // style yet we can't use updateBoundingBox/updateImageContainerSize.
133 // See http://crbug.com/466200.
134 markForLayoutAndParentResourceInvalidation(object);
135 return;
138 if (SVGURIReference::isKnownAttribute(attrName)) {
139 SVGElement::InvalidationGuard invalidationGuard(this);
140 if (inDocument())
141 imageLoader().updateFromElement(ImageLoader::UpdateIgnorePreviousError);
142 else
143 m_needsLoaderURIUpdate = true;
144 return;
147 SVGGraphicsElement::svgAttributeChanged(attrName);
150 bool SVGImageElement::selfHasRelativeLengths() const
152 return m_x->currentValue()->isRelative()
153 || m_y->currentValue()->isRelative()
154 || m_width->currentValue()->isRelative()
155 || m_height->currentValue()->isRelative();
158 LayoutObject* SVGImageElement::createLayoutObject(const ComputedStyle&)
160 return new LayoutSVGImage(this);
163 bool SVGImageElement::haveLoadedRequiredResources()
165 return !m_needsLoaderURIUpdate && !imageLoader().hasPendingActivity();
168 void SVGImageElement::attach(const AttachContext& context)
170 SVGGraphicsElement::attach(context);
172 if (LayoutSVGImage* imageObj = toLayoutSVGImage(layoutObject())) {
173 if (imageObj->imageResource()->hasImage())
174 return;
176 imageObj->imageResource()->setImageResource(imageLoader().image());
180 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
182 SVGGraphicsElement::insertedInto(rootParent);
183 if (!rootParent->inDocument())
184 return InsertionDone;
186 // We can only resolve base URIs properly after tree insertion - hence, URI mutations while
187 // detached are deferred until this point.
188 if (m_needsLoaderURIUpdate) {
189 imageLoader().updateFromElement(ImageLoader::UpdateIgnorePreviousError);
190 m_needsLoaderURIUpdate = false;
191 } else {
192 // A previous loader update may have failed to actually fetch the image if the document
193 // was inactive. In that case, force a re-update (but don't clear previous errors).
194 if (!imageLoader().image())
195 imageLoader().updateFromElement();
198 return InsertionDone;
201 const AtomicString SVGImageElement::imageSourceURL() const
203 return AtomicString(hrefString());
206 void SVGImageElement::didMoveToNewDocument(Document& oldDocument)
208 imageLoader().elementDidMoveToNewDocument();
209 SVGGraphicsElement::didMoveToNewDocument(oldDocument);
212 } // namespace blink