blob: 1a3399e4da0589f9d0f29f5d6cdab49338539bee [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010023#include "core/svg/SVGRectElement.h"
24
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010025#include "core/rendering/svg/RenderSVGRect.h"
26#include "core/rendering/svg/RenderSVGResource.h"
27#include "core/svg/SVGElementInstance.h"
28#include "core/svg/SVGLength.h"
29
30namespace WebCore {
31
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000032inline SVGRectElement::SVGRectElement(Document& document)
33 : SVGGeometryElement(SVGNames::rectTag, document)
Ben Murdoch07a852d2014-03-31 11:51:52 +010034 , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
35 , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
36 , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
37 , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
38 , m_rx(SVGAnimatedLength::create(this, SVGNames::rxAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
39 , m_ry(SVGAnimatedLength::create(this, SVGNames::ryAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040{
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010041 ScriptWrappable::init(this);
Torne (Richard Coles)09380292014-02-21 12:17:33 +000042
43 addToPropertyMap(m_x);
44 addToPropertyMap(m_y);
45 addToPropertyMap(m_width);
46 addToPropertyMap(m_height);
47 addToPropertyMap(m_rx);
48 addToPropertyMap(m_ry);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010049}
50
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000051PassRefPtr<SVGRectElement> SVGRectElement::create(Document& document)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010052{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000053 return adoptRef(new SVGRectElement(document));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010054}
55
56bool SVGRectElement::isSupportedAttribute(const QualifiedName& attrName)
57{
58 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
59 if (supportedAttributes.isEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060 supportedAttributes.add(SVGNames::xAttr);
61 supportedAttributes.add(SVGNames::yAttr);
62 supportedAttributes.add(SVGNames::widthAttr);
63 supportedAttributes.add(SVGNames::heightAttr);
64 supportedAttributes.add(SVGNames::rxAttr);
65 supportedAttributes.add(SVGNames::ryAttr);
66 }
Ben Murdoch591b9582013-07-10 11:41:44 +010067 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010068}
69
70void SVGRectElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
71{
72 SVGParsingError parseError = NoError;
73
74 if (!isSupportedAttribute(name))
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000075 SVGGeometryElement::parseAttribute(name, value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010076 else if (name == SVGNames::xAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010077 m_x->setBaseValueAsString(value, parseError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010078 else if (name == SVGNames::yAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010079 m_y->setBaseValueAsString(value, parseError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010080 else if (name == SVGNames::rxAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010081 m_rx->setBaseValueAsString(value, parseError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082 else if (name == SVGNames::ryAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010083 m_ry->setBaseValueAsString(value, parseError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010084 else if (name == SVGNames::widthAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010085 m_width->setBaseValueAsString(value, parseError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010086 else if (name == SVGNames::heightAttr)
Ben Murdoch07a852d2014-03-31 11:51:52 +010087 m_height->setBaseValueAsString(value, parseError);
Torne (Richard Coles)09380292014-02-21 12:17:33 +000088 else
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010089 ASSERT_NOT_REACHED();
90
91 reportAttributeParsingError(parseError, name, value);
92}
93
94void SVGRectElement::svgAttributeChanged(const QualifiedName& attrName)
95{
96 if (!isSupportedAttribute(attrName)) {
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000097 SVGGeometryElement::svgAttributeChanged(attrName);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010098 return;
99 }
100
101 SVGElementInstance::InvalidationGuard invalidationGuard(this);
Ben Murdoch02772c62013-07-26 10:21:05 +0100102
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100103 bool isLengthAttribute = attrName == SVGNames::xAttr
104 || attrName == SVGNames::yAttr
105 || attrName == SVGNames::widthAttr
106 || attrName == SVGNames::heightAttr
107 || attrName == SVGNames::rxAttr
108 || attrName == SVGNames::ryAttr;
109
110 if (isLengthAttribute)
111 updateRelativeLengthsInformation();
112
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100113 RenderSVGShape* renderer = toRenderSVGShape(this->renderer());
114 if (!renderer)
115 return;
116
117 if (isLengthAttribute) {
118 renderer->setNeedsShapeUpdate();
119 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
120 return;
121 }
122
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100123 ASSERT_NOT_REACHED();
124}
125
126bool SVGRectElement::selfHasRelativeLengths() const
127{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000128 return m_x->currentValue()->isRelative()
129 || m_y->currentValue()->isRelative()
130 || m_width->currentValue()->isRelative()
131 || m_height->currentValue()->isRelative()
132 || m_rx->currentValue()->isRelative()
133 || m_ry->currentValue()->isRelative();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100134}
135
Ben Murdoch591b9582013-07-10 11:41:44 +0100136RenderObject* SVGRectElement::createRenderer(RenderStyle*)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100137{
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100138 return new RenderSVGRect(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100139}
140
141}