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