Skip to content

Commit 770b48f

Browse files
committed
added optional array of tags to exempt from trailing slashes for self-closing tags
1 parent 9663472 commit 770b48f

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
@@ -90,6 +90,13 @@ class Dom
9090
'spacer',
9191
];
9292

93+
/**
94+
* A list of tags where there should be no /> at the end (html5 style)
95+
*
96+
* @var array
97+
*/
98+
protected $noSlash = [];
99+
93100
/**
94101
* Returns the inner html of the root node.
95102
*
@@ -267,6 +274,53 @@ public function clearSelfClosingTags()
267274
return $this;
268275
}
269276

277+
278+
/**
279+
* Adds a tag to the list of self closing tags that should not have a trailing slash
280+
*
281+
* @param $tag
282+
* @return $this
283+
*/
284+
public function addNoSlashTag($tag)
285+
{
286+
if ( ! is_array($tag)) {
287+
$tag = [$tag];
288+
}
289+
foreach ($tag as $value) {
290+
$this->noSlash[] = $value;
291+
}
292+
293+
return $this;
294+
}
295+
296+
/**
297+
* Removes a tag from the list of no-slash tags.
298+
*
299+
* @param $tag
300+
* @return $this
301+
*/
302+
public function removeNoSlashTag($tag)
303+
{
304+
if ( ! is_array($tag)) {
305+
$tag = [$tag];
306+
}
307+
$this->noSlash = array_diff($this->noSlash, $tag);
308+
309+
return $this;
310+
}
311+
312+
/**
313+
* Empties the list of no-slash tags.
314+
*
315+
* @return $this
316+
*/
317+
public function clearNoSlashTags()
318+
{
319+
$this->noSlash = [];
320+
321+
return $this;
322+
}
323+
270324
/**
271325
* Simple wrapper function that returns the first child.
272326
*
@@ -588,6 +642,13 @@ protected function parseTag()
588642

589643
// We force self closing on this tag.
590644
$node->getTag()->selfClosing();
645+
646+
// Should this tag use a trailing slash?
647+
if(in_array($tag, $this->noSlash))
648+
{
649+
$node->getTag()->noTrailingSlash();
650+
}
651+
591652
}
592653

593654
$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
*
@@ -247,7 +267,7 @@ public function makeOpeningTag()
247267
}
248268
}
249269

250-
if ($this->selfClosing) {
270+
if ($this->selfClosing && $this->trailingSlash) {
251271
return $return.' />';
252272
} else {
253273
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)