-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
1857 lines (1551 loc) · 54.9 KB
/
index.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
var neverland = (function (exports) {
'use strict';
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
/*! (c) Andrea Giammarchi - ISC */
var self = {};
try {
self.WeakMap = WeakMap;
} catch (WeakMap) {
// this could be better but 90% of the time
// it's everything developers need as fallback
self.WeakMap = function (id, Object) {
var dP = Object.defineProperty;
var hOP = Object.hasOwnProperty;
var proto = WeakMap.prototype;
proto["delete"] = function (key) {
return this.has(key) && delete key[this._];
};
proto.get = function (key) {
return this.has(key) ? key[this._] : void 0;
};
proto.has = function (key) {
return hOP.call(key, this._);
};
proto.set = function (key, value) {
dP(key, this._, {
configurable: true,
value: value
});
return this;
};
return WeakMap;
function WeakMap(iterable) {
dP(this, '_', {
value: '_@ungap/weakmap' + id++
});
if (iterable) iterable.forEach(add, this);
}
function add(pair) {
this.set(pair[0], pair[1]);
}
}(Math.random(), Object);
}
var WeakMap$1 = self.WeakMap;
/*! (c) Andrea Giammarchi - ISC */
var self$1 = null ||
/* istanbul ignore next */
{};
self$1.CustomEvent = typeof CustomEvent === 'function' ? CustomEvent : function (__p__) {
CustomEvent[__p__] = new CustomEvent('').constructor[__p__];
return CustomEvent;
function CustomEvent(type, init) {
if (!init) init = {};
var e = document.createEvent('CustomEvent');
e.initCustomEvent(type, !!init.bubbles, !!init.cancelable, init.detail);
return e;
}
}('prototype');
var CustomEvent$1 = self$1.CustomEvent;
/*! (c) Andrea Giammarchi - ISC */
var self$2 = null ||
/* istanbul ignore next */
{};
try {
self$2.WeakSet = WeakSet;
} catch (WeakSet) {
// requires a global WeakMap (IE11+)
(function (WeakMap) {
var all = new WeakMap();
var proto = WeakSet.prototype;
proto.add = function (value) {
return all.get(this).set(value, 1), this;
};
proto["delete"] = function (value) {
return all.get(this)["delete"](value);
};
proto.has = function (value) {
return all.get(this).has(value);
};
self$2.WeakSet = WeakSet;
function WeakSet(iterable) {
all.set(this, new WeakMap());
if (iterable) iterable.forEach(this.add, this);
}
})(WeakMap);
}
var WeakSet$1 = self$2.WeakSet;
/*! (c) Andrea Giammarchi */
function disconnected(poly) {
var Event = poly.Event;
var WeakSet = poly.WeakSet;
var notObserving = true;
var observer = null;
return function observe(node) {
if (notObserving) {
notObserving = !notObserving;
observer = new WeakSet();
startObserving(node.ownerDocument);
}
observer.add(node);
return node;
};
function startObserving(document) {
var connected = new WeakSet();
var disconnected = new WeakSet();
try {
new MutationObserver(changes).observe(document, {
subtree: true,
childList: true
});
} catch (o_O) {
var timer = 0;
var records = [];
var reschedule = function reschedule(record) {
records.push(record);
clearTimeout(timer);
timer = setTimeout(function () {
changes(records.splice(timer = 0, records.length));
}, 0);
};
document.addEventListener('DOMNodeRemoved', function (event) {
reschedule({
addedNodes: [],
removedNodes: [event.target]
});
}, true);
document.addEventListener('DOMNodeInserted', function (event) {
reschedule({
addedNodes: [event.target],
removedNodes: []
});
}, true);
}
function changes(records) {
for (var record, length = records.length, i = 0; i < length; i++) {
record = records[i];
dispatchAll(record.removedNodes, 'disconnected', disconnected, connected);
dispatchAll(record.addedNodes, 'connected', connected, disconnected);
}
}
function dispatchAll(nodes, type, wsin, wsout) {
for (var node, event = new Event(type), length = nodes.length, i = 0; i < length; (node = nodes[i++]).nodeType === 1 && dispatchTarget(node, event, type, wsin, wsout)) {
}
}
function dispatchTarget(node, event, type, wsin, wsout) {
if (observer.has(node) && !wsin.has(node)) {
wsout["delete"](node);
wsin.add(node);
node.dispatchEvent(event);
/*
// The event is not bubbling (perf reason: should it?),
// hence there's no way to know if
// stop/Immediate/Propagation() was called.
// Should DOM Level 0 work at all?
// I say it's a YAGNI case for the time being,
// and easy to implement in user-land.
if (!event.cancelBubble) {
var fn = node['on' + type];
if (fn)
fn.call(node, event);
}
*/
}
for (var // apparently is node.children || IE11 ... ^_^;;
// https://github.com/WebReflection/disconnected/issues/1
children = node.children || [], length = children.length, i = 0; i < length; dispatchTarget(children[i++], event, type, wsin, wsout)) {
}
}
}
}
var compat = typeof cancelAnimationFrame === 'function';
var cAF = compat ? cancelAnimationFrame : clearTimeout;
var rAF = compat ? requestAnimationFrame : setTimeout;
function reraf(limit) {
var force, timer, callback, self, args;
reset();
return function reschedule(_callback, _self, _args) {
callback = _callback;
self = _self;
args = _args;
if (!timer) timer = rAF(invoke);
if (--force < 0) stop(true);
return stop;
};
function invoke() {
reset();
callback.apply(self, args || []);
}
function reset() {
force = limit || Infinity;
timer = compat ? 0 : null;
}
function stop(flush) {
var didStop = !!timer;
if (didStop) {
cAF(timer);
if (flush) invoke();
}
return didStop;
}
}
var umap = (function (_) {
return {
// About: get: _.get.bind(_)
// It looks like WebKit/Safari didn't optimize bind at all,
// so that using bind slows it down by 60%.
// Firefox and Chrome are just fine in both cases,
// so let's use the approach that works fast everywhere 👍
get: function get(key) {
return _.get(key);
},
set: function set(key, value) {
return _.set(key, value), value;
}
};
});
/*! (c) Andrea Giammarchi - ISC */
var state = null; // main exports
var augmentor = function augmentor(fn) {
var stack = [];
return function hook() {
var prev = state;
var after = [];
state = {
hook: hook,
args: arguments,
stack: stack,
i: 0,
length: stack.length,
after: after
};
try {
return fn.apply(null, arguments);
} finally {
state = prev;
for (var i = 0, length = after.length; i < length; i++) {
after[i]();
}
}
};
};
var contextual = function contextual(fn) {
var check = true;
var context = null;
var augmented = augmentor(function () {
return fn.apply(context, arguments);
});
return function hook() {
var result = augmented.apply(context = this, arguments); // perform hasEffect check only once
if (check) {
check = !check; // and copy same Array if any FX was used
if (hasEffect(augmented)) effects.set(hook, effects.get(augmented));
}
return result;
};
}; // useReducer
var updates = umap(new WeakMap());
var hookdate = function hookdate(hook, ctx, args) {
hook.apply(ctx, args);
};
var defaults = {
async: false,
always: false
};
var getValue = function getValue(value, f) {
return typeof f == 'function' ? f(value) : f;
};
var useReducer = function useReducer(reducer, value, init, options) {
var i = state.i++;
var _state = state,
hook = _state.hook,
args = _state.args,
stack = _state.stack,
length = _state.length;
if (i === length) state.length = stack.push({});
var ref = stack[i];
ref.args = args;
if (i === length) {
var fn = typeof init === 'function';
var _ref = (fn ? options : init) || options || defaults,
asy = _ref.async,
always = _ref.always;
ref.$ = fn ? init(value) : getValue(void 0, value);
ref._ = asy ? updates.get(hook) || updates.set(hook, reraf()) : hookdate;
ref.f = function (value) {
var $value = reducer(ref.$, value);
if (always || ref.$ !== $value) {
ref.$ = $value;
ref._(hook, null, ref.args);
}
};
}
return [ref.$, ref.f];
}; // useState
var useState = function useState(value, options) {
return useReducer(getValue, value, void 0, options);
}; // useContext
var hooks = new WeakMap();
var invoke = function invoke(_ref2) {
var hook = _ref2.hook,
args = _ref2.args;
hook.apply(null, args);
};
var createContext = function createContext(value) {
var context = {
value: value,
provide: provide
};
hooks.set(context, []);
return context;
};
var useContext = function useContext(context) {
var _state2 = state,
hook = _state2.hook,
args = _state2.args;
var stack = hooks.get(context);
var info = {
hook: hook,
args: args
};
if (!stack.some(update, info)) stack.push(info);
return context.value;
};
function provide(value) {
if (this.value !== value) {
this.value = value;
hooks.get(this).forEach(invoke);
}
}
function update(_ref3) {
var hook = _ref3.hook;
return hook === this.hook;
} // dropEffect, hasEffect, useEffect, useLayoutEffect
var effects = new WeakMap();
var fx = umap(effects);
var stop = function stop() {};
var createEffect = function createEffect(asy) {
return function (effect, guards) {
var i = state.i++;
var _state3 = state,
hook = _state3.hook,
after = _state3.after,
stack = _state3.stack,
length = _state3.length;
if (i < length) {
var info = stack[i];
var _update = info.update,
values = info.values,
_stop = info.stop;
if (!guards || guards.some(different, values)) {
info.values = guards;
if (asy) _stop(asy);
var clean = info.clean;
if (clean) {
info.clean = null;
clean();
}
var _invoke = function _invoke() {
info.clean = effect();
};
if (asy) _update(_invoke);else after.push(_invoke);
}
} else {
var _update2 = asy ? reraf() : stop;
var _info = {
clean: null,
update: _update2,
values: guards,
stop: stop
};
state.length = stack.push(_info);
(fx.get(hook) || fx.set(hook, [])).push(_info);
var _invoke2 = function _invoke2() {
_info.clean = effect();
};
if (asy) _info.stop = _update2(_invoke2);else after.push(_invoke2);
}
};
};
var dropEffect = function dropEffect(hook) {
(effects.get(hook) || []).forEach(function (info) {
var clean = info.clean,
stop = info.stop;
stop();
if (clean) {
info.clean = null;
clean();
}
});
};
var hasEffect = effects.has.bind(effects);
var useEffect = createEffect(true);
var useLayoutEffect = createEffect(false); // useMemo, useCallback
var useMemo = function useMemo(memo, guards) {
var i = state.i++;
var _state4 = state,
stack = _state4.stack,
length = _state4.length;
if (i === length) state.length = stack.push({
$: memo(),
_: guards
});else if (!guards || guards.some(different, stack[i]._)) stack[i] = {
$: memo(),
_: guards
};
return stack[i].$;
};
var useCallback = function useCallback(fn, guards) {
return useMemo(function () {
return fn;
}, guards);
}; // useRef
var useRef = function useRef(value) {
var i = state.i++;
var _state5 = state,
stack = _state5.stack,
length = _state5.length;
if (i === length) state.length = stack.push({
current: value
});
return stack[i];
};
function different(value, i) {
return value !== this[i];
}
/*! (c) Andrea Giammarchi - ISC */
var find = function find(node) {
var firstChild = node.firstChild;
while (firstChild && firstChild.nodeType !== 1) {
firstChild = firstChild.nextSibling;
}
if (firstChild) return firstChild;
throw 'unobservable';
};
var observe = disconnected({
Event: CustomEvent$1,
WeakSet: WeakSet$1
});
var observer = function observer(element, handler) {
var nodeType = element.nodeType;
if (nodeType) {
var node = nodeType === 1 ? element : find(element);
observe(node);
node.addEventListener('disconnected', handler, false);
} else {
var value = element.valueOf(); // give a chance to facades to return a reasonable value
if (value !== element) observer(value, handler);
}
};
var augmentor$1 = function augmentor$1(fn) {
var handler = null;
var hook = augmentor(fn);
return function () {
var node = hook.apply(this, arguments);
if (hasEffect(hook)) observer(node, handler || (handler = dropEffect.bind(null, hook)));
return node;
};
};
var isArray = Array.isArray;
var _ref = [],
slice = _ref.slice;
/*! (c) Andrea Giammarchi - ISC */
// Custom
var UID = '-' + Math.random().toFixed(6) + '%'; // Edge issue!
var UID_IE = false;
try {
if (!function (template, content, tabindex) {
return content in template && (template.innerHTML = '<p ' + tabindex + '="' + UID + '"></p>', template[content].childNodes[0].getAttribute(tabindex) == UID);
}(document.createElement('template'), 'content', 'tabindex')) {
UID = '_dt: ' + UID.slice(1, -1) + ';';
UID_IE = true;
}
} catch (meh) {}
var UIDC = '<!--' + UID + '-->'; // DOM
var COMMENT_NODE = 8;
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var SHOULD_USE_TEXT_CONTENT = /^(?:style|textarea)$/i;
var VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;
/*! (c) Andrea Giammarchi - ISC */
function domsanitizer (template) {
return template.join(UIDC).replace(selfClosing, fullClosing).replace(attrSeeker, attrReplacer);
}
var spaces = ' \\f\\n\\r\\t';
var almostEverything = '[^' + spaces + '\\/>"\'=]+';
var attrName = '[' + spaces + ']+' + almostEverything;
var tagName = '<([A-Za-z]+[A-Za-z0-9:._-]*)((?:';
var attrPartials = '(?:\\s*=\\s*(?:\'[^\']*?\'|"[^"]*?"|<[^>]*?>|' + almostEverything.replace('\\/', '') + '))?)';
var attrSeeker = new RegExp(tagName + attrName + attrPartials + '+)([' + spaces + ']*/?>)', 'g');
var selfClosing = new RegExp(tagName + attrName + attrPartials + '*)([' + spaces + ']*/>)', 'g');
var findAttributes = new RegExp('(' + attrName + '\\s*=\\s*)([\'"]?)' + UIDC + '\\2', 'gi');
function attrReplacer($0, $1, $2, $3) {
return '<' + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
}
function replaceAttributes($0, $1, $2) {
return $1 + ($2 || '"') + UID + ($2 || '"');
}
function fullClosing($0, $1, $2) {
return VOID_ELEMENTS.test($1) ? $0 : '<' + $1 + $2 + '></' + $1 + '>';
}
var ELEMENT_NODE$1 = 1;
var nodeType = 111;
var remove = function remove(_ref) {
var firstChild = _ref.firstChild,
lastChild = _ref.lastChild;
var range = document.createRange();
range.setStartAfter(firstChild);
range.setEndAfter(lastChild);
range.deleteContents();
return firstChild;
};
var diffable = function diffable(node, operation) {
return node.nodeType === nodeType ? 1 / operation < 0 ? operation ? remove(node) : node.lastChild : operation ? node.valueOf() : node.firstChild : node;
};
var persistent = function persistent(fragment) {
var childNodes = fragment.childNodes;
var length = childNodes.length;
if (length < 2) return length ? childNodes[0] : fragment;
var nodes = slice.call(childNodes, 0);
var firstChild = nodes[0];
var lastChild = nodes[length - 1];
return {
ELEMENT_NODE: ELEMENT_NODE$1,
nodeType: nodeType,
firstChild: firstChild,
lastChild: lastChild,
valueOf: function valueOf() {
if (childNodes.length !== length) {
var i = 0;
while (i < length) {
fragment.appendChild(nodes[i++]);
}
}
return fragment;
}
};
};
/*! (c) Andrea Giammarchi - ISC */
var createContent = function (document) {
var FRAGMENT = 'fragment';
var TEMPLATE = 'template';
var HAS_CONTENT = ('content' in create(TEMPLATE));
var createHTML = HAS_CONTENT ? function (html) {
var template = create(TEMPLATE);
template.innerHTML = html;
return template.content;
} : function (html) {
var content = create(FRAGMENT);
var template = create(TEMPLATE);
var childNodes = null;
if (/^[^\S]*?<(col(?:group)?|t(?:head|body|foot|r|d|h))/i.test(html)) {
var selector = RegExp.$1;
template.innerHTML = '<table>' + html + '</table>';
childNodes = template.querySelectorAll(selector);
} else {
template.innerHTML = html;
childNodes = template.childNodes;
}
append(content, childNodes);
return content;
};
return function createContent(markup, type) {
return (type === 'svg' ? createSVG : createHTML)(markup);
};
function append(root, childNodes) {
var length = childNodes.length;
while (length--) {
root.appendChild(childNodes[0]);
}
}
function create(element) {
return element === FRAGMENT ? document.createDocumentFragment() : document.createElementNS('http://www.w3.org/1999/xhtml', element);
} // it could use createElementNS when hasNode is there
// but this fallback is equally fast and easier to maintain
// it is also battle tested already in all IE
function createSVG(svg) {
var content = create(FRAGMENT);
var template = create('div');
template.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';
append(content, template.firstChild.childNodes);
return content;
}
}(document);
/**
* ISC License
*
* Copyright (c) 2020, Andrea Giammarchi, @WebReflection
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* @param {Node} parentNode The container where children live
* @param {Node[]} a The list of current/live children
* @param {Node[]} b The list of future children
* @param {(entry: Node, action: number) => Node} get
* The callback invoked per each entry related DOM operation.
* @param {Node} [before] The optional node used as anchor to insert before.
* @returns {Node[]} The same list of future children.
*/
var udomdiff = (function (parentNode, a, b, get, before) {
var bLength = b.length;
var aEnd = a.length;
var bEnd = bLength;
var aStart = 0;
var bStart = 0;
var map = null;
while (aStart < aEnd || bStart < bEnd) {
// append head, tail, or nodes in between: fast path
if (aEnd === aStart) {
// we could be in a situation where the rest of nodes that
// need to be added are not at the end, and in such case
// the node to `insertBefore`, if the index is more than 0
// must be retrieved, otherwise it's gonna be the first item.
var node = bEnd < bLength ? bStart ? get(b[bStart - 1], -0).nextSibling : get(b[bEnd - bStart], 0) : before;
while (bStart < bEnd) {
parentNode.insertBefore(get(b[bStart++], 1), node);
}
} // remove head or tail: fast path
else if (bEnd === bStart) {
while (aStart < aEnd) {
// remove the node only if it's unknown or not live
if (!map || !map.has(a[aStart])) parentNode.removeChild(get(a[aStart], -1));
aStart++;
}
} // same node: fast path
else if (a[aStart] === b[bStart]) {
aStart++;
bStart++;
} // same tail: fast path
else if (a[aEnd - 1] === b[bEnd - 1]) {
aEnd--;
bEnd--;
} // The once here single last swap "fast path" has been removed in v1.1.0
// https://github.com/WebReflection/udomdiff/blob/single-final-swap/esm/index.js#L69-L85
// reverse swap: also fast path
else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
// this is a "shrink" operation that could happen in these cases:
// [1, 2, 3, 4, 5]
// [1, 4, 3, 2, 5]
// or asymmetric too
// [1, 2, 3, 4, 5]
// [1, 2, 3, 5, 6, 4]
var _node = get(a[--aEnd], -1).nextSibling;
parentNode.insertBefore(get(b[bStart++], 1), get(a[aStart++], -1).nextSibling);
parentNode.insertBefore(get(b[--bEnd], 1), _node); // mark the future index as identical (yeah, it's dirty, but cheap 👍)
// The main reason to do this, is that when a[aEnd] will be reached,
// the loop will likely be on the fast path, as identical to b[bEnd].
// In the best case scenario, the next loop will skip the tail,
// but in the worst one, this node will be considered as already
// processed, bailing out pretty quickly from the map index check
a[aEnd] = b[bEnd];
} // map based fallback, "slow" path
else {
// the map requires an O(bEnd - bStart) operation once
// to store all future nodes indexes for later purposes.
// In the worst case scenario, this is a full O(N) cost,
// and such scenario happens at least when all nodes are different,
// but also if both first and last items of the lists are different
if (!map) {
map = new Map();
var i = bStart;
while (i < bEnd) {
map.set(b[i], i++);
}
} // if it's a future node, hence it needs some handling
if (map.has(a[aStart])) {
// grab the index of such node, 'cause it might have been processed
var index = map.get(a[aStart]); // if it's not already processed, look on demand for the next LCS
if (bStart < index && index < bEnd) {
var _i = aStart; // counts the amount of nodes that are the same in the future
var sequence = 1;
while (++_i < aEnd && _i < bEnd && map.get(a[_i]) === index + sequence) {
sequence++;
} // effort decision here: if the sequence is longer than replaces
// needed to reach such sequence, which would brings again this loop
// to the fast path, prepend the difference before a sequence,
// and move only the future list index forward, so that aStart
// and bStart will be aligned again, hence on the fast path.
// An example considering aStart and bStart are both 0:
// a: [1, 2, 3, 4]
// b: [7, 1, 2, 3, 6]
// this would place 7 before 1 and, from that time on, 1, 2, and 3
// will be processed at zero cost
if (sequence > index - bStart) {
var _node2 = get(a[aStart], 0);
while (bStart < index) {
parentNode.insertBefore(get(b[bStart++], 1), _node2);
}
} // if the effort wasn't good enough, fallback to a replace,
// moving both source and target indexes forward, hoping that some
// similar node will be found later on, to go back to the fast path
else {
parentNode.replaceChild(get(b[bStart++], 1), get(a[aStart++], -1));
}
} // otherwise move the source forward, 'cause there's nothing to do
else aStart++;
} // this node has no meaning in the future list, so it's more than safe
// to remove it, and check the next live node out instead, meaning
// that only the live list index should be forwarded
else parentNode.removeChild(get(a[aStart++], -1));
}
}
return b;
});
/*! (c) Andrea Giammarchi - ISC */
var importNode = function (document, appendChild, cloneNode, createTextNode, importNode) {
var _native = (importNode in document); // IE 11 has problems with cloning templates:
// it "forgets" empty childNodes. This feature-detects that.
var fragment = document.createDocumentFragment();
fragment[appendChild](document[createTextNode]('g'));
fragment[appendChild](document[createTextNode](''));
/* istanbul ignore next */
var content = _native ? document[importNode](fragment, true) : fragment[cloneNode](true);
return content.childNodes.length < 2 ? function importNode(node, deep) {
var clone = node[cloneNode]();
for (var
/* istanbul ignore next */
childNodes = node.childNodes || [], length = childNodes.length, i = 0; deep && i < length; i++) {
clone[appendChild](importNode(childNodes[i], deep));
}
return clone;
} :
/* istanbul ignore next */
_native ? document[importNode] : function (node, deep) {
return node[cloneNode](!!deep);
};
}(document, 'appendChild', 'cloneNode', 'createTextNode', 'importNode');
var trim = ''.trim ||
/* istanbul ignore next */
function () {
return String(this).replace(/^\s+|\s+/g, '');
};
/* istanbul ignore next */
var normalizeAttributes = UID_IE ? function (attributes, parts) {
var html = parts.join(' ');
return parts.slice.call(attributes, 0).sort(function (left, right) {
return html.indexOf(left.name) <= html.indexOf(right.name) ? -1 : 1;
});
} : function (attributes, parts) {
return parts.slice.call(attributes, 0);
};
function find$1(node, path) {
var length = path.length;
var i = 0;
while (i < length) {
node = node.childNodes[path[i++]];
}
return node;
}
function parse(node, holes, parts, path) {
var childNodes = node.childNodes;
var length = childNodes.length;
var i = 0;
while (i < length) {
var child = childNodes[i];
switch (child.nodeType) {
case ELEMENT_NODE:
var childPath = path.concat(i);
parseAttributes(child, holes, parts, childPath);
parse(child, holes, parts, childPath);
break;
case COMMENT_NODE:
var textContent = child.textContent;
if (textContent === UID) {
parts.shift();
holes.push( // basicHTML or other non standard engines
// might end up having comments in nodes
// where they shouldn't, hence this check.
SHOULD_USE_TEXT_CONTENT.test(node.nodeName) ? Text(node, path) : Any(child, path.concat(i)));
} else {
switch (textContent.slice(0, 2)) {
case '/*':
if (textContent.slice(-2) !== '*/') break;
case "\uD83D\uDC7B":
// ghost
node.removeChild(child);
i--;
length--;
}
}
break;
case TEXT_NODE:
// the following ignore is actually covered by browsers
// only basicHTML ends up on previous COMMENT_NODE case
// instead of TEXT_NODE because it knows nothing about
// special style or textarea behavior
/* istanbul ignore if */
if (SHOULD_USE_TEXT_CONTENT.test(node.nodeName) && trim.call(child.textContent) === UIDC) {
parts.shift();
holes.push(Text(node, path));
}
break;
}
i++;
}
}
function parseAttributes(node, holes, parts, path) {
var attributes = node.attributes;
var cache = [];
var remove = [];
var array = normalizeAttributes(attributes, parts);
var length = array.length;
var i = 0;