dom-parser
dom-parser copied to clipboard
parentNode returns null
const domParser = require("dom-parser");
const parser = new domParser();
const html = `
<!-- Based on example.org -->
<!doctype html>
<html>
<body>
<div id="container">
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p id="text"><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`;
const dom = parser.parseFromString(html);
console.log(`#text: ${dom.getElementById('text')}`);
console.log(`parentNode of #text: ${dom.getElementById('text').parentNode}`);
results in:
#text: [object Object]
parentNode of #text: null
const domParser = require("dom-parser");
const parser = new domParser();
const html = `
<div id="container">
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p id="text"><a href="http://www.iana.org/domains/example">More information...</a></p>
<div id="div"></div>
</div>
`;
const dom = parser.parseFromString(html);
console.log(`#text: ${dom.getElementById('text')}`);
console.log(`#div: ${dom.getElementById('div')}`);
console.log(`parentNode of #text: ${dom.getElementById('text').parentNode}`);
console.log(`parentNode of #div: ${dom.getElementById('div').parentNode}`);
"#text: [object Object]"
"#div: [object Object]"
"parentNode of #text: null"
"parentNode of #div: null"
I've encountered this issue only in Safari, weird...
@ckybonist Yeah, really weird. I have run into this issue in Node.js.
I have the same problem in Node.js. Has anyone managed to fix it?
Me too!