Skip to content

Commit 19e23b5

Browse files
committed
Abstracted the Node class as it should have been done in the first place
1 parent 5ab2a4e commit 19e23b5

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

src/PHPHtmlParser/Dom/MockNode.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace PHPHtmlParser\Dom;
3+
4+
/**
5+
* This mock object is used solely for testing the abstract
6+
* class Node with out any potential side effects caused
7+
* by testing a supperclass of Node.
8+
*
9+
* This object is not to be used for any other reason.
10+
*/
11+
class MockNode extends Node {
12+
13+
public function innerHtml() {}
14+
15+
public function outerHtml() {}
16+
17+
public function text() {}
18+
19+
protected function clear() {}
20+
}

src/PHPHtmlParser/Dom/Node.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Dom node object.
99
*/
10-
class Node {
10+
abstract class Node {
1111

1212
/**
1313
* Contains the tag name/type
@@ -621,9 +621,33 @@ public function get_display_size()
621621
return $result;
622622
}
623623

624+
/**
625+
* Gets the inner html of this node.
626+
*
627+
* @return string
628+
*/
629+
abstract public function innerHtml();
630+
631+
/**
632+
* Gets the html of this node, including it's own
633+
* tag.
634+
*
635+
* @return string
636+
*/
637+
abstract public function outerHtml();
638+
639+
/**
640+
* Gets the text of this node (if there is any text).
641+
*
642+
* @return string
643+
*/
644+
abstract public function text();
645+
624646
/**
625647
* Call this when something in the node tree has changed. Like a child has been added
626648
* or a parent has been changed.
649+
*
650+
* @return void
627651
*/
628-
protected function clear() {}
652+
abstract protected function clear();
629653
}

src/PHPHtmlParser/Dom/TextNode.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace PHPHtmlParser\Dom;
33

4-
use PHPHtmlParser\Dom;
5-
64
class TextNode extends Node {
75

86
/**
@@ -69,6 +67,28 @@ public function text()
6967
}
7068
}
7169

70+
/**
71+
* This node has no html, just return the text.
72+
*
73+
* @return string
74+
* @uses $this->text()
75+
*/
76+
public function innerHtml()
77+
{
78+
return $this->text();
79+
}
80+
81+
/**
82+
* This node has no html, just return the text.
83+
*
84+
* @return string
85+
* @uses $this->text()
86+
*/
87+
public function outerHtml()
88+
{
89+
return $this->text();
90+
}
91+
7292
/**
7393
* Call this when something in the node tree has changed. Like a child has been added
7494
* or a parent has been changed.

tests/Node/ChildrenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PHPHtmlParser\Dom\Node;
3+
use PHPHtmlParser\Dom\MockNode as Node;
44

55
class NodeChildTest extends PHPUnit_Framework_TestCase {
66

tests/Node/ParentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PHPHtmlParser\Dom\Node;
3+
use PHPHtmlParser\Dom\MockNode as Node;
44

55
class NodeParentTest extends PHPUnit_Framework_TestCase {
66

0 commit comments

Comments
 (0)