Skip to content

Commit

Permalink
Core: Allow shadow DOM in attachment check
Browse files Browse the repository at this point in the history
Allow isAttached to check shadow DOM for attachment. This change allows
isAttached() function to address shadowDOM elements even if the browser doesn't support getRootNode()

Ref jquerygh-3504, jquerygh-3977
  • Loading branch information
SaptakS committed Mar 26, 2018
1 parent 662083e commit b37ae7e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/var/getShadowRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
define( [
"./document"
], function( document ) {
"use strict";

return function getShadowRoot( elem ) {

// Check if browser supports Shadow DOM
if ( document.head.createShadowRoot || document.head.attachShadow ) {

// check for support of getRootNode function
while ( typeof elem.getRootNode !== "function" ) {
var doc = elem.ownerDocument;
elem = doc && doc.host ? doc.host : doc;
}
return elem.getRootNode( { composed: true } );
}

return false;
};

} );
11 changes: 7 additions & 4 deletions src/var/isAttached.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
define( [
"../core",
"./getShadowRoot",
"../selector" // Get jQuery.contains
], function( jQuery ) {
], function( jQuery, getShadowRoot ) {
"use strict";

return function isAttached( obj ) {
return jQuery.contains( obj.ownerDocument, obj );
};
// Replace the use of contains function to check attachment (gh-3504)
return function isAttached( elem ) {

return jQuery.contains( elem.ownerDocument, elem ) ||
getShadowRoot( elem ) === elem.ownerDocument;
};
} );

0 comments on commit b37ae7e

Please sign in to comment.