• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3                   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 
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)
26 #include "SVGElement.h"
27 
28 #include "CSSCursorImageValue.h"
29 #include "DOMImplementation.h"
30 #include "Document.h"
31 #include "Event.h"
32 #include "EventListener.h"
33 #include "EventNames.h"
34 #include "FrameView.h"
35 #include "HTMLNames.h"
36 #include "MappedAttribute.h"
37 #include "RegisteredEventListener.h"
38 #include "RenderObject.h"
39 #include "SVGCursorElement.h"
40 #include "SVGDocumentExtensions.h"
41 #include "SVGElementInstance.h"
42 #include "SVGNames.h"
43 #include "SVGResource.h"
44 #include "SVGSVGElement.h"
45 #include "SVGURIReference.h"
46 #include "SVGUseElement.h"
47 #include "ScriptEventListener.h"
48 #include "XMLNames.h"
49 
50 namespace WebCore {
51 
52 using namespace HTMLNames;
53 
SVGElement(const QualifiedName & tagName,Document * doc)54 SVGElement::SVGElement(const QualifiedName& tagName, Document* doc)
55     : StyledElement(tagName, doc)
56     , m_shadowParent(0)
57     , m_cursorElement(0)
58     , m_cursorImageValue(0)
59 {
60 }
61 
~SVGElement()62 SVGElement::~SVGElement()
63 {
64     if (m_cursorElement)
65         m_cursorElement->removeClient(this);
66     if (m_cursorImageValue)
67         m_cursorImageValue->removeReferencedElement(this);
68 }
69 
isSupported(StringImpl * feature,StringImpl * version) const70 bool SVGElement::isSupported(StringImpl* feature, StringImpl* version) const
71 {
72     return DOMImplementation::hasFeature(feature, version);
73 }
74 
id() const75 String SVGElement::id() const
76 {
77     return getAttribute(idAttr);
78 }
79 
setId(const String & value,ExceptionCode &)80 void SVGElement::setId(const String& value, ExceptionCode&)
81 {
82     setAttribute(idAttr, value);
83 }
84 
xmlbase() const85 String SVGElement::xmlbase() const
86 {
87     return getAttribute(XMLNames::baseAttr);
88 }
89 
setXmlbase(const String & value,ExceptionCode &)90 void SVGElement::setXmlbase(const String& value, ExceptionCode&)
91 {
92     setAttribute(XMLNames::baseAttr, value);
93 }
94 
ownerSVGElement() const95 SVGSVGElement* SVGElement::ownerSVGElement() const
96 {
97     Node* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
98     while (n) {
99         if (n->hasTagName(SVGNames::svgTag))
100             return static_cast<SVGSVGElement*>(n);
101 
102         n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
103     }
104 
105     return 0;
106 }
107 
viewportElement() const108 SVGElement* SVGElement::viewportElement() const
109 {
110     // This function needs shadow tree support - as RenderSVGContainer uses this function
111     // to determine the "overflow" property. <use> on <symbol> wouldn't work otherwhise.
112     Node* n = isShadowNode() ? const_cast<SVGElement*>(this)->shadowParentNode() : parentNode();
113     while (n) {
114         if (n->hasTagName(SVGNames::svgTag) || n->hasTagName(SVGNames::imageTag) || n->hasTagName(SVGNames::symbolTag))
115             return static_cast<SVGElement*>(n);
116 
117         n = n->isShadowNode() ? n->shadowParentNode() : n->parentNode();
118     }
119 
120     return 0;
121 }
122 
accessDocumentSVGExtensions() const123 SVGDocumentExtensions* SVGElement::accessDocumentSVGExtensions() const
124 {
125 
126     // This function is provided for use by SVGAnimatedProperty to avoid
127     // global inclusion of Document.h in SVG code.
128     return document() ? document()->accessSVGExtensions() : 0;
129 }
130 
mapInstanceToElement(SVGElementInstance * instance)131 void SVGElement::mapInstanceToElement(SVGElementInstance* instance)
132 {
133     ASSERT(instance);
134     ASSERT(!m_elementInstances.contains(instance));
135     m_elementInstances.add(instance);
136 }
137 
removeInstanceMapping(SVGElementInstance * instance)138 void SVGElement::removeInstanceMapping(SVGElementInstance* instance)
139 {
140     ASSERT(instance);
141     ASSERT(m_elementInstances.contains(instance));
142     m_elementInstances.remove(instance);
143 }
144 
instancesForElement() const145 HashSet<SVGElementInstance*> SVGElement::instancesForElement() const
146 {
147     return m_elementInstances;
148 }
149 
parseMappedAttribute(MappedAttribute * attr)150 void SVGElement::parseMappedAttribute(MappedAttribute* attr)
151 {
152     // standard events
153     if (attr->name() == onloadAttr)
154         setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
155     else if (attr->name() == onclickAttr)
156         setAttributeEventListener(eventNames().clickEvent, createAttributeEventListener(this, attr));
157     else if (attr->name() == onmousedownAttr)
158         setAttributeEventListener(eventNames().mousedownEvent, createAttributeEventListener(this, attr));
159     else if (attr->name() == onmousemoveAttr)
160         setAttributeEventListener(eventNames().mousemoveEvent, createAttributeEventListener(this, attr));
161     else if (attr->name() == onmouseoutAttr)
162         setAttributeEventListener(eventNames().mouseoutEvent, createAttributeEventListener(this, attr));
163     else if (attr->name() == onmouseoverAttr)
164         setAttributeEventListener(eventNames().mouseoverEvent, createAttributeEventListener(this, attr));
165     else if (attr->name() == onmouseupAttr)
166         setAttributeEventListener(eventNames().mouseupEvent, createAttributeEventListener(this, attr));
167     else if (attr->name() == SVGNames::onfocusinAttr)
168         setAttributeEventListener(eventNames().DOMFocusInEvent, createAttributeEventListener(this, attr));
169     else if (attr->name() == SVGNames::onfocusoutAttr)
170         setAttributeEventListener(eventNames().DOMFocusOutEvent, createAttributeEventListener(this, attr));
171     else if (attr->name() == SVGNames::onactivateAttr)
172         setAttributeEventListener(eventNames().DOMActivateEvent, createAttributeEventListener(this, attr));
173     else
174         StyledElement::parseMappedAttribute(attr);
175 }
176 
haveLoadedRequiredResources()177 bool SVGElement::haveLoadedRequiredResources()
178 {
179     Node* child = firstChild();
180     while (child) {
181         if (child->isSVGElement() && !static_cast<SVGElement*>(child)->haveLoadedRequiredResources())
182             return false;
183         child = child->nextSibling();
184     }
185     return true;
186 }
187 
hasLoadListener(SVGElement * node)188 static bool hasLoadListener(SVGElement* node)
189 {
190     Node* currentNode = node;
191     while (currentNode && currentNode->isElementNode()) {
192         const RegisteredEventListenerVector& listeners = static_cast<Element*>(currentNode)->eventListeners();
193         size_t size = listeners.size();
194         for (size_t i = 0; i < size; ++i) {
195             const RegisteredEventListener& r = *listeners[i];
196             if (r.eventType() == eventNames().loadEvent && r.useCapture() || currentNode == node)
197                 return true;
198         }
199         currentNode = currentNode->parentNode();
200     }
201 
202     return false;
203 }
204 
sendSVGLoadEventIfPossible(bool sendParentLoadEvents)205 void SVGElement::sendSVGLoadEventIfPossible(bool sendParentLoadEvents)
206 {
207     RefPtr<SVGElement> currentTarget = this;
208     while (currentTarget && currentTarget->haveLoadedRequiredResources()) {
209         RefPtr<Node> parent;
210         if (sendParentLoadEvents)
211             parent = currentTarget->parentNode(); // save the next parent to dispatch too incase dispatching the event changes the tree
212         if (hasLoadListener(currentTarget.get())) {
213             RefPtr<Event> event = Event::create(eventNames().loadEvent, false, false);
214             event->setTarget(currentTarget);
215             currentTarget->dispatchGenericEvent(event.release());
216         }
217         currentTarget = (parent && parent->isSVGElement()) ? static_pointer_cast<SVGElement>(parent) : 0;
218     }
219 }
220 
finishParsingChildren()221 void SVGElement::finishParsingChildren()
222 {
223     StyledElement::finishParsingChildren();
224 
225     // finishParsingChildren() is called when the close tag is reached for an element (e.g. </svg>)
226     // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
227     sendSVGLoadEventIfPossible();
228 }
229 
childShouldCreateRenderer(Node * child) const230 bool SVGElement::childShouldCreateRenderer(Node* child) const
231 {
232     if (child->isSVGElement())
233         return static_cast<SVGElement*>(child)->isValid();
234     return false;
235 }
236 
insertedIntoDocument()237 void SVGElement::insertedIntoDocument()
238 {
239     StyledElement::insertedIntoDocument();
240     SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
241 
242     String resourceId = SVGURIReference::getTarget(id());
243     if (extensions->isPendingResource(resourceId)) {
244         std::auto_ptr<HashSet<SVGStyledElement*> > clients(extensions->removePendingResource(resourceId));
245         if (clients->isEmpty())
246             return;
247 
248         HashSet<SVGStyledElement*>::const_iterator it = clients->begin();
249         const HashSet<SVGStyledElement*>::const_iterator end = clients->end();
250 
251         for (; it != end; ++it)
252             (*it)->buildPendingResource();
253 
254         SVGResource::invalidateClients(*clients);
255     }
256 }
257 
attributeChanged(Attribute * attr,bool preserveDecls)258 void SVGElement::attributeChanged(Attribute* attr, bool preserveDecls)
259 {
260     ASSERT(attr);
261     if (!attr)
262         return;
263 
264     StyledElement::attributeChanged(attr, preserveDecls);
265     svgAttributeChanged(attr->name());
266 }
267 
updateAnimatedSVGAttribute(const String & name) const268 void SVGElement::updateAnimatedSVGAttribute(const String& name) const
269 {
270     ASSERT(!m_areSVGAttributesValid);
271 
272     if (m_synchronizingSVGAttributes)
273         return;
274 
275     m_synchronizingSVGAttributes = true;
276 
277     if (name.isEmpty()) {
278         invokeAllSVGPropertySynchronizers();
279         setSynchronizedSVGAttributes(true);
280     } else
281         invokeSVGPropertySynchronizer(name);
282 
283     m_synchronizingSVGAttributes = false;
284 }
285 
setSynchronizedSVGAttributes(bool value) const286 void SVGElement::setSynchronizedSVGAttributes(bool value) const
287 {
288     m_areSVGAttributesValid = value;
289 }
290 
eventParentNode()291 ContainerNode* SVGElement::eventParentNode()
292 {
293     return m_shadowParent ? m_shadowParent : StyledElement::eventParentNode();
294 }
295 
296 }
297 
298 #endif // ENABLE(SVG)
299