2018-11-12 19:22:46 +00:00
( function ( ) {
2015-10-27 11:15:43 +00:00
/** @class jQuery */
/ * *
2019-12-12 11:36:24 +00:00
* Adds support to find parent elements using . closest with less - than selector syntax .
2015-10-27 11:15:43 +00:00
*
* $ . 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 ) ;
2019-12-12 11:36:24 +00:00
selector = selector . trim ( ) ;
2015-10-27 11:15:43 +00:00
while ( selector && ( matches = selector . match ( /(.*?(?:^|[>\s+~]))(<\s*[^>\s+~]+)(.*?)$/ ) ) ) {
2019-12-12 11:36:24 +00:00
if ( matches [ 1 ] . trim ( ) ) {
2015-10-27 11:15:43 +00:00
$context = $context . find ( matches [ 1 ] ) ;
}
2019-12-12 11:36:24 +00:00
if ( matches [ 2 ] . trim ( ) ) {
2022-05-21 08:36:17 +00:00
$context = $context . closest ( matches [ 2 ] . slice ( 1 ) ) ;
2015-10-27 11:15:43 +00:00
}
2019-12-12 11:36:24 +00:00
selector = matches [ 3 ] . trim ( ) ;
2015-10-27 11:15:43 +00:00
}
if ( selector ) {
$context = $context . find ( selector ) ;
}
return $context ;
}
$ . findWithParent = jQueryFindWithParent ;
/** @class jQuery.fn */
/ * *
* @ param { string } selector
* @ return { jQuery }
2019-12-12 11:36:24 +00:00
* @ see jQuery # findWithParent
2015-10-27 11:15:43 +00:00
* /
$ . fn . findWithParent = function ( selector ) {
var selectors = selector . split ( ',' ) ,
$elements = $ ( ) ,
self = this ;
2022-01-07 15:18:13 +00:00
selectors . forEach ( function ( sel ) {
$elements = $elements . add ( jQueryFindWithParent ( self , sel ) ) ;
2015-10-27 11:15:43 +00:00
} ) ;
return $elements ;
} ;
2018-11-12 19:22:46 +00:00
} ( ) ) ;