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 "SVGResourcesCache.h"
37
#include "SVGSMILElement.h"
38
#include "SVGSVGElement.h"
39
#include "ScriptableDocumentParser.h"
40
#include "XLinkNames.h"
41
#include <wtf/text/AtomicString.h>
45
SVGDocumentExtensions::SVGDocumentExtensions(Document* document)
46
: m_document(document)
47
, m_resourcesCache(adoptPtr(new SVGResourcesCache))
51
SVGDocumentExtensions::~SVGDocumentExtensions()
53
deleteAllValues(m_animatedElements);
54
deleteAllValues(m_pendingResources);
55
deleteAllValues(m_pendingResourcesForRemoval);
58
void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
60
m_timeContainers.add(element);
63
void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
65
m_timeContainers.remove(element);
68
void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
75
// Replaces resource if already present, to handle potential id changes
76
m_resources.set(id, resource);
79
void SVGDocumentExtensions::removeResource(const AtomicString& id)
81
if (id.isEmpty() || !m_resources.contains(id))
84
m_resources.remove(id);
87
RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
92
return m_resources.get(id);
95
void SVGDocumentExtensions::startAnimations()
97
// FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
98
// starting animations for a document will do this "latching"
99
// FIXME: We hold a ref pointers to prevent a shadow tree from getting removed out from underneath us.
100
// In the future we should refactor the use-element to avoid this. See https://webkit.org/b/53704
101
Vector<RefPtr<SVGSVGElement> > timeContainers;
102
timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
103
Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
104
for (Vector<RefPtr<SVGSVGElement> >::iterator itr = timeContainers.begin(); itr != end; ++itr)
105
(*itr)->timeContainer()->begin();
108
void SVGDocumentExtensions::pauseAnimations()
110
HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
111
for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
112
(*itr)->pauseAnimations();
115
void SVGDocumentExtensions::unpauseAnimations()
117
HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
118
for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
119
(*itr)->unpauseAnimations();
122
void SVGDocumentExtensions::dispatchSVGLoadEventToOutermostSVGElements()
124
Vector<RefPtr<SVGSVGElement> > timeContainers;
125
timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
127
Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
128
for (Vector<RefPtr<SVGSVGElement> >::iterator it = timeContainers.begin(); it != end; ++it) {
129
SVGSVGElement* outerSVG = (*it).get();
130
if (!outerSVG->isOutermostSVGSVGElement())
132
outerSVG->sendSVGLoadEventIfPossible();
136
void SVGDocumentExtensions::addAnimationElementToTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
138
ASSERT(targetElement);
139
ASSERT(animationElement);
141
if (HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.get(targetElement)) {
142
animationElementsForTarget->add(animationElement);
146
HashSet<SVGSMILElement*>* animationElementsForTarget = new HashSet<SVGSMILElement*>;
147
animationElementsForTarget->add(animationElement);
148
m_animatedElements.set(targetElement, animationElementsForTarget);
151
void SVGDocumentExtensions::removeAnimationElementFromTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
153
ASSERT(targetElement);
154
ASSERT(animationElement);
156
HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
157
ASSERT(it != m_animatedElements.end());
159
HashSet<SVGSMILElement*>* animationElementsForTarget = it->value;
160
ASSERT(!animationElementsForTarget->isEmpty());
162
animationElementsForTarget->remove(animationElement);
163
if (animationElementsForTarget->isEmpty()) {
164
m_animatedElements.remove(it);
165
delete animationElementsForTarget;
169
void SVGDocumentExtensions::removeAllAnimationElementsFromTarget(SVGElement* targetElement)
171
ASSERT(targetElement);
172
HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
173
if (it == m_animatedElements.end())
176
HashSet<SVGSMILElement*>* animationElementsForTarget = it->value;
177
Vector<SVGSMILElement*> toBeReset;
179
HashSet<SVGSMILElement*>::iterator end = animationElementsForTarget->end();
180
for (HashSet<SVGSMILElement*>::iterator it = animationElementsForTarget->begin(); it != end; ++it)
181
toBeReset.append(*it);
183
Vector<SVGSMILElement*>::iterator vectorEnd = toBeReset.end();
184
for (Vector<SVGSMILElement*>::iterator vectorIt = toBeReset.begin(); vectorIt != vectorEnd; ++vectorIt)
185
(*vectorIt)->resetTargetElement();
188
// FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
189
static int parserLineNumber(Document* document)
191
ScriptableDocumentParser* parser = document->scriptableDocumentParser();
194
return parser->lineNumber().oneBasedInt();
197
static void reportMessage(Document* document, MessageLevel level, const String& message)
199
if (document->frame())
200
document->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, level, message, document->documentURI(), parserLineNumber(document));
203
void SVGDocumentExtensions::reportWarning(const String& message)
205
reportMessage(m_document, WarningMessageLevel, "Warning: " + message);
208
void SVGDocumentExtensions::reportError(const String& message)
210
reportMessage(m_document, ErrorMessageLevel, "Error: " + message);
213
void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGElement* element)
220
// The HashMap add function leaves the map alone and returns a pointer to the element in the
221
// map if the element already exists. So we add with a value of 0, and it either finds the
222
// existing element or adds a new one in a single operation. The ".iterator->value" idiom gets
223
// us to the iterator from add's result, and then to the value inside the hash table.
224
SVGPendingElements*& set = m_pendingResources.add(id, 0).iterator->value;
226
set = new SVGPendingElements;
229
element->setHasPendingResources();
232
bool SVGDocumentExtensions::hasPendingResource(const AtomicString& id) const
237
return m_pendingResources.contains(id);
240
bool SVGDocumentExtensions::isElementPendingResources(SVGElement* element) const
242
// This algorithm takes time proportional to the number of pending resources and need not.
243
// If performance becomes an issue we can keep a counted set of elements and answer the question efficiently.
247
HashMap<AtomicString, SVGPendingElements*>::const_iterator end = m_pendingResources.end();
248
for (HashMap<AtomicString, SVGPendingElements*>::const_iterator it = m_pendingResources.begin(); it != end; ++it) {
249
SVGPendingElements* elements = it->value;
252
if (elements->contains(element))
258
bool SVGDocumentExtensions::isElementPendingResource(SVGElement* element, const AtomicString& id) const
262
if (!hasPendingResource(id))
265
return m_pendingResources.get(id)->contains(element);
268
void SVGDocumentExtensions::removeElementFromPendingResources(SVGElement* element)
272
// Remove the element from pending resources.
273
if (!m_pendingResources.isEmpty() && element->hasPendingResources()) {
274
Vector<AtomicString> toBeRemoved;
275
HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResources.end();
276
for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResources.begin(); it != end; ++it) {
277
SVGPendingElements* elements = it->value;
279
ASSERT(!elements->isEmpty());
281
elements->remove(element);
282
if (elements->isEmpty())
283
toBeRemoved.append(it->key);
286
element->clearHasPendingResourcesIfPossible();
288
// We use the removePendingResource function here because it deals with set lifetime correctly.
289
Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
290
for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
291
removePendingResource(*it);
294
// Remove the element from pending resources that were scheduled for removal.
295
if (!m_pendingResourcesForRemoval.isEmpty()) {
296
Vector<AtomicString> toBeRemoved;
297
HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResourcesForRemoval.end();
298
for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResourcesForRemoval.begin(); it != end; ++it) {
299
SVGPendingElements* elements = it->value;
301
ASSERT(!elements->isEmpty());
303
elements->remove(element);
304
if (elements->isEmpty())
305
toBeRemoved.append(it->key);
308
// We use the removePendingResourceForRemoval function here because it deals with set lifetime correctly.
309
Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
310
for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
311
removePendingResourceForRemoval(*it);
315
PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResource(const AtomicString& id)
317
ASSERT(m_pendingResources.contains(id));
318
return adoptPtr(m_pendingResources.take(id));
321
PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResourceForRemoval(const AtomicString& id)
323
ASSERT(m_pendingResourcesForRemoval.contains(id));
324
return adoptPtr(m_pendingResourcesForRemoval.take(id));
327
void SVGDocumentExtensions::markPendingResourcesForRemoval(const AtomicString& id)
332
ASSERT(!m_pendingResourcesForRemoval.contains(id));
334
SVGPendingElements* existing = m_pendingResources.take(id);
335
if (existing && !existing->isEmpty())
336
m_pendingResourcesForRemoval.add(id, existing);
339
SVGElement* SVGDocumentExtensions::removeElementFromPendingResourcesForRemoval(const AtomicString& id)
344
SVGPendingElements* resourceSet = m_pendingResourcesForRemoval.get(id);
345
if (!resourceSet || resourceSet->isEmpty())
348
SVGPendingElements::iterator firstElement = resourceSet->begin();
349
SVGElement* element = *firstElement;
351
resourceSet->remove(firstElement);
353
if (resourceSet->isEmpty())
354
removePendingResourceForRemoval(id);
359
HashSet<SVGElement*>* SVGDocumentExtensions::setOfElementsReferencingTarget(SVGElement* referencedElement) const
361
ASSERT(referencedElement);
362
const HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::const_iterator it = m_elementDependencies.find(referencedElement);
363
if (it == m_elementDependencies.end())
365
return it->value.get();
368
void SVGDocumentExtensions::addElementReferencingTarget(SVGElement* referencingElement, SVGElement* referencedElement)
370
ASSERT(referencingElement);
371
ASSERT(referencedElement);
373
if (HashSet<SVGElement*>* elements = m_elementDependencies.get(referencedElement)) {
374
elements->add(referencingElement);
378
OwnPtr<HashSet<SVGElement*> > elements = adoptPtr(new HashSet<SVGElement*>);
379
elements->add(referencingElement);
380
m_elementDependencies.set(referencedElement, elements.release());
383
void SVGDocumentExtensions::removeAllTargetReferencesForElement(SVGElement* referencingElement)
385
Vector<SVGElement*> toBeRemoved;
387
HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator end = m_elementDependencies.end();
388
for (HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.begin(); it != end; ++it) {
389
SVGElement* referencedElement = it->key;
390
HashSet<SVGElement*>* referencingElements = it->value.get();
391
HashSet<SVGElement*>::iterator setIt = referencingElements->find(referencingElement);
392
if (setIt == referencingElements->end())
395
referencingElements->remove(setIt);
396
if (referencingElements->isEmpty())
397
toBeRemoved.append(referencedElement);
400
Vector<SVGElement*>::iterator vectorEnd = toBeRemoved.end();
401
for (Vector<SVGElement*>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
402
m_elementDependencies.remove(*it);
405
void SVGDocumentExtensions::removeAllElementReferencesForTarget(SVGElement* referencedElement)
407
ASSERT(referencedElement);
408
HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.find(referencedElement);
409
if (it == m_elementDependencies.end())
411
ASSERT(it->key == referencedElement);
412
Vector<SVGElement*> toBeNotified;
414
HashSet<SVGElement*>* referencingElements = it->value.get();
415
HashSet<SVGElement*>::iterator setEnd = referencingElements->end();
416
for (HashSet<SVGElement*>::iterator setIt = referencingElements->begin(); setIt != setEnd; ++setIt)
417
toBeNotified.append(*setIt);
419
// Force rebuilding the referencingElement so it knows about this change.
420
Vector<SVGElement*>::iterator vectorEnd = toBeNotified.end();
421
for (Vector<SVGElement*>::iterator vectorIt = toBeNotified.begin(); vectorIt != vectorEnd; ++vectorIt) {
422
// Before rebuilding referencingElement ensure it was not removed from under us.
423
if (HashSet<SVGElement*>* referencingElements = setOfElementsReferencingTarget(referencedElement)) {
424
if (referencingElements->contains(*vectorIt))
425
(*vectorIt)->svgAttributeChanged(XLinkNames::hrefAttr);
429
m_elementDependencies.remove(referencedElement);
432
#if ENABLE(SVG_FONTS)
433
void SVGDocumentExtensions::registerSVGFontFaceElement(SVGFontFaceElement* element)
435
m_svgFontFaceElements.add(element);
438
void SVGDocumentExtensions::unregisterSVGFontFaceElement(SVGFontFaceElement* element)
440
ASSERT(m_svgFontFaceElements.contains(element));
441
m_svgFontFaceElements.remove(element);