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 ) {
$context = $ ( $context ) ;
2019-12-12 11:36:24 +00:00
selector = selector . trim ( ) ;
2015-10-27 11:15:43 +00:00
2024-04-19 08:37:25 +00:00
let matches ;
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 ) {
2024-09-16 13:28:41 +00:00
const selectors = selector . split ( ',' ) ;
let $elements = $ ( ) ;
2015-10-27 11:15:43 +00:00
2024-06-07 09:00:28 +00:00
selectors . forEach ( ( sel ) => {
2024-09-16 13:28:41 +00:00
$elements = $elements . add ( jQueryFindWithParent ( this , sel ) ) ;
2015-10-27 11:15:43 +00:00
} ) ;
return $elements ;
} ;
2018-11-12 19:22:46 +00:00
} ( ) ) ;