8fb5460edd357d6103e1b69d5d59a437d2fb826c
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGMPathElement.cpp
1 /*
2  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "core/svg/SVGMPathElement.h"
23
24 #include "XLinkNames.h"
25 #include "core/dom/Document.h"
26 #include "core/svg/SVGAnimateMotionElement.h"
27 #include "core/svg/SVGDocumentExtensions.h"
28 #include "core/svg/SVGPathElement.h"
29
30 namespace WebCore {
31
32 // Animated property definitions
33 DEFINE_ANIMATED_STRING(SVGMPathElement, XLinkNames::hrefAttr, Href, href)
34
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGMPathElement)
36     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
37 END_REGISTER_ANIMATED_PROPERTIES
38
39 inline SVGMPathElement::SVGMPathElement(Document& document)
40     : SVGElement(SVGNames::mpathTag, document)
41 {
42     ScriptWrappable::init(this);
43     registerAnimatedPropertiesForSVGMPathElement();
44 }
45
46 PassRefPtr<SVGMPathElement> SVGMPathElement::create(Document& document)
47 {
48     return adoptRef(new SVGMPathElement(document));
49 }
50
51 SVGMPathElement::~SVGMPathElement()
52 {
53     clearResourceReferences();
54 }
55
56 void SVGMPathElement::buildPendingResource()
57 {
58     clearResourceReferences();
59     if (!inDocument())
60         return;
61
62     AtomicString id;
63     Element* target = SVGURIReference::targetElementFromIRIString(hrefCurrentValue(), document(), &id);
64     if (!target) {
65         // Do not register as pending if we are already pending this resource.
66         if (document().accessSVGExtensions()->isElementPendingResource(this, id))
67             return;
68
69         if (!id.isEmpty()) {
70             document().accessSVGExtensions()->addPendingResource(id, this);
71             ASSERT(hasPendingResources());
72         }
73     } else if (target->isSVGElement()) {
74         // Register us with the target in the dependencies map. Any change of hrefElement
75         // that leads to relayout/repainting now informs us, so we can react to it.
76         document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
77     }
78
79     targetPathChanged();
80 }
81
82 void SVGMPathElement::clearResourceReferences()
83 {
84     document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
85 }
86
87 Node::InsertionNotificationRequest SVGMPathElement::insertedInto(ContainerNode* rootParent)
88 {
89     SVGElement::insertedInto(rootParent);
90     if (rootParent->inDocument())
91         buildPendingResource();
92     return InsertionDone;
93 }
94
95 void SVGMPathElement::removedFrom(ContainerNode* rootParent)
96 {
97     SVGElement::removedFrom(rootParent);
98     notifyParentOfPathChange(rootParent);
99     if (rootParent->inDocument())
100         clearResourceReferences();
101 }
102
103 bool SVGMPathElement::isSupportedAttribute(const QualifiedName& attrName)
104 {
105     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
106     if (supportedAttributes.isEmpty()) {
107         SVGURIReference::addSupportedAttributes(supportedAttributes);
108     }
109     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
110 }
111
112 void SVGMPathElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
113 {
114     if (!isSupportedAttribute(name)) {
115         SVGElement::parseAttribute(name, value);
116         return;
117     }
118
119     if (SVGURIReference::parseAttribute(name, value))
120         return;
121
122     ASSERT_NOT_REACHED();
123 }
124
125 void SVGMPathElement::svgAttributeChanged(const QualifiedName& attrName)
126 {
127     if (!isSupportedAttribute(attrName)) {
128         SVGElement::svgAttributeChanged(attrName);
129         return;
130     }
131
132     SVGElementInstance::InvalidationGuard invalidationGuard(this);
133
134     if (SVGURIReference::isKnownAttribute(attrName)) {
135         buildPendingResource();
136         return;
137     }
138
139     ASSERT_NOT_REACHED();
140 }
141
142 SVGPathElement* SVGMPathElement::pathElement()
143 {
144     Element* target = targetElementFromIRIString(hrefCurrentValue(), document());
145     if (target && target->hasTagName(SVGNames::pathTag))
146         return toSVGPathElement(target);
147     return 0;
148 }
149
150 void SVGMPathElement::targetPathChanged()
151 {
152     notifyParentOfPathChange(parentNode());
153 }
154
155 void SVGMPathElement::notifyParentOfPathChange(ContainerNode* parent)
156 {
157     if (parent && parent->hasTagName(SVGNames::animateMotionTag))
158         toSVGAnimateMotionElement(parent)->updateAnimationPath();
159 }
160
161 } // namespace WebCore