Commit c2e316c6 authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(dom): `generateSelector()` escapes characters

fixes #108
parent 30e75036
Pipeline #264414693 passed with stages
in 11 minutes and 30 seconds
......@@ -442,6 +442,45 @@ describe("HtmlElement", () => {
expect(el.generateSelector()).toEqual("#foo > p");
});
it("should handle colon in id", () => {
expect.assertions(1);
const document = parser.parseHtml(`
<div>
<div id="foo:">
<p></p>
</div>
</div>
`);
const el = document.querySelector("p");
expect(el.generateSelector()).toEqual("#foo\\: > p");
});
it("should handle space in id", () => {
expect.assertions(1);
const document = parser.parseHtml(`
<div>
<div id="foo ">
<p></p>
</div>
</div>
`);
const el = document.querySelector("p");
expect(el.generateSelector()).toEqual("#foo\\ > p");
});
it("should handle bracket in id", () => {
expect.assertions(1);
const document = parser.parseHtml(`
<div>
<div id="foo[bar]">
<p></p>
</div>
</div>
`);
const el = document.querySelector("p");
expect(el.generateSelector()).toEqual("#foo\\[bar\\] > p");
});
it("should normalize tagnames", () => {
expect.assertions(1);
const document = parser.parseHtml(`<dIV></DIv>`);
......
......@@ -143,9 +143,10 @@ export class HtmlElement extends DOMNode {
for (let cur: HtmlElement = this; cur.parent; cur = cur.parent) {
/* if a unique id is present, use it and short-circuit */
if (cur.id) {
const matches = root.querySelectorAll(`#${cur.id}`);
const escaped = cur.id.replace(/([:[\] ])/g, "\\$1");
const matches = root.querySelectorAll(`#${escaped}`);
if (matches.length === 1) {
parts.push(`#${cur.id}`);
parts.push(`#${escaped}`);
break;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment