jsdom crash: Cannot call method 'appendChild' of null #389

Closed
lbdremy opened this Issue Jan 24, 2012 · 22 comments

Comments

Projects
None yet
10 participants
Contributor

lbdremy commented Jan 24, 2012

documentElement is null because the HTML retrieved does not contain the body tag, so the first time that the appendChild() function is called to append the script tag, the error occurs. I know that's madness but it happens. :S
How do you want to handle this case?

I'm going to attach a test for this issue.

Owner

tmpvar commented Jan 24, 2012

in your test, <html> should be the documentElement no?

I think what you may have meant is that since there is no body, you cant add a script to it. I think in this case we should ensure a body exists before inserting the script tag.

Contributor

lbdremy commented Jan 30, 2012

The html tag should be the documentElement but here window.document.documentElement is null, the html tag is present and the only tag missing in this piece of html is the body tag.

If the body tag is present in the html page window.document.documentElement is not null.
So it appears that something goes wrong for the window.document.documentElement if the body tag is missing and this should not occur.
Do you agree or I missed something?

the error log of the test:

✖ jsdom/index/issue_389_documentElement_is_null
Cannot call method 'appendChild' of null TypeError: Cannot call method 'appendChild' of null
    at /home/lbdremy/workspace/nodejs/jsdom/lib/jsdom.js:249:41
    at Array.forEach (native)
    at /home/lbdremy/workspace/nodejs/jsdom/lib/jsdom.js:231:18
    at Object.env (/home/lbdremy/workspace/nodejs/jsdom/lib/jsdom.js:261:5)
    at Object.<anonymous> (/home/lbdremy/workspace/nodejs/jsdom/test/jsdom/index.js:1263:11)
    at Object.<anonymous> (/home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/core.js:233:16)
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/core.js:233:16
    at Object.runTest (/home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/core.js:69:9)
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/core.js:115:25
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/deps/async.js:508:13
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/deps/async.js:118:13
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/deps/async.js:129:25
    at /home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/deps/async.js:510:17
    at Array.<anonymous> (/home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/types.js:146:17)
    at EventEmitter._tickCallback (node.js:126:26) Error
    at /home/lbdremy/workspace/nodejs/jsdom/test/runner:185:58
    at Array.forEach (native)
    at Object.testDone (/home/lbdremy/workspace/nodejs/jsdom/test/runner:170:18)
    at Array.0 (/home/lbdremy/workspace/nodejs/jsdom/node_modules/nodeunit/lib/types.js:145:25)
    at EventEmitter._tickCallback (node.js:126:26)

Owner

tmpvar commented Jan 30, 2012

I completely agree, <html> is the documentElement in html documents.

Any updates with this issue? still happening to me..

Happening to me too... :(

├─┬ jsdom@0.2.15
│ ├─┬ contextify@0.1.3
│ │ └── bindings@1.0.0
│ ├── cssom@0.2.5
│ └── htmlparser@1.7.6

Is there a fix/workaround for this issue?

Collaborator

domenic commented Oct 30, 2012

Sounds like something worth prioritizing for the next release. Possibly parsing-related—shouldn't the parser give us an empty body tag in this case?

Hi, same problem here, but I think I've got a hint. If you use jsdom to parse http://richskinnypretty.com/ you get this error, but if you parse the following http://richskinnypretty.blogspot.com.es/ you don't
The former has iframes, the later doesn't. So I think the problem is parsing a document with iframes

Collaborator

domenic commented Apr 15, 2013

This issue no longer contains a test case, so I can't really see if it's fixed or how to fix it. But, very soon we're going to have a new release with a new HTML parser that will likely not have this problem.

domenic closed this Apr 15, 2013

+1 , just experienced this issue with release 0.6.1

Collaborator

domenic commented May 13, 2013

@pdelanauze were you able to create a test case we can reproduce this with?

Contributor

fb55 commented May 14, 2013

@domenic #617 also suffers from this problem. Haven't tracked it down yet.

@ghost

ghost commented Sep 6, 2013

+1, I am also experiencing this.

Collaborator

domenic commented Sep 6, 2013

@tamilvendhank as with everyone else in this thread, I will ask: were you able to create a test case we can reproduce this with?

@ghost

ghost commented Sep 6, 2013

I am running behind deadline now. But, I will definitely create a test case in the weekend. Is there any template available for creating testcase?

Collaborator

domenic commented Sep 6, 2013

Something like this would work:

https://github.com/tmpvar/jsdom/blob/master/test/jsdom/parsing.js#L136-L146

Basically just find a string that you can do var doc = jsdom(theString) and then do some minimal operations that trigger the error.

@ghost

ghost commented Sep 6, 2013

ok, I will do it. Thanks!

jhnns commented Apr 7, 2014

I've created a minimal test-case that triggers the error:

jsdom.env({
    html: "<html></html>",
    src: "''",
    done: function () { console.log("done"); }
});
/media/sf_dev/test/node_modules/jsdom/lib/jsdom.js:294
      window.document.documentElement.appendChild(script);
                                      ^
TypeError: Cannot call method 'appendChild' of null
    at /media/sf_dev/test/node_modules/jsdom/lib/jsdom.js:294:39

The html has no body-element which causes jsdom to break when the Javascript-src '' is appended to document.documentElement

Collaborator

domenic commented Apr 8, 2014

Ugh, another one caused by the parser not handling empty documents correctly :(

domenic reopened this Apr 8, 2014

jhnns commented Apr 8, 2014

👍

Many ad networks are using these empty documents to do all kind of iframe magic.

domenic referenced this issue Jul 9, 2014

Closed

Add tests for all outstanding [parsing] issues #829

14 of 14 tasks complete

domenic closed this in b7d69c5 Jul 9, 2014

jhnns commented Jul 10, 2014

Cool! Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment