Skip to content

Commit 213b0ae

Browse files
authored
Merge pull request paquettg#106 from billythekid/master
added optional array of tags to exempt from trailing slashes for self…
2 parents 71434b8 + 770b48f commit 213b0ae

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/PHPHtmlParser/Dom.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class Dom
9898
'wbr'
9999
];
100100

101+
/**
102+
* A list of tags where there should be no /> at the end (html5 style)
103+
*
104+
* @var array
105+
*/
106+
protected $noSlash = [];
107+
101108
/**
102109
* Returns the inner html of the root node.
103110
*
@@ -275,6 +282,53 @@ public function clearSelfClosingTags()
275282
return $this;
276283
}
277284

285+
286+
/**
287+
* Adds a tag to the list of self closing tags that should not have a trailing slash
288+
*
289+
* @param $tag
290+
* @return $this
291+
*/
292+
public function addNoSlashTag($tag)
293+
{
294+
if ( ! is_array($tag)) {
295+
$tag = [$tag];
296+
}
297+
foreach ($tag as $value) {
298+
$this->noSlash[] = $value;
299+
}
300+
301+
return $this;
302+
}
303+
304+
/**
305+
* Removes a tag from the list of no-slash tags.
306+
*
307+
* @param $tag
308+
* @return $this
309+
*/
310+
public function removeNoSlashTag($tag)
311+
{
312+
if ( ! is_array($tag)) {
313+
$tag = [$tag];
314+
}
315+
$this->noSlash = array_diff($this->noSlash, $tag);
316+
317+
return $this;
318+
}
319+
320+
/**
321+
* Empties the list of no-slash tags.
322+
*
323+
* @return $this
324+
*/
325+
public function clearNoSlashTags()
326+
{
327+
$this->noSlash = [];
328+
329+
return $this;
330+
}
331+
278332
/**
279333
* Simple wrapper function that returns the first child.
280334
*
@@ -596,6 +650,13 @@ protected function parseTag()
596650

597651
// We force self closing on this tag.
598652
$node->getTag()->selfClosing();
653+
654+
// Should this tag use a trailing slash?
655+
if(in_array($tag, $this->noSlash))
656+
{
657+
$node->getTag()->noTrailingSlash();
658+
}
659+
599660
}
600661

601662
$this->content->fastForward(1);

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class Tag
3333
*/
3434
protected $selfClosing = false;
3535

36+
/**
37+
* If self-closing, will this use a trailing slash. />
38+
*
39+
* @var bool
40+
*/
41+
protected $trailingSlash = true;
42+
3643
/**
3744
* Tag noise
3845
*/
@@ -99,6 +106,19 @@ public function selfClosing()
99106
return $this;
100107
}
101108

109+
110+
/**
111+
* Sets the tag to not use a trailing slash.
112+
*
113+
* @return $this
114+
*/
115+
public function noTrailingSlash()
116+
{
117+
$this->trailingSlash = false;
118+
119+
return $this;
120+
}
121+
102122
/**
103123
* Checks if the tag is self closing.
104124
*
@@ -258,7 +278,7 @@ public function makeOpeningTag()
258278
}
259279
}
260280

261-
if ($this->selfClosing) {
281+
if ($this->selfClosing && $this->trailingSlash) {
262282
return $return.' />';
263283
} else {
264284
return $return.'>';

tests/DomTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public function testLoadClosingTagOnSelfClosing()
8888
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
8989
}
9090

91+
public function testLoadClosingTagOnSelfClosingNoSlash()
92+
{
93+
$dom = new Dom;
94+
$dom->addNoSlashTag("br");
95+
96+
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
97+
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
98+
}
99+
91100
public function testLoadClosingTagAddSelfClosingTag()
92101
{
93102
$dom = new Dom;

0 commit comments

Comments
 (0)