From 22276c271d687f9eca7e132f4ad2eb5a38c50f5b Mon Sep 17 00:00:00 2001 From: Hamed Baatour Date: Sun, 17 May 2020 15:18:07 +0300 Subject: [PATCH] fix: return valid escaped ids escape any spaces or any unsupported characters of returned ids in order to be used safely with document.querySelector() --- css-path.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/css-path.js b/css-path.js index 4b2b3c9..40017e0 100644 --- a/css-path.js +++ b/css-path.js @@ -35,7 +35,8 @@ var trim = require('trim') , id = elm.getAttribute('id') if (id) { - list.unshift(tag + '#' + trim(id)) + var escapeSelector = function(selector) {return "".replace.call(selector,/(^[^_a-zA-Z\u00a0-\uffff]|[^-_a-zA-Z0-9\u00a0-\uffff])/g, "\\$1")}; + list.unshift(tag + '#' + escapeSelector(trim(id))) return list } @@ -57,4 +58,4 @@ var trim = require('trim') module.exports = function (elm, rootNode) { return path(elm, rootNode, []).join(' > ') -} \ No newline at end of file +}