-
Notifications
You must be signed in to change notification settings - Fork 956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Throws an error if the document element is null #439
Conversation
src/sizzle.js
Outdated
@@ -580,6 +580,9 @@ setDocument = Sizzle.setDocument = function( node ) { | |||
|
|||
// Return early if doc is invalid or already selected | |||
if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) { | |||
if ( !document ) { | |||
throw new Error( "Sizzle requires the document element" ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to start synthesizing errors for bad environment, but I would be willing to remove some of these guards and let errors happen naturally. Please try updating the early return condition to just if ( doc === document )
and see what happens with the test suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
if ( doc === document )
- couldn't build, got errorsPhantomJS 2.1.1 (Linux 0.0.0) selector class FAILED Died on test #24 global code@test/unit/selector.js:256:11: undefined is not a constructor (evaluating 'document.createElement("fieldset")') assert@dist/sizzle.js:387:33 setDocument@dist/sizzle.js:612:29 matchesSelector@dist/sizzle.js:988:14 test/unit/selector.js:298:36 runTest@node_modules/qunitjs/qunit/qunit.js:843:32 run@node_modules/qunitjs/qunit/qunit.js:828:11 node_modules/qunitjs/qunit/qunit.js:970:14 process@node_modules/qunitjs/qunit/qunit.js:629:24 begin@node_modules/qunitjs/qunit/qunit.js:611:9 node_modules/qunitjs/qunit/qunit.js:671:9 Expected 27 assertions, but 24 were run global code@test/unit/selector.js:256:11 ...
-
if ( doc === document || doc.nodeType !== 9 )
- built, tested, no errorsThough it's pretty much like the original version, it produces a better error
TypeError: "docElem is null"
ifdocument.documentElement === null
, instead ofTypeError: "document is undefined"
This is not related to this PR: test/index.html
every test fails on Firefox (beforeEach failed on element: context is null
), Chrome wouldn't load the test (jquery.js:8526 Access to XMLHttpRequest at 'Sizzle/test/data/fixtures.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
) but it works on Edge - all the tests pass, is this a known issue or should I open a new issue?
Closes jquerygh-439 (cherry picked from commit 7d92424)
This is a resubmit of jquerygh-439 that was lost during the switch from JSHint + JSCS to ESLint Ref jquerygh-442 Ref jquerygh-439 (cherry picked from commit 7d92424)
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
This is a resubmit of jquerygh-439 that was lost during the switch from JSHint + JSCS to ESLint Ref jquerygh-442 Ref jquerygh-439 (cherry picked from commit 7d92424)
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Ref jquery/sizzle#442 Ref jquery/sizzle#439
With new selector code doing less convoluted support tests, it was possible to extract a lot of logic out of setDocument & also reduce size. This commit also backports jquery/sizzle#439 that was reverted by mistake during a switch from JSHint + JSCS to ESLint. Closes gh-4462 Ref jquery/sizzle#442 Ref jquery/sizzle#439
For the record, this change was accidentally reverted in #442 (62061d0) and I tried to bring it back in #452 but I discovered it breaks running Sizzle with context inside of a I created #458 to not lose track of an issue. That said, it's not likely we'll fix the issue unless someone submits a PR as Sizzle is slowly getting retired and jQuery 4.0 will no longer depend on it. |
If Sizzle/jQuery is injected to a page before the document element is constructed (
window.document.documentElement === null
), it fails to initialize. I would like to reference jquery/jquery#3333 to describe the issue.setDocument()
however simply returnsdocument
in such case -- even if the variabledocument
hasn't been assigned yet (when Sizzle is initializing).It later produces an error
TypeError: "document is undefined"
inassert()
though the problem was actually there whensetDocument()
silently returned. This makes it harder for a dev-user to know what's just happened aswindow.document
exists butwindow.document.documentElement
doesn't. This pull request is intended to make the error verbose.