OSDN Git Service

Merge Webkit at r70949: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / svg / SVGElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2008 Rob Buis <buis@kde.org>
4  * Copyright (C) 2008 Apple Inc. All rights reserved.
5  * Copyright (C) 2008 Alp Toker <alp@atoker.com>
6  * Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25
26 #if ENABLE(SVG)
27 #include "SVGElement.h"
28
29 #include "Attribute.h"
30 #include "CSSCursorImageValue.h"
31 #include "DOMImplementation.h"
32 #include "Document.h"
33 #include "Event.h"
34 #include "EventListener.h"
35 #include "EventNames.h"
36 #include "FrameView.h"
37 #include "HTMLNames.h"
38 #include "RegisteredEventListener.h"
39 #include "RenderObject.h"
40 #include "SVGCursorElement.h"
41 #include "SVGElementInstance.h"
42 #include "SVGElementRareData.h"
43 #include "SVGNames.h"
44 #include "SVGSVGElement.h"
45 #include "SVGStyledLocatableElement.h"
46 #include "SVGTextElement.h"
47 #include "SVGURIReference.h"
48 #include "SVGUseElement.h"
49 #include "ScriptEventListener.h"
50 #include "XMLNames.h"
51
52 namespace WebCore {
53
54 using namespace HTMLNames;
55
56 SVGElement::SVGElement(const QualifiedName& tagName, Document* document)
57     : StyledElement(tagName, document, CreateSVGElement)
58 {
59 }
60
61 PassRefPtr<SVGElement> SVGElement::create(const QualifiedName& tagName, Document* document)
62 {
63     return adoptRef(new SVGElement(tagName, document));
64 }
65
66 SVGElement::~SVGElement()
67 {
68     if (!hasRareSVGData())
69         ASSERT(!SVGElementRareData::rareDataMap().contains(this));
70     else {
71         SVGElementRareData::SVGElementRareDataMap& rareDataMap = SVGElementRareData::rareDataMap();
72         SVGElementRareData::SVGElementRareDataMap::iterator it = rareDataMap.find(this);
73         ASSERT(it != rareDataMap.end());
74
75         SVGElementRareData* rareData = it->second;
76         if (SVGCursorElement* cursorElement = rareData->cursorElement())
77             cursorElement->removeClient(this);
78         if (CSSCursorImageValue* cursorImageValue = rareData->cursorImageValue())
79             cursorImageValue->removeReferencedElement(this);
80
81         delete rareData;
82         rareDataMap.remove(it);
83     }
84 }
85
86 SVGElementRareData* SVGElement::rareSVGData() const
87 {
88     ASSERT(hasRareSVGData());
89     return SVGElementRareData::rareDataFromMap(this);
90 }
91
92 SVGElementRareData* SVGElement::ensureRareSVGData()
93 {
94     if (hasRareSVGData())
95         return rareSVGData();
96
97     ASSERT(!SVGElementRareData::rareDataMap().contains(this));
98     SVGElementRareData* data = new SVGElementRareData;
99     SVGElementRareData::rareDataMap().set(this, data);
100     setHasRareSVGData();
101     return data;
102 }
103
104 bool SVGElement::isSupported(StringImpl* feature, StringImpl* version) const
105 {
106     return DOMImplementation::hasFeature(feature, version);
107 }
108
109 String SVGElement::xmlbase() const
110 {
111     return getAttribute(XMLNames::baseAttr);
112 }
113
114 void SVGElement::setXmlbase(const String& value, ExceptionCode&)
115 {
116     setAttribute(XMLNames::baseAttr, value);
117 }
118
119 SVGSVGElement* SVGElement::ownerSVGElement() const
120 {
121     ContainerNode* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
122     while (n) {
123         if (n->hasTagName(SVGNames::svgTag))
124             return static_cast<SVGSVGElement*>(n);
125
126         n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
127     }
128
129     return 0;
130 }
131
132 SVGElement* SVGElement::viewportElement() const
133 {
134     // This function needs shadow tree support - as RenderSVGContainer uses this function
135     // to determine the "overflow" property. <use> on <symbol> wouldn't work otherwhise.
136     ContainerNode* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
137     while (n) {
138         if (n->hasTagName(SVGNames::svgTag) || n->hasTagName(SVGNames::imageTag) || n->hasTagName(SVGNames::symbolTag))
139             return static_cast<SVGElement*>(n);
140
141         n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
142     }
143
144     return 0;
145 }
146
147 SVGDocumentExtensions* SVGElement::accessDocumentSVGExtensions() const
148 {
149     // This function is provided for use by SVGAnimatedProperty to avoid
150     // global inclusion of Document.h in SVG code.
151     return document() ? document()->accessSVGExtensions() : 0;
152 }
153  
154 void SVGElement::mapInstanceToElement(SVGElementInstance* instance)
155 {
156     ASSERT(instance);
157
158     HashSet<SVGElementInstance*>& instances = ensureRareSVGData()->elementInstances();
159     ASSERT(!instances.contains(instance));
160
161     instances.add(instance);
162 }
163  
164 void SVGElement::removeInstanceMapping(SVGElementInstance* instance)
165 {
166     ASSERT(instance);
167     ASSERT(hasRareSVGData());
168
169     HashSet<SVGElementInstance*>& instances = rareSVGData()->elementInstances();
170     ASSERT(instances.contains(instance));
171
172     instances.remove(instance);
173 }
174
175 const HashSet<SVGElementInstance*>& SVGElement::instancesForElement() const
176 {
177     if (!hasRareSVGData()) {
178         DEFINE_STATIC_LOCAL(HashSet<SVGElementInstance*>, emptyInstances, ());
179         return emptyInstances;
180     }
181     return rareSVGData()->elementInstances();
182 }
183
184 bool SVGElement::boundingBox(FloatRect& rect, SVGLocatable::StyleUpdateStrategy styleUpdateStrategy) const
185 {
186     if (isStyledLocatable()) {
187         rect = static_cast<const SVGStyledLocatableElement*>(this)->getBBox(styleUpdateStrategy);
188         return true;
189     }
190     if (hasTagName(SVGNames::textTag)) {
191         rect = static_cast<const SVGTextElement*>(this)->getBBox(styleUpdateStrategy);
192         return true;
193     }
194     return false;
195 }
196
197 void SVGElement::setCursorElement(SVGCursorElement* cursorElement)
198 {
199     ensureRareSVGData()->setCursorElement(cursorElement);
200 }
201
202 void SVGElement::setCursorImageValue(CSSCursorImageValue* cursorImageValue)
203 {
204     ensureRareSVGData()->setCursorImageValue(cursorImageValue);
205 }
206
207 void SVGElement::parseMappedAttribute(Attribute* attr)
208 {
209     // standard events
210     if (attr->name() == onloadAttr)
211         setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
212     else if (attr->name() == onclickAttr)
213         setAttributeEventListener(eventNames().clickEvent, createAttributeEventListener(this, attr));
214     else if (attr->name() == onmousedownAttr)
215         setAttributeEventListener(eventNames().mousedownEvent, createAttributeEventListener(this, attr));
216     else if (attr->name() == onmousemoveAttr)
217         setAttributeEventListener(eventNames().mousemoveEvent, createAttributeEventListener(this, attr));
218     else if (attr->name() == onmouseoutAttr)
219         setAttributeEventListener(eventNames().mouseoutEvent, createAttributeEventListener(this, attr));
220     else if (attr->name() == onmouseoverAttr)
221         setAttributeEventListener(eventNames().mouseoverEvent, createAttributeEventListener(this, attr));
222     else if (attr->name() == onmouseupAttr)
223         setAttributeEventListener(eventNames().mouseupEvent, createAttributeEventListener(this, attr));
224     else if (attr->name() == SVGNames::onfocusinAttr)
225         setAttributeEventListener(eventNames().focusinEvent, createAttributeEventListener(this, attr));
226     else if (attr->name() == SVGNames::onfocusoutAttr)
227         setAttributeEventListener(eventNames().focusoutEvent, createAttributeEventListener(this, attr));
228     else if (attr->name() == SVGNames::onactivateAttr)
229         setAttributeEventListener(eventNames().DOMActivateEvent, createAttributeEventListener(this, attr));
230     else
231         StyledElement::parseMappedAttribute(attr);
232 }
233
234 bool SVGElement::haveLoadedRequiredResources()
235 {
236     Node* child = firstChild();
237     while (child) {
238         if (child->isSVGElement() && !static_cast<SVGElement*>(child)->haveLoadedRequiredResources())
239             return false;
240         child = child->nextSibling();
241     }
242     return true;
243 }
244
245 static bool hasLoadListener(Node* node)
246 {
247     if (node->hasEventListeners(eventNames().loadEvent))
248         return true;
249
250     for (node = node->parentNode(); node && node->isElementNode(); node = node->parentNode()) {
251         const EventListenerVector& entry = node->getEventListeners(eventNames().loadEvent);
252         for (size_t i = 0; i < entry.size(); ++i) {
253             if (entry[i].useCapture)
254                 return true;
255         }
256     }
257
258     return false;
259 }
260
261 void SVGElement::sendSVGLoadEventIfPossible(bool sendParentLoadEvents)
262 {
263     RefPtr<SVGElement> currentTarget = this;
264     while (currentTarget && currentTarget->haveLoadedRequiredResources()) {
265         RefPtr<Node> parent;
266         if (sendParentLoadEvents)
267             parent = currentTarget->parentNode(); // save the next parent to dispatch too incase dispatching the event changes the tree
268         if (hasLoadListener(currentTarget.get())) {
269             RefPtr<Event> event = Event::create(eventNames().loadEvent, false, false);
270             event->setTarget(currentTarget);
271             currentTarget->dispatchGenericEvent(event.release());
272         }
273         currentTarget = (parent && parent->isSVGElement()) ? static_pointer_cast<SVGElement>(parent) : 0;
274     }
275 }
276
277 void SVGElement::finishParsingChildren()
278 {
279     StyledElement::finishParsingChildren();
280
281     // finishParsingChildren() is called when the close tag is reached for an element (e.g. </svg>)
282     // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
283     sendSVGLoadEventIfPossible();
284 }
285
286 bool SVGElement::childShouldCreateRenderer(Node* child) const
287 {
288     if (child->isSVGElement())
289         return static_cast<SVGElement*>(child)->isValid();
290     return false;
291 }
292
293 void SVGElement::insertedIntoDocument()
294 {
295     StyledElement::insertedIntoDocument();
296
297     if (!needsPendingResourceHandling())
298         return;
299
300     SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
301     String resourceId = getIdAttribute();
302     if (!extensions->isPendingResource(resourceId))
303         return;
304     
305     OwnPtr<SVGDocumentExtensions::SVGPendingElements> clients(extensions->removePendingResource(resourceId));
306     if (clients->isEmpty())
307         return;
308
309     const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
310     for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it)
311         (*it)->buildPendingResource();
312 }
313
314 void SVGElement::attributeChanged(Attribute* attr, bool preserveDecls)
315 {
316     ASSERT(attr);
317     if (!attr)
318         return;
319
320     StyledElement::attributeChanged(attr, preserveDecls);
321
322     // When an animated SVG property changes through SVG DOM, svgAttributeChanged() is called, not attributeChanged().
323     // Next time someone tries to access the XML attributes, the synchronization code starts. During that synchronization
324     // SVGAnimatedPropertySynchronizer may call NamedNodeMap::removeAttribute(), which in turn calls attributeChanged().
325     // At this point we're not allowed to call svgAttributeChanged() again - it may lead to extra work being done, or crashes
326     // see bug https://bugs.webkit.org/show_bug.cgi?id=40994.
327     if (isSynchronizingSVGAttributes())
328         return;
329
330     // Changes to the style attribute are processed lazily (see Element::getAttribute() and related methods),
331     // so we don't want changes to the style attribute to result in extra work here.
332     if (attr->name() != HTMLNames::styleAttr)
333         svgAttributeChanged(attr->name());
334 }
335
336 void SVGElement::updateAnimatedSVGAttribute(const QualifiedName& name) const
337 {
338     if (isSynchronizingSVGAttributes() || areSVGAttributesValid())
339         return;
340
341     setIsSynchronizingSVGAttributes();
342
343     const_cast<SVGElement*>(this)->synchronizeProperty(name);
344     if (name == anyQName())
345         setAreSVGAttributesValid();
346
347     clearIsSynchronizingSVGAttributes();
348 }
349
350 ContainerNode* SVGElement::eventParentNode()
351 {
352     if (ContainerNode* shadowParent = shadowParentNode())
353         return shadowParent;
354     return StyledElement::eventParentNode();
355 }
356
357 }
358
359 #endif // ENABLE(SVG)