Debian Codegraph @

extensions/Thanks/modules/jquery.findWithParent.js

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