Skip to content

Commit

Permalink
Add nodes.parentNodes() and nodes.nonparentNodes().
Browse files Browse the repository at this point in the history
Ref: Add a method for nodes.nonparentNodes() to get the childless nodes #3182
  • Loading branch information
maxkfranz committed Dec 4, 2023
1 parent d82a5bc commit 4f207f2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions documentation/docmaker.json
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,31 @@
]
},

{
"name": "nodes.parentNodes",
"descr": "Get all parent nodes (i.e. has compound children) in the calling collection.",
"formats": [
{
"args": [
{ "name": "selector", "descr": "A selector used to filter the resultant collection.", "optional": true }
]
}
]
},

{
"name": "nodes.nonparentNodes",
"pureAliases": ["eles.childlessNodes"],
"descr": "Get all non-parent nodes (i.e. has no compound children) in the calling collection.",
"formats": [
{
"args": [
{ "name": "selector", "descr": "A selector used to filter the resultant collection.", "optional": true }
]
}
]
},

{
"name": "nodes.children",
"descr": "Get all compound child (i.e. direct descendant) nodes of each node in the collection.",
Expand Down
9 changes: 9 additions & 0 deletions src/collection/compounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ let elesfn = ({
add( this.children() );

return this.spawn( elements, true ).filter( selector );
},

parentNodes: function(selector) {
return this.filter(el => el.isParent()).filter(selector);
},

nonparentNodes: function(selector) {
return this.filter(el => el.isChildless()).filter(selector);
}
});

Expand Down Expand Up @@ -215,5 +223,6 @@ elesfn.forEachUpAndDown = function( fn, includeSelf = true ){

// aliases
elesfn.ancestors = elesfn.parents;
elesfn.childlessNodes = elesfn.nonparentNodes;

export default elesfn;
9 changes: 9 additions & 0 deletions test/collection-compound-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe('Collection compound nodes', function(){
expect( cy.elements().nonorphans().same( n2.add(n3).add(n4) ) ).to.be.true;
});

it('nodes.parentNodes()', function(){
expect( cy.elements().parentNodes().same( n1.add(n2) ) ).to.be.true;
});

it('nodes.nonparentNodes()', function(){
expect( cy.elements().nonparentNodes().same( n3.add(n4) ) ).to.be.true;
expect( cy.elements().childlessNodes().same( n3.add(n4) ) ).to.be.true; // alias
});

it('child.position() moves parent', function(){
var p1 = {
x: n2.position().x,
Expand Down

0 comments on commit 4f207f2

Please sign in to comment.