querySelector throws when applied to Document fragments #523

marcelklehr opened this Issue Oct 20, 2012 · 4 comments


None yet

2 participants

var window = jsdom.jsdom('<a href="http://foo"></a>', null, {features:{QuerySelector: true}}).createWindow()


contains = Sizzle.contains = docElem.contains ?
TypeError: Cannot read property 'contains' of null
    at module.exports (jsdom\lib\jsdom\selectors\sizzle.js:334:37)
    at exports.applyQuerySelector.addSizzle (jsdom\lib\jsdom\selectors\index.js:6:26)
    at Object.exports.applyQuerySelector.doc.querySelector (jsdom\lib\jsdom\selectors\index.js:12:12)

I want to be able to use css selectors on document fragments, too, though!

domenic commented Oct 20, 2012

Nice catch! Will be sure to fix as part of #508; in the meantime, feel free to work up a pull request to make it work with the existing Sizzle-based querySelector.


I'm not very knowladgable about jsdom development, so I'll post my suggestion here:

Change line 27 in lib/jsdom/selectors/sizzle.js from

docElem = document.documentElement,


docElem = document.documentElement || document.firstChild,

This works for me. I dunno if it passes all your tests, though...


An other approach would be to always set document.documentElement to the highest level dom elment that exists. But I dunno where documentElement is set so....


Oh, my above suggestion won't work for markups like

<a href="http://foo">foo</a><a href="http://bar">bar</a>

Perhaps, you should construct a synthetic 'gobal' element in this case.

@domenic domenic added parsing and removed TESTME querySelector labels Jul 9, 2014
@domenic domenic referenced this issue Jul 9, 2014

Add tests for all outstanding [parsing] issues #829

14 of 14 tasks complete
@domenic domenic closed this in aae6fd6 Jul 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment