@rettangoli/fe 1.1.1 → 1.1.2

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.
package/README.md CHANGED
@@ -195,7 +195,7 @@ Use this workflow:
195
195
  Docker image:
196
196
 
197
197
  ```bash
198
- IMAGE="han4wluc/rtgl:playwright-v1.57.0-rtgl-v1.0.12"
198
+ IMAGE="han4wluc/rtgl:playwright-v1.57.0-rtgl-v1.1.0"
199
199
  ```
200
200
 
201
201
  Dashboard suite:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rettangoli/fe",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Frontend framework for building reactive web components",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -12,13 +12,61 @@ const COMMON_LINK_STYLE_TEXT = `
12
12
  }
13
13
  `;
14
14
 
15
+ const RENDER_TARGET_ATTR = "data-rtgl-render-target";
16
+ const RENDER_TARGET_FLAG = "__rtglRenderTarget";
17
+
18
+ const hasRenderTargetAttr = (node) => {
19
+ if (!node || typeof node !== "object") {
20
+ return false;
21
+ }
22
+
23
+ if (typeof node.getAttribute === "function") {
24
+ return node.getAttribute(RENDER_TARGET_ATTR) !== null;
25
+ }
26
+
27
+ return node[RENDER_TARGET_FLAG] === true;
28
+ };
29
+
30
+ const markRenderTarget = (node) => {
31
+ if (!node || typeof node !== "object") {
32
+ return;
33
+ }
34
+
35
+ if (typeof node.setAttribute === "function") {
36
+ node.setAttribute(RENDER_TARGET_ATTR, "");
37
+ } else {
38
+ node[RENDER_TARGET_FLAG] = true;
39
+ }
40
+ };
41
+
42
+ const findExistingRenderTarget = (shadow) => {
43
+ if (!shadow || typeof shadow !== "object") {
44
+ return undefined;
45
+ }
46
+
47
+ if (typeof shadow.querySelector === "function") {
48
+ return shadow.querySelector(`[${RENDER_TARGET_ATTR}]`) ?? shadow.firstElementChild;
49
+ }
50
+
51
+ if (Array.isArray(shadow.childNodes)) {
52
+ return shadow.childNodes.find(hasRenderTargetAttr) ?? shadow.childNodes[0];
53
+ }
54
+
55
+ if (Array.isArray(shadow.children)) {
56
+ return shadow.children.find(hasRenderTargetAttr) ?? shadow.children[0];
57
+ }
58
+
59
+ return shadow.firstElementChild;
60
+ };
61
+
15
62
  export const initializeComponentDom = ({
16
63
  host,
17
64
  cssText,
18
65
  createStyleSheet = () => new CSSStyleSheet(),
19
66
  createElement = (tagName) => document.createElement(tagName),
20
67
  }) => {
21
- const shadow = host.attachShadow({ mode: "open" });
68
+ const existingShadow = host.shadowRoot;
69
+ const shadow = existingShadow ?? host.attachShadow({ mode: "open" });
22
70
 
23
71
  const commonStyleSheet = createStyleSheet();
24
72
  commonStyleSheet.replaceSync(COMMON_LINK_STYLE_TEXT);
@@ -33,11 +81,22 @@ export const initializeComponentDom = ({
33
81
 
34
82
  shadow.adoptedStyleSheets = adoptedStyleSheets;
35
83
 
36
- const renderTarget = createElement("div");
37
- renderTarget.style.cssText = "display: contents;";
38
- shadow.appendChild(renderTarget);
39
- if (!renderTarget.parentNode) {
40
- host.appendChild(renderTarget);
84
+ let renderTarget = findExistingRenderTarget(shadow);
85
+
86
+ if (!renderTarget) {
87
+ renderTarget = createElement("div");
88
+ renderTarget.style.cssText = "display: contents;";
89
+ markRenderTarget(renderTarget);
90
+ shadow.appendChild(renderTarget);
91
+ } else {
92
+ renderTarget.style.cssText = "display: contents;";
93
+ if (!hasRenderTargetAttr(renderTarget)) {
94
+ markRenderTarget(renderTarget);
95
+ }
96
+ }
97
+
98
+ if (renderTarget.parentNode !== shadow) {
99
+ shadow.appendChild(renderTarget);
41
100
  }
42
101
  host.style.display = "contents";
43
102