-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation-summary.js
1396 lines (1396 loc) · 55.3 KB
/
mutation-summary.js
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 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var MutationObserverCtor;
if (typeof WebKitMutationObserver !== 'undefined')
MutationObserverCtor = WebKitMutationObserver;
else
MutationObserverCtor = MutationObserver;
if (MutationObserverCtor === undefined) {
console.error('DOM Mutation Observers are required.');
console.error('https://developer.mozilla.org/en-US/docs/DOM/MutationObserver');
throw Error('DOM Mutation Observers are required');
}
var NodeMap = (function () {
function NodeMap() {
this.nodes = [];
this.values = [];
}
NodeMap.prototype.isIndex = function (s) {
return +s === s >>> 0;
};
NodeMap.prototype.nodeId = function (node) {
var id = node[NodeMap.ID_PROP];
if (!id)
id = node[NodeMap.ID_PROP] = NodeMap.nextId_++;
return id;
};
NodeMap.prototype.set = function (node, value) {
var id = this.nodeId(node);
this.nodes[id] = node;
this.values[id] = value;
};
NodeMap.prototype.get = function (node) {
var id = this.nodeId(node);
return this.values[id];
};
NodeMap.prototype.has = function (node) {
return this.nodeId(node) in this.nodes;
};
NodeMap.prototype.delete = function (node) {
var id = this.nodeId(node);
delete this.nodes[id];
this.values[id] = undefined;
};
NodeMap.prototype.keys = function () {
var nodes = [];
for (var id in this.nodes) {
if (!this.isIndex(id))
continue;
nodes.push(this.nodes[id]);
}
return nodes;
};
NodeMap.ID_PROP = '__mutation_summary_node_map_id__';
NodeMap.nextId_ = 1;
return NodeMap;
})();
/**
* var reachableMatchableProduct = [
* // STAYED_OUT, ENTERED, STAYED_IN, EXITED
* [ STAYED_OUT, STAYED_OUT, STAYED_OUT, STAYED_OUT ], // STAYED_OUT
* [ STAYED_OUT, ENTERED, ENTERED, STAYED_OUT ], // ENTERED
* [ STAYED_OUT, ENTERED, STAYED_IN, EXITED ], // STAYED_IN
* [ STAYED_OUT, STAYED_OUT, EXITED, EXITED ] // EXITED
* ];
*/
var Movement;
(function (Movement) {
Movement[Movement["STAYED_OUT"] = 0] = "STAYED_OUT";
Movement[Movement["ENTERED"] = 1] = "ENTERED";
Movement[Movement["STAYED_IN"] = 2] = "STAYED_IN";
Movement[Movement["REPARENTED"] = 3] = "REPARENTED";
Movement[Movement["REORDERED"] = 4] = "REORDERED";
Movement[Movement["EXITED"] = 5] = "EXITED";
})(Movement || (Movement = {}));
function enteredOrExited(changeType) {
return changeType === Movement.ENTERED || changeType === Movement.EXITED;
}
var NodeChange = (function () {
function NodeChange(node, childList, attributes, characterData, oldParentNode, added, attributeOldValues, characterDataOldValue) {
if (childList === void 0) { childList = false; }
if (attributes === void 0) { attributes = false; }
if (characterData === void 0) { characterData = false; }
if (oldParentNode === void 0) { oldParentNode = null; }
if (added === void 0) { added = false; }
if (attributeOldValues === void 0) { attributeOldValues = null; }
if (characterDataOldValue === void 0) { characterDataOldValue = null; }
this.node = node;
this.childList = childList;
this.attributes = attributes;
this.characterData = characterData;
this.oldParentNode = oldParentNode;
this.added = added;
this.attributeOldValues = attributeOldValues;
this.characterDataOldValue = characterDataOldValue;
this.isCaseInsensitive =
this.node.nodeType === Node.ELEMENT_NODE &&
this.node instanceof HTMLElement &&
this.node.ownerDocument instanceof HTMLDocument;
}
NodeChange.prototype.getAttributeOldValue = function (name) {
if (!this.attributeOldValues)
return undefined;
if (this.isCaseInsensitive)
name = name.toLowerCase();
return this.attributeOldValues[name];
};
NodeChange.prototype.getAttributeNamesMutated = function () {
var names = [];
if (!this.attributeOldValues)
return names;
for (var name in this.attributeOldValues) {
names.push(name);
}
return names;
};
NodeChange.prototype.attributeMutated = function (name, oldValue) {
this.attributes = true;
this.attributeOldValues = this.attributeOldValues || {};
if (name in this.attributeOldValues)
return;
this.attributeOldValues[name] = oldValue;
};
NodeChange.prototype.characterDataMutated = function (oldValue) {
if (this.characterData)
return;
this.characterData = true;
this.characterDataOldValue = oldValue;
};
// Note: is it possible to receive a removal followed by a removal. This
// can occur if the removed node is added to an non-observed node, that
// node is added to the observed area, and then the node removed from
// it.
NodeChange.prototype.removedFromParent = function (parent) {
this.childList = true;
if (this.added || this.oldParentNode)
this.added = false;
else
this.oldParentNode = parent;
};
NodeChange.prototype.insertedIntoParent = function () {
this.childList = true;
this.added = true;
};
// An node's oldParent is
// -its present parent, if its parentNode was not changed.
// -null if the first thing that happened to it was an add.
// -the node it was removed from if the first thing that happened to it
// was a remove.
NodeChange.prototype.getOldParent = function () {
if (this.childList) {
if (this.oldParentNode)
return this.oldParentNode;
if (this.added)
return null;
}
return this.node.parentNode;
};
return NodeChange;
})();
var ChildListChange = (function () {
function ChildListChange() {
this.added = new NodeMap();
this.removed = new NodeMap();
this.maybeMoved = new NodeMap();
this.oldPrevious = new NodeMap();
this.moved = undefined;
}
return ChildListChange;
})();
var TreeChanges = (function (_super) {
__extends(TreeChanges, _super);
function TreeChanges(rootNode, mutations) {
_super.call(this);
this.rootNode = rootNode;
this.reachableCache = undefined;
this.wasReachableCache = undefined;
this.anyParentsChanged = false;
this.anyAttributesChanged = false;
this.anyCharacterDataChanged = false;
for (var m = 0; m < mutations.length; m++) {
var mutation = mutations[m];
switch (mutation.type) {
case 'childList':
this.anyParentsChanged = true;
for (var i = 0; i < mutation.removedNodes.length; i++) {
var node = mutation.removedNodes[i];
this.getChange(node).removedFromParent(mutation.target);
}
for (var i = 0; i < mutation.addedNodes.length; i++) {
var node = mutation.addedNodes[i];
this.getChange(node).insertedIntoParent();
}
break;
case 'attributes':
this.anyAttributesChanged = true;
var change = this.getChange(mutation.target);
change.attributeMutated(mutation.attributeName, mutation.oldValue);
break;
case 'characterData':
this.anyCharacterDataChanged = true;
var change = this.getChange(mutation.target);
change.characterDataMutated(mutation.oldValue);
break;
}
}
}
TreeChanges.prototype.getChange = function (node) {
var change = this.get(node);
if (!change) {
change = new NodeChange(node);
this.set(node, change);
}
return change;
};
TreeChanges.prototype.getOldParent = function (node) {
var change = this.get(node);
return change ? change.getOldParent() : node.parentNode;
};
TreeChanges.prototype.getIsReachable = function (node) {
if (node === this.rootNode)
return true;
if (!node)
return false;
this.reachableCache = this.reachableCache || new NodeMap();
var isReachable = this.reachableCache.get(node);
if (isReachable === undefined) {
isReachable = this.getIsReachable(node.parentNode);
this.reachableCache.set(node, isReachable);
}
return isReachable;
};
// A node wasReachable if its oldParent wasReachable.
TreeChanges.prototype.getWasReachable = function (node) {
if (node === this.rootNode)
return true;
if (!node)
return false;
this.wasReachableCache = this.wasReachableCache || new NodeMap();
var wasReachable = this.wasReachableCache.get(node);
if (wasReachable === undefined) {
wasReachable = this.getWasReachable(this.getOldParent(node));
this.wasReachableCache.set(node, wasReachable);
}
return wasReachable;
};
TreeChanges.prototype.reachabilityChange = function (node) {
if (this.getIsReachable(node)) {
return this.getWasReachable(node) ?
Movement.STAYED_IN : Movement.ENTERED;
}
return this.getWasReachable(node) ?
Movement.EXITED : Movement.STAYED_OUT;
};
return TreeChanges;
})(NodeMap);
var MutationProjection = (function () {
// TOOD(any)
function MutationProjection(rootNode, mutations, selectors, calcReordered, calcOldPreviousSibling) {
this.rootNode = rootNode;
this.mutations = mutations;
this.selectors = selectors;
this.calcReordered = calcReordered;
this.calcOldPreviousSibling = calcOldPreviousSibling;
this.treeChanges = new TreeChanges(rootNode, mutations);
this.entered = [];
this.exited = [];
this.stayedIn = new NodeMap();
this.visited = new NodeMap();
this.childListChangeMap = undefined;
this.characterDataOnly = undefined;
this.matchCache = undefined;
this.processMutations();
}
MutationProjection.prototype.processMutations = function () {
if (!this.treeChanges.anyParentsChanged &&
!this.treeChanges.anyAttributesChanged)
return;
var changedNodes = this.treeChanges.keys();
for (var i = 0; i < changedNodes.length; i++) {
this.visitNode(changedNodes[i], undefined);
}
};
MutationProjection.prototype.visitNode = function (node, parentReachable) {
if (this.visited.has(node))
return;
this.visited.set(node, true);
var change = this.treeChanges.get(node);
var reachable = parentReachable;
// node inherits its parent's reachability change unless
// its parentNode was mutated.
if ((change && change.childList) || reachable == undefined)
reachable = this.treeChanges.reachabilityChange(node);
if (reachable === Movement.STAYED_OUT)
return;
// Cache match results for sub-patterns.
this.matchabilityChange(node);
if (reachable === Movement.ENTERED) {
this.entered.push(node);
}
else if (reachable === Movement.EXITED) {
this.exited.push(node);
this.ensureHasOldPreviousSiblingIfNeeded(node);
}
else if (reachable === Movement.STAYED_IN) {
var movement = Movement.STAYED_IN;
if (change && change.childList) {
if (change.oldParentNode !== node.parentNode) {
movement = Movement.REPARENTED;
this.ensureHasOldPreviousSiblingIfNeeded(node);
}
else if (this.calcReordered && this.wasReordered(node)) {
movement = Movement.REORDERED;
}
}
this.stayedIn.set(node, movement);
}
if (reachable === Movement.STAYED_IN)
return;
// reachable === ENTERED || reachable === EXITED.
for (var child = node.firstChild; child; child = child.nextSibling) {
this.visitNode(child, reachable);
}
};
MutationProjection.prototype.ensureHasOldPreviousSiblingIfNeeded = function (node) {
if (!this.calcOldPreviousSibling)
return;
this.processChildlistChanges();
var parentNode = node.parentNode;
var nodeChange = this.treeChanges.get(node);
if (nodeChange && nodeChange.oldParentNode)
parentNode = nodeChange.oldParentNode;
var change = this.childListChangeMap.get(parentNode);
if (!change) {
change = new ChildListChange();
this.childListChangeMap.set(parentNode, change);
}
if (!change.oldPrevious.has(node)) {
change.oldPrevious.set(node, node.previousSibling);
}
};
MutationProjection.prototype.getChanged = function (summary, selectors, characterDataOnly) {
this.selectors = selectors;
this.characterDataOnly = characterDataOnly;
for (var i = 0; i < this.entered.length; i++) {
var node = this.entered[i];
var matchable = this.matchabilityChange(node);
if (matchable === Movement.ENTERED || matchable === Movement.STAYED_IN)
summary.added.push(node);
}
var stayedInNodes = this.stayedIn.keys();
for (var i = 0; i < stayedInNodes.length; i++) {
var node = stayedInNodes[i];
var matchable = this.matchabilityChange(node);
if (matchable === Movement.ENTERED) {
summary.added.push(node);
}
else if (matchable === Movement.EXITED) {
summary.removed.push(node);
}
else if (matchable === Movement.STAYED_IN && (summary.reparented || summary.reordered)) {
var movement = this.stayedIn.get(node);
if (summary.reparented && movement === Movement.REPARENTED)
summary.reparented.push(node);
else if (summary.reordered && movement === Movement.REORDERED)
summary.reordered.push(node);
}
}
for (var i = 0; i < this.exited.length; i++) {
var node = this.exited[i];
var matchable = this.matchabilityChange(node);
if (matchable === Movement.EXITED || matchable === Movement.STAYED_IN)
summary.removed.push(node);
}
};
MutationProjection.prototype.getOldParentNode = function (node) {
var change = this.treeChanges.get(node);
if (change && change.childList)
return change.oldParentNode ? change.oldParentNode : null;
var reachabilityChange = this.treeChanges.reachabilityChange(node);
if (reachabilityChange === Movement.STAYED_OUT || reachabilityChange === Movement.ENTERED)
throw Error('getOldParentNode requested on invalid node.');
return node.parentNode;
};
MutationProjection.prototype.getOldPreviousSibling = function (node) {
var parentNode = node.parentNode;
var nodeChange = this.treeChanges.get(node);
if (nodeChange && nodeChange.oldParentNode)
parentNode = nodeChange.oldParentNode;
var change = this.childListChangeMap.get(parentNode);
if (!change)
throw Error('getOldPreviousSibling requested on invalid node.');
return change.oldPrevious.get(node);
};
MutationProjection.prototype.getOldAttribute = function (element, attrName) {
var change = this.treeChanges.get(element);
if (!change || !change.attributes)
throw Error('getOldAttribute requested on invalid node.');
var value = change.getAttributeOldValue(attrName);
if (value === undefined)
throw Error('getOldAttribute requested for unchanged attribute name.');
return value;
};
MutationProjection.prototype.attributeChangedNodes = function (includeAttributes) {
if (!this.treeChanges.anyAttributesChanged)
return {}; // No attributes mutations occurred.
var attributeFilter;
var caseInsensitiveFilter;
if (includeAttributes) {
attributeFilter = {};
caseInsensitiveFilter = {};
for (var i = 0; i < includeAttributes.length; i++) {
var attrName = includeAttributes[i];
attributeFilter[attrName] = true;
caseInsensitiveFilter[attrName.toLowerCase()] = attrName;
}
}
var result = {};
var nodes = this.treeChanges.keys();
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
var change = this.treeChanges.get(node);
if (!change.attributes)
continue;
if (Movement.STAYED_IN !== this.treeChanges.reachabilityChange(node) ||
Movement.STAYED_IN !== this.matchabilityChange(node)) {
continue;
}
var element = node;
var changedAttrNames = change.getAttributeNamesMutated();
for (var j = 0; j < changedAttrNames.length; j++) {
var attrName = changedAttrNames[j];
if (attributeFilter &&
!attributeFilter[attrName] &&
!(change.isCaseInsensitive && caseInsensitiveFilter[attrName])) {
continue;
}
var oldValue = change.getAttributeOldValue(attrName);
if (oldValue === element.getAttribute(attrName))
continue;
if (caseInsensitiveFilter && change.isCaseInsensitive)
attrName = caseInsensitiveFilter[attrName];
result[attrName] = result[attrName] || [];
result[attrName].push(element);
}
}
return result;
};
MutationProjection.prototype.getOldCharacterData = function (node) {
var change = this.treeChanges.get(node);
if (!change || !change.characterData)
throw Error('getOldCharacterData requested on invalid node.');
return change.characterDataOldValue;
};
MutationProjection.prototype.getCharacterDataChanged = function () {
if (!this.treeChanges.anyCharacterDataChanged)
return []; // No characterData mutations occurred.
var nodes = this.treeChanges.keys();
var result = [];
for (var i = 0; i < nodes.length; i++) {
var target = nodes[i];
if (Movement.STAYED_IN !== this.treeChanges.reachabilityChange(target))
continue;
var change = this.treeChanges.get(target);
if (!change.characterData ||
target.textContent == change.characterDataOldValue)
continue;
result.push(target);
}
return result;
};
MutationProjection.prototype.computeMatchabilityChange = function (selector, el) {
if (!this.matchCache)
this.matchCache = [];
if (!this.matchCache[selector.uid])
this.matchCache[selector.uid] = new NodeMap();
var cache = this.matchCache[selector.uid];
var result = cache.get(el);
if (result === undefined) {
result = selector.matchabilityChange(el, this.treeChanges.get(el));
cache.set(el, result);
}
return result;
};
MutationProjection.prototype.matchabilityChange = function (node) {
var _this = this;
// TODO(rafaelw): Include PI, CDATA?
// Only include text nodes.
if (this.characterDataOnly) {
switch (node.nodeType) {
case Node.COMMENT_NODE:
case Node.TEXT_NODE:
return Movement.STAYED_IN;
default:
return Movement.STAYED_OUT;
}
}
// No element filter. Include all nodes.
if (!this.selectors)
return Movement.STAYED_IN;
// Element filter. Exclude non-elements.
if (node.nodeType !== Node.ELEMENT_NODE)
return Movement.STAYED_OUT;
var el = node;
var matchChanges = this.selectors.map(function (selector) {
return _this.computeMatchabilityChange(selector, el);
});
var accum = Movement.STAYED_OUT;
var i = 0;
while (accum !== Movement.STAYED_IN && i < matchChanges.length) {
switch (matchChanges[i]) {
case Movement.STAYED_IN:
accum = Movement.STAYED_IN;
break;
case Movement.ENTERED:
if (accum === Movement.EXITED)
accum = Movement.STAYED_IN;
else
accum = Movement.ENTERED;
break;
case Movement.EXITED:
if (accum === Movement.ENTERED)
accum = Movement.STAYED_IN;
else
accum = Movement.EXITED;
break;
}
i++;
}
return accum;
};
MutationProjection.prototype.getChildlistChange = function (el) {
var change = this.childListChangeMap.get(el);
if (!change) {
change = new ChildListChange();
this.childListChangeMap.set(el, change);
}
return change;
};
MutationProjection.prototype.processChildlistChanges = function () {
if (this.childListChangeMap)
return;
this.childListChangeMap = new NodeMap();
for (var i = 0; i < this.mutations.length; i++) {
var mutation = this.mutations[i];
if (mutation.type != 'childList')
continue;
if (this.treeChanges.reachabilityChange(mutation.target) !== Movement.STAYED_IN &&
!this.calcOldPreviousSibling)
continue;
var change = this.getChildlistChange(mutation.target);
var oldPrevious = mutation.previousSibling;
function recordOldPrevious(node, previous) {
if (!node ||
change.oldPrevious.has(node) ||
change.added.has(node) ||
change.maybeMoved.has(node))
return;
if (previous &&
(change.added.has(previous) ||
change.maybeMoved.has(previous)))
return;
change.oldPrevious.set(node, previous);
}
for (var j = 0; j < mutation.removedNodes.length; j++) {
var node = mutation.removedNodes[j];
recordOldPrevious(node, oldPrevious);
if (change.added.has(node)) {
change.added.delete(node);
}
else {
change.removed.set(node, true);
change.maybeMoved.delete(node);
}
oldPrevious = node;
}
recordOldPrevious(mutation.nextSibling, oldPrevious);
for (var j = 0; j < mutation.addedNodes.length; j++) {
var node = mutation.addedNodes[j];
if (change.removed.has(node)) {
change.removed.delete(node);
change.maybeMoved.set(node, true);
}
else {
change.added.set(node, true);
}
}
}
};
MutationProjection.prototype.wasReordered = function (node) {
if (!this.treeChanges.anyParentsChanged)
return false;
this.processChildlistChanges();
var parentNode = node.parentNode;
var nodeChange = this.treeChanges.get(node);
if (nodeChange && nodeChange.oldParentNode)
parentNode = nodeChange.oldParentNode;
var change = this.childListChangeMap.get(parentNode);
if (!change)
return false;
if (change.moved)
return change.moved.get(node);
change.moved = new NodeMap();
var pendingMoveDecision = new NodeMap();
function isMoved(node) {
if (!node)
return false;
if (!change.maybeMoved.has(node))
return false;
var didMove = change.moved.get(node);
if (didMove !== undefined)
return didMove;
if (pendingMoveDecision.has(node)) {
didMove = true;
}
else {
pendingMoveDecision.set(node, true);
didMove = getPrevious(node) !== getOldPrevious(node);
}
if (pendingMoveDecision.has(node)) {
pendingMoveDecision.delete(node);
change.moved.set(node, didMove);
}
else {
didMove = change.moved.get(node);
}
return didMove;
}
var oldPreviousCache = new NodeMap();
function getOldPrevious(node) {
var oldPrevious = oldPreviousCache.get(node);
if (oldPrevious !== undefined)
return oldPrevious;
oldPrevious = change.oldPrevious.get(node);
while (oldPrevious &&
(change.removed.has(oldPrevious) || isMoved(oldPrevious))) {
oldPrevious = getOldPrevious(oldPrevious);
}
if (oldPrevious === undefined)
oldPrevious = node.previousSibling;
oldPreviousCache.set(node, oldPrevious);
return oldPrevious;
}
var previousCache = new NodeMap();
function getPrevious(node) {
if (previousCache.has(node))
return previousCache.get(node);
var previous = node.previousSibling;
while (previous && (change.added.has(previous) || isMoved(previous)))
previous = previous.previousSibling;
previousCache.set(node, previous);
return previous;
}
change.maybeMoved.keys().forEach(isMoved);
return change.moved.get(node);
};
return MutationProjection;
})();
var Summary = (function () {
function Summary(projection, query) {
var _this = this;
this.projection = projection;
this.added = [];
this.removed = [];
this.reparented = query.all || query.element || query.characterData ? [] : undefined;
this.reordered = query.all ? [] : undefined;
projection.getChanged(this, query.elementFilter, query.characterData);
if (query.all || query.attribute || query.attributeList) {
var filter = query.attribute ? [query.attribute] : query.attributeList;
var attributeChanged = projection.attributeChangedNodes(filter);
if (query.attribute) {
this.valueChanged = attributeChanged[query.attribute] || [];
}
else {
this.attributeChanged = attributeChanged;
if (query.attributeList) {
query.attributeList.forEach(function (attrName) {
if (!_this.attributeChanged.hasOwnProperty(attrName))
_this.attributeChanged[attrName] = [];
});
}
}
}
if (query.all || query.characterData) {
var characterDataChanged = projection.getCharacterDataChanged();
if (query.characterData)
this.valueChanged = characterDataChanged;
else
this.characterDataChanged = characterDataChanged;
}
if (this.reordered)
this.getOldPreviousSibling = projection.getOldPreviousSibling.bind(projection);
}
Summary.prototype.getOldParentNode = function (node) {
return this.projection.getOldParentNode(node);
};
Summary.prototype.getOldAttribute = function (node, name) {
return this.projection.getOldAttribute(node, name);
};
Summary.prototype.getOldCharacterData = function (node) {
return this.projection.getOldCharacterData(node);
};
Summary.prototype.getOldPreviousSibling = function (node) {
return this.projection.getOldPreviousSibling(node);
};
return Summary;
})();
// TODO(rafaelw): Allow ':' and '.' as valid name characters.
var validNameInitialChar = /[a-zA-Z_]+/;
var validNameNonInitialChar = /[a-zA-Z0-9_\-]+/;
// TODO(rafaelw): Consider allowing backslash in the attrValue.
// TODO(rafaelw): There's got a to be way to represent this state machine
// more compactly???
function escapeQuotes(value) {
return '"' + value.replace(/"/, '\\\"') + '"';
}
var Qualifier = (function () {
function Qualifier() {
}
Qualifier.prototype.matches = function (oldValue) {
if (oldValue === null)
return false;
if (this.attrValue === undefined)
return true;
if (!this.contains)
return this.attrValue == oldValue;
var tokens = oldValue.split(' ');
for (var i = 0; i < tokens.length; i++) {
if (this.attrValue === tokens[i])
return true;
}
return false;
};
Qualifier.prototype.toString = function () {
if (this.attrName === 'class' && this.contains)
return '.' + this.attrValue;
if (this.attrName === 'id' && !this.contains)
return '#' + this.attrValue;
if (this.contains)
return '[' + this.attrName + '~=' + escapeQuotes(this.attrValue) + ']';
if ('attrValue' in this)
return '[' + this.attrName + '=' + escapeQuotes(this.attrValue) + ']';
return '[' + this.attrName + ']';
};
return Qualifier;
})();
var Selector = (function () {
function Selector() {
this.uid = Selector.nextUid++;
this.qualifiers = [];
}
Object.defineProperty(Selector.prototype, "caseInsensitiveTagName", {
get: function () {
return this.tagName.toUpperCase();
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "selectorString", {
get: function () {
return this.tagName + this.qualifiers.join('');
},
enumerable: true,
configurable: true
});
Selector.prototype.isMatching = function (el) {
return el[Selector.matchesSelector](this.selectorString);
};
Selector.prototype.wasMatching = function (el, change, isMatching) {
if (!change || !change.attributes)
return isMatching;
var tagName = change.isCaseInsensitive ? this.caseInsensitiveTagName : this.tagName;
if (tagName !== '*' && tagName !== el.tagName)
return false;
var attributeOldValues = [];
var anyChanged = false;
for (var i = 0; i < this.qualifiers.length; i++) {
var qualifier = this.qualifiers[i];
var oldValue = change.getAttributeOldValue(qualifier.attrName);
attributeOldValues.push(oldValue);
anyChanged = anyChanged || (oldValue !== undefined);
}
if (!anyChanged)
return isMatching;
for (var i = 0; i < this.qualifiers.length; i++) {
var qualifier = this.qualifiers[i];
var oldValue = attributeOldValues[i];
if (oldValue === undefined)
oldValue = el.getAttribute(qualifier.attrName);
if (!qualifier.matches(oldValue))
return false;
}
return true;
};
Selector.prototype.matchabilityChange = function (el, change) {
var isMatching = this.isMatching(el);
if (isMatching)
return this.wasMatching(el, change, isMatching) ? Movement.STAYED_IN : Movement.ENTERED;
else
return this.wasMatching(el, change, isMatching) ? Movement.EXITED : Movement.STAYED_OUT;
};
Selector.parseSelectors = function (input) {
var selectors = [];
var currentSelector;
var currentQualifier;
function newSelector() {
if (currentSelector) {
if (currentQualifier) {
currentSelector.qualifiers.push(currentQualifier);
currentQualifier = undefined;
}
selectors.push(currentSelector);
}
currentSelector = new Selector();
}
function newQualifier() {
if (currentQualifier)
currentSelector.qualifiers.push(currentQualifier);
currentQualifier = new Qualifier();
}
var WHITESPACE = /\s/;
var valueQuoteChar;
var SYNTAX_ERROR = 'Invalid or unsupported selector syntax.';
var SELECTOR = 1;
var TAG_NAME = 2;
var QUALIFIER = 3;
var QUALIFIER_NAME_FIRST_CHAR = 4;
var QUALIFIER_NAME = 5;
var ATTR_NAME_FIRST_CHAR = 6;
var ATTR_NAME = 7;
var EQUIV_OR_ATTR_QUAL_END = 8;
var EQUAL = 9;
var ATTR_QUAL_END = 10;
var VALUE_FIRST_CHAR = 11;
var VALUE = 12;
var QUOTED_VALUE = 13;
var SELECTOR_SEPARATOR = 14;
var state = SELECTOR;
var i = 0;
while (i < input.length) {
var c = input[i++];
switch (state) {
case SELECTOR:
if (c.match(validNameInitialChar)) {
newSelector();
currentSelector.tagName = c;
state = TAG_NAME;
break;
}
if (c == '*') {
newSelector();
currentSelector.tagName = '*';
state = QUALIFIER;
break;
}
if (c == '.') {
newSelector();
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '#') {
newSelector();
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = 'id';
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '[') {
newSelector();
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = '';
state = ATTR_NAME_FIRST_CHAR;
break;
}
if (c.match(WHITESPACE))
break;
throw Error(SYNTAX_ERROR);
case TAG_NAME:
if (c.match(validNameNonInitialChar)) {
currentSelector.tagName += c;
break;
}
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '#') {
newQualifier();
currentQualifier.attrName = 'id';
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '[') {
newQualifier();
currentQualifier.attrName = '';
state = ATTR_NAME_FIRST_CHAR;
break;
}
if (c.match(WHITESPACE)) {
state = SELECTOR_SEPARATOR;
break;
}
if (c == ',') {
state = SELECTOR;
break;
}
throw Error(SYNTAX_ERROR);
case QUALIFIER:
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '#') {
newQualifier();
currentQualifier.attrName = 'id';
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '[') {
newQualifier();
currentQualifier.attrName = '';
state = ATTR_NAME_FIRST_CHAR;
break;
}
if (c.match(WHITESPACE)) {
state = SELECTOR_SEPARATOR;
break;
}
if (c == ',') {
state = SELECTOR;
break;
}
throw Error(SYNTAX_ERROR);
case QUALIFIER_NAME_FIRST_CHAR:
if (c.match(validNameInitialChar)) {
currentQualifier.attrValue = c;
state = QUALIFIER_NAME;
break;
}
throw Error(SYNTAX_ERROR);
case QUALIFIER_NAME:
if (c.match(validNameNonInitialChar)) {
currentQualifier.attrValue += c;
break;
}
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '#') {
newQualifier();
currentQualifier.attrName = 'id';
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
if (c == '[') {
newQualifier();
state = ATTR_NAME_FIRST_CHAR;
break;
}
if (c.match(WHITESPACE)) {
state = SELECTOR_SEPARATOR;
break;
}
if (c == ',') {
state = SELECTOR;
break;
}
throw Error(SYNTAX_ERROR);