@@ -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+
413436const 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} ;
0 commit comments