dom-parser icon indicating copy to clipboard operation
dom-parser copied to clipboard

parentNode returns null

Open SukkaW opened this issue 5 years ago • 4 comments

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

SukkaW avatar Oct 15 '19 17:10 SukkaW

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"

SukkaW avatar Oct 27 '19 13:10 SukkaW

I've encountered this issue only in Safari, weird...

ckybonist avatar Nov 01 '19 06:11 ckybonist

@ckybonist Yeah, really weird. I have run into this issue in Node.js.

SukkaW avatar Nov 01 '19 07:11 SukkaW

I have the same problem in Node.js. Has anyone managed to fix it?

Undero13 avatar Nov 03 '19 10:11 Undero13

Me too!

mister-teddy avatar Apr 14 '23 16:04 mister-teddy