Skip to content

Commit 91c276e

Browse files
committed
added .parent method
1 parent 7879dba commit 91c276e

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

src/Rct567/DomQuery/DomQuery.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff 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) {

tests/Rct567/DomQuery/Tests/DomQueryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

0 commit comments

Comments
 (0)