2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2007 Rob Buis <buis@kde.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "SVGDocumentExtensions.h"
28 #include "DOMWindow.h"
30 #include "EventListener.h"
32 #include "FrameLoader.h"
34 #include "SMILTimeContainer.h"
35 #include "SVGElement.h"
36 #include "SVGSMILElement.h"
37 #include "SVGSVGElement.h"
38 #include "ScriptableDocumentParser.h"
39 #include <wtf/text/AtomicString.h>
43 SVGDocumentExtensions::SVGDocumentExtensions(Document* document)
44 : m_document(document)
45 , m_resourcesCache(adoptPtr(new SVGResourcesCache))
49 SVGDocumentExtensions::~SVGDocumentExtensions()
51 deleteAllValues(m_animatedElements);
52 deleteAllValues(m_pendingResources);
55 void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
57 m_timeContainers.add(element);
60 void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
62 m_timeContainers.remove(element);
65 void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
72 // Replaces resource if already present, to handle potential id changes
73 m_resources.set(id, resource);
76 void SVGDocumentExtensions::removeResource(const AtomicString& id)
78 if (id.isEmpty() || !m_resources.contains(id))
81 m_resources.remove(id);
84 RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
89 return m_resources.get(id);
92 void SVGDocumentExtensions::startAnimations()
94 // FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
95 // starting animations for a document will do this "latching"
96 #if ENABLE(SVG_ANIMATION)
97 // FIXME: We hold a ref pointers to prevent a shadow tree from getting removed out from underneath us.
98 // In the future we should refactor the use-element to avoid this. See https://webkit.org/b/53704
99 Vector<RefPtr<SVGSVGElement> > timeContainers;
100 timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
101 Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
102 for (Vector<RefPtr<SVGSVGElement> >::iterator itr = timeContainers.begin(); itr != end; ++itr)
103 (*itr)->timeContainer()->begin();
107 void SVGDocumentExtensions::pauseAnimations()
109 HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
110 for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
111 (*itr)->pauseAnimations();
114 void SVGDocumentExtensions::unpauseAnimations()
116 HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
117 for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
118 (*itr)->unpauseAnimations();
121 bool SVGDocumentExtensions::sampleAnimationAtTime(const String& elementId, SVGSMILElement* element, double time)
123 #if !ENABLE(SVG_ANIMATION)
124 UNUSED_PARAM(elementId);
125 UNUSED_PARAM(element);
130 SMILTimeContainer* container = element->timeContainer();
131 if (!container || container->isPaused())
134 container->sampleAnimationAtTime(elementId, time);
139 void SVGDocumentExtensions::addAnimationElementToTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
141 ASSERT(targetElement);
142 ASSERT(animationElement);
144 if (HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.get(targetElement)) {
145 animationElementsForTarget->add(animationElement);
149 HashSet<SVGSMILElement*>* animationElementsForTarget = new HashSet<SVGSMILElement*>;
150 animationElementsForTarget->add(animationElement);
151 m_animatedElements.set(targetElement, animationElementsForTarget);
154 void SVGDocumentExtensions::removeAnimationElementFromTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
156 ASSERT(targetElement);
157 ASSERT(animationElement);
159 HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
160 ASSERT(it != m_animatedElements.end());
162 HashSet<SVGSMILElement*>* animationElementsForTarget = it->second;
163 ASSERT(!animationElementsForTarget->isEmpty());
165 animationElementsForTarget->remove(animationElement);
166 if (animationElementsForTarget->isEmpty()) {
167 m_animatedElements.remove(it);
168 delete animationElementsForTarget;
172 void SVGDocumentExtensions::removeAllAnimationElementsFromTarget(SVGElement* targetElement)
174 ASSERT(targetElement);
175 HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.take(targetElement);
176 if (!animationElementsForTarget)
178 #if ENABLE(SVG_ANIMATION)
179 HashSet<SVGSMILElement*>::iterator it = animationElementsForTarget->begin();
180 HashSet<SVGSMILElement*>::iterator end = animationElementsForTarget->end();
181 for (; it != end; ++it)
182 (*it)->resetTargetElement();
183 delete animationElementsForTarget;
185 ASSERT_NOT_REACHED();
189 // FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
190 static int parserLineNumber(Document* document)
192 ScriptableDocumentParser* parser = document->scriptableDocumentParser();
195 return parser->lineNumber();
198 static void reportMessage(Document* document, MessageLevel level, const String& message)
200 if (Frame* frame = document->frame())
201 frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, level, message, parserLineNumber(document), document->documentURI());
204 void SVGDocumentExtensions::reportWarning(const String& message)
206 reportMessage(m_document, WarningMessageLevel, "Warning: " + message);
209 void SVGDocumentExtensions::reportError(const String& message)
211 reportMessage(m_document, ErrorMessageLevel, "Error: " + message);
214 void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGStyledElement* element)
221 if (m_pendingResources.contains(id))
222 m_pendingResources.get(id)->add(element);
224 SVGPendingElements* set = new SVGPendingElements;
227 m_pendingResources.add(id, set);
230 element->setHasPendingResources();
233 bool SVGDocumentExtensions::hasPendingResources(const AtomicString& id) const
238 return m_pendingResources.contains(id);
241 bool SVGDocumentExtensions::isElementInPendingResources(SVGStyledElement* element) const
245 if (m_pendingResources.isEmpty())
248 HashMap<AtomicString, SVGPendingElements*>::const_iterator end = m_pendingResources.end();
249 for (HashMap<AtomicString, SVGPendingElements*>::const_iterator it = m_pendingResources.begin(); it != end; ++it) {
250 SVGPendingElements* elements = it->second;
253 if (elements->contains(element))
259 void SVGDocumentExtensions::removeElementFromPendingResources(SVGStyledElement* element)
263 if (m_pendingResources.isEmpty() || !element->hasPendingResources())
266 Vector<AtomicString> toBeRemoved;
267 HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResources.end();
268 for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResources.begin(); it != end; ++it) {
269 SVGPendingElements* elements = it->second;
271 ASSERT(!elements->isEmpty());
273 elements->remove(element);
274 if (elements->isEmpty())
275 toBeRemoved.append(it->first);
278 element->clearHasPendingResourcesIfPossible();
280 if (toBeRemoved.isEmpty())
283 Vector<AtomicString>::iterator endVector = toBeRemoved.end();
284 for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != endVector; ++it)
285 m_pendingResources.remove(*it);
288 PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResource(const AtomicString& id)
290 ASSERT(m_pendingResources.contains(id));
292 OwnPtr<SVGPendingElements> set = adoptPtr(m_pendingResources.get(id));
293 m_pendingResources.remove(id);
294 return set.release();