8000 fix: handle id with comma · html-validate/html-validate@435abf5 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 435abf5

Browse files
committed
fix: handle id with comma
fixes #299
1 parent f2bf099 commit 435abf5

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

src/__snapshots__/regression.spec.ts.snap

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ exports[`regression tests test-files/issues/issue-138-srcset-line-wrapping.html
66

77
exports[`regression tests test-files/issues/issue-154-case-insensitive-attr.html 1`] = `[]`;
88

9+
exports[`regression tests test-files/issues/issue-299-comma-id.html 1`] = `
10+
[
11+
{
12+
"errorCount": 1 10000 ,
13+
"filePath": "test-files/issues/issue-299-comma-id.html",
14+
"messages": [
15+
{
16+
"column": 10,
17+
"context": {
18+
"id": "start,-end",
19+
"kind": 4,
20+
},
21+
"line": 1,
22+
"message": "element id "start,-end" must only contain letters, digits, dash and underscore characters",
23+
"offset": 9,
24+
"ruleId": "valid-id",
25+
"ruleUrl": "https://html-validate.org/rules/valid-id.html",
26+
"selector": "#start\\,-end",
27+
"severity": 2,
28+
"size": 10,
29+
},
30+
],
31+
"source": "<div id="start,-end"></div>
32+
",
33+
"warningCount": 0,
34+
},
35+
]
36+
`;
37+
938
exports[`regression tests test-files/issues/issue27-disable-block.html 1`] = `[]`;
1039

1140
exports[`regression tests test-files/issues/issue35-dynamic-values.html 1`] = `[]`;

src/dom/htmlelement.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,17 @@ describe("HtmlElement", () => {
989989
expect(el.map((it) => it.id)).toEqual(["third"]);
990990
});
991991

992+
it("should handle escaped comma", async () => {
993+
expect.assertions(2);
994+
const markup = /* HTML */ ` <div id="foo,bar"></div> `;
995+
const resolvedConfig = await Config.empty().resolve();
996+
const parser = new Parser(resolvedConfig);
997+
const document = parser.parseHtml(markup);
998+
const el = document.querySelector("#foo\\,bar");
999+
expect(el?.tagName).toBe("div");
1000+
expect(el?.id).toBe("foo,bar");
1001+
});
1002+
9921003
it("should return null if nothing matches", () => {
9931004
expect.assertions(1);
9941005
const el = document.querySelector("foobar");
@@ -1025,6 +1036,18 @@ describe("HtmlElement", () => {
10251036
expect(el[2].tagName).toBe("p");
10261037
});
10271038

1039+
it("should handle escaped comma", async () => {
1040+
expect.assertions(3);
1041+
const markup = /* HTML */ ` <div id="foo,bar"></div> `;
1042+
const resolvedConfig = await Config.empty().resolve();
1043+
const parser = new Parser(resolvedConfig);
1044+
const document = parser.parseHtml(markup);
1045+
const el = document.querySelectorAll("#foo\\,bar");
1046+
expect(el).toHaveLength(1);
1047+
expect(el[0].tagName).toBe("div");
1048+
expect(el[0].id).toBe("foo,bar");
1049+
});
1050+
10281051
it("should return [] when nothing matches", () => {
10291052
expect.assertions(1);
10301053
const el = document.querySelectorAll("missing");

src/dom/htmlelement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ export class HtmlElement extends DOMNode {
652652
if (!selectorList) {
653653
return;
654654
}
655-
for (const selector of selectorList.split(/,\s*/)) {
655+
for (const selector of selectorList.split(/(?<!\\),\s*/)) {
656656
const pattern = new Selector(selector);
657657
yield* pattern.match(this);
658658
}

src/dom/selector/selector.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ describe("generateIdSelector()", () => {
168168
const id = "123foo";
169169
expect(generateIdSelector(id)).toBe('[id="123foo"]');
170170
});
171+
172+
it("should handle comma", () => {
173+
expect.assertions(1);
174+
const id = "foo,bar";
175+
expect(generateIdSelector(id)).toBe("#foo\\,bar");
176+
});
171177
});
172178

173179
describe("Selector", () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="start,-end"></div>

0 commit comments

Comments
 (0)
0