Skip to content

Commit 5aa7a52

Browse files
committed
feat(core dom): Add escape_css_id method.
Get an escaped CSS selector for a given id string. id selectors should - but don't have to - start with a letter. If the id starts with a number or a dash, it should be escaped. This method does that for you.
1 parent 082c233 commit 5aa7a52

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@patternslib/pat-tiptap": "^4.8.2",
4545
"@patternslib/pat-upload": "^3.1.1",
4646
"copy-webpack-plugin": "^11.0.0",
47+
"css.escape": "^1.5.1",
4748
"modernizr": "^3.12.0",
4849
"pegjs": "0.11.0-master.b7b87ea"
4950
},

src/core/dom.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,29 @@ const get_visible_ratio = (el, container) => {
410410
return visible_ratio;
411411
};
412412

413+
/**
414+
* Get an escaped CSS selector for a given id string.
415+
*
416+
* id selectors should - but don't have to - start with a letter.
417+
* If the id starts with a number or a dash, it should be escaped.
418+
* This method does that for you.
419+
*
420+
* Alse see:
421+
* - https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
422+
* - https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape
423+
*
424+
* @param {String} id - The id to escape.
425+
*
426+
* @returns {String} - The escaped CSS selector.
427+
*
428+
* @example
429+
* escape_css_id_selector("#123"); // returns "#\\31 23""
430+
* escape_css_id_selector("#-123"); // returns "#-\\31 23"
431+
*/
432+
const escape_css_id = (id) => {
433+
return `#${CSS.escape(id.split("#")[1])}`;
434+
};
435+
413436
const dom = {
414437
toNodeArray: toNodeArray,
415438
querySelectorAllAndMe: querySelectorAllAndMe,
@@ -432,6 +455,7 @@ const dom = {
432455
delete_data: delete_data,
433456
template: template,
434457
get_visible_ratio: get_visible_ratio,
458+
escape_css_id: escape_css_id,
435459
add_event_listener: events.add_event_listener, // BBB export. TODO: Remove in an upcoming version.
436460
remove_event_listener: events.remove_event_listener, // BBB export. TODO: Remove in an upcoming version.
437461
};

src/core/dom.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,21 @@ describe("core.dom tests", () => {
856856
});
857857
});
858858
});
859+
860+
describe("escape_css_id", function () {
861+
it("returns a standard id selector as-is", function () {
862+
expect(dom.escape_css_id("#foo")).toBe("#foo");
863+
});
864+
865+
it("returns an escaped version when the id is a number", function () {
866+
expect(dom.escape_css_id("#123")).toBe("#\\31 23");
867+
});
868+
869+
it("returns an escaped version when the id starts with a number", function () {
870+
expect(dom.escape_css_id("#1foo")).toBe("#\\31 foo");
871+
});
872+
873+
it("returns an escaped version when the id starts with a dash", function () {
874+
expect(dom.escape_css_id("#-1-2-3")).toBe("#-\\31 -2-3");
875+
});
876+
});

src/setup-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ import dom from "./core/dom";
1919
dom.is_visible = (el) => {
2020
return !el.hidden && el.style.display !== "none";
2121
};
22+
23+
// polyfill css.escape for jsdom
24+
import("css.escape");

0 commit comments

Comments
 (0)