Skip to content

Commit

Permalink
add custom-name devoder (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
martrapp authored Jan 31, 2024
1 parent 098b6da commit c1e5a97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-poets-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-vtbot": patch
---

Fixed the check for illegal view transition names which didn't interprete encoded characters correctly
11 changes: 10 additions & 1 deletion components/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function astroContextIds() {
return { inStyleSheets, inElements };
}

let div: HTMLDivElement = document.createElement('div');

// finds all elements of a _the current document_ with a given property in a style sheet
// document.styleSheets does not seem to work for arbitrary documents
export function elementsWithPropertyinStylesheet(
Expand All @@ -45,7 +47,7 @@ export function elementsWithPropertyinStylesheet(
const style = sheet.ownerNode as HTMLElement;
const definedNames = new Set<string>();
const matches = style?.innerHTML.matchAll(new RegExp(`${property}:\\s*([^;}]*)`, 'g'));
[...matches].forEach((match) => definedNames.add(match[1]));
[...matches].forEach((match) => definedNames.add(decode(property, match[1])));
try {
[...sheet.cssRules].forEach((rule) => {
if (rule instanceof CSSStyleRule && rule.style[property]) {
Expand All @@ -64,6 +66,13 @@ export function elementsWithPropertyinStylesheet(
}
});
return map;

function decode(prop: string, value: string): string {
div.style[prop] = '';
div.style[prop] = value;
const res = div.style[prop];
return res || value;
}
}

// finds all elements of a document with a given property in their style attribute
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/src/pages/linter/nine.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Linter from 'astro-vtbot/components/Linter.astro';
<Layout title="Linter9">
<Linter expanded slot="head" />
<main>
<a id="toten" href="/linter/ten/">To page 10</a>
<h1>Linter 9</h1>
<a id="toten" href="/linter/ten/" transition:name="👜✨">To page 10</a>
<h1 transition:name="123$%&">Linter 9</h1>
<p style="view-transition-name: -123abc">Some content</p>
</main>
</Layout>
Expand Down

0 comments on commit c1e5a97

Please sign in to comment.