Skip to content

Commit

Permalink
Merge pull request #42 from natemoo-re/fix/attr-newlines
Browse files Browse the repository at this point in the history
Handle attributes with linebreaks
  • Loading branch information
natemoo-re authored Nov 28, 2022
2 parents 090c49c + 218d535 commit d538826
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-buckets-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ultrahtml": patch
---

Update attribute handling to account for attributes with newlines
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const VOID_TAGS = new Set<string>([
"wbr",
]);
const RAW_TAGS = new Set<string>(["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;

Expand Down
22 changes: 22 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@ describe("attributes", () => {
} = parse(`<div test a="b" c="1"></div>`);
expect(attributes).toMatchObject({ test: "", a: "b", c: "1" });
});
it("with linebreaks", async () => {
const {
children: [{ attributes }],
} = parse(`<div a="1
2
3"></div>`);
expect(attributes).toMatchObject({ a: "1\n2\n3" });
});
it("with single quote", async () => {
const {
children: [{ attributes }],
} = parse(`<div a="nate'
s"></div>`);
expect(attributes).toMatchObject({ a: "nate'\ns" });
});
it("with escaped double quote", async () => {
const {
children: [{ attributes }],
} = parse(`<div a="\"never
more\""></div>`);
expect(attributes).toMatchObject({ a: "\"never\nmore\"" });
});
});

0 comments on commit d538826

Please sign in to comment.