-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SVGSMILElement.cpp
1232 lines (1074 loc) · 43 KB
/
SVGSMILElement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2023 Apple Inc. All rights reserved.
* Copyright (C) 2013-2014 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "SVGSMILElement.h"
#include "AddEventListenerOptions.h"
#include "CSSPropertyNames.h"
#include "Document.h"
#include "Event.h"
#include "EventListener.h"
#include "EventNames.h"
#include "EventSender.h"
#include "FloatConversion.h"
#include "FrameView.h"
#include "Page.h"
#include "SMILTimeContainer.h"
#include "SVGDocumentExtensions.h"
#include "SVGElementInlines.h"
#include "SVGElementTypeHelpers.h"
#include "SVGNames.h"
#include "SVGParserUtilities.h"
#include "SVGSVGElement.h"
#include "SVGURIReference.h"
#include "SVGUseElement.h"
#include "XLinkNames.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/MathExtras.h>
#include <wtf/RobinHoodHashSet.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
#include <wtf/text/StringToIntegerConversion.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGSMILElement);
static SMILEventSender& smilEventSender()
{
static NeverDestroyed<SMILEventSender> sender;
return sender;
}
static const AtomString& indefiniteAtom()
{
static MainThreadNeverDestroyed<const AtomString> indefiniteValue("indefinite"_s);
return indefiniteValue;
}
// This is used for duration type time values that can't be negative.
static const double invalidCachedTime = -1.;
class ConditionEventListener final : public EventListener {
public:
static Ref<ConditionEventListener> create(SVGSMILElement* animation, SVGSMILElement::Condition* condition)
{
return adoptRef(*new ConditionEventListener(animation, condition));
}
static const ConditionEventListener* cast(const EventListener* listener)
{
return listener->type() == ConditionEventListenerType
? static_cast<const ConditionEventListener*>(listener)
: nullptr;
}
bool operator==(const EventListener& other) const final;
void disconnectAnimation()
{
m_animation = nullptr;
}
private:
ConditionEventListener(SVGSMILElement* animation, SVGSMILElement::Condition* condition)
: EventListener(ConditionEventListenerType)
, m_animation(animation)
, m_condition(condition)
{
}
void handleEvent(ScriptExecutionContext&, Event&) final;
SVGSMILElement* m_animation;
SVGSMILElement::Condition* m_condition;
};
bool ConditionEventListener::operator==(const EventListener& listener) const
{
if (const ConditionEventListener* conditionEventListener = ConditionEventListener::cast(&listener))
return m_animation == conditionEventListener->m_animation && m_condition == conditionEventListener->m_condition;
return false;
}
void ConditionEventListener::handleEvent(ScriptExecutionContext&, Event&)
{
if (!m_animation)
return;
m_animation->handleConditionEvent(m_condition);
}
SVGSMILElement::Condition::Condition(Type type, BeginOrEnd beginOrEnd, const String& baseID, const AtomString& name, SMILTime offset, int repeats)
: m_type(type)
, m_beginOrEnd(beginOrEnd)
, m_baseID(baseID)
, m_name(name)
, m_offset(offset)
, m_repeats(repeats)
{
}
SVGSMILElement::SVGSMILElement(const QualifiedName& tagName, Document& doc, UniqueRef<SVGPropertyRegistry>&& propertyRegistry)
: SVGElement(tagName, doc, WTFMove(propertyRegistry))
, m_attributeName(anyQName())
, m_conditionsConnected(false)
, m_hasEndEventConditions(false)
, m_isWaitingForFirstInterval(true)
, m_intervalBegin(SMILTime::unresolved())
, m_intervalEnd(SMILTime::unresolved())
, m_previousIntervalBegin(SMILTime::unresolved())
, m_activeState(Inactive)
, m_lastPercent(0)
, m_lastRepeat(0)
, m_nextProgressTime(0)
, m_documentOrderIndex(0)
, m_cachedDur(invalidCachedTime)
, m_cachedRepeatDur(invalidCachedTime)
, m_cachedRepeatCount(invalidCachedTime)
, m_cachedMin(invalidCachedTime)
, m_cachedMax(invalidCachedTime)
{
resolveFirstInterval();
}
SVGSMILElement::~SVGSMILElement()
{
clearResourceReferences();
smilEventSender().cancelEvent(*this);
disconnectConditions();
if (m_timeContainer && m_targetElement && hasValidAttributeName())
m_timeContainer->unschedule(this, m_targetElement.get(), m_attributeName);
}
void SVGSMILElement::clearResourceReferences()
{
removeElementReference();
}
void SVGSMILElement::clearTarget()
{
setTargetElement(nullptr);
}
void SVGSMILElement::buildPendingResource()
{
clearResourceReferences();
if (!isConnected()) {
// Reset the target element if we are no longer in the document.
setTargetElement(nullptr);
return;
}
AtomString id;
RefPtr<Element> target;
auto& href = getAttribute(SVGNames::hrefAttr, XLinkNames::hrefAttr);
if (href.isEmpty())
target = parentElement();
else {
auto result = SVGURIReference::targetElementFromIRIString(href.string(), treeScope());
target = WTFMove(result.element);
id = WTFMove(result.identifier);
}
RefPtr svgTarget = is<SVGElement>(target) && target->isConnected() ? downcast<SVGElement>(target.get()) : nullptr;
if (svgTarget != targetElement())
setTargetElement(svgTarget.get());
if (!svgTarget) {
// Do not register as pending if we are already pending this resource.
if (document().accessSVGExtensions().isPendingResource(*this, id))
return;
if (!id.isEmpty()) {
document().accessSVGExtensions().addPendingResource(id, *this);
ASSERT(hasPendingResources());
}
} else
svgTarget->addReferencingElement(*this);
}
inline QualifiedName SVGSMILElement::constructAttributeName() const
{
auto parseResult = Document::parseQualifiedName(attributeWithoutSynchronization(SVGNames::attributeNameAttr));
if (parseResult.hasException())
return anyQName();
auto [prefix, localName] = parseResult.releaseReturnValue();
if (prefix.isNull())
return { nullAtom(), localName, nullAtom() };
auto namespaceURI = lookupNamespaceURI(prefix);
if (namespaceURI.isEmpty())
return anyQName();
return { nullAtom(), localName, namespaceURI };
}
inline void SVGSMILElement::updateAttributeName()
{
setAttributeName(constructAttributeName());
}
static inline void clearTimesWithDynamicOrigins(Vector<SMILTimeWithOrigin>& timeList)
{
timeList.removeAllMatching([] (const SMILTimeWithOrigin& time) {
return time.originIsScript();
});
}
void SVGSMILElement::reset()
{
stopAnimation(m_targetElement.get());
m_activeState = Inactive;
m_isWaitingForFirstInterval = true;
m_intervalBegin = SMILTime::unresolved();
m_intervalEnd = SMILTime::unresolved();
m_previousIntervalBegin = SMILTime::unresolved();
m_lastPercent = 0;
m_lastRepeat = 0;
m_nextProgressTime = 0;
resolveFirstInterval();
}
Node::InsertedIntoAncestorResult SVGSMILElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
SVGElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (!insertionType.connectedToDocument)
return InsertedIntoAncestorResult::Done;
// Verify we are not in <use> instance tree.
ASSERT(!isInShadowTree() || !is<SVGUseElement>(shadowHost()));
updateAttributeName();
RefPtr owner = ownerSVGElement();
if (!owner)
return InsertedIntoAncestorResult::Done;
m_timeContainer = &owner->timeContainer();
m_timeContainer->setDocumentOrderIndexesDirty();
// "If no attribute is present, the default begin value (an offset-value of 0) must be evaluated."
if (!hasAttributeWithoutSynchronization(SVGNames::beginAttr))
m_beginTimes.append(SMILTimeWithOrigin());
if (m_isWaitingForFirstInterval)
resolveFirstInterval();
if (m_timeContainer)
m_timeContainer->notifyIntervalsChanged();
return InsertedIntoAncestorResult::NeedsPostInsertionCallback;
}
void SVGSMILElement::didFinishInsertingNode()
{
buildPendingResource();
}
void SVGSMILElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
if (removalType.disconnectedFromDocument) {
clearResourceReferences();
disconnectConditions();
setTargetElement(nullptr);
setAttributeName(anyQName());
animationAttributeChanged();
m_timeContainer = nullptr;
}
SVGElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
}
bool SVGSMILElement::hasValidAttributeName() const
{
return attributeName() != anyQName();
}
SMILTime SVGSMILElement::parseOffsetValue(StringView data)
{
bool ok;
double result = 0;
auto parse = data.stripWhiteSpace();
if (parse.endsWith('h'))
result = parse.left(parse.length() - 1).toDouble(ok) * 60 * 60;
else if (parse.endsWith("min"_s))
result = parse.left(parse.length() - 3).toDouble(ok) * 60;
else if (parse.endsWith("ms"_s))
result = parse.left(parse.length() - 2).toDouble(ok) / 1000;
else if (parse.endsWith('s'))
result = parse.left(parse.length() - 1).toDouble(ok);
else
result = parse.toDouble(ok);
if (!ok || !SMILTime(result).isFinite())
return SMILTime::unresolved();
return result;
}
SMILTime SVGSMILElement::parseClockValue(StringView data)
{
if (data.isNull())
return SMILTime::unresolved();
auto parse = data.stripWhiteSpace();
if (parse == indefiniteAtom())
return SMILTime::indefinite();
double result = 0;
bool ok;
size_t doublePointOne = parse.find(':');
size_t doublePointTwo = parse.find(':', doublePointOne + 1);
if (doublePointOne == 2 && doublePointTwo == 5 && parse.length() >= 8) {
auto hour = parseInteger<uint8_t>(parse.left(2));
auto minute = parseInteger<uint8_t>(parse.substring(3, 2));
if (!hour || !minute)
return SMILTime::unresolved();
result = *hour * 60 * 60 + *minute * 60 + parse.substring(6).toDouble(ok);
} else if (doublePointOne == 2 && doublePointTwo == notFound && parse.length() >= 5) {
auto minute = parseInteger<uint8_t>(parse.left(2));
if (!minute)
return SMILTime::unresolved();
result = *minute * 60 + parse.substring(3).toDouble(ok);
} else
return parseOffsetValue(parse);
if (!ok || !SMILTime(result).isFinite())
return SMILTime::unresolved();
return result;
}
static void sortTimeList(Vector<SMILTimeWithOrigin>& timeList)
{
std::sort(timeList.begin(), timeList.end());
}
bool SVGSMILElement::parseCondition(StringView value, BeginOrEnd beginOrEnd)
{
auto parseString = value.stripWhiteSpace();
double sign = 1.;
size_t pos = parseString.find('+');
if (pos == notFound) {
pos = parseString.find('-');
if (pos != notFound)
sign = -1.;
}
StringView conditionString;
SMILTime offset = 0;
if (pos == notFound)
conditionString = parseString;
else {
conditionString = parseString.left(pos).stripWhiteSpace();
auto offsetString = parseString.substring(pos + 1).stripWhiteSpace();
offset = parseOffsetValue(offsetString);
if (offset.isUnresolved())
return false;
offset = offset * sign;
}
if (conditionString.isEmpty())
return false;
pos = conditionString.find('.');
StringView baseID;
StringView nameView;
if (pos == notFound)
nameView = conditionString;
else {
baseID = conditionString.left(pos);
nameView = conditionString.substring(pos + 1);
}
if (nameView.isEmpty())
return false;
AtomString nameString;
Condition::Type type;
int repeats = -1;
if (nameView.startsWith("repeat("_s) && nameView.endsWith(')')) {
// FIXME: For repeat events we just need to add the data carrying TimeEvent class and fire the events at appropiate times.
auto parsedRepeat = parseInteger<unsigned>(nameView.substring(7, nameView.length() - 8));
if (!parsedRepeat)
return false;
// FIXME: By assigning an unsigned to a signed, this can turn large integers into negative numbers.
repeats = *parsedRepeat;
nameString = "repeat"_s;
type = Condition::EventBase;
} else if (nameView == "begin"_s || nameView == "end"_s) {
if (baseID.isEmpty())
return false;
type = Condition::Syncbase;
nameString = nameView.toAtomString();
} else if (nameView.startsWith("accesskey("_s)) {
// FIXME: accesskey() support.
type = Condition::AccessKey;
nameString = nameView.toAtomString();
} else {
type = Condition::EventBase;
nameString = nameView.toAtomString();
}
m_conditions.append(Condition(type, beginOrEnd, baseID.toString(), WTFMove(nameString), offset, repeats));
if (type == Condition::EventBase && beginOrEnd == End)
m_hasEndEventConditions = true;
return true;
}
void SVGSMILElement::parseBeginOrEnd(StringView parseString, BeginOrEnd beginOrEnd)
{
Vector<SMILTimeWithOrigin>& timeList = beginOrEnd == Begin ? m_beginTimes : m_endTimes;
if (beginOrEnd == End)
m_hasEndEventConditions = false;
HashSet<double> existing;
for (auto& time : timeList)
existing.add(time.time().value());
for (auto string : parseString.split(';')) {
SMILTime value = parseClockValue(string);
if (value.isUnresolved())
parseCondition(string, beginOrEnd);
else if (!existing.contains(value.value()))
timeList.append(SMILTimeWithOrigin(value, SMILTimeWithOrigin::ParserOrigin));
}
sortTimeList(timeList);
}
bool SVGSMILElement::isSupportedAttribute(const QualifiedName& attrName)
{
static NeverDestroyed supportedAttributes = MemoryCompactLookupOnlyRobinHoodHashSet<QualifiedName> {
SVGNames::beginAttr,
SVGNames::endAttr,
SVGNames::durAttr,
SVGNames::repeatDurAttr,
SVGNames::repeatCountAttr,
SVGNames::minAttr,
SVGNames::maxAttr,
SVGNames::attributeNameAttr,
SVGNames::hrefAttr,
XLinkNames::hrefAttr,
};
return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
}
void SVGSMILElement::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name == SVGNames::beginAttr) {
if (!m_conditions.isEmpty()) {
disconnectConditions();
m_conditions.clear();
parseBeginOrEnd(attributeWithoutSynchronization(SVGNames::endAttr), End);
}
parseBeginOrEnd(value, Begin);
if (isConnected())
connectConditions();
} else if (name == SVGNames::endAttr) {
if (!m_conditions.isEmpty()) {
disconnectConditions();
m_conditions.clear();
parseBeginOrEnd(attributeWithoutSynchronization(SVGNames::beginAttr), Begin);
}
parseBeginOrEnd(value, End);
if (isConnected())
connectConditions();
} else if (name == SVGNames::onendAttr)
setAttributeEventListener(eventNames().endEventEvent, name, value);
else if (name == SVGNames::onbeginAttr)
setAttributeEventListener(eventNames().beginEventEvent, name, value);
else
SVGElement::parseAttribute(name, value);
}
void SVGSMILElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (!isSupportedAttribute(attrName)) {
SVGElement::svgAttributeChanged(attrName);
return;
}
if (attrName == SVGNames::durAttr)
m_cachedDur = invalidCachedTime;
else if (attrName == SVGNames::repeatDurAttr)
m_cachedRepeatDur = invalidCachedTime;
else if (attrName == SVGNames::repeatCountAttr)
m_cachedRepeatCount = invalidCachedTime;
else if (attrName == SVGNames::minAttr)
m_cachedMin = invalidCachedTime;
else if (attrName == SVGNames::maxAttr)
m_cachedMax = invalidCachedTime;
else if (attrName == SVGNames::attributeNameAttr)
updateAttributeName();
else if (attrName.matches(SVGNames::hrefAttr) || attrName.matches(XLinkNames::hrefAttr)) {
InstanceInvalidationGuard guard(*this);
buildPendingResource();
} else if (isConnected()) {
if (attrName == SVGNames::beginAttr)
beginListChanged(elapsed());
else if (attrName == SVGNames::endAttr)
endListChanged(elapsed());
}
animationAttributeChanged();
}
inline Element* SVGSMILElement::eventBaseFor(const Condition& condition)
{
return condition.m_baseID.isEmpty() ? targetElement() : treeScope().getElementById(condition.m_baseID);
}
void SVGSMILElement::connectConditions()
{
if (m_conditionsConnected)
disconnectConditions();
m_conditionsConnected = true;
for (auto& condition : m_conditions) {
if (condition.m_type == Condition::EventBase) {
ASSERT(!condition.m_syncbase);
RefPtr eventBase = eventBaseFor(condition);
if (!eventBase)
continue;
ASSERT(!condition.m_eventListener);
condition.m_eventListener = ConditionEventListener::create(this, &condition);
eventBase->addEventListener(condition.m_name, *condition.m_eventListener, false);
} else if (condition.m_type == Condition::Syncbase) {
ASSERT(!condition.m_baseID.isEmpty());
condition.m_syncbase = treeScope().getElementById(condition.m_baseID);
if (!condition.m_syncbase)
continue;
if (!is<SVGSMILElement>(*condition.m_syncbase)) {
condition.m_syncbase = nullptr;
continue;
}
downcast<SVGSMILElement>(*condition.m_syncbase).addTimeDependent(this);
}
}
}
void SVGSMILElement::disconnectConditions()
{
if (!m_conditionsConnected)
return;
m_conditionsConnected = false;
for (auto& condition : m_conditions) {
if (condition.m_type == Condition::EventBase) {
ASSERT(!condition.m_syncbase);
if (!condition.m_eventListener)
continue;
// Note: It's a memory optimization to try to remove our condition
// event listener, but it's not guaranteed to work, since we have
// no guarantee that eventBaseFor() will be able to find our condition's
// original eventBase. So, we also have to disconnect ourselves from
// our condition event listener, in case it later fires.
RefPtr eventBase = eventBaseFor(condition);
if (eventBase)
eventBase->removeEventListener(condition.m_name, *condition.m_eventListener, false);
condition.m_eventListener->disconnectAnimation();
condition.m_eventListener = nullptr;
} else if (condition.m_type == Condition::Syncbase) {
if (condition.m_syncbase)
downcast<SVGSMILElement>(condition.m_syncbase.get())->removeTimeDependent(this);
}
condition.m_syncbase = nullptr;
}
}
void SVGSMILElement::setAttributeName(const QualifiedName& attributeName)
{
if (m_timeContainer && m_targetElement && m_attributeName != attributeName) {
if (hasValidAttributeName())
m_timeContainer->unschedule(this, m_targetElement.get(), m_attributeName);
m_attributeName = attributeName;
if (hasValidAttributeName())
m_timeContainer->schedule(this, m_targetElement.get(), m_attributeName);
} else
m_attributeName = attributeName;
// Only clear the animated type, if we had a target before.
if (m_targetElement)
stopAnimation(m_targetElement.get());
}
void SVGSMILElement::setTargetElement(SVGElement* target)
{
if (m_timeContainer && hasValidAttributeName()) {
if (m_targetElement)
m_timeContainer->unschedule(this, m_targetElement.get(), m_attributeName);
if (target)
m_timeContainer->schedule(this, target, m_attributeName);
}
if (m_targetElement) {
// Clear values that may depend on the previous target.
stopAnimation(m_targetElement.get());
disconnectConditions();
}
// If the animation state is not Inactive, always reset to a clear state before leaving the old target element.
if (m_activeState != Inactive)
endedActiveInterval();
m_targetElement = target;
}
SMILTime SVGSMILElement::elapsed() const
{
return m_timeContainer ? m_timeContainer->elapsed() : 0;
}
bool SVGSMILElement::isInactive() const
{
return m_activeState == Inactive;
}
bool SVGSMILElement::isFrozen() const
{
return m_activeState == Frozen;
}
SVGSMILElement::Restart SVGSMILElement::restart() const
{
static MainThreadNeverDestroyed<const AtomString> never("never"_s);
static MainThreadNeverDestroyed<const AtomString> whenNotActive("whenNotActive"_s);
const AtomString& value = attributeWithoutSynchronization(SVGNames::restartAttr);
if (value == never)
return RestartNever;
if (value == whenNotActive)
return RestartWhenNotActive;
return RestartAlways;
}
SVGSMILElement::FillMode SVGSMILElement::fill() const
{
static MainThreadNeverDestroyed<const AtomString> freeze("freeze"_s);
const AtomString& value = attributeWithoutSynchronization(SVGNames::fillAttr);
return value == freeze ? FillFreeze : FillRemove;
}
SMILTime SVGSMILElement::dur() const
{
if (m_cachedDur != invalidCachedTime)
return m_cachedDur;
const AtomString& value = attributeWithoutSynchronization(SVGNames::durAttr);
SMILTime clockValue = parseClockValue(value);
return m_cachedDur = clockValue <= 0 ? SMILTime::unresolved() : clockValue;
}
SMILTime SVGSMILElement::repeatDur() const
{
if (m_cachedRepeatDur != invalidCachedTime)
return m_cachedRepeatDur;
const AtomString& value = attributeWithoutSynchronization(SVGNames::repeatDurAttr);
SMILTime clockValue = parseClockValue(value);
m_cachedRepeatDur = clockValue <= 0 ? SMILTime::unresolved() : clockValue;
return m_cachedRepeatDur;
}
// So a count is not really a time but let just all pretend we did not notice.
SMILTime SVGSMILElement::repeatCount() const
{
if (m_cachedRepeatCount != invalidCachedTime)
return m_cachedRepeatCount;
const AtomString& value = attributeWithoutSynchronization(SVGNames::repeatCountAttr);
if (value.isNull())
return SMILTime::unresolved();
if (value == indefiniteAtom())
return SMILTime::indefinite();
bool ok;
double result = value.string().toDouble(&ok);
return m_cachedRepeatCount = ok && result > 0 ? result : SMILTime::unresolved();
}
SMILTime SVGSMILElement::maxValue() const
{
if (m_cachedMax != invalidCachedTime)
return m_cachedMax;
const AtomString& value = attributeWithoutSynchronization(SVGNames::maxAttr);
SMILTime result = parseClockValue(value);
return m_cachedMax = (result.isUnresolved() || result <= 0) ? SMILTime::indefinite() : result;
}
SMILTime SVGSMILElement::minValue() const
{
if (m_cachedMin != invalidCachedTime)
return m_cachedMin;
const AtomString& value = attributeWithoutSynchronization(SVGNames::minAttr);
SMILTime result = parseClockValue(value);
return m_cachedMin = (result.isUnresolved() || result < 0) ? 0 : result;
}
SMILTime SVGSMILElement::simpleDuration() const
{
return std::min(dur(), SMILTime::indefinite());
}
void SVGSMILElement::addBeginTime(SMILTime eventTime, SMILTime beginTime, SMILTimeWithOrigin::Origin origin)
{
ASSERT(!std::isnan(beginTime.value()));
m_beginTimes.append(SMILTimeWithOrigin(beginTime, origin));
sortTimeList(m_beginTimes);
beginListChanged(eventTime);
}
void SVGSMILElement::addEndTime(SMILTime eventTime, SMILTime endTime, SMILTimeWithOrigin::Origin origin)
{
ASSERT(!std::isnan(endTime.value()));
m_endTimes.append(SMILTimeWithOrigin(endTime, origin));
sortTimeList(m_endTimes);
endListChanged(eventTime);
}
inline SMILTime extractTimeFromVector(const SMILTimeWithOrigin* position)
{
return position->time();
}
SMILTime SVGSMILElement::findInstanceTime(BeginOrEnd beginOrEnd, SMILTime minimumTime, bool equalsMinimumOK) const
{
const Vector<SMILTimeWithOrigin>& list = beginOrEnd == Begin ? m_beginTimes : m_endTimes;
int sizeOfList = list.size();
if (!sizeOfList)
return beginOrEnd == Begin ? SMILTime::unresolved() : SMILTime::indefinite();
const SMILTimeWithOrigin* result = approximateBinarySearch<const SMILTimeWithOrigin, SMILTime>(list, sizeOfList, minimumTime, extractTimeFromVector);
int indexOfResult = result - list.begin();
ASSERT_WITH_SECURITY_IMPLICATION(indexOfResult < sizeOfList);
if (list[indexOfResult].time() < minimumTime && indexOfResult < sizeOfList - 1)
++indexOfResult;
const SMILTime& currentTime = list[indexOfResult].time();
// The special value "indefinite" does not yield an instance time in the begin list.
if (currentTime.isIndefinite() && beginOrEnd == Begin)
return SMILTime::unresolved();
if (currentTime < minimumTime)
return beginOrEnd == Begin ? SMILTime::unresolved() : SMILTime::indefinite();
if (currentTime > minimumTime)
return currentTime;
ASSERT(currentTime == minimumTime);
if (equalsMinimumOK)
return currentTime;
// If the equals is not accepted, return the next bigger item in the list.
SMILTime nextTime = currentTime;
while (indexOfResult < sizeOfList - 1) {
nextTime = list[indexOfResult + 1].time();
if (nextTime > minimumTime)
return nextTime;
++indexOfResult;
}
return beginOrEnd == Begin ? SMILTime::unresolved() : SMILTime::indefinite();
}
SMILTime SVGSMILElement::repeatingDuration() const
{
// Computing the active duration
// http://www.w3.org/TR/SMIL2/smil-timing.html#Timing-ComputingActiveDur
SMILTime repeatCount = this->repeatCount();
SMILTime repeatDur = this->repeatDur();
SMILTime simpleDuration = this->simpleDuration();
if (!simpleDuration || (repeatDur.isUnresolved() && repeatCount.isUnresolved()))
return simpleDuration;
SMILTime repeatCountDuration = simpleDuration * repeatCount;
return std::min(repeatCountDuration, std::min(repeatDur, SMILTime::indefinite()));
}
SMILTime SVGSMILElement::resolveActiveEnd(SMILTime resolvedBegin, SMILTime resolvedEnd) const
{
// Computing the active duration
// http://www.w3.org/TR/SMIL2/smil-timing.html#Timing-ComputingActiveDur
SMILTime preliminaryActiveDuration;
if (!resolvedEnd.isUnresolved() && dur().isUnresolved() && repeatDur().isUnresolved() && repeatCount().isUnresolved())
preliminaryActiveDuration = resolvedEnd - resolvedBegin;
else if (!resolvedEnd.isFinite())
preliminaryActiveDuration = repeatingDuration();
else
preliminaryActiveDuration = std::min(repeatingDuration(), resolvedEnd - resolvedBegin);
SMILTime minValue = this->minValue();
SMILTime maxValue = this->maxValue();
if (minValue > maxValue) {
// Ignore both.
// http://www.w3.org/TR/2001/REC-smil-animation-20010904/#MinMax
minValue = 0;
maxValue = SMILTime::indefinite();
}
return resolvedBegin + std::min(maxValue, std::max(minValue, preliminaryActiveDuration));
}
void SVGSMILElement::resolveInterval(bool first, SMILTime& beginResult, SMILTime& endResult) const
{
// See the pseudocode in http://www.w3.org/TR/SMIL3/smil-timing.html#q90.
SMILTime beginAfter = first ? -std::numeric_limits<double>::infinity() : m_intervalEnd;
SMILTime lastIntervalTempEnd = std::numeric_limits<double>::infinity();
while (true) {
bool equalsMinimumOK = !first || m_intervalEnd > m_intervalBegin;
SMILTime tempBegin = findInstanceTime(Begin, beginAfter, equalsMinimumOK);
if (tempBegin.isUnresolved())
break;
SMILTime tempEnd;
if (m_endTimes.isEmpty())
tempEnd = resolveActiveEnd(tempBegin, SMILTime::indefinite());
else {
tempEnd = findInstanceTime(End, tempBegin, true);
if ((first && tempBegin == tempEnd && tempEnd == lastIntervalTempEnd) || (!first && tempEnd == m_intervalEnd))
tempEnd = findInstanceTime(End, tempBegin, false);
if (tempEnd.isUnresolved()) {
if (!m_endTimes.isEmpty() && !m_hasEndEventConditions)
break;
}
tempEnd = resolveActiveEnd(tempBegin, tempEnd);
}
if (!first || (tempEnd > 0 || (!tempBegin.value() && !tempEnd.value()))) {
beginResult = tempBegin;
endResult = tempEnd;
return;
}
beginAfter = tempEnd;
lastIntervalTempEnd = tempEnd;
}
beginResult = SMILTime::unresolved();
endResult = SMILTime::unresolved();
}
void SVGSMILElement::resolveFirstInterval()
{
SMILTime begin;
SMILTime end;
resolveInterval(true, begin, end);
ASSERT(!begin.isIndefinite());
if (!begin.isUnresolved() && (begin != m_intervalBegin || end != m_intervalEnd)) {
bool wasUnresolved = m_intervalBegin.isUnresolved();
m_intervalBegin = begin;
m_intervalEnd = end;
notifyDependentsIntervalChanged(wasUnresolved ? NewInterval : ExistingInterval);
m_nextProgressTime = std::min(m_nextProgressTime, m_intervalBegin);
if (m_timeContainer)
m_timeContainer->notifyIntervalsChanged();
}
}
void SVGSMILElement::resolveNextInterval(bool notifyDependents)
{
SMILTime begin;
SMILTime end;
resolveInterval(false, begin, end);
ASSERT(!begin.isIndefinite());
if (!begin.isUnresolved() && begin != m_intervalBegin) {
m_intervalBegin = begin;
m_intervalEnd = end;
if (notifyDependents)
notifyDependentsIntervalChanged(NewInterval);
m_nextProgressTime = std::min(m_nextProgressTime, m_intervalBegin);
}
}
SMILTime SVGSMILElement::nextProgressTime() const
{
return m_nextProgressTime;
}
void SVGSMILElement::beginListChanged(SMILTime eventTime)
{
if (m_isWaitingForFirstInterval)
resolveFirstInterval();
else {
if (restart() == RestartNever)
return;
SMILTime newBegin = findInstanceTime(Begin, eventTime, true);
if (newBegin.isFinite() && (m_intervalEnd <= eventTime || newBegin < m_intervalBegin)) {
// Begin time changed, re-resolve the interval.
SMILTime oldBegin = m_intervalBegin;
m_intervalEnd = eventTime;
resolveInterval(false, m_intervalBegin, m_intervalEnd);
ASSERT(!m_intervalBegin.isUnresolved());
if (m_intervalBegin != oldBegin) {
if (m_activeState == Active && m_intervalBegin > eventTime) {
m_activeState = determineActiveState(eventTime);
if (m_activeState != Active)
endedActiveInterval();
}
notifyDependentsIntervalChanged(ExistingInterval);
}
}
}
m_nextProgressTime = elapsed();
if (m_timeContainer)
m_timeContainer->notifyIntervalsChanged();
}
void SVGSMILElement::endListChanged(SMILTime)
{
SMILTime elapsed = this->elapsed();
if (m_isWaitingForFirstInterval)
resolveFirstInterval();
else if (elapsed < m_intervalEnd && m_intervalBegin.isFinite()) {
SMILTime newEnd = findInstanceTime(End, m_intervalBegin, false);
if (newEnd < m_intervalEnd) {
newEnd = resolveActiveEnd(m_intervalBegin, newEnd);
if (newEnd != m_intervalEnd) {
m_intervalEnd = newEnd;
notifyDependentsIntervalChanged(ExistingInterval);
}
}
}
m_nextProgressTime = elapsed;
if (m_timeContainer)
m_timeContainer->notifyIntervalsChanged();
}
void SVGSMILElement::checkRestart(SMILTime elapsed)
{
ASSERT(!m_isWaitingForFirstInterval);
ASSERT(elapsed >= m_intervalBegin);
Restart restart = this->restart();
if (restart == RestartNever)
return;
if (elapsed < m_intervalEnd) {
if (restart != RestartAlways)
return;
SMILTime nextBegin = findInstanceTime(Begin, m_intervalBegin, false);
if (nextBegin < m_intervalEnd) {
m_intervalEnd = nextBegin;
notifyDependentsIntervalChanged(ExistingInterval);
}
}
if (elapsed >= m_intervalEnd)
resolveNextInterval(true);
}
void SVGSMILElement::seekToIntervalCorrespondingToTime(SMILTime elapsed)
{
ASSERT(!m_isWaitingForFirstInterval);
ASSERT(elapsed >= m_intervalBegin);
// Manually seek from interval to interval, just as if the animation would run regulary.
while (true) {
// Figure out the next value in the begin time list after the current interval begin.
SMILTime nextBegin = findInstanceTime(Begin, m_intervalBegin, false);
// If the 'nextBegin' time is unresolved (eg. just one defined interval), we're done seeking.
if (nextBegin.isUnresolved())
return;
// If the 'nextBegin' time is larger than or equal to the current interval end time, we're done seeking.
// If the 'elapsed' time is smaller than the next begin interval time, we're done seeking.
if (nextBegin < m_intervalEnd && elapsed >= nextBegin) {
// End current interval, and start a new interval from the 'nextBegin' time.
m_intervalEnd = nextBegin;
resolveNextInterval(false);
continue;
}