hyperchad_renderer_vanilla_js 0.3.0

HyperChad Vanilla JS HTML renderer package
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
/**
 * @typedef {object} ConfigHead
 *
 * @property {'merge' | 'append' | 'morph' | 'none'} [style]
 * @property {boolean} [block]
 * @property {boolean} [ignore]
 * @property {function(Element): boolean} [shouldPreserve]
 * @property {function(Element): boolean} [shouldReAppend]
 * @property {function(Element): boolean} [shouldRemove]
 * @property {function(Element, {added: Node[], kept: Element[], removed: Element[]}): void} [afterHeadMorphed]
 */

/**
 * @typedef {object} ConfigCallbacks
 *
 * @property {function(Node): boolean} [beforeNodeAdded]
 * @property {function(Node): void} [afterNodeAdded]
 * @property {function(Element, Node): boolean} [beforeNodeMorphed]
 * @property {function(Element, Node): void} [afterNodeMorphed]
 * @property {function(Element): boolean} [beforeNodeRemoved]
 * @property {function(Element): void} [afterNodeRemoved]
 * @property {function(string, Element, "update" | "remove"): boolean} [beforeAttributeUpdated]
 */

/**
 * @typedef {object} Config
 *
 * @property {'outerHTML' | 'innerHTML'} [morphStyle]
 * @property {boolean} [ignoreActive]
 * @property {boolean} [ignoreActiveValue]
 * @property {boolean} [restoreFocus]
 * @property {ConfigCallbacks} [callbacks]
 * @property {ConfigHead} [head]
 */

/**
 * @typedef {function} NoOp
 *
 * @returns {void}
 */

/**
 * @typedef {object} ConfigHeadInternal
 *
 * @property {'merge' | 'append' | 'morph' | 'none'} style
 * @property {boolean} [block]
 * @property {boolean} [ignore]
 * @property {(function(Element): boolean) | NoOp} shouldPreserve
 * @property {(function(Element): boolean) | NoOp} shouldReAppend
 * @property {(function(Element): boolean) | NoOp} shouldRemove
 * @property {(function(Element, {added: Node[], kept: Element[], removed: Element[]}): void) | NoOp} afterHeadMorphed
 */

/**
 * @typedef {object} ConfigCallbacksInternal
 *
 * @property {(function(Node): boolean) | NoOp} beforeNodeAdded
 * @property {(function(Node): void) | NoOp} afterNodeAdded
 * @property {(function(Node, Node): boolean) | NoOp} beforeNodeMorphed
 * @property {(function(Node, Node): void) | NoOp} afterNodeMorphed
 * @property {(function(Node): boolean) | NoOp} beforeNodeRemoved
 * @property {(function(Node): void) | NoOp} afterNodeRemoved
 * @property {(function(string, Element, "update" | "remove"): boolean) | NoOp} beforeAttributeUpdated
 */

/**
 * @typedef {object} ConfigInternal
 *
 * @property {'outerHTML' | 'innerHTML'} morphStyle
 * @property {boolean} [ignoreActive]
 * @property {boolean} [ignoreActiveValue]
 * @property {boolean} [restoreFocus]
 * @property {ConfigCallbacksInternal} callbacks
 * @property {ConfigHeadInternal} head
 */

/**
 * @typedef {Object} IdSets
 * @property {Set<string>} persistentIds
 * @property {Map<Node, Set<string>>} idMap
 */

/**
 * @typedef {Function} Morph
 *
 * @param {Element | Document} oldNode
 * @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
 * @param {Config} [config]
 * @returns {undefined | Node[]}
 */

// base IIFE to define idiomorph
/**
 *
 * @type {{defaults: ConfigInternal, morph: Morph}}
 */
