diff --git a/.changeset/perfect-buckets-add.md b/.changeset/perfect-buckets-add.md new file mode 100644 index 0000000..4e3b232 --- /dev/null +++ b/.changeset/perfect-buckets-add.md @@ -0,0 +1,5 @@ +--- +"ultrahtml": patch +--- + +Update attribute handling to account for attributes with newlines diff --git a/src/index.ts b/src/index.ts index db5d4e1..7f6498f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -101,7 +101,7 @@ const VOID_TAGS = new Set([ "wbr", ]); const RAW_TAGS = new Set(["script", "style"]); -const SPLIT_ATTRS_RE = /([\@\.a-z0-9_\:\-]*)\s*?=?\s*?(['"]?)(.*?)\2\s+/gim; +const SPLIT_ATTRS_RE = /([\@\.a-z0-9_\:\-]*)\s*?=?\s*?(['"]?)([\s\S]*?)\2\s+/gim; const DOM_PARSER_RE = /(?:<(\/?)([a-zA-Z][a-zA-Z0-9\:-]*)(?:\s([^>]*?))?((?:\s*\/)?)>|(<\!\-\-)([\s\S]*?)(\-\->)|(<\!)([\s\S]*?)(>))/gm; diff --git a/test/basic.test.ts b/test/basic.test.ts index 9b18ae1..288c423 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -70,4 +70,26 @@ describe("attributes", () => { } = parse(`
`); expect(attributes).toMatchObject({ test: "", a: "b", c: "1" }); }); + it("with linebreaks", async () => { + const { + children: [{ attributes }], + } = parse(`
`); + expect(attributes).toMatchObject({ a: "1\n2\n3" }); + }); + it("with single quote", async () => { + const { + children: [{ attributes }], + } = parse(`
`); + expect(attributes).toMatchObject({ a: "nate'\ns" }); + }); + it("with escaped double quote", async () => { + const { + children: [{ attributes }], + } = parse(`
`); + expect(attributes).toMatchObject({ a: "\"never\nmore\"" }); + }); });