Skip to content

Commit 3721c9d

Browse files
authored
Merge pull request #18 from Kdecherf/feat/prune-selfclosed-tags
Neutralize or remove illegal self-closing tags
2 parents 7619855 + 2295cf7 commit 3721c9d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

htmLawed.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ function hl_tag($t)
966966
if ($t == '>') {
967967
return '>';
968968
}
969-
if (!preg_match('`^<(/?)([a-zA-Z][^\s>]*)([^>]*?)\s?>$`m', $t, $m)) { // Get tag with element name and attributes
969+
if (!preg_match('`^<(/?)([a-zA-Z][^\s>]*)([^>]*?)\s?(?P<selfclosing> /)?>$`m', $t, $m)) { // Get tag with element name and attributes
970970
return str_replace(array('<', '>'), array('&lt;', '&gt;'), $t);
971971
}
972972

@@ -1008,6 +1008,13 @@ function hl_tag($t)
10081008
// Handle closing tag.
10091009

10101010
static $emptyEleAr = array('area'=>1, 'br'=>1, 'col'=>1, 'command'=>1, 'embed'=>1, 'hr'=>1, 'img'=>1, 'input'=>1, 'isindex'=>1, 'keygen'=>1, 'link'=>1, 'meta'=>1, 'param'=>1, 'source'=>1, 'track'=>1, 'wbr'=>1);
1011+
if (!empty($m['selfclosing']) || false !== strpos($m[2], '/')) {
1012+
if (!isset($emptyEleAr[$ele])) {
1013+
return ($C['keep_bad'] % 2
1014+
? str_replace(array('<', '>'), array('&lt;', '&gt;'), $t)
1015+
: '');
1016+
}
1017+
}
10111018
if (!empty($m[1])) {
10121019
return(
10131020
!isset($emptyEleAr[$ele])

tests/HTMLawedTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,42 @@ public function testPictureBlocks()
4242
$output = htmLawed($input);
4343
$this->assertSame($input, $output);
4444
}
45+
46+
public function dataForSelfClosingTags()
47+
{
48+
return [
49+
'void element, missing trailing slash' => [
50+
'<div><img src="a.jpg" alt="image"></div>',
51+
'<div><img src="a.jpg" alt="image" /></div>',
52+
],
53+
'void element, trailing slash' => [
54+
'<div><img src="a.jpg" alt="image" /></div>',
55+
],
56+
'standard element, end tag' => [
57+
'<div><p>Hello world</p></div>',
58+
],
59+
'standard element, illegal self-closing tag' => [
60+
'<div><p/></div>',
61+
'<div></div>',
62+
],
63+
'standard element with attributes, illegal self-closing tag' => [
64+
'<div><p id="a" /></div>',
65+
'<div></div>',
66+
],
67+
'standard element, missing end tag' => [
68+
'<div><p>Hello world</p>',
69+
'<div><p>Hello world</p></div>',
70+
],
71+
];
72+
}
73+
74+
/**
75+
* @dataProvider dataForSelfClosingTags
76+
*/
77+
public function testSelfClosingTags($input, $expectedOutput = null)
78+
{
79+
$output = htmLawed($input);
80+
81+
$this->assertSame($expectedOutput ?: $input, $output);
82+
}
4583
}

0 commit comments

Comments
 (0)