Skip to content

Commit

Permalink
Selector: Use shallow equality to compare documents to avoid IE/Edge …
Browse files Browse the repository at this point in the history
…crashes

IE/Edge sometimes crash when comparing documents between frames using the strict
equality operator (`===` & `!==`). Funnily enough, shallow comparisons
(`==` & `!=`) work without crashing.

Fixes jquerygh-4441
  • Loading branch information
mgol committed Aug 30, 2019
1 parent f810080 commit 6b1c6a7
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ function find( selector, context, results, seed ) {
// Try to shortcut find operations (as opposed to filters) in HTML documents
if ( !seed ) {

if ( ( context ? context.ownerDocument || context : preferredDoc ) !== document ) {
// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
if ( ( context ? context.ownerDocument || context : preferredDoc ) != document ) {
setDocument( context );
}
context = context || document;
Expand Down Expand Up @@ -425,7 +429,11 @@ function setDocument( node ) {
doc = node ? node.ownerDocument || node : preferredDoc;

// Return early if doc is invalid or already selected
if ( doc === document || doc.nodeType !== 9 ) {
// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
if ( doc == document || doc.nodeType !== 9 ) {
return;
}

Expand All @@ -436,7 +444,11 @@ function setDocument( node ) {

// Support: IE 9 - 11+, Edge 12 - 18+
// Accessing iframe documents after unload throws "permission denied" errors (jQuery #13936)
if ( preferredDoc !== document &&
// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
if ( preferredDoc != document &&
( subWindow = document.defaultView ) && subWindow.top !== subWindow ) {

// Support: IE 9 - 11+, Edge 12 - 18+
Expand All @@ -451,7 +463,11 @@ find.matches = function( expr, elements ) {
find.matchesSelector = function( elem, expr ) {

// Set document vars if needed
if ( ( elem.ownerDocument || elem ) !== document ) {
// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
if ( ( elem.ownerDocument || elem ) != document ) {
setDocument( elem );
}

Expand Down Expand Up @@ -1413,14 +1429,24 @@ function matcherFromGroupMatchers( elementMatchers, setMatchers ) {
dirrunsUnique = ( dirruns += contextBackup == null ? 1 : Math.random() || 0.1 );

if ( outermost ) {
outermostContext = context === document || context || outermost;

// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
outermostContext = context == document || context || outermost;
}

// Add elements passing elementMatchers directly to results
for ( ; ( elem = elems[ i ] ) != null; i++ ) {
if ( byElement && elem ) {
j = 0;
if ( !context && elem.ownerDocument !== document ) {

// Support: IE 11+, Edge 17 - 18+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
// two documents; shallow comparisons work.
// eslint-disable-next-line eqeqeq
if ( !context && elem.ownerDocument != document ) {
setDocument( elem );
xml = !documentIsHTML;
}
Expand Down

0 comments on commit 6b1c6a7

Please sign in to comment.