1 /*
2 Copyright (C) 2006 Apple Computer, Inc.
3 (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
4
5 This file is part of the WebKit project
6
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 */
22
23 #include "config.h"
24
25 #if ENABLE(SVG) && ENABLE(SVG_FOREIGN_OBJECT)
26 #include "SVGForeignObjectElement.h"
27
28 #include "CSSPropertyNames.h"
29 #include "MappedAttribute.h"
30 #include "RenderForeignObject.h"
31 #include "SVGLength.h"
32 #include "SVGNames.h"
33 #include <wtf/Assertions.h>
34
35 namespace WebCore {
36
SVGForeignObjectElement(const QualifiedName & tagName,Document * doc)37 SVGForeignObjectElement::SVGForeignObjectElement(const QualifiedName& tagName, Document *doc)
38 : SVGStyledTransformableElement(tagName, doc)
39 , SVGTests()
40 , SVGLangSpace()
41 , SVGExternalResourcesRequired()
42 , m_x(this, SVGNames::xAttr, LengthModeWidth)
43 , m_y(this, SVGNames::yAttr, LengthModeHeight)
44 , m_width(this, SVGNames::widthAttr, LengthModeWidth)
45 , m_height(this, SVGNames::heightAttr, LengthModeHeight)
46 {
47 }
48
~SVGForeignObjectElement()49 SVGForeignObjectElement::~SVGForeignObjectElement()
50 {
51 }
52
parseMappedAttribute(MappedAttribute * attr)53 void SVGForeignObjectElement::parseMappedAttribute(MappedAttribute* attr)
54 {
55 const AtomicString& value = attr->value();
56 if (attr->name() == SVGNames::xAttr)
57 setXBaseValue(SVGLength(LengthModeWidth, value));
58 else if (attr->name() == SVGNames::yAttr)
59 setYBaseValue(SVGLength(LengthModeHeight, value));
60 else if (attr->name() == SVGNames::widthAttr)
61 setWidthBaseValue(SVGLength(LengthModeWidth, value));
62 else if (attr->name() == SVGNames::heightAttr)
63 setHeightBaseValue(SVGLength(LengthModeHeight, value));
64 else {
65 if (SVGTests::parseMappedAttribute(attr))
66 return;
67 if (SVGLangSpace::parseMappedAttribute(attr))
68 return;
69 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
70 return;
71 SVGStyledTransformableElement::parseMappedAttribute(attr);
72 }
73 }
74
75 // TODO: Move this function in some SVG*Element base class, as SVGSVGElement / SVGImageElement will need the same logic!
76
77 // This function mimics addCSSProperty and StyledElement::attributeChanged.
78 // In HTML code, you'd always call addCSSProperty from your derived parseMappedAttribute()
79 // function - though in SVG code we need to move this logic into svgAttributeChanged, in
80 // order to support SVG DOM changes (which don't use the parseMappedAttribute/attributeChanged).
81 // If we'd ignore SVG DOM, we could use _exactly_ the same logic as HTML.
addCSSPropertyAndNotifyAttributeMap(StyledElement * element,const QualifiedName & name,int cssProperty,const String & value)82 static inline void addCSSPropertyAndNotifyAttributeMap(StyledElement* element, const QualifiedName& name, int cssProperty, const String& value)
83 {
84 ASSERT(element);
85
86 if (!element)
87 return;
88
89 NamedMappedAttrMap* attrs = element->mappedAttributes();
90 ASSERT(attrs);
91
92 if (!attrs)
93 return;
94
95 Attribute* attr = attrs->getAttributeItem(name);
96 if (!attr || !attr->isMappedAttribute())
97 return;
98
99 MappedAttribute* mappedAttr = static_cast<MappedAttribute*>(attr);
100
101 // This logic is only meant to be used for entries that have to be parsed and are mapped to eNone. Assert that.
102 MappedAttributeEntry entry;
103 bool needToParse = element->mapToEntry(mappedAttr->name(), entry);
104
105 ASSERT(needToParse);
106 ASSERT(entry == eNone);
107
108 if (!needToParse || entry != eNone)
109 return;
110
111 if (mappedAttr->decl()) {
112 mappedAttr->setDecl(0);
113 attrs->declRemoved();
114 }
115
116 element->setNeedsStyleRecalc();
117 element->addCSSProperty(mappedAttr, cssProperty, value);
118
119 if (CSSMappedAttributeDeclaration* decl = mappedAttr->decl()) {
120 // Add the decl to the table in the appropriate spot.
121 element->setMappedAttributeDecl(entry, mappedAttr, decl);
122
123 decl->setMappedState(entry, mappedAttr->name(), mappedAttr->value());
124 decl->setParent(0);
125 decl->setNode(0);
126
127 attrs->declAdded();
128 }
129 }
130
svgAttributeChanged(const QualifiedName & attrName)131 void SVGForeignObjectElement::svgAttributeChanged(const QualifiedName& attrName)
132 {
133 SVGStyledTransformableElement::svgAttributeChanged(attrName);
134
135 if (attrName == SVGNames::widthAttr) {
136 addCSSPropertyAndNotifyAttributeMap(this, attrName, CSSPropertyWidth, width().valueAsString());
137 return;
138 } else if (attrName == SVGNames::heightAttr) {
139 addCSSPropertyAndNotifyAttributeMap(this, attrName, CSSPropertyHeight, height().valueAsString());
140 return;
141 }
142
143 if (!renderer())
144 return;
145
146 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr ||
147 SVGTests::isKnownAttribute(attrName) ||
148 SVGLangSpace::isKnownAttribute(attrName) ||
149 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
150 SVGStyledTransformableElement::isKnownAttribute(attrName))
151 renderer()->setNeedsLayout(true);
152 }
153
createRenderer(RenderArena * arena,RenderStyle *)154 RenderObject* SVGForeignObjectElement::createRenderer(RenderArena* arena, RenderStyle*)
155 {
156 return new (arena) RenderForeignObject(this);
157 }
158
childShouldCreateRenderer(Node * child) const159 bool SVGForeignObjectElement::childShouldCreateRenderer(Node* child) const
160 {
161 // Skip over SVG rules which disallow non-SVG kids
162 return StyledElement::childShouldCreateRenderer(child);
163 }
164
165 } // namespace WebCore
166
167 #endif // ENABLE(SVG) && ENABLE(SVG_FOREIGN_OBJECT)
168