Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored and astrobot-houston committed Jan 23, 2024
1 parent e6945bc commit 659087b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/astro/src/vite-plugin-html/transform/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
visit(tree, (node: Root | RootContent) => {
if (node.type === 'text' || node.type === 'comment') {
if (needsEscape(node.value)) {
s.overwrite(node.position!.start.offset!, node.position!.end.offset!, escapeTemplateLiteralCharacters(node.value));
s.overwrite(
node.position!.start.offset!,
node.position!.end.offset!,
escapeTemplateLiteralCharacters(node.value)
);
}
} else if (node.type === 'element') {
if (!node.properties) return;
for (let [key, value] of Object.entries(node.properties)) {
key = key.replace(/([A-Z])/g, '-$1').toLowerCase()
key = key.replace(/([A-Z])/g, '-$1').toLowerCase();
const newKey = needsEscape(key) ? escapeTemplateLiteralCharacters(key) : key;
const newValue = needsEscape(value) ? escapeTemplateLiteralCharacters(value) : value;
if (newKey === key && newValue === value) continue;
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-html/transform/slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const rehypeSlots: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
const text = file.value
.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0)
.toString();
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`);
s.overwrite(
start,
end,
`\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`
);
}
});
};
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/vite-plugin-html/transform/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function replaceAttribute(s: MagicString, node: Element, key: string, new

// Embedding in our own template literal expression requires escaping
// any meaningful template literal characters in the user's code!
const NEEDS_ESCAPE_RE = /[`\\]|\$\{/g
const NEEDS_ESCAPE_RE = /[`\\]|\$\{/g;

export function needsEscape(value: any): value is string {
// Reset the RegExp's global state
Expand All @@ -41,10 +41,10 @@ export function escapeTemplateLiteralCharacters(value: string) {
let startIndex = 0;
let segment = '';
let text = '';

// Rather than a naive `String.replace()`, we have to iterate through
// the raw contents to properly handle existing backslashes
while ([char] = NEEDS_ESCAPE_RE.exec(value) ?? []) {
while (([char] = NEEDS_ESCAPE_RE.exec(value) ?? [])) {
// Final loop when char === undefined, append trailing content
if (!char) {
text += value.slice(startIndex);
Expand Down

0 comments on commit 659087b

Please sign in to comment.