Skip to content

Commit 94acecd

Browse files
committed
AC-1102: Static test to verify self-closing tags for non-void html elements
1 parent e3f4d87 commit 94acecd

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 84 additions & 0 deletions
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+
'HtmlTemplates.Tag.SelfClosing'
79+
);
80+
}
81+
}
82+
}
83+
}
84+
}

Magento2/ruleset.xml

Lines changed: 7 additions & 0 deletions
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

251258
<!-- Severity 7 warnings: General code issues. -->

0 commit comments

Comments
 (0)