Skip to content

Commit

Permalink
fix data-* access
Browse files Browse the repository at this point in the history
  • Loading branch information
martrapp committed Nov 13, 2024
1 parent 2b9946c commit f5d63a8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function init() {

function pageSwap() {
if (self.crossingStorage) {
self.crossingStorage.setItem('@vtbag/element-crossing', retrieve());
self.crossingStorage.setItem('@vtbag/element-crossing', collect());
} else {
top!.sessionStorage.setItem('@vtbag/element-crossing', JSON.stringify(retrieve()));
top!.sessionStorage.setItem('@vtbag/element-crossing', JSON.stringify(collect()));
}
}

Expand All @@ -52,7 +52,7 @@ function pageReveal() {
}
}

function retrieve() {
function collect() {
const known = new Set<string>();
const values: ElementSpec[] = [];
self.document.querySelectorAll<HTMLElement>('[data-vtbag-x]').forEach((element) => {
Expand Down Expand Up @@ -139,7 +139,9 @@ function elementSpec(element: HTMLElement) {
case 'bool':
case 'num':
case 'prop':
const val = element[key as keyof HTMLElement];
const val = key.startsWith('data-')
? element.dataset[key.substring(5)]
: element[key as keyof HTMLElement];
const type = typeof val;
specs.push({
kind: type === 'boolean' ? 'bool' : type === 'number' ? 'num' : 'prop',
Expand Down Expand Up @@ -226,7 +228,11 @@ function restore(values: ElementSpec[]) {
}
break;
case 'prop':
(element as any)[s.key] = s.value;
if (s.key.startsWith('data-')) {
element!.dataset[s.key.substring(5)] = s.value;
} else {
(element as any)[s.key] = s.value;
}
break;
case 'bool':
(element as any)[s.key] = s.value === 'true';
Expand Down

0 comments on commit f5d63a8

Please sign in to comment.