Skip to content

Commit adc7d5a

Browse files
Merge pull request #59 from magento-trigger/AC-1102
AC-1102: Static test to verify self-closing tags for non-void html elements
2 parents d552f6a + 592dcfc commit adc7d5a

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento2\Sniffs\Html;
10+
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use PHP_CodeSniffer\Files\File;
13+
14+
/**
15+
* Sniff for self-closing tags
16+
*/
17+
class HtmlSelfClosingTagsSniff implements Sniff
18+
{
19+
/**
20+
* List of void elements
21+
*
22+
* https://www.w3.org/TR/html51/syntax.html#writing-html-documents-elements
23+
*
24+
* @var string[]
25+
*/
26+
private $voidElements = [
27+
'area',
28+
'base',
29+
'br',
30+
'col',
31+
'embed',
32+
'hr',
33+
'img',
34+
'input',
35+
'keygen',
36+
'link',
37+
'menuitem',
38+
'meta',
39+
'param',
40+
'source',
41+
'track',
42+
'wbr',
43+
];
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function register()
49+
{
50+
return [T_INLINE_HTML];
51+
}
52+
53+
/**
54+
* Detect use of self-closing tag with non-void html element
55+
*
56+
* @param File $phpcsFile
57+
* @param int $stackPtr
58+
* @return int|void
59+
*/
60+
public function process(File $phpcsFile, $stackPtr)
61+
{
62+
if ($stackPtr !== 0) {
63+
return;
64+
}
65+
$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
66+
67+
if (empty($html)) {
68+
return;
69+
}
70+
71+
if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
72+
foreach ($matches as $match) {
73+
if (!in_array($match[1], $this->voidElements)) {
74+
$phpcsFile->addError(
75+
'Avoid using self-closing tag with non-void html element'
76+
. ' - "' . $match[0] . PHP_EOL,
77+
null,
78+
'HtmlSelfClosingNonVoidTag'
79+
);
80+
}
81+
}
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
8+
<html>
9+
<head>
10+
<base/>
11+
<link/>
12+
</head>
13+
<body>
14+
<area alt=""/>
15+
<br/>
16+
<table>
17+
<colgroup>
18+
<col/>
19+
</colgroup>
20+
</table>
21+
<embed/>
22+
<hr/>
23+
<img src="" alt=""/>
24+
<input type="text" id="test_input"/>
25+
<keygen/>
26+
<link/>
27+
<meta/>
28+
<param name="" value=""/>
29+
<video>
30+
<source/>
31+
<track src=""/>
32+
</video>
33+
<wbr/>
34+
35+
<label for="test_input"/>
36+
<style type="text/css"/>
37+
<div/>
38+
<span/>
39+
<text/>
40+
<render/>
41+
<each/>
42+
<translate/>
43+
<scope/>
44+
</body>
45+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Html;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class HtmlSelfClosingTagsUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [1 => 9];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList()
24+
{
25+
return [];
26+
}
27+
}

Magento2/ruleset.xml

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
<type>error</type>
117117
</rule>
118118

119+
<rule ref="Magento2.Html.HtmlSelfClosingTags">
120+
<severity>10</severity>
121+
<type>error</type>
122+
<exclude-pattern>*\.xml$</exclude-pattern>
123+
</rule>
119124
<!-- Severity 9 warnings: Possible security and issues that may cause bugs. -->
120125
<rule ref="Generic.Files.ByteOrderMark">
121126
<severity>9</severity>
@@ -153,6 +158,7 @@
153158
<rule ref="Magento2.Html.HtmlBinding">
154159
<severity>9</severity>
155160
<type>warning</type>
161+
<exclude-pattern>*\.xml$</exclude-pattern>
156162
</rule>
157163

158164
<!-- Severity 8 warnings: Magento specific code issues and design violations. -->
@@ -246,6 +252,7 @@
246252
<rule ref="Magento2.Html.HtmlDirective">
247253
<severity>8</severity>
248254
<type>warning</type>
255+
<exclude-pattern>*\.xml$</exclude-pattern>
249256
</rule>
250257
<rule ref="Magento2.Legacy.WidgetXML.FoundObsoleteNode">
251258
<include-pattern>*\/widget.xml$</include-pattern>

0 commit comments

Comments
 (0)