blob: 67b22ffd8939e1bafa11458bf873e83e44b9ab8d [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
zimmermann@webkit.org711245f2010-08-03 19:39:01 +00002 * 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>
krit@webkit.org9c3caeb2014-07-25 09:52:25 +00005 * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
zimmermann@webkit.org711245f2010-08-03 19:39:01 +00006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
darinb9481ed2006-03-20 02:57:59 +000022
23#include "config.h"
darincfcabdb2006-04-02 02:41:24 +000024#include "SVGImageElement.h"
darinb9481ed2006-03-20 02:57:59 +000025
darincfcabdb2006-04-02 02:41:24 +000026#include "CSSPropertyNames.h"
zimmermann@webkit.org1423ab52010-09-01 14:01:15 +000027#include "RenderImageResource.h"
darincfcabdb2006-04-02 02:41:24 +000028#include "RenderSVGImage.h"
zimmermann@webkit.org5bed57f2010-07-16 08:26:24 +000029#include "RenderSVGResource.h"
zimmermann@webkit.org42ab4a72010-12-03 11:14:32 +000030#include "SVGNames.h"
darincfcabdb2006-04-02 02:41:24 +000031#include "XLinkNames.h"
svillar@igalia.com55cc6d12014-05-08 10:59:17 +000032#include <wtf/NeverDestroyed.h>
darinb9481ed2006-03-20 02:57:59 +000033
eseidel91574f42006-10-05 13:52:39 +000034namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000035
zimmermann@webkit.orgab0da702010-12-01 12:17:12 +000036// Animated property definitions
37DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::xAttr, X, x)
38DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::yAttr, Y, y)
39DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::widthAttr, Width, width)
40DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::heightAttr, Height, height)
zimmermann@webkit.orge50a8b12010-12-03 09:44:21 +000041DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
zimmermann@webkit.org15de2b82010-12-02 15:19:03 +000042DEFINE_ANIMATED_STRING(SVGImageElement, XLinkNames::hrefAttr, Href, href)
zimmermann@webkit.orge50a8b12010-12-03 09:44:21 +000043DEFINE_ANIMATED_BOOLEAN(SVGImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
zimmermann@webkit.orgab0da702010-12-01 12:17:12 +000044
zimmermann@webkit.orga2857422011-07-09 11:26:10 +000045BEGIN_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)
ch.dumez@sisa.samsung.coma47b6182013-06-28 15:58:03 +000053 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
zimmermann@webkit.orga2857422011-07-09 11:26:10 +000054END_REGISTER_ANIMATED_PROPERTIES
55
weinig@apple.com52e786e2013-09-15 18:06:06 +000056inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document& document)
ch.dumez@sisa.samsung.coma47b6182013-06-28 15:58:03 +000057 : SVGGraphicsElement(tagName, document)
zimmermann@webkit.org8fc45392010-01-27 01:51:39 +000058 , m_x(LengthModeWidth)
59 , m_y(LengthModeHeight)
60 , m_width(LengthModeWidth)
61 , m_height(LengthModeHeight)
akling@apple.com68b7ee02014-02-05 18:25:26 +000062 , m_imageLoader(*this)
darinb9481ed2006-03-20 02:57:59 +000063{
zimmermann@webkit.orga2857422011-07-09 11:26:10 +000064 registerAnimatedPropertiesForSVGImageElement();
darinb9481ed2006-03-20 02:57:59 +000065}
66
weinig@apple.com10923572015-01-07 20:56:24 +000067Ref<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document& document)
darin@apple.com3cbd5cb2010-08-30 06:51:56 +000068{
weinig@apple.com10923572015-01-07 20:56:24 +000069 return adoptRef(*new SVGImageElement(tagName, document));
darin@apple.com3cbd5cb2010-08-30 06:51:56 +000070}
71
commit-queue@webkit.org66ec2932016-01-26 21:19:12 +000072bool SVGImageElement::hasSingleSecurityOrigin() const
73{
74 auto* renderer = downcast<RenderSVGImage>(this->renderer());
zalan@apple.comee2f4512017-05-12 03:55:57 +000075 if (!renderer || !renderer->imageResource().cachedImage())
commit-queue@webkit.org66ec2932016-01-26 21:19:12 +000076 return true;
77 auto* image = renderer->imageResource().cachedImage()->image();
78 return !image || image->hasSingleSecurityOrigin();
79}
80
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +000081bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
82{
svillar@igalia.com55cc6d12014-05-08 10:59:17 +000083 static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
84 if (supportedAttributes.get().isEmpty()) {
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +000085 SVGLangSpace::addSupportedAttributes(supportedAttributes);
86 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
87 SVGURIReference::addSupportedAttributes(supportedAttributes);
svillar@igalia.com55cc6d12014-05-08 10:59:17 +000088 supportedAttributes.get().add(SVGNames::xAttr);
89 supportedAttributes.get().add(SVGNames::yAttr);
90 supportedAttributes.get().add(SVGNames::widthAttr);
91 supportedAttributes.get().add(SVGNames::heightAttr);
92 supportedAttributes.get().add(SVGNames::preserveAspectRatioAttr);
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +000093 }
svillar@igalia.com55cc6d12014-05-08 10:59:17 +000094 return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +000095}
96
akling@apple.com43e9d042012-11-18 16:55:06 +000097void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
darinb9481ed2006-03-20 02:57:59 +000098{
darin@apple.com74c44cc2015-03-29 20:40:15 +000099 if (name == SVGNames::preserveAspectRatioAttr) {
commit-queue@webkit.orgc69191d2016-11-11 06:29:44 +0000100 SVGPreserveAspectRatioValue preserveAspectRatio;
akling@apple.com43e9d042012-11-18 16:55:06 +0000101 preserveAspectRatio.parse(value);
zimmermann@webkit.org330ba7c2012-03-28 13:14:20 +0000102 setPreserveAspectRatioBaseValue(preserveAspectRatio);
darin@apple.com74c44cc2015-03-29 20:40:15 +0000103 return;
104 }
105
106 SVGParsingError parseError = NoError;
107
108 if (name == SVGNames::xAttr)
weinig@apple.comf7867302016-11-14 21:18:41 +0000109 setXBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
darin@apple.com74c44cc2015-03-29 20:40:15 +0000110 else if (name == SVGNames::yAttr)
weinig@apple.comf7867302016-11-14 21:18:41 +0000111 setYBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
darin@apple.com74c44cc2015-03-29 20:40:15 +0000112 else if (name == SVGNames::widthAttr)
weinig@apple.comf7867302016-11-14 21:18:41 +0000113 setWidthBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
akling@apple.com43e9d042012-11-18 16:55:06 +0000114 else if (name == SVGNames::heightAttr)
weinig@apple.comf7867302016-11-14 21:18:41 +0000115 setHeightBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
commit-queue@webkit.org1a416c92011-08-04 22:42:30 +0000116
akling@apple.com43e9d042012-11-18 16:55:06 +0000117 reportAttributeParsingError(parseError, name, value);
darin@apple.com74c44cc2015-03-29 20:40:15 +0000118
119 SVGGraphicsElement::parseAttribute(name, value);
120 SVGExternalResourcesRequired::parseAttribute(name, value);
121 SVGURIReference::parseAttribute(name, value);
darinb9481ed2006-03-20 02:57:59 +0000122}
123
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +0000124void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
zimmermann1dab1922007-01-10 11:40:53 +0000125{
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +0000126 if (!isSupportedAttribute(attrName)) {
ch.dumez@sisa.samsung.coma47b6182013-06-28 15:58:03 +0000127 SVGGraphicsElement::svgAttributeChanged(attrName);
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +0000128 return;
129 }
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +0000130
bfulgham@apple.com984c94d2015-02-05 18:07:10 +0000131 InstanceInvalidationGuard guard(*this);
krit@webkit.org3b520fe2014-07-22 13:27:38 +0000132
133 if (attrName == SVGNames::widthAttr
134 || attrName == SVGNames::heightAttr) {
135 invalidateSVGPresentationAttributeStyle();
136 return;
137 }
138
zimmermann@webkit.orgbe3cd242010-07-05 14:32:20 +0000139 bool isLengthAttribute = attrName == SVGNames::xAttr
140 || attrName == SVGNames::yAttr
141 || attrName == SVGNames::widthAttr
142 || attrName == SVGNames::heightAttr;
143
144 if (isLengthAttribute)
145 updateRelativeLengthsInformation();
146
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +0000147 if (SVGURIReference::isKnownAttribute(attrName)) {
148 m_imageLoader.updateFromElementIgnoringPreviousError();
149 return;
150 }
151
cdumez@apple.come1628492014-10-18 00:21:40 +0000152 auto* renderer = this->renderer();
zimmermann@webkit.org19f965a2010-04-13 10:35:19 +0000153 if (!renderer)
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +0000154 return;
155
krit@webkit.org7e4b6752010-10-15 20:02:34 +0000156 if (isLengthAttribute) {
cdumez@apple.come1628492014-10-18 00:21:40 +0000157 if (downcast<RenderSVGImage>(*renderer).updateImageViewport())
commit-queue@webkit.org48a92842013-12-16 19:23:24 +0000158 RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
krit@webkit.org7e4b6752010-10-15 20:02:34 +0000159 return;
160 }
161
162 if (attrName == SVGNames::preserveAspectRatioAttr
zimmermann@webkit.org19f965a2010-04-13 10:35:19 +0000163 || SVGLangSpace::isKnownAttribute(attrName)
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +0000164 || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
commit-queue@webkit.org48a92842013-12-16 19:23:24 +0000165 RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +0000166 return;
167 }
168
169 ASSERT_NOT_REACHED();
zimmermann1dab1922007-01-10 11:40:53 +0000170}
171
antti@apple.com454418f2016-04-25 19:49:23 +0000172RenderPtr<RenderElement> SVGImageElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
darinb9481ed2006-03-20 02:57:59 +0000173{
aestes@apple.com13aae082016-01-02 08:03:08 +0000174 return createRenderer<RenderSVGImage>(*this, WTFMove(style));
darinb9481ed2006-03-20 02:57:59 +0000175}
176
eseideleb0627e2006-08-15 06:12:39 +0000177bool SVGImageElement::haveLoadedRequiredResources()
178{
commit-queue@webkit.orgdbef5a52012-09-17 09:05:13 +0000179 return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
eseideleb0627e2006-08-15 06:12:39 +0000180}
181
antti@apple.com0f2ac5b2013-08-18 20:01:20 +0000182void SVGImageElement::didAttachRenderers()
darinb9481ed2006-03-20 02:57:59 +0000183{
cdumez@apple.come1628492014-10-18 00:21:40 +0000184 if (auto* imageObj = downcast<RenderSVGImage>(renderer())) {
zalan@apple.comee2f4512017-05-12 03:55:57 +0000185 if (imageObj->imageResource().cachedImage())
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000186 return;
187
akling@apple.comdcf6c8f2014-01-20 17:27:08 +0000188 imageObj->imageResource().setCachedImage(m_imageLoader.image());
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000189 }
190}
191
akling@apple.com2e55ebf2013-10-04 18:51:32 +0000192Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode& rootParent)
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000193{
ch.dumez@sisa.samsung.coma47b6182013-06-28 15:58:03 +0000194 SVGGraphicsElement::insertedInto(rootParent);
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000195 if (!rootParent.isConnected())
commit-queue@webkit.org9ea00802012-04-17 06:40:55 +0000196 return InsertionDone;
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000197 // Update image loader, as soon as we're living in the tree.
198 // We can only resolve base URIs properly, after that!
199 m_imageLoader.updateFromElement();
commit-queue@webkit.org9ea00802012-04-17 06:40:55 +0000200 return InsertionDone;
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000201}
202
commit-queue@webkit.org25fd37e2013-03-13 21:16:50 +0000203const AtomicString& SVGImageElement::imageSourceURL() const
zimmermann@webkit.org953b7442008-09-20 20:19:45 +0000204{
commit-queue@webkit.org25fd37e2013-03-13 21:16:50 +0000205 return getAttribute(XLinkNames::hrefAttr);
darinb9481ed2006-03-20 02:57:59 +0000206}
207
darin@apple.com5ffbb5c2013-09-27 16:39:41 +0000208void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
beidson@apple.coma4fb38f2008-03-27 04:08:17 +0000209{
ch.dumez@sisa.samsung.coma47b6182013-06-28 15:58:03 +0000210 SVGGraphicsElement::addSubresourceAttributeURLs(urls);
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000211
akling@apple.com622b1a42013-08-30 14:30:12 +0000212 addSubresourceURL(urls, document().completeURL(href()));
beidson@apple.coma4fb38f2008-03-27 04:08:17 +0000213}
214
rniwa@webkit.orgca240892017-06-07 08:02:46 +0000215void SVGImageElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument)
darin@apple.com21e3cf92010-04-06 01:38:15 +0000216{
commit-queue@webkit.org4b3425062015-04-02 00:22:54 +0000217 m_imageLoader.elementDidMoveToNewDocument();
rniwa@webkit.orgca240892017-06-07 08:02:46 +0000218 SVGGraphicsElement::didMoveToNewDocument(oldDocument, newDocument);
darin@apple.com21e3cf92010-04-06 01:38:15 +0000219}
220
eseidel91574f42006-10-05 13:52:39 +0000221}