blob: c8ec6001258c06737052ec7d8a1febd275488d00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
( function () {
/** @class jQuery */
/**
* Adds support to find parent elements using .closest with less-than selector syntax.
*
* $.findWithParent( $div, "< html div < body" ); // find closest parent of $div "html", find child "div" of it, find closest parent "body" of that, return "body"
* $( '#foo' ).findWithParent( '.bar < .baz' ); // find child ".bar" of "#foo", return closest parent ".baz" from there
*
* @method findWithParent
* @param {jQuery|HTMLElement|string} $context
* @param {string} selector
* @return {jQuery}
*/
function jQueryFindWithParent( $context, selector ) {
var matches;
$context = $( $context );
selector = selector.trim();
while ( selector && ( matches = selector.match( /(.*?(?:^|[>\s+~]))(<\s*[^>\s+~]+)(.*?)$/ ) ) ) {
if ( matches[ 1 ].trim() ) {
$context = $context.find( matches[ 1 ] );
}
if ( matches[ 2 ].trim() ) {
$context = $context.closest( matches[ 2 ].substr( 1 ) );
}
selector = matches[ 3 ].trim();
}
if ( selector ) {
$context = $context.find( selector );
}
return $context;
}
$.findWithParent = jQueryFindWithParent;
/** @class jQuery.fn */
/**
* @param {string} selector
* @return {jQuery}
* @see jQuery#findWithParent
*/
$.fn.findWithParent = function ( selector ) {
var selectors = selector.split( ',' ),
$elements = $(),
self = this;
selectors.forEach( function ( selector ) {
$elements = $elements.add( jQueryFindWithParent( self, selector ) );
} );
return $elements;
};
}() );
|