File tree Expand file tree Collapse file tree 3 files changed +43
-2
lines changed
tests/Rct567/DomQuery/Tests Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ echo $links->get(-1)->textContent; // output 3
5656
5757- ` .find( selector ) `
5858- ` .children( [selector] ) `
59+ - ` .parent( [selector] ) `
5960- ` .next( [selector] ) `
6061- ` .prev( [selector] ) `
6162
Original file line number Diff line number Diff line change @@ -367,6 +367,33 @@ public function children($selector='*') {
367367
368368 }
369369
370+ /**
371+ * Get parent
372+ * Get the parent of each element in the current set of matched elements, optionally filtered by a selector
373+ * @return self|void
374+ */
375+ public function parent ($ selector =null ) {
376+
377+ if (isset ($ this ->document ) && $ this ->length > 0 ) {
378+
379+ $ result = new self ($ this ->document );
380+
381+ foreach ($ this ->nodes as $ node ) {
382+
383+ if (!is_null ($ node ->parentNode )) {
384+ $ result ->addDomNode ($ node ->parentNode );
385+ }
386+
387+ }
388+
389+ if ($ selector ) $ result = $ result ->filter ($ selector );
390+
391+ return $ result ;
392+
393+ }
394+
395+ }
396+
370397 /**
371398 * Reduce the set of matched elements to those that match the selector
372399 *
@@ -494,7 +521,6 @@ public function last($selector=null) {
494521 /**
495522 * Returns DomQuery with immediately following sibling of all nodes
496523 * jQuery: Get the immediately following sibling of each element in the set of matched elements.
497- * @todo: If a selector is provided, it retrieves the previous sibling only if it matches that selector.
498524 * @return self|void
499525 */
500526 public function next ($ selector =null ) {
@@ -522,7 +548,6 @@ public function next($selector=null) {
522548 /**
523549 * Returns DomQuery with immediately preceding sibling of all nodes
524550 * jQuery: Get the immediately preceding sibling of each element in the set of matched elements.
525- * @todo If a selector is provided, it retrieves the previous sibling only if it matches that selector.
526551 * @return self|void
527552 */
528553 public function prev ($ selector =null ) {
Original file line number Diff line number Diff line change @@ -64,6 +64,21 @@ public function testFirstLast() {
6464
6565 }
6666
67+ /*
68+ * Test get parent
69+ */
70+ public function testParent () {
71+
72+ $ dom = new DomQuery ('<a></a><a></a><a class="link">
73+ <b><span></span></b>
74+ </a> ' );
75+
76+ $ this ->assertEquals ('b ' , $ dom ->find ('span ' )->parent ()->tagName );
77+ $ this ->assertEquals ('link ' , $ dom ->find ('span ' )->parent ('b ' )->parent ('a ' )->class );
78+ $ this ->assertEquals (0 , $ dom ->find ('span ' )->parent ('div ' )->length );
79+
80+ }
81+
6782 /*
6883 * Test traversing nodes from readme
6984 */
You can’t perform that action at this time.
0 commit comments