Skip to content

Commit 39b1bab

Browse files
committed
Cleaned up code base and added new tests
1 parent d0c27a2 commit 39b1bab

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class AbstractNode
4444
/**
4545
* The unique id of the class. Given by PHP.
4646
*
47-
* @var string
47+
* @var int
4848
*/
4949
protected $id;
5050

@@ -55,6 +55,13 @@ abstract class AbstractNode
5555
*/
5656
protected $encode;
5757

58+
/**
59+
* An array of all the children.
60+
*
61+
* @var array
62+
*/
63+
protected $children = [];
64+
5865
/**
5966
* Creates a unique id for this node.
6067
*/
@@ -242,7 +249,8 @@ public function hasNextSibling(): bool
242249
{
243250
try
244251
{
245-
$sibling = $this->nextSibling();
252+
$this->nextSibling();
253+
246254
// sibling found, return true;
247255
return true;
248256
}
@@ -418,7 +426,7 @@ public function ancestorByTag(string $tag): AbstractNode
418426
*
419427
* @param string $selector
420428
* @param int $nth
421-
* @return array|AbstractNode
429+
* @return mixed
422430
*/
423431
public function find(string $selector, int $nth = null)
424432
{

src/PHPHtmlParser/Dom/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Collection implements IteratorAggregate, ArrayAccess, Countable
2828
*
2929
* @param string $method
3030
* @param array $arguments
31-
* @return mixed;
31+
* @return mixed
3232
* @throws EmptyCollectionException
3333
*/
3434
public function __call(string $method, array $arguments)
@@ -72,7 +72,7 @@ public function __toString(): string
7272
if ($node instanceof AbstractNode) {
7373
return (string)$node;
7474
} else {
75-
throw new EmptyCollectionException('The collection does not contain any Nodes.');
75+
return '';
7676
}
7777
}
7878

src/PHPHtmlParser/Options.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
* @package PHPHtmlParser
88
* @property bool whitespaceTextNode
99
* @property bool strict
10-
* @property bool enforceEncoding
10+
* @property string|null enforceEncoding
11+
* @property bool cleanupInput
12+
* @property bool removeScripts
13+
* @property bool removeStyles
14+
* @property bool preserveLineBreaks
15+
* @property bool removeDoubleSpace
1116
*/
1217
class Options
1318
{

tests/CollectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public function testCallNoNodes()
3939
$collection->innerHtml();
4040
}
4141

42+
public function testNoNodeString()
43+
{
44+
$collection = new Collection();
45+
$string = (string) $collection;
46+
$this->assertEmpty($string);
47+
}
48+
4249
public function testCallMagic()
4350
{
4451
$root = new HtmlNode(new Tag('root'));
@@ -114,4 +121,27 @@ public function testToArray()
114121
$lastA = end($array);
115122
$this->assertEquals($child3->id(), $lastA->id());
116123
}
124+
125+
public function testGetIterator()
126+
{
127+
$collection = new Collection();
128+
$iterator = $collection->getIterator();
129+
$this->assertTrue($iterator instanceof \ArrayIterator);
130+
131+
}
132+
133+
public function testOffsetSet()
134+
{
135+
$collection = new Collection();
136+
$collection->offsetSet(7, true);
137+
$this->assertTrue($collection->offsetGet(7));
138+
}
139+
140+
public function testOffsetUnset()
141+
{
142+
$collection = new Collection();
143+
$collection->offsetSet(7, true);
144+
$collection->offsetUnset(7);
145+
$this->assertTrue(is_null($collection->offsetGet(7)));
146+
}
117147
}

tests/Node/TextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testAncestorByTag()
3030
public function testPreserveEntity()
3131
{
3232
$node = new TextNode('i');
33-
$text = $node->innerhtml;
33+
$text = $node->outerhtml;
3434
$this->assertEquals('i', $text);
3535
}
3636

0 commit comments

Comments
 (0)