Skip to content

Commit 53d0840

Browse files
committed
Selector: Use shallow equality to compare documents to avoid IE/Edge 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
1 parent 29a9544 commit 53d0840

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/selector.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ function selectorError( msg ) {
154154
throw new Error( "Syntax error, unrecognized expression: " + msg );
155155
}
156156

157+
function areSameDocs( doc1, doc2 ) {
158+
159+
// Support: IE 11+, Edge 17 - 18+
160+
// IE/Edge sometimes throw a "Permission denied" error when strict-comparing
161+
// two documents; shallow comparisons work.
162+
// eslint-disable-next-line eqeqeq
163+
return doc1 == doc2;
164+
}
165+
157166
function find( selector, context, results, seed ) {
158167
var m, i, elem, nid, match, groups, newSelector,
159168
newContext = context && context.ownerDocument,
@@ -173,7 +182,10 @@ function find( selector, context, results, seed ) {
173182
// Try to shortcut find operations (as opposed to filters) in HTML documents
174183
if ( !seed ) {
175184

176-
if ( ( context ? context.ownerDocument || context : preferredDoc ) !== document ) {
185+
if ( !areSameDocs(
186+
context ? context.ownerDocument || context : preferredDoc,
187+
document
188+
) ) {
177189
setDocument( context );
178190
}
179191
context = context || document;
@@ -425,7 +437,7 @@ function setDocument( node ) {
425437
doc = node ? node.ownerDocument || node : preferredDoc;
426438

427439
// Return early if doc is invalid or already selected
428-
if ( doc === document || doc.nodeType !== 9 ) {
440+
if ( areSameDocs( doc, document ) || doc.nodeType !== 9 ) {
429441
return;
430442
}
431443

@@ -436,7 +448,7 @@ function setDocument( node ) {
436448

437449
// Support: IE 9 - 11+, Edge 12 - 18+
438450
// Accessing iframe documents after unload throws "permission denied" errors (jQuery #13936)
439-
if ( preferredDoc !== document &&
451+
if ( !areSameDocs( preferredDoc, document ) &&
440452
( subWindow = document.defaultView ) && subWindow.top !== subWindow ) {
441453

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

453465
// Set document vars if needed
454-
if ( ( elem.ownerDocument || elem ) !== document ) {
466+
if ( !areSameDocs( elem.ownerDocument || elem, document ) ) {
455467
setDocument( elem );
456468
}
457469

@@ -1413,14 +1425,15 @@ function matcherFromGroupMatchers( elementMatchers, setMatchers ) {
14131425
dirrunsUnique = ( dirruns += contextBackup == null ? 1 : Math.random() || 0.1 );
14141426

14151427
if ( outermost ) {
1416-
outermostContext = context === document || context || outermost;
1428+
outermostContext = areSameDocs( context, document ) || context || outermost;
14171429
}
14181430

14191431
// Add elements passing elementMatchers directly to results
14201432
for ( ; ( elem = elems[ i ] ) != null; i++ ) {
14211433
if ( byElement && elem ) {
14221434
j = 0;
1423-
if ( !context && elem.ownerDocument !== document ) {
1435+
1436+
if ( !context && !areSameDocs( elem.ownerDocument, document ) ) {
14241437
setDocument( elem );
14251438
xml = !documentIsHTML;
14261439
}

0 commit comments

Comments
 (0)