Skip to content

Commit

Permalink
Core: Use isAttached to check for attachment of element
Browse files Browse the repository at this point in the history
This change replaces the use of contains to check for attachment
by isAttached function

Ref jquerygh-3504
  • Loading branch information
SaptakS committed Mar 2, 2018
1 parent 294a369 commit cb0ebc4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/css/curCSS.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
define( [
"../core",
"../var/isAttached",
"./var/rboxStyle",
"./var/rnumnonpx",
"./var/getStyles",
"./support",
"../selector" // Get jQuery.contains
], function( jQuery, rboxStyle, rnumnonpx, getStyles, support ) {
"./support"
], function( jQuery, isAttached, rboxStyle, rnumnonpx, getStyles, support ) {

"use strict";

Expand All @@ -26,7 +26,7 @@ function curCSS( elem, name, computed ) {
if ( computed ) {
ret = computed.getPropertyValue( name ) || computed[ name ];

if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) {
if ( ret === "" && !isAttached( elem ) ) {
ret = jQuery.style( elem, name );
}

Expand Down
6 changes: 3 additions & 3 deletions src/css/var/isHiddenWithinTree.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
define( [
"../../core",
"../../selector"
"../../var/isAttached"

// css is assumed
], function( jQuery ) {
], function( jQuery, isAttached ) {
"use strict";

// isHiddenWithinTree reports if an element has a non-"none" display style (inline and/or
Expand All @@ -27,7 +27,7 @@ define( [
// Support: Firefox <=43 - 45
// Disconnected elements can have computed display: none, so first confirm that elem is
// in the document.
jQuery.contains( elem.ownerDocument, elem ) &&
isAttached( elem ) &&

jQuery.css( elem, "display" ) === "none";
};
Expand Down
7 changes: 4 additions & 3 deletions src/manipulation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define( [
"./core",
"./var/isAttached",
"./var/concat",
"./var/isFunction",
"./var/push",
Expand All @@ -23,7 +24,7 @@ define( [
"./traversing",
"./selector",
"./event"
], function( jQuery, concat, isFunction, push, access,
], function( jQuery, isAttached, concat, isFunction, push, access,
rcheckableType, rtagName, rscriptType,
wrapMap, getAll, setGlobalEval, buildFragment, support,
dataPriv, dataUser, acceptData, DOMEval, nodeName ) {
Expand Down Expand Up @@ -223,7 +224,7 @@ function remove( elem, selector, keepData ) {
}

if ( node.parentNode ) {
if ( keepData && jQuery.contains( node.ownerDocument, node ) ) {
if ( keepData && isAttached( node ) ) {
setGlobalEval( getAll( node, "script" ) );
}
node.parentNode.removeChild( node );
Expand All @@ -241,7 +242,7 @@ jQuery.extend( {
clone: function( elem, dataAndEvents, deepDataAndEvents ) {
var i, l, srcElements, destElements,
clone = elem.cloneNode( true ),
inPage = jQuery.contains( elem.ownerDocument, elem );
inPage = isAttached( elem );

// Fix IE cloning issues
if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
Expand Down
9 changes: 5 additions & 4 deletions src/manipulation/buildFragment.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
define( [
"../core",
"../core/toType",
"../var/isAttached",
"./var/rtagName",
"./var/rscriptType",
"./wrapMap",
"./getAll",
"./setGlobalEval"
], function( jQuery, toType, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) {
], function( jQuery, toType, isAttached, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) {

"use strict";

var rhtml = /<|&#?\w+;/;

function buildFragment( elems, context, scripts, selection, ignored ) {
var elem, tmp, tag, wrap, contains, j,
var elem, tmp, tag, wrap, attached, j,
fragment = context.createDocumentFragment(),
nodes = [],
i = 0,
Expand Down Expand Up @@ -77,13 +78,13 @@ function buildFragment( elems, context, scripts, selection, ignored ) {
continue;
}

contains = jQuery.contains( elem.ownerDocument, elem );
attached = isAttached( elem );

// Append to fragment
tmp = getAll( fragment.appendChild( elem ), "script" );

// Preserve script evaluation history
if ( contains ) {
if ( attached ) {
setGlobalEval( tmp );
}

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

return function isAttached( obj ) {
return jQuery.contains( obj.ownerDocument, obj );
};

} );

0 comments on commit cb0ebc4

Please sign in to comment.