@limetech/lime-elements 39.12.3 → 39.12.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -183,6 +183,817 @@ function attributeToPropName(attrName) {
183
183
  return attrName.replaceAll(/-([a-z])/g, (_, letter) => letter.toUpperCase());
184
184
  }
185
185
 
186
+ var DOCUMENT_FRAGMENT_NODE = 11;
187
+
188
+ function morphAttrs(fromNode, toNode) {
189
+ var toNodeAttrs = toNode.attributes;
190
+ var attr;
191
+ var attrName;
192
+ var attrNamespaceURI;
193
+ var attrValue;
194
+ var fromValue;
195
+
196
+ // document-fragments dont have attributes so lets not do anything
197
+ if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE || fromNode.nodeType === DOCUMENT_FRAGMENT_NODE) {
198
+ return;
199
+ }
200
+
201
+ // update attributes on original DOM element
202
+ for (var i = toNodeAttrs.length - 1; i >= 0; i--) {
203
+ attr = toNodeAttrs[i];
204
+ attrName = attr.name;
205
+ attrNamespaceURI = attr.namespaceURI;
206
+ attrValue = attr.value;
207
+
208
+ if (attrNamespaceURI) {
209
+ attrName = attr.localName || attrName;
210
+ fromValue = fromNode.getAttributeNS(attrNamespaceURI, attrName);
211
+
212
+ if (fromValue !== attrValue) {
213
+ if (attr.prefix === 'xmlns'){
214
+ attrName = attr.name; // It's not allowed to set an attribute with the XMLNS namespace without specifying the `xmlns` prefix
215
+ }
216
+ fromNode.setAttributeNS(attrNamespaceURI, attrName, attrValue);
217
+ }
218
+ } else {
219
+ fromValue = fromNode.getAttribute(attrName);
220
+
221
+ if (fromValue !== attrValue) {
222
+ fromNode.setAttribute(attrName, attrValue);
223
+ }
224
+ }
225
+ }
226
+
227
+ // Remove any extra attributes found on the original DOM element that
228
+ // weren't found on the target element.
229
+ var fromNodeAttrs = fromNode.attributes;
230
+
231
+ for (var d = fromNodeAttrs.length - 1; d >= 0; d--) {
232
+ attr = fromNodeAttrs[d];
233
+ attrName = attr.name;
234
+ attrNamespaceURI = attr.namespaceURI;
235
+
236
+ if (attrNamespaceURI) {
237
+ attrName = attr.localName || attrName;
238
+
239
+ if (!toNode.hasAttributeNS(attrNamespaceURI, attrName)) {
240
+ fromNode.removeAttributeNS(attrNamespaceURI, attrName);
241
+ }
242
+ } else {
243
+ if (!toNode.hasAttribute(attrName)) {
244
+ fromNode.removeAttribute(attrName);
245
+ }
246
+ }
247
+ }
248
+ }
249
+
250
+ var range; // Create a range object for efficently rendering strings to elements.
251
+ var NS_XHTML = 'http://www.w3.org/1999/xhtml';
252
+
253
+ var doc = typeof document === 'undefined' ? undefined : document;
254
+ var HAS_TEMPLATE_SUPPORT = !!doc && 'content' in doc.createElement('template');
255
+ var HAS_RANGE_SUPPORT = !!doc && doc.createRange && 'createContextualFragment' in doc.createRange();
256
+
257
+ function createFragmentFromTemplate(str) {
258
+ var template = doc.createElement('template');
259
+ template.innerHTML = str;
260
+ return template.content.childNodes[0];
261
+ }
262
+
263
+ function createFragmentFromRange(str) {
264
+ if (!range) {
265
+ range = doc.createRange();
266
+ range.selectNode(doc.body);
267
+ }
268
+
269
+ var fragment = range.createContextualFragment(str);
270
+ return fragment.childNodes[0];
271
+ }
272
+
273
+ function createFragmentFromWrap(str) {
274
+ var fragment = doc.createElement('body');
275
+ fragment.innerHTML = str;
276
+ return fragment.childNodes[0];
277
+ }
278
+
279
+ /**
280
+ * This is about the same
281
+ * var html = new DOMParser().parseFromString(str, 'text/html');
282
+ * return html.body.firstChild;
283
+ *
284
+ * @method toElement
285
+ * @param {String} str
286
+ */
287
+ function toElement(str) {
288
+ str = str.trim();
289
+ if (HAS_TEMPLATE_SUPPORT) {
290
+ // avoid restrictions on content for things like `<tr><th>Hi</th></tr>` which
291
+ // createContextualFragment doesn't support
292
+ // <template> support not available in IE
293
+ return createFragmentFromTemplate(str);
294
+ } else if (HAS_RANGE_SUPPORT) {
295
+ return createFragmentFromRange(str);
296
+ }
297
+
298
+ return createFragmentFromWrap(str);
299
+ }
300
+
301
+ /**
302
+ * Returns true if two node's names are the same.
303
+ *
304
+ * NOTE: We don't bother checking `namespaceURI` because you will never find two HTML elements with the same
305
+ * nodeName and different namespace URIs.
306
+ *
307
+ * @param {Element} a
308
+ * @param {Element} b The target element
309
+ * @return {boolean}
310
+ */
311
+ function compareNodeNames(fromEl, toEl) {
312
+ var fromNodeName = fromEl.nodeName;
313
+ var toNodeName = toEl.nodeName;
314
+ var fromCodeStart, toCodeStart;
315
+
316
+ if (fromNodeName === toNodeName) {
317
+ return true;
318
+ }
319
+
320
+ fromCodeStart = fromNodeName.charCodeAt(0);
321
+ toCodeStart = toNodeName.charCodeAt(0);
322
+
323
+ // If the target element is a virtual DOM node or SVG node then we may
324
+ // need to normalize the tag name before comparing. Normal HTML elements that are
325
+ // in the "http://www.w3.org/1999/xhtml"
326
+ // are converted to upper case
327
+ if (fromCodeStart <= 90 && toCodeStart >= 97) { // from is upper and to is lower
328
+ return fromNodeName === toNodeName.toUpperCase();
329
+ } else if (toCodeStart <= 90 && fromCodeStart >= 97) { // to is upper and from is lower
330
+ return toNodeName === fromNodeName.toUpperCase();
331
+ } else {
332
+ return false;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Create an element, optionally with a known namespace URI.
338
+ *
339
+ * @param {string} name the element name, e.g. 'div' or 'svg'
340
+ * @param {string} [namespaceURI] the element's namespace URI, i.e. the value of
341
+ * its `xmlns` attribute or its inferred namespace.
342
+ *
343
+ * @return {Element}
344
+ */
345
+ function createElementNS(name, namespaceURI) {
346
+ return !namespaceURI || namespaceURI === NS_XHTML ?
347
+ doc.createElement(name) :
348
+ doc.createElementNS(namespaceURI, name);
349
+ }
350
+
351
+ /**
352
+ * Copies the children of one DOM element to another DOM element
353
+ */
354
+ function moveChildren(fromEl, toEl) {
355
+ var curChild = fromEl.firstChild;
356
+ while (curChild) {
357
+ var nextChild = curChild.nextSibling;
358
+ toEl.appendChild(curChild);
359
+ curChild = nextChild;
360
+ }
361
+ return toEl;
362
+ }
363
+
364
+ function syncBooleanAttrProp(fromEl, toEl, name) {
365
+ if (fromEl[name] !== toEl[name]) {
366
+ fromEl[name] = toEl[name];
367
+ if (fromEl[name]) {
368
+ fromEl.setAttribute(name, '');
369
+ } else {
370
+ fromEl.removeAttribute(name);
371
+ }
372
+ }
373
+ }
374
+
375
+ var specialElHandlers = {
376
+ OPTION: function(fromEl, toEl) {
377
+ var parentNode = fromEl.parentNode;
378
+ if (parentNode) {
379
+ var parentName = parentNode.nodeName.toUpperCase();
380
+ if (parentName === 'OPTGROUP') {
381
+ parentNode = parentNode.parentNode;
382
+ parentName = parentNode && parentNode.nodeName.toUpperCase();
383
+ }
384
+ if (parentName === 'SELECT' && !parentNode.hasAttribute('multiple')) {
385
+ if (fromEl.hasAttribute('selected') && !toEl.selected) {
386
+ // Workaround for MS Edge bug where the 'selected' attribute can only be
387
+ // removed if set to a non-empty value:
388
+ // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12087679/
389
+ fromEl.setAttribute('selected', 'selected');
390
+ fromEl.removeAttribute('selected');
391
+ }
392
+ // We have to reset select element's selectedIndex to -1, otherwise setting
393
+ // fromEl.selected using the syncBooleanAttrProp below has no effect.
394
+ // The correct selectedIndex will be set in the SELECT special handler below.
395
+ parentNode.selectedIndex = -1;
396
+ }
397
+ }
398
+ syncBooleanAttrProp(fromEl, toEl, 'selected');
399
+ },
400
+ /**
401
+ * The "value" attribute is special for the <input> element since it sets
402
+ * the initial value. Changing the "value" attribute without changing the
403
+ * "value" property will have no effect since it is only used to the set the
404
+ * initial value. Similar for the "checked" attribute, and "disabled".
405
+ */
406
+ INPUT: function(fromEl, toEl) {
407
+ syncBooleanAttrProp(fromEl, toEl, 'checked');
408
+ syncBooleanAttrProp(fromEl, toEl, 'disabled');
409
+
410
+ if (fromEl.value !== toEl.value) {
411
+ fromEl.value = toEl.value;
412
+ }
413
+
414
+ if (!toEl.hasAttribute('value')) {
415
+ fromEl.removeAttribute('value');
416
+ }
417
+ },
418
+
419
+ TEXTAREA: function(fromEl, toEl) {
420
+ var newValue = toEl.value;
421
+ if (fromEl.value !== newValue) {
422
+ fromEl.value = newValue;
423
+ }
424
+
425
+ var firstChild = fromEl.firstChild;
426
+ if (firstChild) {
427
+ // Needed for IE. Apparently IE sets the placeholder as the
428
+ // node value and vise versa. This ignores an empty update.
429
+ var oldValue = firstChild.nodeValue;
430
+
431
+ if (oldValue == newValue || (!newValue && oldValue == fromEl.placeholder)) {
432
+ return;
433
+ }
434
+
435
+ firstChild.nodeValue = newValue;
436
+ }
437
+ },
438
+ SELECT: function(fromEl, toEl) {
439
+ if (!toEl.hasAttribute('multiple')) {
440
+ var selectedIndex = -1;
441
+ var i = 0;
442
+ // We have to loop through children of fromEl, not toEl since nodes can be moved
443
+ // from toEl to fromEl directly when morphing.
444
+ // At the time this special handler is invoked, all children have already been morphed
445
+ // and appended to / removed from fromEl, so using fromEl here is safe and correct.
446
+ var curChild = fromEl.firstChild;
447
+ var optgroup;
448
+ var nodeName;
449
+ while(curChild) {
450
+ nodeName = curChild.nodeName && curChild.nodeName.toUpperCase();
451
+ if (nodeName === 'OPTGROUP') {
452
+ optgroup = curChild;
453
+ curChild = optgroup.firstChild;
454
+ // handle empty optgroups
455
+ if (!curChild) {
456
+ curChild = optgroup.nextSibling;
457
+ optgroup = null;
458
+ }
459
+ } else {
460
+ if (nodeName === 'OPTION') {
461
+ if (curChild.hasAttribute('selected')) {
462
+ selectedIndex = i;
463
+ break;
464
+ }
465
+ i++;
466
+ }
467
+ curChild = curChild.nextSibling;
468
+ if (!curChild && optgroup) {
469
+ curChild = optgroup.nextSibling;
470
+ optgroup = null;
471
+ }
472
+ }
473
+ }
474
+
475
+ fromEl.selectedIndex = selectedIndex;
476
+ }
477
+ }
478
+ };
479
+
480
+ var ELEMENT_NODE = 1;
481
+ var DOCUMENT_FRAGMENT_NODE$1 = 11;
482
+ var TEXT_NODE = 3;
483
+ var COMMENT_NODE = 8;
484
+
485
+ function noop() {}
486
+
487
+ function defaultGetNodeKey(node) {
488
+ if (node) {
489
+ return (node.getAttribute && node.getAttribute('id')) || node.id;
490
+ }
491
+ }
492
+
493
+ function morphdomFactory(morphAttrs) {
494
+
495
+ return function morphdom(fromNode, toNode, options) {
496
+ if (!options) {
497
+ options = {};
498
+ }
499
+
500
+ if (typeof toNode === 'string') {
501
+ if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML') {
502
+ var toNodeHtml = toNode;
503
+ toNode = doc.createElement('html');
504
+ toNode.innerHTML = toNodeHtml;
505
+ } else if (fromNode.nodeName === 'BODY') {
506
+ var toNodeBody = toNode;
507
+ toNode = doc.createElement('html');
508
+ toNode.innerHTML = toNodeBody;
509
+ // Extract the body element from the created HTML structure
510
+ var bodyElement = toNode.querySelector('body');
511
+ if (bodyElement) {
512
+ toNode = bodyElement;
513
+ }
514
+ } else {
515
+ toNode = toElement(toNode);
516
+ }
517
+ } else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE$1) {
518
+ toNode = toNode.firstElementChild;
519
+ }
520
+
521
+ var getNodeKey = options.getNodeKey || defaultGetNodeKey;
522
+ var onBeforeNodeAdded = options.onBeforeNodeAdded || noop;
523
+ var onNodeAdded = options.onNodeAdded || noop;
524
+ var onBeforeElUpdated = options.onBeforeElUpdated || noop;
525
+ var onElUpdated = options.onElUpdated || noop;
526
+ var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop;
527
+ var onNodeDiscarded = options.onNodeDiscarded || noop;
528
+ var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop;
529
+ var skipFromChildren = options.skipFromChildren || noop;
530
+ var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); };
531
+ var childrenOnly = options.childrenOnly === true;
532
+
533
+ // This object is used as a lookup to quickly find all keyed elements in the original DOM tree.
534
+ var fromNodesLookup = Object.create(null);
535
+ var keyedRemovalList = [];
536
+
537
+ function addKeyedRemoval(key) {
538
+ keyedRemovalList.push(key);
539
+ }
540
+
541
+ function walkDiscardedChildNodes(node, skipKeyedNodes) {
542
+ if (node.nodeType === ELEMENT_NODE) {
543
+ var curChild = node.firstChild;
544
+ while (curChild) {
545
+
546
+ var key = undefined;
547
+
548
+ if (skipKeyedNodes && (key = getNodeKey(curChild))) {
549
+ // If we are skipping keyed nodes then we add the key
550
+ // to a list so that it can be handled at the very end.
551
+ addKeyedRemoval(key);
552
+ } else {
553
+ // Only report the node as discarded if it is not keyed. We do this because
554
+ // at the end we loop through all keyed elements that were unmatched
555
+ // and then discard them in one final pass.
556
+ onNodeDiscarded(curChild);
557
+ if (curChild.firstChild) {
558
+ walkDiscardedChildNodes(curChild, skipKeyedNodes);
559
+ }
560
+ }
561
+
562
+ curChild = curChild.nextSibling;
563
+ }
564
+ }
565
+ }
566
+
567
+ /**
568
+ * Removes a DOM node out of the original DOM
569
+ *
570
+ * @param {Node} node The node to remove
571
+ * @param {Node} parentNode The nodes parent
572
+ * @param {Boolean} skipKeyedNodes If true then elements with keys will be skipped and not discarded.
573
+ * @return {undefined}
574
+ */
575
+ function removeNode(node, parentNode, skipKeyedNodes) {
576
+ if (onBeforeNodeDiscarded(node) === false) {
577
+ return;
578
+ }
579
+
580
+ if (parentNode) {
581
+ parentNode.removeChild(node);
582
+ }
583
+
584
+ onNodeDiscarded(node);
585
+ walkDiscardedChildNodes(node, skipKeyedNodes);
586
+ }
587
+
588
+ // // TreeWalker implementation is no faster, but keeping this around in case this changes in the future
589
+ // function indexTree(root) {
590
+ // var treeWalker = document.createTreeWalker(
591
+ // root,
592
+ // NodeFilter.SHOW_ELEMENT);
593
+ //
594
+ // var el;
595
+ // while((el = treeWalker.nextNode())) {
596
+ // var key = getNodeKey(el);
597
+ // if (key) {
598
+ // fromNodesLookup[key] = el;
599
+ // }
600
+ // }
601
+ // }
602
+
603
+ // // NodeIterator implementation is no faster, but keeping this around in case this changes in the future
604
+ //
605
+ // function indexTree(node) {
606
+ // var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT);
607
+ // var el;
608
+ // while((el = nodeIterator.nextNode())) {
609
+ // var key = getNodeKey(el);
610
+ // if (key) {
611
+ // fromNodesLookup[key] = el;
612
+ // }
613
+ // }
614
+ // }
615
+
616
+ function indexTree(node) {
617
+ if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) {
618
+ var curChild = node.firstChild;
619
+ while (curChild) {
620
+ var key = getNodeKey(curChild);
621
+ if (key) {
622
+ fromNodesLookup[key] = curChild;
623
+ }
624
+
625
+ // Walk recursively
626
+ indexTree(curChild);
627
+
628
+ curChild = curChild.nextSibling;
629
+ }
630
+ }
631
+ }
632
+
633
+ indexTree(fromNode);
634
+
635
+ function handleNodeAdded(el) {
636
+ onNodeAdded(el);
637
+
638
+ var curChild = el.firstChild;
639
+ while (curChild) {
640
+ var nextSibling = curChild.nextSibling;
641
+
642
+ var key = getNodeKey(curChild);
643
+ if (key) {
644
+ var unmatchedFromEl = fromNodesLookup[key];
645
+ // if we find a duplicate #id node in cache, replace `el` with cache value
646
+ // and morph it to the child node.
647
+ if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) {
648
+ curChild.parentNode.replaceChild(unmatchedFromEl, curChild);
649
+ morphEl(unmatchedFromEl, curChild);
650
+ } else {
651
+ handleNodeAdded(curChild);
652
+ }
653
+ } else {
654
+ // recursively call for curChild and it's children to see if we find something in
655
+ // fromNodesLookup
656
+ handleNodeAdded(curChild);
657
+ }
658
+
659
+ curChild = nextSibling;
660
+ }
661
+ }
662
+
663
+ function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) {
664
+ // We have processed all of the "to nodes". If curFromNodeChild is
665
+ // non-null then we still have some from nodes left over that need
666
+ // to be removed
667
+ while (curFromNodeChild) {
668
+ var fromNextSibling = curFromNodeChild.nextSibling;
669
+ if ((curFromNodeKey = getNodeKey(curFromNodeChild))) {
670
+ // Since the node is keyed it might be matched up later so we defer
671
+ // the actual removal to later
672
+ addKeyedRemoval(curFromNodeKey);
673
+ } else {
674
+ // NOTE: we skip nested keyed nodes from being removed since there is
675
+ // still a chance they will be matched up later
676
+ removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
677
+ }
678
+ curFromNodeChild = fromNextSibling;
679
+ }
680
+ }
681
+
682
+ function morphEl(fromEl, toEl, childrenOnly) {
683
+ var toElKey = getNodeKey(toEl);
684
+
685
+ if (toElKey) {
686
+ // If an element with an ID is being morphed then it will be in the final
687
+ // DOM so clear it out of the saved elements collection
688
+ delete fromNodesLookup[toElKey];
689
+ }
690
+
691
+ if (!childrenOnly) {
692
+ // optional
693
+ var beforeUpdateResult = onBeforeElUpdated(fromEl, toEl);
694
+ if (beforeUpdateResult === false) {
695
+ return;
696
+ } else if (beforeUpdateResult instanceof HTMLElement) {
697
+ fromEl = beforeUpdateResult;
698
+ // reindex the new fromEl in case it's not in the same
699
+ // tree as the original fromEl
700
+ // (Phoenix LiveView sometimes returns a cloned tree,
701
+ // but keyed lookups would still point to the original tree)
702
+ indexTree(fromEl);
703
+ }
704
+
705
+ // update attributes on original DOM element first
706
+ morphAttrs(fromEl, toEl);
707
+ // optional
708
+ onElUpdated(fromEl);
709
+
710
+ if (onBeforeElChildrenUpdated(fromEl, toEl) === false) {
711
+ return;
712
+ }
713
+ }
714
+
715
+ if (fromEl.nodeName !== 'TEXTAREA') {
716
+ morphChildren(fromEl, toEl);
717
+ } else {
718
+ specialElHandlers.TEXTAREA(fromEl, toEl);
719
+ }
720
+ }
721
+
722
+ function morphChildren(fromEl, toEl) {
723
+ var skipFrom = skipFromChildren(fromEl, toEl);
724
+ var curToNodeChild = toEl.firstChild;
725
+ var curFromNodeChild = fromEl.firstChild;
726
+ var curToNodeKey;
727
+ var curFromNodeKey;
728
+
729
+ var fromNextSibling;
730
+ var toNextSibling;
731
+ var matchingFromEl;
732
+
733
+ // walk the children
734
+ outer: while (curToNodeChild) {
735
+ toNextSibling = curToNodeChild.nextSibling;
736
+ curToNodeKey = getNodeKey(curToNodeChild);
737
+
738
+ // walk the fromNode children all the way through
739
+ while (!skipFrom && curFromNodeChild) {
740
+ fromNextSibling = curFromNodeChild.nextSibling;
741
+
742
+ if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) {
743
+ curToNodeChild = toNextSibling;
744
+ curFromNodeChild = fromNextSibling;
745
+ continue outer;
746
+ }
747
+
748
+ curFromNodeKey = getNodeKey(curFromNodeChild);
749
+
750
+ var curFromNodeType = curFromNodeChild.nodeType;
751
+
752
+ // this means if the curFromNodeChild doesnt have a match with the curToNodeChild
753
+ var isCompatible = undefined;
754
+
755
+ if (curFromNodeType === curToNodeChild.nodeType) {
756
+ if (curFromNodeType === ELEMENT_NODE) {
757
+ // Both nodes being compared are Element nodes
758
+
759
+ if (curToNodeKey) {
760
+ // The target node has a key so we want to match it up with the correct element
761
+ // in the original DOM tree
762
+ if (curToNodeKey !== curFromNodeKey) {
763
+ // The current element in the original DOM tree does not have a matching key so
764
+ // let's check our lookup to see if there is a matching element in the original
765
+ // DOM tree
766
+ if ((matchingFromEl = fromNodesLookup[curToNodeKey])) {
767
+ if (fromNextSibling === matchingFromEl) {
768
+ // Special case for single element removals. To avoid removing the original
769
+ // DOM node out of the tree (since that can break CSS transitions, etc.),
770
+ // we will instead discard the current node and wait until the next
771
+ // iteration to properly match up the keyed target element with its matching
772
+ // element in the original tree
773
+ isCompatible = false;
774
+ } else {
775
+ // We found a matching keyed element somewhere in the original DOM tree.
776
+ // Let's move the original DOM node into the current position and morph
777
+ // it.
778
+
779
+ // NOTE: We use insertBefore instead of replaceChild because we want to go through
780
+ // the `removeNode()` function for the node that is being discarded so that
781
+ // all lifecycle hooks are correctly invoked
782
+ fromEl.insertBefore(matchingFromEl, curFromNodeChild);
783
+
784
+ // fromNextSibling = curFromNodeChild.nextSibling;
785
+
786
+ if (curFromNodeKey) {
787
+ // Since the node is keyed it might be matched up later so we defer
788
+ // the actual removal to later
789
+ addKeyedRemoval(curFromNodeKey);
790
+ } else {
791
+ // NOTE: we skip nested keyed nodes from being removed since there is
792
+ // still a chance they will be matched up later
793
+ removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
794
+ }
795
+
796
+ curFromNodeChild = matchingFromEl;
797
+ curFromNodeKey = getNodeKey(curFromNodeChild);
798
+ }
799
+ } else {
800
+ // The nodes are not compatible since the "to" node has a key and there
801
+ // is no matching keyed node in the source tree
802
+ isCompatible = false;
803
+ }
804
+ }
805
+ } else if (curFromNodeKey) {
806
+ // The original has a key
807
+ isCompatible = false;
808
+ }
809
+
810
+ isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild);
811
+ if (isCompatible) {
812
+ // We found compatible DOM elements so transform
813
+ // the current "from" node to match the current
814
+ // target DOM node.
815
+ // MORPH
816
+ morphEl(curFromNodeChild, curToNodeChild);
817
+ }
818
+
819
+ } else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) {
820
+ // Both nodes being compared are Text or Comment nodes
821
+ isCompatible = true;
822
+ // Simply update nodeValue on the original node to
823
+ // change the text value
824
+ if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) {
825
+ curFromNodeChild.nodeValue = curToNodeChild.nodeValue;
826
+ }
827
+
828
+ }
829
+ }
830
+
831
+ if (isCompatible) {
832
+ // Advance both the "to" child and the "from" child since we found a match
833
+ // Nothing else to do as we already recursively called morphChildren above
834
+ curToNodeChild = toNextSibling;
835
+ curFromNodeChild = fromNextSibling;
836
+ continue outer;
837
+ }
838
+
839
+ // No compatible match so remove the old node from the DOM and continue trying to find a
840
+ // match in the original DOM. However, we only do this if the from node is not keyed
841
+ // since it is possible that a keyed node might match up with a node somewhere else in the
842
+ // target tree and we don't want to discard it just yet since it still might find a
843
+ // home in the final DOM tree. After everything is done we will remove any keyed nodes
844
+ // that didn't find a home
845
+ if (curFromNodeKey) {
846
+ // Since the node is keyed it might be matched up later so we defer
847
+ // the actual removal to later
848
+ addKeyedRemoval(curFromNodeKey);
849
+ } else {
850
+ // NOTE: we skip nested keyed nodes from being removed since there is
851
+ // still a chance they will be matched up later
852
+ removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
853
+ }
854
+
855
+ curFromNodeChild = fromNextSibling;
856
+ } // END: while(curFromNodeChild) {}
857
+
858
+ // If we got this far then we did not find a candidate match for
859
+ // our "to node" and we exhausted all of the children "from"
860
+ // nodes. Therefore, we will just append the current "to" node
861
+ // to the end
862
+ if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) {
863
+ // MORPH
864
+ if(!skipFrom){ addChild(fromEl, matchingFromEl); }
865
+ morphEl(matchingFromEl, curToNodeChild);
866
+ } else {
867
+ var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild);
868
+ if (onBeforeNodeAddedResult !== false) {
869
+ if (onBeforeNodeAddedResult) {
870
+ curToNodeChild = onBeforeNodeAddedResult;
871
+ }
872
+
873
+ if (curToNodeChild.actualize) {
874
+ curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc);
875
+ }
876
+ addChild(fromEl, curToNodeChild);
877
+ handleNodeAdded(curToNodeChild);
878
+ }
879
+ }
880
+
881
+ curToNodeChild = toNextSibling;
882
+ curFromNodeChild = fromNextSibling;
883
+ }
884
+
885
+ cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey);
886
+
887
+ var specialElHandler = specialElHandlers[fromEl.nodeName];
888
+ if (specialElHandler) {
889
+ specialElHandler(fromEl, toEl);
890
+ }
891
+ } // END: morphChildren(...)
892
+
893
+ var morphedNode = fromNode;
894
+ var morphedNodeType = morphedNode.nodeType;
895
+ var toNodeType = toNode.nodeType;
896
+
897
+ if (!childrenOnly) {
898
+ // Handle the case where we are given two DOM nodes that are not
899
+ // compatible (e.g. <div> --> <span> or <div> --> TEXT)
900
+ if (morphedNodeType === ELEMENT_NODE) {
901
+ if (toNodeType === ELEMENT_NODE) {
902
+ if (!compareNodeNames(fromNode, toNode)) {
903
+ onNodeDiscarded(fromNode);
904
+ morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI));
905
+ }
906
+ } else {
907
+ // Going from an element node to a text node
908
+ morphedNode = toNode;
909
+ }
910
+ } else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node
911
+ if (toNodeType === morphedNodeType) {
912
+ if (morphedNode.nodeValue !== toNode.nodeValue) {
913
+ morphedNode.nodeValue = toNode.nodeValue;
914
+ }
915
+
916
+ return morphedNode;
917
+ } else {
918
+ // Text node to something else
919
+ morphedNode = toNode;
920
+ }
921
+ }
922
+ }
923
+
924
+ if (morphedNode === toNode) {
925
+ // The "to node" was not compatible with the "from node" so we had to
926
+ // toss out the "from node" and use the "to node"
927
+ onNodeDiscarded(fromNode);
928
+ } else {
929
+ if (toNode.isSameNode && toNode.isSameNode(morphedNode)) {
930
+ return;
931
+ }
932
+
933
+ morphEl(morphedNode, toNode, childrenOnly);
934
+
935
+ // We now need to loop over any keyed nodes that might need to be
936
+ // removed. We only do the removal if we know that the keyed node
937
+ // never found a match. When a keyed node is matched up we remove
938
+ // it out of fromNodesLookup and we use fromNodesLookup to determine
939
+ // if a keyed node has been matched up or not
940
+ if (keyedRemovalList) {
941
+ for (var i=0, len=keyedRemovalList.length; i<len; i++) {
942
+ var elToRemove = fromNodesLookup[keyedRemovalList[i]];
943
+ if (elToRemove) {
944
+ removeNode(elToRemove, elToRemove.parentNode, false);
945
+ }
946
+ }
947
+ }
948
+ }
949
+
950
+ if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) {
951
+ if (morphedNode.actualize) {
952
+ morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc);
953
+ }
954
+ // If we had to swap out the from node with a new node because the old
955
+ // node was not compatible with the target node then we need to
956
+ // replace the old DOM node in the original DOM tree. This is only
957
+ // possible if the original DOM node was part of a DOM tree which
958
+ // we know is the case if it has a parent node.
959
+ fromNode.parentNode.replaceChild(morphedNode, fromNode);
960
+ }
961
+
962
+ return morphedNode;
963
+ };
964
+ }
965
+
966
+ var morphdom = morphdomFactory(morphAttrs);
967
+
968
+ /**
969
+ * Morph the children of `container` to match the given HTML string.
970
+ *
971
+ * Uses morphdom to diff the existing DOM against the new HTML and apply
972
+ * only the minimum changes. This preserves existing DOM nodes (including
973
+ * custom elements with internal state) that haven't changed.
974
+ *
975
+ * @param container - The parent element whose children should be morphed.
976
+ * @param html - The new HTML content for the container's children.
977
+ */
978
+ function morphChildren(container, html = '') {
979
+ // morphdom's second argument must be a single root element. We wrap
980
+ // the new HTML in a <div> purely to satisfy that requirement — the
981
+ // tag name doesn't matter because childrenOnly makes morphdom skip
982
+ // the root and only diff the children. The container element itself
983
+ // is never compared or replaced, so it can be any element type.
984
+ try {
985
+ morphdom(container, `<div>${html}</div>`, {
986
+ childrenOnly: true,
987
+ });
988
+ }
989
+ catch (error) {
990
+ // Fall back to innerHTML so that content is at least visible,
991
+ // even though custom elements will be destroyed and recreated.
992
+ console.warn('morphdom failed, falling back to innerHTML:', error);
993
+ container.innerHTML = html;
994
+ }
995
+ }
996
+
186
997
  /**
187
998
  * Default whitelist of lime-elements components that are safe to render
188
999
  * inside `limel-markdown`.
@@ -311,7 +1122,7 @@ const Markdown = class {
311
1122
  lazyLoadImages: this.lazyLoadImages,
312
1123
  removeEmptyParagraphs: this.removeEmptyParagraphs,
313
1124
  });
314
- this.rootElement.innerHTML = html;
1125
+ morphChildren(this.rootElement, html);
315
1126
  // Hydration parses JSON attribute values (e.g. link='{"href":"..."}')
316
1127
  // into JS properties. URL sanitization happens here because
317
1128
  // rehype-sanitize can't inspect values inside JSON strings.
@@ -335,7 +1146,7 @@ const Markdown = class {
335
1146
  this.cleanupImageIntersectionObserver();
336
1147
  }
337
1148
  render() {
338
- return (index$1.h(index$1.Host, { key: 'd3c5e71466ad7fa2723a0a44bc6ba6742e597ca1' }, index$1.h("div", { key: 'ff45056e1a3ad465bdea9026b0c9674d911607a2', id: "markdown", ref: (el) => (this.rootElement = el) })));
1149
+ return (index$1.h(index$1.Host, { key: '9d88a42e047b6701215699ab5459af3275e51693' }, index$1.h("div", { key: '83a5801839318bf47eeacbf53e320e4628559bfa', id: "markdown", ref: (el) => (this.rootElement = el) })));
339
1150
  }
340
1151
  setupImageIntersectionObserver() {
341
1152
  if (this.lazyLoadImages) {