var Idiomorph = (function () {
    'use strict';

    /**
     * @typedef {object} MorphContext
     *
     * @property {Element} target
     * @property {Element} newContent
     * @property {ConfigInternal} config
     * @property {ConfigInternal['morphStyle']} morphStyle
     * @property {ConfigInternal['ignoreActive']} ignoreActive
     * @property {ConfigInternal['ignoreActiveValue']} ignoreActiveValue
     * @property {ConfigInternal['restoreFocus']} restoreFocus
     * @property {Map<Node, Set<string>>} idMap
     * @property {Set<string>} persistentIds
     * @property {ConfigInternal['callbacks']} callbacks
     * @property {ConfigInternal['head']} head
     * @property {HTMLDivElement} pantry
     */

    //=============================================================================
    // AND NOW IT BEGINS...
    //=============================================================================

    const noOp = () => {};
    /**
     * Default configuration values, updatable by users now
     * @type {ConfigInternal}
     */
    const defaults = {
        morphStyle: 'outerHTML',
        callbacks: {
            beforeNodeAdded: noOp,
            afterNodeAdded: noOp,
            beforeNodeMorphed: noOp,
            afterNodeMorphed: noOp,
            beforeNodeRemoved: noOp,
            afterNodeRemoved: noOp,
            beforeAttributeUpdated: noOp,
        },
        head: {
            style: 'merge',
            shouldPreserve: (elt) => elt.getAttribute('im-preserve') === 'true',
            shouldReAppend: (elt) =>
                elt.getAttribute('im-re-append') === 'true',
            shouldRemove: noOp,
            afterHeadMorphed: noOp,
        },
        restoreFocus: true,
    };

    /**
     * Core idiomorph function for morphing one DOM tree to another
     *
     * @param {Element | Document} oldNode
     * @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
     * @param {Config} [config]
     * @returns {Promise<Node[]> | Node[]}
     */
    function morph(oldNode, newContent, config = {}) {
        oldNode = normalizeElement(oldNode);
        const newNode = normalizeParent(newContent);
        const ctx = createMorphContext(oldNode, newNode, config);

        const morphedNodes = saveAndRestoreFocus(ctx, () => {
            return withHeadBlocking(
                ctx,
                oldNode,
                newNode,
                /** @param {MorphContext} ctx */ (ctx) => {
                    if (ctx.morphStyle === 'innerHTML') {
                        morphChildren(ctx, oldNode, newNode);
                        return Array.from(oldNode.childNodes);
                    } else {
                        return morphOuterHTML(ctx, oldNode, newNode);
                    }
                },
            );
        });

        ctx.pantry.remove();
        return morphedNodes;
    }

    /**
     * Morph just the outerHTML of the oldNode to the newContent
     * We have to be careful because the oldNode could have siblings which need to be untouched
     * @param {MorphContext} ctx
     * @param {Element} oldNode
     * @param {Element} newNode
     * @returns {Node[]}
     */
    function morphOuterHTML(ctx, oldNode, newNode) {
        const oldParent = normalizeParent(oldNode);

        // basis for calulating which nodes were morphed
        // since there may be unmorphed sibling nodes
        let childNodes = Array.from(oldParent.childNodes);
        const index = childNodes.indexOf(oldNode);
        // how many elements are to the right of the oldNode
        const rightMargin = childNodes.length - (index + 1);

        morphChildren(
            ctx,
            oldParent,
            newNode,
            // these two optional params are the secret sauce
            oldNode, // start point for iteration
            oldNode.nextSibling, // end point for iteration
        );

        // return just the morphed nodes
        childNodes = Array.from(oldParent.childNodes);
        return childNodes.slice(index, childNodes.length - rightMargin);
    }

    /**
     * @param {MorphContext} ctx
     * @param {Function} fn
     * @returns {Promise<Node[]> | Node[]}
     */
    function saveAndRestoreFocus(ctx, fn) {
        if (!ctx.config.restoreFocus) return fn();
        let activeElement =
            /** @type {HTMLInputElement|HTMLTextAreaElement|null} */ (
                document.activeElement
            );

        // don't bother if the active element is not an input or textarea
        if (
            !(
                activeElement instanceof HTMLInputElement ||
                activeElement instanceof HTMLTextAreaElement
            )
        ) {
            return fn();
        }

        const {
            id: activeElementId,
            selectionStart,
            selectionEnd,
        } = activeElement;

        const results = fn();

        if (activeElementId && activeElementId !== document.activeElement?.id) {
            activeElement = ctx.target.querySelector(`#${activeElementId}`);
            activeElement?.focus();
        }
        if (activeElement && !activeElement.selectionEnd && selectionEnd) {
            activeElement.setSelectionRange(selectionStart, selectionEnd);
        }

        return results;
    }

    const morphChildren = (function () {
        /**
         * This is the core algorithm for matching up children.  The idea is to use id sets to try to match up
         * nodes as faithfully as possible.  We greedily match, which allows us to keep the algorithm fast, but
         * by using id sets, we are able to better match up with content deeper in the DOM.
         *
         * Basic algorithm:
         * - for each node in the new content:
         *   - search self and siblings for an id set match, falling back to a soft match
         *   - if match found
         *     - remove any nodes up to the match:
         *       - pantry persistent nodes
         *       - delete the rest
         *     - morph the match
         *   - elsif no match found, and node is persistent
         *     - find its match by querying the old root (future) and pantry (past)
         *     - move it and its children here
         *     - morph it
         *   - else
         *     - create a new node from scratch as a last result
         *
         * @param {MorphContext} ctx the merge context
         * @param {Element} oldParent the old content that we are merging the new content into
         * @param {Element} newParent the parent element of the new content
         * @param {Node|null} [insertionPoint] the point in the DOM we start morphing at (defaults to first child)
         * @param {Node|null} [endPoint] the point in the DOM we stop morphing at (defaults to after last child)
         */
        function morphChildren(
            ctx,
            oldParent,
            newParent,
            insertionPoint = null,
            endPoint = null,
        ) {
            // normalize
            if (
                oldParent instanceof HTMLTemplateElement &&
                newParent instanceof HTMLTemplateElement
            ) {
                // @ts-ignore we can pretend the DocumentFragment is an Element
                oldParent = oldParent.content;
                // @ts-ignore ditto
                newParent = newParent.content;
            }
            insertionPoint ||= oldParent.firstChild;

            // run through all the new content
            for (const newChild of newParent.childNodes) {
                // once we reach the end of the old parent content skip to the end and insert the rest
                if (insertionPoint && insertionPoint != endPoint) {
                    const bestMatch = findBestMatch(
                        ctx,
                        newChild,
                        insertionPoint,
                        endPoint,
                    );
                    if (bestMatch) {
                        // if the node to morph is not at the insertion point then remove/move up to it
                        if (bestMatch !== insertionPoint) {
                            removeNodesBetween(ctx, insertionPoint, bestMatch);
                        }
                        morphNode(bestMatch, newChild, ctx);
                        insertionPoint = bestMatch.nextSibling;
                        continue;
                    }
                }

                // if the matching node is elsewhere in the original content
                if (
                    newChild instanceof Element &&
                    ctx.persistentIds.has(newChild.id)
                ) {
                    // move it and all its children here and morph
                    const movedChild = moveBeforeById(
                        oldParent,
                        newChild.id,
                        insertionPoint,
                        ctx,
                    );
                    morphNode(movedChild, newChild, ctx);
                    insertionPoint = movedChild.nextSibling;
                    continue;
                }

                // last resort: insert the new node from scratch
                const insertedNode = createNode(
                    oldParent,
                    newChild,
                    insertionPoint,
                    ctx,
                );
                // could be null if beforeNodeAdded prevented insertion
                if (insertedNode) {
                    insertionPoint = insertedNode.nextSibling;
                }
            }

            // remove any remaining old nodes that didn't match up with new content
            while (insertionPoint && insertionPoint != endPoint) {
                const tempNode = insertionPoint;
                insertionPoint = insertionPoint.nextSibling;
                removeNode(ctx, tempNode);
            }
        }

        /**
         * This performs the action of inserting a new node while handling situations where the node contains
         * elements with persistent ids and possible state info we can still preserve by moving in and then morphing
         *
         * @param {Element} oldParent
         * @param {Node} newChild
         * @param {Node|null} insertionPoint
         * @param {MorphContext} ctx
         * @returns {Node|null}
         */
        function createNode(oldParent, newChild, insertionPoint, ctx) {
            if (ctx.callbacks.beforeNodeAdded(newChild) === false) return null;
            if (ctx.idMap.has(newChild)) {
                // node has children with ids with possible state so create a dummy elt of same type and apply full morph algorithm
                const newEmptyChild = document.createElement(
                    /** @type {Element} */ (newChild).tagName,
                );
                oldParent.insertBefore(newEmptyChild, insertionPoint);
                morphNode(newEmptyChild, newChild, ctx);
                ctx.callbacks.afterNodeAdded(newEmptyChild);
                return newEmptyChild;
            } else {
                // optimisation: no id state to preserve so we can just insert a clone of the newChild and its descendants
                const newClonedChild = document.importNode(newChild, true); // importNode to not mutate newParent
                oldParent.insertBefore(newClonedChild, insertionPoint);
                ctx.callbacks.afterNodeAdded(newClonedChild);
                return newClonedChild;
            }
        }

        //=============================================================================
        // Matching Functions
        //=============================================================================
        const findBestMatch = (function () {
            /**
             * Scans forward from the startPoint to the endPoint looking for a match
             * for the node. It looks for an id set match first, then a soft match.
             * We abort softmatching if we find two future soft matches, to reduce churn.
             * @param {Node} node
             * @param {MorphContext} ctx
             * @param {Node | null} startPoint
             * @param {Node | null} endPoint
             * @returns {Node | null}
             */
            function findBestMatch(ctx, node, startPoint, endPoint) {
                let softMatch = null;
                let nextSibling = node.nextSibling;
                let siblingSoftMatchCount = 0;

                let cursor = startPoint;
                while (cursor && cursor != endPoint) {
                    // soft matching is a prerequisite for id set matching
                    if (isSoftMatch(cursor, node)) {
                        if (isIdSetMatch(ctx, cursor, node)) {
                            return cursor; // found an id set match, we're done!
                        }

                        // we haven't yet saved a soft match fallback
                        if (softMatch === null) {
                            // the current soft match will hard match something else in the future, leave it
                            if (!ctx.idMap.has(cursor)) {
                                // save this as the fallback if we get through the loop without finding a hard match
                                softMatch = cursor;
                            }
                        }
                    }
                    if (
                        softMatch === null &&
                        nextSibling &&
                        isSoftMatch(cursor, nextSibling)
                    ) {
                        // The next new node has a soft match with this node, so
                        // increment the count of future soft matches
                        siblingSoftMatchCount++;
                        nextSibling = nextSibling.nextSibling;

                        // If there are two future soft matches, block soft matching for this node to allow
                        // future siblings to soft match. This is to reduce churn in the DOM when an element
                        // is prepended.
                        if (siblingSoftMatchCount >= 2) {
                            softMatch = undefined;
                        }
                    }

                    // if the current node contains active element, stop looking for better future matches,
                    // because if one is found, this node will be moved to the pantry, reparenting it and thus losing focus
                    if (cursor.contains(document.activeElement)) break;

                    cursor = cursor.nextSibling;
                }

                return softMatch || null;
            }

            /**
             *
             * @param {MorphContext} ctx
             * @param {Node} oldNode
             * @param {Node} newNode
             * @returns {boolean}
             */
            function isIdSetMatch(ctx, oldNode, newNode) {
                let oldSet = ctx.idMap.get(oldNode);
                let newSet = ctx.idMap.get(newNode);

                if (!newSet || !oldSet) return false;

                for (const id of oldSet) {
                    // a potential match is an id in the new and old nodes that
                    // has not already been merged into the DOM
                    // But the newNode content we call this on has not been
                    // merged yet and we don't allow duplicate IDs so it is simple
                    if (newSet.has(id)) {
                        return true;
                    }
                }
                return false;
            }

            /**
             *
             * @param {Node} oldNode
             * @param {Node} newNode
             * @returns {boolean}
             */
            function isSoftMatch(oldNode, newNode) {
                // ok to cast: if one is not element, `id` and `tagName` will be undefined and we'll just compare that.
                const oldElt = /** @type {Element} */ (oldNode);
                const newElt = /** @type {Element} */ (newNode);

                return (
                    oldElt.nodeType === newElt.nodeType &&
                    oldElt.tagName === newElt.tagName &&
                    // If oldElt has an `id` with possible state and it doesn't match newElt.id then avoid morphing.
                    // We'll still match an anonymous node with an IDed newElt, though, because if it got this far,
                    // its not persistent, and new nodes can't have any hidden state.
                    (!oldElt.id || oldElt.id === newElt.id)
                );
            }

            return findBestMatch;
        })();

        //=============================================================================
        // DOM Manipulation Functions
        //=============================================================================

        /**
         * Gets rid of an unwanted DOM node; strategy depends on nature of its reuse:
         * - Persistent nodes will be moved to the pantry for later reuse
         * - Other nodes will have their hooks called, and then are removed
         * @param {MorphContext} ctx
         * @param {Node} node
         */
        function removeNode(ctx, node) {
            // are we going to id set match this later?
            if (ctx.idMap.has(node)) {
                // skip callbacks and move to pantry
                moveBefore(ctx.pantry, node, null);
            } else {
                // remove for realsies
                if (ctx.callbacks.beforeNodeRemoved(node) === false) return;
                node.parentNode?.removeChild(node);
                ctx.callbacks.afterNodeRemoved(node);
            }
        }

        /**
         * Remove nodes between the start and end nodes
         * @param {MorphContext} ctx
         * @param {Node} startInclusive
         * @param {Node} endExclusive
         * @returns {Node|null}
         */
        function removeNodesBetween(ctx, startInclusive, endExclusive) {
            /** @type {Node | null} */
            let cursor = startInclusive;
            // remove nodes until the endExclusive node
            while (cursor && cursor !== endExclusive) {
                let tempNode = /** @type {Node} */ (cursor);
                cursor = cursor.nextSibling;
                removeNode(ctx, tempNode);
            }
            return cursor;
        }

        /**
         * Search for an element by id within the document and pantry, and move it using moveBefore.
         *
         * @param {Element} parentNode - The parent node to which the element will be moved.
         * @param {string} id - The ID of the element to be moved.
         * @param {Node | null} after - The reference node to insert the element before.
         *                              If `null`, the element is appended as the last child.
         * @param {MorphContext} ctx
         * @returns {Element} The found element
         */
        function moveBeforeById(parentNode, id, after, ctx) {
            const target =
                /** @type {Element} - will always be found */
                (
                    ctx.target.querySelector(`#${id}`) ||
                        ctx.pantry.querySelector(`#${id}`)
                );
            removeElementFromAncestorsIdMaps(target, ctx);
            moveBefore(parentNode, target, after);
            return target;
        }

        /**
         * Removes an element from its ancestors' id maps. This is needed when an element is moved from the
         * "future" via `moveBeforeId`. Otherwise, its erstwhile ancestors could be mistakenly moved to the
         * pantry rather than being deleted, preventing their removal hooks from being called.
         *
         * @param {Element} element - element to remove from its ancestors' id maps
         * @param {MorphContext} ctx
         */
        function removeElementFromAncestorsIdMaps(element, ctx) {
            const id = element.id;
            /** @ts-ignore - safe to loop in this way **/
            while ((element = element.parentNode)) {
                let idSet = ctx.idMap.get(element);
                if (idSet) {
                    idSet.delete(id);
                    if (!idSet.size) {
                        ctx.idMap.delete(element);
                    }
                }
            }
        }

        /**
         * Moves an element before another element within the same parent.
         * Uses the proposed `moveBefore` API if available (and working), otherwise falls back to `insertBefore`.
         * This is essentialy a forward-compat wrapper.
         *
         * @param {Element} parentNode - The parent node containing the after element.
         * @param {Node} element - The element to be moved.
         * @param {Node | null} after - The reference node to insert `element` before.
         *                              If `null`, `element` is appended as the last child.
         */
        function moveBefore(parentNode, element, after) {
            // @ts-ignore - use proposed moveBefore feature
            if (parentNode.moveBefore) {
                try {
                    // @ts-ignore - use proposed moveBefore feature
                    parentNode.moveBefore(element, after);
                } catch {
                    // fall back to insertBefore as some browsers may fail on moveBefore when trying to move Dom disconnected nodes to pantry
                    parentNode.insertBefore(element, after);
                }
            } else {
                parentNode.insertBefore(element, after);
            }
        }

        return morphChildren;
    })();

    //=============================================================================
    // Single Node Morphing Code
    //=============================================================================
    const morphNode = (function () {
        /**
         * @param {Node} oldNode root node to merge content into
         * @param {Node} newContent new content to merge
         * @param {MorphContext} ctx the merge context
         * @returns {Node | null} the element that ended up in the DOM
         */
        function morphNode(oldNode, newContent, ctx) {
            if (ctx.ignoreActive && oldNode === document.activeElement) {
                // don't morph focused element
                return null;
            }

            if (
                ctx.callbacks.beforeNodeMorphed(oldNode, newContent) === false
            ) {
                return oldNode;
            }

            if (oldNode instanceof HTMLHeadElement && ctx.head.ignore) {
                // ignore the head element
            } else if (
                oldNode instanceof HTMLHeadElement &&
                ctx.head.style !== 'morph'
            ) {
                // ok to cast: if newContent wasn't also a <head>, it would've got caught in the `!isSoftMatch` branch above
                handleHeadElement(
                    oldNode,
                    /** @type {HTMLHeadElement} */ (newContent),
                    ctx,
                );
            } else {
                morphAttributes(oldNode, newContent, ctx);
                if (!ignoreValueOfActiveElement(oldNode, ctx)) {
                    // @ts-ignore newContent can be a node here because .firstChild will be null
                    morphChildren(ctx, oldNode, newContent);
                }
            }
            ctx.callbacks.afterNodeMorphed(oldNode, newContent);
            return oldNode;
        }

        /**
         * syncs the oldNode to the newNode, copying over all attributes and
         * inner element state from the newNode to the oldNode
         *
         * @param {Node} oldNode the node to copy attributes & state to
         * @param {Node} newNode the node to copy attributes & state from
         * @param {MorphContext} ctx the merge context
         */
        function morphAttributes(oldNode, newNode, ctx) {
            let type = newNode.nodeType;

            // if is an element type, sync the attributes from the
            // new node into the new node
            if (type === 1 /* element type */) {
                const oldElt = /** @type {Element} */ (oldNode);
                const newElt = /** @type {Element} */ (newNode);

                const oldAttributes = oldElt.attributes;
                const newAttributes = newElt.attributes;
                for (const newAttribute of newAttributes) {
                    if (
                        ignoreAttribute(
                            newAttribute.name,
                            oldElt,
                            'update',
                            ctx,
                        )
                    ) {
                        continue;
                    }
                    if (
                        oldElt.getAttribute(newAttribute.name) !==
                        newAttribute.value
                    ) {
                        oldElt.setAttribute(
                            newAttribute.name,
                            newAttribute.value,
                        );
                    }
                }
                // iterate backwards to avoid skipping over items when a delete occurs
                for (let i = oldAttributes.length - 1; 0 <= i; i--) {
                    const oldAttribute = oldAttributes[i];

                    // toAttributes is a live NamedNodeMap, so iteration+mutation is unsafe
                    // e.g. custom element attribute callbacks can remove other attributes
                    if (!oldAttribute) continue;

                    if (!newElt.hasAttribute(oldAttribute.name)) {
                        if (
                            ignoreAttribute(
                                oldAttribute.name,
                                oldElt,
                                'remove',
                                ctx,
                            )
                        ) {
                            continue;
                        }
                        oldElt.removeAttribute(oldAttribute.name);
                    }
                }

                if (!ignoreValueOfActiveElement(oldElt, ctx)) {
                    syncInputValue(oldElt, newElt, ctx);
                }
            }

            // sync text nodes
            if (type === 8 /* comment */ || type === 3 /* text */) {
                if (oldNode.nodeValue !== newNode.nodeValue) {
                    oldNode.nodeValue = newNode.nodeValue;
                }
            }
        }

        /**
         * NB: many bothans died to bring us information:
         *
         *  https://github.com/patrick-steele-idem/morphdom/blob/master/src/specialElHandlers.js
         *  https://github.com/choojs/nanomorph/blob/master/lib/morph.jsL113
         *
         * @param {Element} oldElement the element to sync the input value to
         * @param {Element} newElement the element to sync the input value from
         * @param {MorphContext} ctx the merge context
         */
        function syncInputValue(oldElement, newElement, ctx) {
            if (
                oldElement instanceof HTMLInputElement &&
                newElement instanceof HTMLInputElement &&
                newElement.type !== 'file'
            ) {
                let newValue = newElement.value;
                let oldValue = oldElement.value;

                // sync boolean attributes
                syncBooleanAttribute(oldElement, newElement, 'checked', ctx);
                syncBooleanAttribute(oldElement, newElement, 'disabled', ctx);

                if (!newElement.hasAttribute('value')) {
                    if (!ignoreAttribute('value', oldElement, 'remove', ctx)) {
                        oldElement.value = '';
                        oldElement.removeAttribute('value');
                    }
                } else if (oldValue !== newValue) {
                    if (!ignoreAttribute('value', oldElement, 'update', ctx)) {
                        oldElement.setAttribute('value', newValue);
                        oldElement.value = newValue;
                    }
                }
                // TODO: QUESTION(1cg): this used to only check `newElement` unlike the other branches -- why?
                // did I break something?
            } else if (
                oldElement instanceof HTMLOptionElement &&
                newElement instanceof HTMLOptionElement
            ) {
                syncBooleanAttribute(oldElement, newElement, 'selected', ctx);
            } else if (
                oldElement instanceof HTMLTextAreaElement &&
                newElement instanceof HTMLTextAreaElement
            ) {
                let newValue = newElement.value;
                let oldValue = oldElement.value;
                if (ignoreAttribute('value', oldElement, 'update', ctx)) {
                    return;
                }
                if (newValue !== oldValue) {
                    oldElement.value = newValue;
                }
                if (
                    oldElement.firstChild &&
                    oldElement.firstChild.nodeValue !== newValue
                ) {
                    oldElement.firstChild.nodeValue = newValue;
                }
            }
        }

        /**
         * @param {Element} oldElement element to write the value to
         * @param {Element} newElement element to read the value from
         * @param {string} attributeName the attribute name
         * @param {MorphContext} ctx the merge context
         */
        function syncBooleanAttribute(
            oldElement,
            newElement,
            attributeName,
            ctx,
        ) {
            // @ts-ignore this function is only used on boolean attrs that are reflected as dom properties
            const newLiveValue = newElement[attributeName],
                // @ts-ignore ditto
                oldLiveValue = oldElement[attributeName];
            if (newLiveValue !== oldLiveValue) {
                const ignoreUpdate = ignoreAttribute(
                    attributeName,
                    oldElement,
                    'update',
                    ctx,
                );
                if (!ignoreUpdate) {
                    // update attribute's associated DOM property
                    // @ts-ignore this function is only used on boolean attrs that are reflected as dom properties
                    oldElement[attributeName] = newElement[attributeName];
                }
                if (newLiveValue) {
                    if (!ignoreUpdate) {
                        // https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML
                        // this is the correct way to set a boolean attribute to "true"
                        oldElement.setAttribute(attributeName, '');
                    }
                } else {
                    if (
                        !ignoreAttribute(
                            attributeName,
                            oldElement,
                            'remove',
                            ctx,
                        )
                    ) {
                        oldElement.removeAttribute(attributeName);
                    }
                }
            }
        }

        /**
         * @param {string} attr the attribute to be mutated
         * @param {Element} element the element that is going to be updated
         * @param {"update" | "remove"} updateType
         * @param {MorphContext} ctx the merge context
         * @returns {boolean} true if the attribute should be ignored, false otherwise
         */
        function ignoreAttribute(attr, element, updateType, ctx) {
            if (
                attr === 'value' &&
                ctx.ignoreActiveValue &&
                element === document.activeElement
            ) {
                return true;
            }
            return (
                ctx.callbacks.beforeAttributeUpdated(
                    attr,
                    element,
                    updateType,
                ) === false
            );
        }

        /**
         * @param {Node} possibleActiveElement
         * @param {MorphContext} ctx
         * @returns {boolean}
         */
        function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
            return (
                !!ctx.ignoreActiveValue &&
                possibleActiveElement === document.activeElement &&
                possibleActiveElement !== document.body
            );
        }

        return morphNode;
    })();

    //=============================================================================
    // Head Management Functions
    //=============================================================================
    /**
     * @param {MorphContext} ctx
     * @param {Element} oldNode
     * @param {Element} newNode
     * @param {function} callback
     * @returns {Node[] | Promise<Node[]>}
     */
    function withHeadBlocking(ctx, oldNode, newNode, callback) {
        if (ctx.head.block) {
            const oldHead = oldNode.querySelector('head');
            const newHead = newNode.querySelector('head');
            if (oldHead && newHead) {
                const promises = handleHeadElement(oldHead, newHead, ctx);
                // when head promises resolve, proceed ignoring the head tag
                return Promise.all(promises).then(() => {
                    const newCtx = Object.assign(ctx, {
                        head: {
                            block: false,
                            ignore: true,
                        },
                    });
                    return callback(newCtx);
                });
            }
        }
        // just proceed if we not head blocking
        return callback(ctx);
    }

    /**
     *  The HEAD tag can be handled specially, either w/ a 'merge' or 'append' style
     *
     * @param {Element} oldHead
     * @param {Element} newHead
     * @param {MorphContext} ctx
     * @returns {Promise<void>[]}
     */
    function handleHeadElement(oldHead, newHead, ctx) {
        let added = [];
        let removed = [];
        let preserved = [];
        let nodesToAppend = [];

        // put all new head elements into a Map, by their outerHTML
        let srcToNewHeadNodes = new Map();
        for (const newHeadChild of newHead.children) {
            srcToNewHeadNodes.set(newHeadChild.outerHTML, newHeadChild);
        }

        // for each elt in the current head
        for (const currentHeadElt of oldHead.children) {
            // If the current head element is in the map
            let inNewContent = srcToNewHeadNodes.has(currentHeadElt.outerHTML);
            let isReAppended = ctx.head.shouldReAppend(currentHeadElt);
            let isPreserved = ctx.head.shouldPreserve(currentHeadElt);
            if (inNewContent || isPreserved) {
                if (isReAppended) {
                    // remove the current version and let the new version replace it and re-execute
                    removed.push(currentHeadElt);
                } else {
                    // this element already exists and should not be re-appended, so remove it from
                    // the new content map, preserving it in the DOM
                    srcToNewHeadNodes.delete(currentHeadElt.outerHTML);
                    preserved.push(currentHeadElt);
                }
            } else {
                if (ctx.head.style === 'append') {
                    // we are appending and this existing element is not new content
                    // so if and only if it is marked for re-append do we do anything
                    if (isReAppended) {
                        removed.push(currentHeadElt);
                        nodesToAppend.push(currentHeadElt);
                    }
                } else {
                    // if this is a merge, we remove this content since it is not in the new head
                    if (ctx.head.shouldRemove(currentHeadElt) !== false) {
                        removed.push(currentHeadElt);
                    }
                }
            }
        }

        // Push the remaining new head elements in the Map into the
        // nodes to append to the head tag
        nodesToAppend.push(...srcToNewHeadNodes.values());

        let promises = [];
        for (const newNode of nodesToAppend) {
            // TODO: This could theoretically be null, based on type
            let newElt = /** @type {ChildNode} */ (
                document
                    .createRange()
                    .createContextualFragment(newNode.outerHTML).firstChild
            );
            if (ctx.callbacks.beforeNodeAdded(newElt) !== false) {
                if (
                    ('href' in newElt && newElt.href) ||
                    ('src' in newElt && newElt.src)
                ) {
                    /** @type {(result?: any) => void} */ let resolve;
                    let promise = new Promise(function (_resolve) {
                        resolve = _resolve;
                    });
                    newElt.addEventListener('load', function () {
                        resolve();
                    });
                    promises.push(promise);
                }
                oldHead.appendChild(newElt);
                ctx.callbacks.afterNodeAdded(newElt);
                added.push(newElt);
            }
        }

        // remove all removed elements, after we have appended the new elements to avoid
        // additional network requests for things like style sheets
        for (const removedElement of removed) {
            if (ctx.callbacks.beforeNodeRemoved(removedElement) !== false) {
                oldHead.removeChild(removedElement);
                ctx.callbacks.afterNodeRemoved(removedElement);
            }
        }

        ctx.head.afterHeadMorphed(oldHead, {
            added: added,
            kept: preserved,
            removed: removed,
        });
        return promises;
    }

    //=============================================================================
    // Create Morph Context Functions
    //=============================================================================
    const createMorphContext = (function () {
        /**
         *
         * @param {Element} oldNode
         * @param {Element} newContent
         * @param {Config} config
         * @returns {MorphContext}
         */
        function createMorphContext(oldNode, newContent, config) {
            const { persistentIds, idMap } = createIdMaps(oldNode, newContent);

            const mergedConfig = mergeDefaults(config);
            const morphStyle = mergedConfig.morphStyle || 'outerHTML';
            if (!['innerHTML', 'outerHTML'].includes(morphStyle)) {
                throw `Do not understand how to morph style ${morphStyle}`;
            }

            return {
                target: oldNode,
                newContent: newContent,
                config: mergedConfig,
                morphStyle: morphStyle,
                ignoreActive: mergedConfig.ignoreActive,
                ignoreActiveValue: mergedConfig.ignoreActiveValue,
                restoreFocus: mergedConfig.restoreFocus,
                idMap: idMap,
                persistentIds: persistentIds,
                pantry: createPantry(),
                callbacks: mergedConfig.callbacks,
                head: mergedConfig.head,
            };
        }

        /**
         * Deep merges the config object and the Idiomorph.defaults object to
         * produce a final configuration object
         * @param {Config} config
         * @returns {ConfigInternal}
         */
        function mergeDefaults(config) {
            let finalConfig = Object.assign({}, defaults);

            // copy top level stuff into final config
            Object.assign(finalConfig, config);

            // copy callbacks into final config (do this to deep merge the callbacks)
            finalConfig.callbacks = Object.assign(
                {},
                defaults.callbacks,
                config.callbacks,
            );

            // copy head config into final config  (do this to deep merge the head)
            finalConfig.head = Object.assign({}, defaults.head, config.head);

            return finalConfig;
        }

        /**
         * @returns {HTMLDivElement}
         */
        function createPantry() {
            const pantry = document.createElement('div');
            pantry.hidden = true;
            document.body.insertAdjacentElement('afterend', pantry);
            return pantry;
        }

        /**
         * Returns all elements with an ID contained within the root element and its descendants
         *
         * @param {Element} root
         * @returns {Element[]}
         */
        function findIdElements(root) {
            let elements = Array.from(root.querySelectorAll('[id]'));
            if (root.id) {
                elements.push(root);
            }
            return elements;
        }

        /**
         * A bottom-up algorithm that populates a map of Element -> IdSet.
         * The idSet for a given element is the set of all IDs contained within its subtree.
         * As an optimzation, we filter these IDs through the given list of persistent IDs,
         * because we don't need to bother considering IDed elements that won't be in the new content.
         *
         * @param {Map<Node, Set<string>>} idMap
         * @param {Set<string>} persistentIds
         * @param {Element} root
         * @param {Element[]} elements
         */
        function populateIdMapWithTree(idMap, persistentIds, root, elements) {
            for (const elt of elements) {
                if (persistentIds.has(elt.id)) {
                    /** @type {Element|null} */
                    let current = elt;
                    // walk up the parent hierarchy of that element, adding the id
                    // of element to the parent's id set
                    while (current) {
                        let idSet = idMap.get(current);
                        // if the id set doesn't exist, create it and insert it in the map
                        if (idSet == null) {
                            idSet = new Set();
                            idMap.set(current, idSet);
                        }
                        idSet.add(elt.id);

                        if (current === root) break;
                        current = current.parentElement;
                    }
                }
            }
        }

        /**
         * This function computes a map of nodes to all ids contained within that node (inclusive of the
         * node).  This map can be used to ask if two nodes have intersecting sets of ids, which allows
         * for a looser definition of "matching" than tradition id matching, and allows child nodes
         * to contribute to a parent nodes matching.
         *
         * @param {Element} oldContent  the old content that will be morphed
         * @param {Element} newContent  the new content to morph to
         * @returns {IdSets}
         */
        function createIdMaps(oldContent, newContent) {
            const oldIdElements = findIdElements(oldContent);
            const newIdElements = findIdElements(newContent);

            const persistentIds = createPersistentIds(
                oldIdElements,
                newIdElements,
            );

            /** @type {Map<Node, Set<string>>} */
            let idMap = new Map();
            populateIdMapWithTree(
                idMap,
                persistentIds,
                oldContent,
                oldIdElements,
            );

            /** @ts-ignore - if newContent is a duck-typed parent, pass its single child node as the root to halt upwards iteration */
            const newRoot = newContent.__idiomorphRoot || newContent;
            populateIdMapWithTree(idMap, persistentIds, newRoot, newIdElements);

            return { persistentIds, idMap };
        }

        /**
         * This function computes the set of ids that persist between the two contents excluding duplicates
         *
         * @param {Element[]} oldIdElements
         * @param {Element[]} newIdElements
         * @returns {Set<string>}
         */
        function createPersistentIds(oldIdElements, newIdElements) {
            let duplicateIds = new Set();

            /** @type {Map<string, string>} */
            let oldIdTagNameMap = new Map();
            for (const { id, tagName } of oldIdElements) {
                if (oldIdTagNameMap.has(id)) {
                    duplicateIds.add(id);
                } else {
                    oldIdTagNameMap.set(id, tagName);
                }
            }

            let persistentIds = new Set();
            for (const { id, tagName } of newIdElements) {
                if (persistentIds.has(id)) {
                    duplicateIds.add(id);
                } else if (oldIdTagNameMap.get(id) === tagName) {
                    persistentIds.add(id);
                }
                // skip if tag types mismatch because its not possible to morph one tag into another
            }

            for (const id of duplicateIds) {
                persistentIds.delete(id);
            }
            return persistentIds;
        }

        return createMorphContext;
    })();

    //=============================================================================
    // HTML Normalization Functions
    //=============================================================================
    const { normalizeElement, normalizeParent } = (function () {
        /** @type {WeakSet<Node>} */
        const generatedByIdiomorph = new WeakSet();

        /**
         *
         * @param {Element | Document} content
         * @returns {Element}
         */
        function normalizeElement(content) {
            if (content instanceof Document) {
                return content.documentElement;
            } else {
                return content;
            }
        }

        /**
         *
         * @param {null | string | Node | HTMLCollection | Node[] | Document & {generatedByIdiomorph:boolean}} newContent
         * @returns {Element}
         */
        function normalizeParent(newContent) {
            if (newContent == null) {
                return document.createElement('div'); // dummy parent element
            } else if (typeof newContent === 'string') {
                return normalizeParent(parseContent(newContent));
            } else if (
                generatedByIdiomorph.has(/** @type {Element} */ (newContent))
            ) {
                // the template tag created by idiomorph parsing can serve as a dummy parent
                return /** @type {Element} */ (newContent);
            } else if (newContent instanceof Node) {
                if (newContent.parentNode) {
                    // we can't use the parent directly because newContent may have siblings
                    // that we don't want in the morph, and reparenting might be expensive (TODO is it?),
                    // so we create a duck-typed parent node instead.
                    return createDuckTypedParent(newContent);
                } else {
                    // a single node is added as a child to a dummy parent
                    const dummyParent = document.createElement('div');
                    dummyParent.append(newContent);
                    return dummyParent;
                }
            } else {
                // all nodes in the array or HTMLElement collection are consolidated under
                // a single dummy parent element
                const dummyParent = document.createElement('div');
                for (const elt of [...newContent]) {
                    dummyParent.append(elt);
                }
                return dummyParent;
            }
        }

        /**
         * Creates a fake duck-typed parent element to wrap a single node, without actually reparenting it.
         * "If it walks like a duck, and quacks like a duck, then it must be a duck!" -- James Whitcomb Riley (1849–1916)
         *
         * @param {Node} newContent
         * @returns {Element}
         */
        function createDuckTypedParent(newContent) {
            return /** @type {Element} */ (
                /** @type {unknown} */ ({
                    childNodes: [newContent],
                    /** @ts-ignore - cover your eyes for a minute, tsc */
                    querySelectorAll: (s) => {
                        /** @ts-ignore */
                        const elements = newContent.querySelectorAll(s);
                        /** @ts-ignore */
                        return newContent.matches(s)
                            ? [newContent, ...elements]
                            : elements;
                    },
                    /** @ts-ignore */
                    insertBefore: (n, r) =>
                        newContent.parentNode.insertBefore(n, r),
                    /** @ts-ignore */
                    moveBefore: (n, r) =>
                        newContent.parentNode.moveBefore(n, r),
                    // for later use with populateIdMapWithTree to halt upwards iteration
                    get __idiomorphRoot() {
                        return newContent;
                    },
                })
            );
        }

        /**
         *
         * @param {string} newContent
         * @returns {Node | null | DocumentFragment}
         */
        function parseContent(newContent) {
            let parser = new DOMParser();

            // remove svgs to avoid false-positive matches on head, etc.
            let contentWithSvgsRemoved = newContent.replace(
                /<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim,
                '',
            );

            // if the newContent contains a html, head or body tag, we can simply parse it w/o wrapping
            if (
                contentWithSvgsRemoved.match(/<\/html>/) ||
                contentWithSvgsRemoved.match(/<\/head>/) ||
                contentWithSvgsRemoved.match(/<\/body>/)
            ) {
                let content = parser.parseFromString(newContent, 'text/html');
                // if it is a full HTML document, return the document itself as the parent container
                if (contentWithSvgsRemoved.match(/<\/html>/)) {
                    generatedByIdiomorph.add(content);
                    return content;
                } else {
                    // otherwise return the html element as the parent container
                    let htmlElement = content.firstChild;
                    if (htmlElement) {
                        generatedByIdiomorph.add(htmlElement);
                    }
                    return htmlElement;
                }
            } else {
                // if it is partial HTML, wrap it in a template tag to provide a parent element and also to help
                // deal with touchy tags like tr, tbody, etc.
                let responseDoc = parser.parseFromString(
                    '<body><template>' + newContent + '</template></body>',
                    'text/html',
                );
                let content = /** @type {HTMLTemplateElement} */ (
                    responseDoc.body.querySelector('template')
                ).content;
                generatedByIdiomorph.add(content);
                return content;
            }
        }

        return { normalizeElement, normalizeParent };
    })();

    //=============================================================================
    // This is what ends up becoming the Idiomorph global object
    //=============================================================================
    return {
        morph,
        defaults,
    };
})();

export { Idiomorph };