Skip to content

Commit

Permalink
Closest added, v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhannes committed Mar 14, 2017
1 parent 912126f commit e25afbb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ lq('li').last(); // Get the last matched li item.
lq('li').parent(); // Get the parent of the first matched item.
lq('ul').children(); // Return the children of all selected objects
lq('li').eq(0); // Get the first matched li item. By index, zero-based.
lq('ul').find('div'); // Get all div's in all matched ul elements. (querySelector match)
lq('ul').find('div'); // Traverse the DOM down from UL and select ALL DIVs in each UL.
lq('ul').closest('div'); // Traverse the DOM up from UL and select the first DIV for each UL.

// Advanced selection
lq('li').filter(function(item) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "litequery",
"version": "0.1.2",
"version": "0.1.3",
"description": "",
"main": "/dist/litequery-simple.js",
"scripts": {
Expand Down
39 changes: 39 additions & 0 deletions src/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,45 @@ export function selection( obj ) {
return select( items );
};

/**
* Return closest parent by selector from the current selections
*
* @param {String} selectItem Selector string
* @return {Object}
*/
obj.closest = function( selectItem ) {
let items = [];

function traverse( element, previous = null ) {
if ( !element.parentElement ) {
return [];
}

let potential = select( selectItem, element.parentElement ).elements;
let len = potential.length;

if ( potential && len ) {
if ( !previous || len === 1 ) {
return potential;
}

for ( let i = 0; i < len; i++ ) {
if ( potential[i] === previous ) {
return [potential[i]];
}
}
}

return traverse( element.parentElement, element );
}

obj.apply( ( element ) => {
items.push( ...traverse( element ) );
} );

return select( items );
};

/**
* Filter items
*
Expand Down
13 changes: 13 additions & 0 deletions tests/selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ describe( "Selection", () => {
expect( select( "div.parent" ).find( ".child" ).elements[0].className ).to.be.equal( "child" );
} );

it( "should find closest parent elements correctly", function () {
document.body.innerHTML = "<div class='parent'> <div class='child'> <div class='subchild1'>...</div> </div> <div class='child'> <div class='subchild2'>...</div> </div> </div>";

expect( select( ".subchild1" ).closest( ".child" ).elements.length ).to.be.equal( 1 );
expect( select( ".subchild1" ).closest( ".child" ).elements[0].className ).to.be.equal( "child" );

expect( select( ".subchild1" ).closest( ".parent" ).elements.length ).to.be.equal( 1 );
expect( select( ".subchild1" ).closest( ".parent" ).elements[0].className ).to.be.equal( "parent" );

expect( select( ".subchild2" ).closest( ".parent" ).elements.length ).to.be.equal( 1 );
expect( select( ".subchild2" ).closest( ".parent" ).elements[0].className ).to.be.equal( "parent" );
} );

it( "should filter elements correctly", function () {
document.body.innerHTML = "<span class='item'>...</span> <span class='notthis'>...</span>";

Expand Down

0 comments on commit e25afbb

Please sign in to comment.