Skip to content

Commit 6db7ff3

Browse files
authored
Merge pull request #42 from MatAtBread/v0.15.2
Fix case where element ID is changed, making ID cache invalid. Weakly…
2 parents 4b7b771 + 6e29765 commit 6db7ff3

File tree

9 files changed

+52
-43
lines changed

9 files changed

+52
-43
lines changed

module/dist/ai-ui.cjs

Lines changed: 5 additions & 4 deletions
Large diffs are not rendered by default.

module/dist/ai-ui.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/dist/ai-ui.min.cjs

Lines changed: 8 additions & 8 deletions
Large diffs are not rendered by default.

module/dist/ai-ui.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/dist/ai-ui.min.mjs

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

module/dist/ai-ui.mjs

Lines changed: 5 additions & 4 deletions
Large diffs are not rendered by default.

module/esm/ai-ui.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ export const tag = function (_1, _2, _3) {
123123
},
124124
get: (target, p, receiver) => {
125125
if (typeof p === 'string') {
126+
// Check if we've cached this ID already
126127
if (p in target) {
127-
if (this.contains(target[p]))
128-
return target[p];
128+
// Check the element is still contained within this element with the same ID
129+
const ref = target[p].deref();
130+
if (ref && ref.id === p && this.contains(ref))
131+
return ref;
129132
delete target[p];
130133
}
131134
let e;
@@ -143,7 +146,7 @@ export const tag = function (_1, _2, _3) {
143146
e = this.querySelector('#' + CSS.escape(p)) ?? undefined;
144147
}
145148
if (e)
146-
target[p] = e;
149+
target[p] = new WeakRef(e);
147150
return e;
148151
}
149152
}

module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@
5959
},
6060
"type": "module",
6161
"types": "./esm/ai-ui.d.ts",
62-
"version": "0.15.1"
62+
"version": "0.15.2"
6363
}

module/src/ai-ui.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const tag = <TagLoader>function <Tags extends string,
184184
set: idsInaccessible,
185185
get(this: Element) {
186186
// Now we've been accessed, create the proxy
187-
const idProxy = new Proxy(Object.create(null), {
187+
const idProxy = new Proxy(Object.create(null) as Record<string, WeakRef<Element>>, {
188188
apply: idsInaccessible,
189189
construct: idsInaccessible,
190190
defineProperty: idsInaccessible,
@@ -211,9 +211,12 @@ export const tag = <TagLoader>function <Tags extends string,
211211
},
212212
get: (target, p, receiver) => {
213213
if (typeof p === 'string') {
214+
// Check if we've cached this ID already
214215
if (p in target) {
215-
if (this.contains(target[p]))
216-
return target[p];
216+
// Check the element is still contained within this element with the same ID
217+
const ref = target[p].deref();
218+
if (ref && ref.id === p && this.contains(ref))
219+
return ref;
217220
delete target[p];
218221
}
219222
let e: Element | undefined;
@@ -229,7 +232,7 @@ export const tag = <TagLoader>function <Tags extends string,
229232
} else {
230233
e = this.querySelector('#' + CSS.escape(p)) ?? undefined;
231234
}
232-
if (e) target[p] = e;
235+
if (e) target[p] = new WeakRef(e);
233236
return e;
234237
}
235238
}

0 commit comments

Comments
 (0)