Closed Toilal closed 3 years ago
If you want to specify a literal backslash, you need to escape it, otherwise, it's essentially escaping the .
The following should work:
const nhp = require('node-html-parser');
const res = nhp.parse('<div id="equipment-basin.material"></div>');
console.log(res.querySelector('#equipment-basin\\.material'));
Sadly it's not consistent with V8 dom parser, but it was on v4.1.4. A single backslash should be OK.
I am sorry to hear that this has caused issues for you. That said, I'll respond to a few points and hope it can help clear things up.
It [worked] on v4.1.4
I tested with 4.1.3
, 4.1.4
, 4.1.5
, and 5.0.0
. Unfortunately, however, each had the same result.
It's not consistent with V8 dom parser
Here is the result with v8:
html:
select:
The issue is not a matter of the parser. Rather, it's how strings are handled. If you put \.
in a string, you're essentially telling it:
.
The problem is, the dot does not need escaping in storing a string, so it effectively does not change how the string is stored
Below is an example which shows how strings are stored. It will help demonstrate what the actual parse library is receiving and why it's not working.
Hope that helps!
I just upgraded from
v4.1.4
tov4.1.5
and it seems there's a regression when using escape characters in selectors.Consider this HTML element
This can find the element with
v4.1.4
but fails to find it withv4.1.5