Skip to content

Commit beeea2f

Browse files
committed
Fixed bug in htmldoc::children() where it selected the root nodes instead of their children.
Updated `htmldoc::collection` to return itself instead of `void`. Added `htmldoc::parent()` to get the parents of each item in the collection. Added tests and updated `children()` test as this was incorrect.
1 parent d9c4137 commit beeea2f

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/htmldoc.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public function __get(string $var) : mixed {
7272
return $this->config;
7373
} elseif ($var === 'length') {
7474
return \count($this->children);
75+
} else {
76+
return $this->children[0]->{$var};
7577
}
7678
return null;
7779
}
@@ -123,8 +125,7 @@ public function offsetUnset(mixed $i) : void {
123125
public function offsetGet(mixed $i) : mixed { // return reference so you can set it like an array
124126
if (isset($this->children[$i])) {
125127
$obj = new htmldoc($this->config);
126-
$obj->collection([$this->children[$i]]);
127-
return $obj;
128+
return $obj->collection([$this->children[$i]]);
128129
}
129130
return null;
130131
}
@@ -137,8 +138,7 @@ public function offsetGet(mixed $i) : mixed { // return reference so you can set
137138
public function current() : mixed {
138139
if (isset($this->children[$this->pointer])) {
139140
$obj = new htmldoc($this->config);
140-
$obj->collection([$this->children[$this->pointer]]);
141-
return $obj;
141+
return $obj->collection([$this->children[$this->pointer]]);
142142
}
143143
return null;
144144
}
@@ -357,16 +357,17 @@ public function get(int $index = null) : tag|array|null {
357357
// return all children if no index
358358
if ($index === null) {
359359
return $children;
360-
}
360+
} else {
361361

362-
// check if index is minus
363-
if ($index < 0) {
364-
$index = \count($children) + $index;
365-
}
362+
// check if index is minus
363+
if ($index < 0) {
364+
$index = \count($children) + $index;
365+
}
366366

367-
// return index if set
368-
if (isset($children[$index])) {
369-
return $children[$index];
367+
// return index if set
368+
if (isset($children[$index])) {
369+
return $children[$index];
370+
}
370371
}
371372
return null;
372373
}
@@ -443,7 +444,23 @@ public function eq(int $index) : htmldoc {
443444
* @return htmldoc A new htmldoc object
444445
*/
445446
public function children() : htmldoc {
446-
return $this->find('>*');
447+
return $this->find('*>*');
448+
}
449+
450+
/**
451+
* Generate a new htmldoc object containing all the child tags of the parents
452+
*
453+
* @return htmldoc A new htmldoc object
454+
*/
455+
public function parent() : htmldoc {
456+
$doc = new htmldoc($this->config);
457+
$nodes = [];
458+
foreach ($this->children AS $item) {
459+
if (\get_class($item) === 'hexydec\\html\\tag' && ($parent = $this->children[0]->parent) !== null) {
460+
$nodes[] = $parent;
461+
}
462+
}
463+
return $doc->collection($nodes);
447464
}
448465

449466
/**
@@ -489,7 +506,7 @@ public function text() : string {
489506
* @param array $nodes An array of nodes to add to the collection
490507
* @return void
491508
*/
492-
protected function collection(array $nodes) : void {
509+
protected function collection(array $nodes) : htmldoc {
493510

494511
// only store unique nodes as some find operations can produce the same node multiple times
495512
$unique = [];
@@ -499,6 +516,7 @@ protected function collection(array $nodes) : void {
499516
}
500517
}
501518
$this->children = $unique;
519+
return $this;
502520
}
503521

504522
/**

tests/findHtmldocTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public function testCanTraverseElements() {
8181

8282
$this->assertEquals('<div id="first" class="first">First</div>', $doc->find('.positions > *')->first()->html(), 'Can find first element');
8383
$this->assertEquals('<div class="last">Last</div>', $doc->find('.positions > *')->last()->html(), 'Can find last element');
84-
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.positions > *')->eq(1)->html(), 'Can specific element');
85-
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.positions > *')->eq(-2)->html(), 'Can specific element');
86-
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.find')->children()->html(), 'Can specific element');
84+
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.positions > *')->eq(1)->html(), 'Can find specific element');
85+
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.positions > *')->eq(-2)->html(), 'Can find specific element');
86+
$this->assertEquals('<h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a>', $doc->find('.find')->children()->html(), 'Can extract children of an element');
87+
$this->assertEquals('<div class="find"><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p><a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a></div>', $doc->find('.find__heading')->parent()->html(), 'Can specific element');
8788

88-
$this->assertEquals(3, count($doc->find('.positions > *')->get()));
89+
$this->assertEquals(3, \count($doc->find('.positions > *')->get()));
8990
$this->assertEquals('<div class="last">Last</div>', $doc->find('.positions > *')->get(2)->html());
9091
$this->assertEquals('<div class="last">Last</div>', $doc->find('.positions > *')->get(-1)->html());
9192

0 commit comments

Comments
 (0)