· 7 years ago · Feb 19, 2018, 10:36 PM
1/*
2Ancestry - jquery.ancestry.js
3As discussed in the jQuery Development Google Group.
4Released under the MIT license.
5
6Involved: Michael Geary, Diego Perini, John-David Dalton, John Resig, and Nathan Hammond
7Compiled: Nathan Hammond
8*/
9
10jQuery.comparePosition = function ( element, context) {
11 jQuery.comparePosition =
12 document.documentElement.compareDocumentPosition ?
13 function ( element, context ) {
14 return !!( element.compareDocumentPosition(context) & 8 );
15 }
16 : document.documentElement.contains ?
17 function ( element, context ) {
18 return element != context && context.contains(element);
19 }
20 :
21 function ( element, context ) {
22 for ( ; element != context; element = element.parentNode )
23 if ( !element ) return false;
24 return true;
25 };
26 return jQuery.comparePosition( element, context );
27}
28
29jQuery.fn.ancestorOf = function ( context ) {
30 return this.filter(function() {
31 return jQuery.comparePosition( context, this );
32 });
33};
34jQuery.fn.descendantOf = function ( context ) {
35 return this.filter(function() {
36 return jQuery.comparePosition( this, context );
37 });
38};
39
40/*
41Noted here if you wish to add parallel functionality to $()
42
43if ( selector.nodeType ) {
44 // Handle $(DOMElement, context)
45 if ( context && !jQuery.comparePosition( selector, context ) ) {
46 return jQuery( [] );
47 }
48 // Handle $(DOMElement)
49 this[0] = selector;
50 this.length = 1;
51 return this;
52}
53*/