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