From c5799aafcc8031959275788ee541c17a61edaaf5 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 28 Nov 2022 10:48:08 -0600 Subject: [PATCH 1/2] handle attributes with linebreaks --- .changeset/perfect-buckets-add.md | 5 +++++ src/index.ts | 2 +- test/basic.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/perfect-buckets-add.md 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..763ab5a 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*?(['"]?)([\w\W]*?)\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\"" }); + }); }); From 218d5354baa5f4f7d0ea28aa9a361f19d372d692 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 28 Nov 2022 10:49:06 -0600 Subject: [PATCH 2/2] Update index.ts --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 763ab5a..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*?(['"]?)([\w\W]*?)\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;