Skip to content

Commit f9128ac

Browse files
committed
Added tests for the Static Facade
Fixed a few issues brought to light from the new tests
1 parent 939f7a2 commit f9128ac

File tree

5 files changed

+421
-5
lines changed

5 files changed

+421
-5
lines changed

src/PHPHtmlParser/Dom.php

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ class Dom {
5151
*/
5252
protected $size;
5353

54+
/**
55+
* A list of tags which will always be self closing
56+
*
57+
* @var array
58+
*/
59+
protected $selfClosing = [
60+
'img',
61+
'br',
62+
'input',
63+
'meta',
64+
'link',
65+
'hr',
66+
'base',
67+
'embed',
68+
'spacer',
69+
];
70+
5471
/**
5572
* Returns the inner html of the root node.
5673
*
@@ -131,6 +148,54 @@ public function find($selector, $nth = null)
131148
return $this->root->find($selector, $nth);
132149
}
133150

151+
/**
152+
* Adds the tag (or tags in an array) to the list of tags that will always
153+
* be self closing.
154+
*
155+
* @param string|array $tag
156+
* @chainable
157+
*/
158+
public function addSelfClosingTag($tag)
159+
{
160+
if ( ! is_array($tag))
161+
{
162+
$tag = [$tag];
163+
}
164+
foreach ($tag as $value)
165+
{
166+
$this->selfClosing[] = $value;
167+
}
168+
return $this;
169+
}
170+
171+
/**
172+
* Removes the tag (or tags in an array) from the list of tags that will
173+
* always be self closing.
174+
*
175+
* @param string|array $tag
176+
* @chainable
177+
*/
178+
public function removeSelfClosingTag($tag)
179+
{
180+
if ( ! is_array($tag))
181+
{
182+
$tag = [$tag];
183+
}
184+
$this->selfClosing = array_diff($this->selfClosing, $tag);
185+
return $this;
186+
}
187+
188+
/**
189+
* Sets the list of self closing tags to empty.
190+
*
191+
* @chainable
192+
*/
193+
public function clearSelfClosingTags()
194+
{
195+
$this->selfClosing = [];
196+
return $this;
197+
}
198+
134199
/**
135200
* Simple wrapper function that returns the first child.
136201
*
@@ -309,6 +374,11 @@ protected function parse()
309374
continue;
310375
}
311376

377+
if ( ! isset($info['node']))
378+
{
379+
continue;
380+
}
381+
312382
$node = $info['node'];
313383
$activeNode->addChild($node);
314384

@@ -354,9 +424,20 @@ protected function parseTag()
354424
// move to end of tag
355425
$this->content->copyUntil('>');
356426
$this->content->fastForward(1);
357-
$return['status'] = true;
358-
$return['closing'] = true;
359-
$return['tag'] = strtolower($tag);
427+
428+
// check if this closing tag counts
429+
$tag = strtolower($tag);
430+
if (in_array($tag, $this->selfClosing))
431+
{
432+
$return['status'] = true;
433+
return $return;
434+
}
435+
else
436+
{
437+
$return['status'] = true;
438+
$return['closing'] = true;
439+
$return['tag'] = strtolower($tag);
440+
}
360441
return $return;
361442
}
362443

@@ -433,6 +514,11 @@ protected function parseTag()
433514
$node->getTag()->selfClosing();
434515
$this->content->fastForward(1);
435516
}
517+
elseif (in_array($tag, $this->selfClosing))
518+
{
519+
// We force self closing on this tag.
520+
$node->getTag()->selfClosing();
521+
}
436522

437523
$this->content->fastForward(1);
438524

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function noise($noise)
110110
*/
111111
public function setAttribute($key, $value)
112112
{
113+
$key = strtolower($key);
113114
$this->attr[$key] = $value;
114115
return $this;
115116
}

tests/DomTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ public function testLoadNoClosingTag()
4343
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div><br />', $root->outerHtml);
4444
}
4545

46+
public function testLoadAttributeOnSelfClosing()
47+
{
48+
$dom = new Dom;
49+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br class="both" />');
50+
$br = $dom->find('br', 0);
51+
$this->assertEquals('both', $br->getAttribute('class'));
52+
}
53+
54+
public function testLoadClosingTagOnSelfClosing()
55+
{
56+
$dom = new Dom;
57+
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
58+
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
59+
}
60+
61+
public function testLoadUpperCase()
62+
{
63+
$dom = new Dom;
64+
$dom->load('<DIV CLASS="ALL"><BR><P>hEY BRO, <A HREF="GOOGLE.COM" DATA-QUOTE="\"">CLICK HERE</A></BR></DIV>');
65+
$this->assertEquals('<br /><p>hEY BRO, <a href="GOOGLE.COM" data-quote="\"">CLICK HERE</a></p>', $dom->find('div', 0)->innerHtml);
66+
}
67+
4668
public function testLoadWithFile()
4769
{
4870
$dom = new Dom;

tests/StaticDomTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public function testLoadWithFile()
2424

2525
public function testFind()
2626
{
27-
Dom::load('tests/small.html');
28-
$this->assertEquals('VonBurgermeister', Dom::find('.post-user font', 0)->text);
27+
Dom::load('tests/horrible.html');
28+
$this->assertEquals('<input type="submit" tabindex="0" name="submit" value="Информации" />', Dom::find('table input', 1)->outerHtml);
29+
}
30+
31+
public function testFindI()
32+
{
33+
Dom::load('tests/horrible.html');
34+
$this->assertEquals('[ Досие бр:12928 ]', Dom::find('i')[0]->innerHtml);
2935
}
3036
}

0 commit comments

Comments
 (0)