@api-client/ui 0.4.3 → 0.4.4

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Searches for an element with the given ID in parent shadow roots
3
3
  */
4
- export declare function findElementInShadowRoots(id: string, from: Element): HTMLElement | null;
4
+ export declare function findElementInShadowRoots(id: string, from: HTMLElement): HTMLElement | null;
5
5
  //# sourceMappingURL=Dom.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Dom.d.ts","sourceRoot":"","sources":["../../../src/lib/Dom.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,WAAW,GAAG,IAAI,CAsBtF"}
1
+ {"version":3,"file":"Dom.d.ts","sourceRoot":"","sources":["../../../src/lib/Dom.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,WAAW,GAAG,IAAI,CAqC1F"}
@@ -9,15 +9,31 @@ export function findElementInShadowRoots(id, from) {
9
9
  }
10
10
  // If not found in document, search in parent shadow roots
11
11
  let current = from;
12
+ const queryId = `#${id}`;
12
13
  while (current) {
13
- const shadowRoot = current.shadowRoot;
14
- if (shadowRoot) {
15
- const element = shadowRoot.getElementById(id);
14
+ // Check if current element has a shadow root and search within it
15
+ if (current.shadowRoot) {
16
+ const element = current.shadowRoot.querySelector(queryId);
16
17
  if (element) {
17
18
  return element;
18
19
  }
19
20
  }
20
- current = current.parentElement ?? current.shadowRoot?.host;
21
+ // Move up the DOM tree
22
+ if (current.parentNode) {
23
+ if (current.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
24
+ // We're inside a shadow root, move to the shadow host
25
+ const shadowRoot = current.parentNode;
26
+ current = shadowRoot.host;
27
+ }
28
+ else {
29
+ // Regular DOM traversal
30
+ current = current.parentElement;
31
+ }
32
+ }
33
+ else {
34
+ // No more parents
35
+ current = null;
36
+ }
21
37
  }
22
38
  return null;
23
39
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Dom.js","sourceRoot":"","sources":["../../../src/lib/Dom.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,wBAAwB,CAAC,EAAU,EAAE,IAAa;IAChE,gCAAgC;IAChC,MAAM,OAAO,GAAG,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAA;IAC3C,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,OAAO,CAAA;IAChB,CAAC;IACD,0DAA0D;IAC1D,IAAI,OAAO,GAA+B,IAAI,CAAA;IAE9C,OAAO,OAAO,EAAE,CAAC;QACf,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAA;QACrC,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,OAAO,GAAG,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC,CAAA;YAC7C,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,OAAsB,CAAA;YAC/B,CAAC;QACH,CAAC;QAED,OAAO,GAAG,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,UAAU,EAAE,IAAI,CAAA;IAC7D,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["/**\n * Searches for an element with the given ID in parent shadow roots\n */\nexport function findElementInShadowRoots(id: string, from: Element): HTMLElement | null {\n // First try to find in document\n const rootElm = document.getElementById(id)\n if (rootElm) {\n return rootElm\n }\n // If not found in document, search in parent shadow roots\n let current: Element | null | undefined = from\n\n while (current) {\n const shadowRoot = current.shadowRoot\n if (shadowRoot) {\n const element = shadowRoot.getElementById(id)\n if (element) {\n return element as HTMLElement\n }\n }\n\n current = current.parentElement ?? current.shadowRoot?.host\n }\n\n return null\n}\n"]}
1
+ {"version":3,"file":"Dom.js","sourceRoot":"","sources":["../../../src/lib/Dom.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,wBAAwB,CAAC,EAAU,EAAE,IAAiB;IACpE,gCAAgC;IAChC,MAAM,OAAO,GAAG,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAA;IAC3C,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,0DAA0D;IAC1D,IAAI,OAAO,GAAuB,IAAI,CAAA;IACtC,MAAM,OAAO,GAAG,IAAI,EAAE,EAAE,CAAA;IAExB,OAAO,OAAO,EAAE,CAAC;QACf,kEAAkE;QAClE,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YACzD,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,OAAsB,CAAA;YAC/B,CAAC;QACH,CAAC;QAED,uBAAuB;QACvB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,KAAK,IAAI,CAAC,sBAAsB,EAAE,CAAC;gBAChE,sDAAsD;gBACtD,MAAM,UAAU,GAAG,OAAO,CAAC,UAAwB,CAAA;gBACnD,OAAO,GAAG,UAAU,CAAC,IAAmB,CAAA;YAC1C,CAAC;iBAAM,CAAC;gBACN,wBAAwB;gBACxB,OAAO,GAAG,OAAO,CAAC,aAAa,CAAA;YACjC,CAAC;QACH,CAAC;aAAM,CAAC;YACN,kBAAkB;YAClB,OAAO,GAAG,IAAI,CAAA;QAChB,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["/**\n * Searches for an element with the given ID in parent shadow roots\n */\nexport function findElementInShadowRoots(id: string, from: HTMLElement): HTMLElement | null {\n // First try to find in document\n const rootElm = document.getElementById(id)\n if (rootElm) {\n return rootElm\n }\n\n // If not found in document, search in parent shadow roots\n let current: HTMLElement | null = from\n const queryId = `#${id}`\n\n while (current) {\n // Check if current element has a shadow root and search within it\n if (current.shadowRoot) {\n const element = current.shadowRoot.querySelector(queryId)\n if (element) {\n return element as HTMLElement\n }\n }\n\n // Move up the DOM tree\n if (current.parentNode) {\n if (current.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {\n // We're inside a shadow root, move to the shadow host\n const shadowRoot = current.parentNode as ShadowRoot\n current = shadowRoot.host as HTMLElement\n } else {\n // Regular DOM traversal\n current = current.parentElement\n }\n } else {\n // No more parents\n current = null\n }\n }\n\n return null\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@api-client/ui",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Internal UI component library for the API Client ecosystem.",
5
5
  "license": "UNLICENSED",
6
6
  "main": "build/src/index.js",
package/src/lib/Dom.ts CHANGED
@@ -1,25 +1,40 @@
1
1
  /**
2
2
  * Searches for an element with the given ID in parent shadow roots
3
3
  */
4
- export function findElementInShadowRoots(id: string, from: Element): HTMLElement | null {
4
+ export function findElementInShadowRoots(id: string, from: HTMLElement): HTMLElement | null {
5
5
  // First try to find in document
6
6
  const rootElm = document.getElementById(id)
7
7
  if (rootElm) {
8
8
  return rootElm
9
9
  }
10
+
10
11
  // If not found in document, search in parent shadow roots
11
- let current: Element | null | undefined = from
12
+ let current: HTMLElement | null = from
13
+ const queryId = `#${id}`
12
14
 
13
15
  while (current) {
14
- const shadowRoot = current.shadowRoot
15
- if (shadowRoot) {
16
- const element = shadowRoot.getElementById(id)
16
+ // Check if current element has a shadow root and search within it
17
+ if (current.shadowRoot) {
18
+ const element = current.shadowRoot.querySelector(queryId)
17
19
  if (element) {
18
20
  return element as HTMLElement
19
21
  }
20
22
  }
21
23
 
22
- current = current.parentElement ?? current.shadowRoot?.host
24
+ // Move up the DOM tree
25
+ if (current.parentNode) {
26
+ if (current.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
27
+ // We're inside a shadow root, move to the shadow host
28
+ const shadowRoot = current.parentNode as ShadowRoot
29
+ current = shadowRoot.host as HTMLElement
30
+ } else {
31
+ // Regular DOM traversal
32
+ current = current.parentElement
33
+ }
34
+ } else {
35
+ // No more parents
36
+ current = null
37
+ }
23
38
  }
24
39
 
25
40
  return null
@@ -0,0 +1,231 @@
1
+ import { assert } from '@open-wc/testing'
2
+ import { findElementInShadowRoots } from '../../src/lib/Dom.js'
3
+
4
+ describe('lib', () => {
5
+ describe('Dom', () => {
6
+ describe('findElementInShadowRoots', () => {
7
+ let container: HTMLDivElement
8
+
9
+ beforeEach(() => {
10
+ container = document.createElement('div')
11
+ container.id = 'test-container'
12
+ document.body.appendChild(container)
13
+ })
14
+
15
+ afterEach(() => {
16
+ document.body.removeChild(container)
17
+ })
18
+
19
+ it('finds element in document root', () => {
20
+ const element = document.createElement('div')
21
+ element.id = 'test-element'
22
+ container.appendChild(element)
23
+
24
+ const fromElement = document.createElement('div')
25
+ container.appendChild(fromElement)
26
+
27
+ const result = findElementInShadowRoots('test-element', fromElement)
28
+ assert.equal(result, element)
29
+ })
30
+
31
+ it('returns null when element is not found', () => {
32
+ const fromElement = document.createElement('div')
33
+ container.appendChild(fromElement)
34
+
35
+ const result = findElementInShadowRoots('non-existent', fromElement)
36
+ assert.isNull(result)
37
+ })
38
+
39
+ it('finds element in parent shadow root', () => {
40
+ // Create a custom element with shadow root
41
+ const shadowHost = document.createElement('div')
42
+ const shadowRoot = shadowHost.attachShadow({ mode: 'open' })
43
+ // Add target element to shadow root
44
+ const targetElement = document.createElement('div')
45
+ targetElement.id = 'shadow-element'
46
+ shadowRoot.appendChild(targetElement)
47
+
48
+ // Add shadow host to container
49
+ container.appendChild(shadowHost)
50
+
51
+ // Create a child element inside the shadow root to search from
52
+ const fromElement = document.createElement('div')
53
+ shadowRoot.appendChild(fromElement)
54
+ const result = findElementInShadowRoots('shadow-element', fromElement)
55
+ assert.ok(result)
56
+ assert.equal(result.id, 'shadow-element')
57
+ })
58
+
59
+ it('finds element in nested shadow roots', () => {
60
+ // Create outer shadow host
61
+ const outerShadowHost = document.createElement('div')
62
+ outerShadowHost.id = 'outer-shadow-host'
63
+ const outerShadowRoot = outerShadowHost.attachShadow({ mode: 'open' })
64
+
65
+ // Create inner shadow host inside outer shadow root
66
+ const innerShadowHost = document.createElement('div')
67
+ innerShadowHost.id = 'inner-shadow-host'
68
+ const innerShadowRoot = innerShadowHost.attachShadow({ mode: 'open' })
69
+ outerShadowRoot.appendChild(innerShadowHost)
70
+
71
+ // Add target element to outer shadow root
72
+ const targetElement = document.createElement('div')
73
+ targetElement.id = 'nested-element'
74
+ outerShadowRoot.appendChild(targetElement)
75
+
76
+ // Add container to document
77
+ container.appendChild(outerShadowHost)
78
+
79
+ // Search from element in inner shadow root
80
+ const fromElement = document.createElement('div')
81
+ fromElement.id = 'search-from'
82
+ innerShadowRoot.appendChild(fromElement)
83
+
84
+ const result = findElementInShadowRoots('nested-element', fromElement)
85
+ assert.equal(result, targetElement)
86
+ })
87
+
88
+ it('searches through multiple shadow root levels', () => {
89
+ // Create a deeply nested shadow structure
90
+ const level1Host = document.createElement('div')
91
+ const level1Shadow = level1Host.attachShadow({ mode: 'open' })
92
+
93
+ const level2Host = document.createElement('div')
94
+ const level2Shadow = level2Host.attachShadow({ mode: 'open' })
95
+ level1Shadow.appendChild(level2Host)
96
+
97
+ const level3Host = document.createElement('div')
98
+ const level3Shadow = level3Host.attachShadow({ mode: 'open' })
99
+ level2Shadow.appendChild(level3Host)
100
+
101
+ // Add target element at level 1
102
+ const targetElement = document.createElement('div')
103
+ targetElement.id = 'deep-element'
104
+ level1Shadow.appendChild(targetElement)
105
+
106
+ container.appendChild(level1Host)
107
+
108
+ // Search from deepest level
109
+ const fromElement = document.createElement('div')
110
+ level3Shadow.appendChild(fromElement)
111
+
112
+ const result = findElementInShadowRoots('deep-element', fromElement)
113
+ assert.equal(result, targetElement)
114
+ })
115
+
116
+ it('prefers document element over shadow root element', () => {
117
+ // Create element in document
118
+ const documentElement = document.createElement('div')
119
+ documentElement.id = 'duplicate-id'
120
+ documentElement.textContent = 'document'
121
+ container.appendChild(documentElement)
122
+
123
+ // Create shadow host with element with same ID
124
+ const shadowHost = document.createElement('div')
125
+ const shadowRoot = shadowHost.attachShadow({ mode: 'open' })
126
+
127
+ const shadowElement = document.createElement('div')
128
+ shadowElement.id = 'duplicate-id'
129
+ shadowElement.textContent = 'shadow'
130
+ shadowRoot.appendChild(shadowElement)
131
+
132
+ container.appendChild(shadowHost)
133
+
134
+ const fromElement = document.createElement('div')
135
+ shadowRoot.appendChild(fromElement)
136
+
137
+ const result = findElementInShadowRoots('duplicate-id', fromElement)
138
+ assert.equal(result, documentElement)
139
+ assert.isNotNull(result)
140
+ assert.equal(result.textContent, 'document')
141
+ })
142
+
143
+ it('handles elements without parent', () => {
144
+ const isolatedElement = document.createElement('div')
145
+
146
+ const result = findElementInShadowRoots('test-id', isolatedElement)
147
+ assert.isNull(result)
148
+ })
149
+
150
+ it('handles shadow root host traversal', () => {
151
+ // Create a shadow host
152
+ const shadowHost = document.createElement('div')
153
+ const shadowRoot = shadowHost.attachShadow({ mode: 'open' })
154
+ container.appendChild(shadowHost)
155
+
156
+ // Add target element to container (outside shadow)
157
+ const targetElement = document.createElement('div')
158
+ targetElement.id = 'outside-shadow'
159
+ container.appendChild(targetElement)
160
+
161
+ // Search from inside shadow root
162
+ const fromElement = document.createElement('div')
163
+ shadowRoot.appendChild(fromElement)
164
+
165
+ const result = findElementInShadowRoots('outside-shadow', fromElement)
166
+ assert.equal(result, targetElement)
167
+ })
168
+
169
+ it('handles elements in current shadow root', () => {
170
+ const shadowHost = document.createElement('div')
171
+ const shadowRoot = shadowHost.attachShadow({ mode: 'open' })
172
+ container.appendChild(shadowHost)
173
+
174
+ // Add target element to same shadow root
175
+ const targetElement = document.createElement('div')
176
+ targetElement.id = 'same-shadow'
177
+ shadowRoot.appendChild(targetElement)
178
+
179
+ // Add search element to same shadow root
180
+ const fromElement = document.createElement('div')
181
+ shadowRoot.appendChild(fromElement)
182
+
183
+ const result = findElementInShadowRoots('same-shadow', fromElement)
184
+ assert.equal(result, targetElement)
185
+ })
186
+
187
+ it('handles complex shadow DOM structure with mixed content', () => {
188
+ // Create complex structure: document -> shadow1 -> regular div -> shadow2
189
+ const shadow1Host = document.createElement('div')
190
+ const shadow1Root = shadow1Host.attachShadow({ mode: 'open' })
191
+ container.appendChild(shadow1Host)
192
+
193
+ const regularDiv = document.createElement('div')
194
+ shadow1Root.appendChild(regularDiv)
195
+
196
+ const shadow2Host = document.createElement('div')
197
+ const shadow2Root = shadow2Host.attachShadow({ mode: 'open' })
198
+ regularDiv.appendChild(shadow2Host)
199
+
200
+ // Add target to shadow1
201
+ const targetElement = document.createElement('div')
202
+ targetElement.id = 'complex-target'
203
+ shadow1Root.appendChild(targetElement)
204
+
205
+ // Search from shadow2
206
+ const fromElement = document.createElement('div')
207
+ shadow2Root.appendChild(fromElement)
208
+
209
+ const result = findElementInShadowRoots('complex-target', fromElement)
210
+ assert.equal(result, targetElement)
211
+ })
212
+
213
+ it('returns null for invalid ID characters', () => {
214
+ const element = document.createElement('div')
215
+ element.id = 'valid-id'
216
+ container.appendChild(element)
217
+
218
+ const fromElement = document.createElement('div')
219
+ container.appendChild(fromElement)
220
+
221
+ // Test with empty string
222
+ const result1 = findElementInShadowRoots('', fromElement)
223
+ assert.isNull(result1)
224
+
225
+ // Test with whitespace
226
+ const result2 = findElementInShadowRoots(' ', fromElement)
227
+ assert.isNull(result2)
228
+ })
229
+ })
230
+ })
231
+ })
@@ -81,7 +81,7 @@ export default /** @type TestRunnerConfig */ ({
81
81
  },
82
82
  browserStartTimeout: 20000,
83
83
  testsStartTimeout: 20000,
84
- testsFinishTimeout: 60000,
84
+ testsFinishTimeout: 30000,
85
85
 
86
86
  coverageConfig: {
87
87
  report: true,