@@ -172,10 +172,7 @@ function find( selector, context, results, seed ) {
172172
173173 // Try to shortcut find operations (as opposed to filters) in HTML documents
174174 if ( ! seed ) {
175-
176- if ( ( context ? context . ownerDocument || context : preferredDoc ) !== document ) {
177- setDocument ( context ) ;
178- }
175+ setDocument ( context ) ;
179176 context = context || document ;
180177
181178 if ( documentIsHTML ) {
@@ -425,7 +422,11 @@ function setDocument( node ) {
425422 doc = node ? node . ownerDocument || node : preferredDoc ;
426423
427424 // Return early if doc is invalid or already selected
428- if ( doc === document || doc . nodeType !== 9 ) {
425+ // Support: IE 11+, Edge 17 - 18+
426+ // IE/Edge sometimes throw a "Permission denied" error when strict-comparing
427+ // two documents; shallow comparisons work.
428+ // eslint-disable-next-line eqeqeq
429+ if ( doc == document || doc . nodeType !== 9 ) {
429430 return ;
430431 }
431432
@@ -436,7 +437,11 @@ function setDocument( node ) {
436437
437438 // Support: IE 9 - 11+, Edge 12 - 18+
438439 // Accessing iframe documents after unload throws "permission denied" errors (jQuery #13936)
439- if ( preferredDoc !== document &&
440+ // Support: IE 11+, Edge 17 - 18+
441+ // IE/Edge sometimes throw a "Permission denied" error when strict-comparing
442+ // two documents; shallow comparisons work.
443+ // eslint-disable-next-line eqeqeq
444+ if ( preferredDoc != document &&
440445 ( subWindow = document . defaultView ) && subWindow . top !== subWindow ) {
441446
442447 // Support: IE 9 - 11+, Edge 12 - 18+
@@ -449,11 +454,7 @@ find.matches = function( expr, elements ) {
449454} ;
450455
451456find . matchesSelector = function ( elem , expr ) {
452-
453- // Set document vars if needed
454- if ( ( elem . ownerDocument || elem ) !== document ) {
455- setDocument ( elem ) ;
456- }
457+ setDocument ( elem ) ;
457458
458459 if ( documentIsHTML &&
459460 ! nonnativeSelectorCache [ expr + " " ] &&
@@ -1413,14 +1414,24 @@ function matcherFromGroupMatchers( elementMatchers, setMatchers ) {
14131414 dirrunsUnique = ( dirruns += contextBackup == null ? 1 : Math . random ( ) || 0.1 ) ;
14141415
14151416 if ( outermost ) {
1416- outermostContext = context === document || context || outermost ;
1417+
1418+ // Support: IE 11+, Edge 17 - 18+
1419+ // IE/Edge sometimes throw a "Permission denied" error when strict-comparing
1420+ // two documents; shallow comparisons work.
1421+ // eslint-disable-next-line eqeqeq
1422+ outermostContext = context == document || context || outermost ;
14171423 }
14181424
14191425 // Add elements passing elementMatchers directly to results
14201426 for ( ; ( elem = elems [ i ] ) != null ; i ++ ) {
14211427 if ( byElement && elem ) {
14221428 j = 0 ;
1423- if ( ! context && elem . ownerDocument !== document ) {
1429+
1430+ // Support: IE 11+, Edge 17 - 18+
1431+ // IE/Edge sometimes throw a "Permission denied" error when strict-comparing
1432+ // two documents; shallow comparisons work.
1433+ // eslint-disable-next-line eqeqeq
1434+ if ( ! context && elem . ownerDocument != document ) {
14241435 setDocument ( elem ) ;
14251436 xml = ! documentIsHTML ;
14261437 }
0 commit comments