fix(syntax): stop swallowing literal backslashes in attribute values

readQuoted treated \X as an escape for any X, so a single literal
backslash in any quoted attribute value was silently dropped
(data-path="C:\Users" parsed as C:Users) and a doubled backslash
collapsed to one. Only the delimiter and the backslash itself are
escapes now; every other backslash is a literal character.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 11:42:29 +05:30
co-authored by Claude Opus 5
parent 63316111cb
commit fb24cc7ec3
2 changed files with 58 additions and 4 deletions
+5 -4
View File
@@ -1022,12 +1022,13 @@ export function parseHtmlView(src: string, pos: number): { nodes: ViewNode[]; en
const quote = src[i]; const quote = src[i];
if (quote !== '"' && quote !== "'") return fail("Expected a quoted attribute value"); if (quote !== '"' && quote !== "'") return fail("Expected a quoted attribute value");
i++; i++;
// Backslash-escapes the delimiter (and anything else) so an attribute // Backslash escapes the delimiter -- so an attribute value such as the
// value -- e.g. an `api="fn({ a: \"b\" })"` call expression -- can carry // call expression `api="fn({ a: \"b\" })"` can carry the same quote
// the same quote character it's wrapped in. // character it's wrapped in -- and escapes itself. Every other backslash
// is a literal one, so a value like "C:\Users\name" survives intact.
let value = ""; let value = "";
while (i < src.length && src[i] !== quote) { while (i < src.length && src[i] !== quote) {
if (src[i] === "\\" && i + 1 < src.length) { if (src[i] === "\\" && (src[i + 1] === quote || src[i + 1] === "\\")) {
value += src[i + 1]; value += src[i + 1];
i += 2; i += 2;
continue; continue;
@@ -0,0 +1,53 @@
import { expect, test } from "bun:test";
import { parse } from "../src/index.ts";
// One literal backslash character. Composing the .wrn sources below by
// concatenation keeps the escaping unambiguous at a glance.
const BS = String.fromCharCode(92);
const Q = '"';
const view = (attr: string) =>
`page Esc {\n view {\n <main>\n <div ${attr}>x</div>\n </main>\n }\n}\n`;
const attrValue = (source: string): string => {
const ast = parse(source) as unknown;
const found: string[] = [];
const walk = (node: unknown): void => {
if (!node || typeof node !== "object") return;
if (Array.isArray(node)) {
node.forEach(walk);
return;
}
const record = node as Record<string, unknown>;
if (typeof record.value === "string" && typeof record.name === "string") {
found.push(record.value);
}
for (const key of Object.keys(record)) walk(record[key]);
};
walk(ast);
return found[found.length - 1] ?? "";
};
test("a backslash-escaped delimiter becomes a bare quote", () => {
// api="fn({ a: \"b\" })" -> fn({ a: "b" })
const attr = `api=${Q}fn({ a: ${BS}${Q}b${BS}${Q} })${Q}`;
expect(attrValue(view(attr))).toBe(`fn({ a: ${Q}b${Q} })`);
});
test("a doubled backslash collapses to exactly one", () => {
// data-x="a\\b" -> a\b
const attr = `data-x=${Q}a${BS}${BS}b${Q}`;
expect(attrValue(view(attr))).toBe(`a${BS}b`);
});
test("a lone backslash before an ordinary character is preserved", () => {
// data-path="C:\Users\name" must survive the lexer intact.
const attr = `data-path=${Q}C:${BS}Users${BS}name${Q}`;
expect(attrValue(view(attr))).toBe(`C:${BS}Users${BS}name`);
});
test("a lone backslash before an escape-looking character is preserved verbatim", () => {
// data-x="a\nb" is two characters a, backslash, n, b -- not a newline.
const attr = `data-x=${Q}a${BS}nb${Q}`;
expect(attrValue(view(attr))).toBe(`a${BS}nb`);
});