From 4f207f291ca8c0f07eaba8836fa0d145f984db71 Mon Sep 17 00:00:00 2001 From: Max Franz Date: Mon, 4 Dec 2023 14:39:52 -0500 Subject: [PATCH] Add `nodes.parentNodes()` and `nodes.nonparentNodes()`. Ref: Add a method for nodes.nonparentNodes() to get the childless nodes #3182 --- documentation/docmaker.json | 25 +++++++++++++++++++++++++ src/collection/compounds.js | 9 +++++++++ test/collection-compound-nodes.js | 9 +++++++++ 3 files changed, 43 insertions(+) diff --git a/documentation/docmaker.json b/documentation/docmaker.json index b11bc3d44e..cf8dbbec36 100644 --- a/documentation/docmaker.json +++ b/documentation/docmaker.json @@ -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.", diff --git a/src/collection/compounds.js b/src/collection/compounds.js index 33aebed255..fb15f145e0 100644 --- a/src/collection/compounds.js +++ b/src/collection/compounds.js @@ -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); } }); @@ -215,5 +223,6 @@ elesfn.forEachUpAndDown = function( fn, includeSelf = true ){ // aliases elesfn.ancestors = elesfn.parents; +elesfn.childlessNodes = elesfn.nonparentNodes; export default elesfn; diff --git a/test/collection-compound-nodes.js b/test/collection-compound-nodes.js index d2ca1804e3..a3e3c3988f 100644 --- a/test/collection-compound-nodes.js +++ b/test/collection-compound-nodes.js @@ -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,