@limetech/lime-elements 39.12.4 → 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.
@@ -3,6 +3,7 @@ import { markdownToHTML } from "./markdown-parser";
3
3
  import { globalConfig } from "../../global/config";
4
4
  import { ImageIntersectionObserver } from "./image-intersection-observer";
5
5
  import { hydrateCustomElements } from "./hydrate-custom-elements";
6
+ import { morphChildren } from "./morph-dom";
6
7
  import { DEFAULT_MARKDOWN_WHITELIST } from "./default-whitelist";
7
8
  /**
8
9
  * The Markdown component receives markdown syntax
@@ -95,7 +96,7 @@ export class Markdown {
95
96
  lazyLoadImages: this.lazyLoadImages,
96
97
  removeEmptyParagraphs: this.removeEmptyParagraphs,
97
98
  });
98
- this.rootElement.innerHTML = html;
99
+ morphChildren(this.rootElement, html);
99
100
  // Hydration parses JSON attribute values (e.g. link='{"href":"..."}')
100
101
  // into JS properties. URL sanitization happens here because
101
102
  // rehype-sanitize can't inspect values inside JSON strings.
@@ -119,7 +120,7 @@ export class Markdown {
119
120
  this.cleanupImageIntersectionObserver();
120
121
  }
121
122
  render() {
122
- return (h(Host, { key: 'd3c5e71466ad7fa2723a0a44bc6ba6742e597ca1' }, h("div", { key: 'ff45056e1a3ad465bdea9026b0c9674d911607a2', id: "markdown", ref: (el) => (this.rootElement = el) })));
123
+ return (h(Host, { key: '9d88a42e047b6701215699ab5459af3275e51693' }, h("div", { key: '83a5801839318bf47eeacbf53e320e4628559bfa', id: "markdown", ref: (el) => (this.rootElement = el) })));
123
124
  }
124
125
  setupImageIntersectionObserver() {
125
126
  if (this.lazyLoadImages) {
@@ -0,0 +1,29 @@
1
+ import morphdom from "morphdom";
2
+ /**
3
+ * Morph the children of `container` to match the given HTML string.
4
+ *
5
+ * Uses morphdom to diff the existing DOM against the new HTML and apply
6
+ * only the minimum changes. This preserves existing DOM nodes (including
7
+ * custom elements with internal state) that haven't changed.
8
+ *
9
+ * @param container - The parent element whose children should be morphed.
10
+ * @param html - The new HTML content for the container's children.
11
+ */
12
+ export function morphChildren(container, html = '') {
13
+ // morphdom's second argument must be a single root element. We wrap
14
+ // the new HTML in a <div> purely to satisfy that requirement — the
15
+ // tag name doesn't matter because childrenOnly makes morphdom skip
16
+ // the root and only diff the children. The container element itself
17
+ // is never compared or replaced, so it can be any element type.
18
+ try {
19
+ morphdom(container, `<div>${html}</div>`, {
20
+ childrenOnly: true,
21
+ });
22
+ }
23
+ catch (error) {
24
+ // Fall back to innerHTML so that content is at least visible,
25
+ // even though custom elements will be destroyed and recreated.
26
+ console.warn('morphdom failed, falling back to innerHTML:', error);
27
+ container.innerHTML = html;
28
+ }
29
+ }