Skip to content

Commit e341455

Browse files
committed
AC-1999: Static test to check void closing tags
1 parent 4691601 commit e341455

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,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Sniffs\Html;
9+
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Sniff for void closing tags.
15+
*/
16+
class HtmlClosingVoidTagsSniff implements Sniff
17+
{
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
private const WARNING_MESSAGE =
24+
'Avoid using closing slash with void tags, this can lead to unexpected behavior - "%s"';
25+
26+
/**
27+
* Warning violation code.
28+
*
29+
* @var string
30+
*/
31+
private const WARNING_CODE = 'HtmlClosingVoidElements';
32+
33+
/**
34+
* List of void elements.
35+
*
36+
* https://html.spec.whatwg.org/multipage/syntax.html#void-elements
37+
*
38+
* @var string[]
39+
*/
40+
private const HTML_VOID_ELEMENTS = [
41+
'area',
42+
'base',
43+
'br',
44+
'col',
45+
'embed',
46+
'hr',
47+
'input',
48+
'keygen',
49+
'link',
50+
'menuitem',
51+
'meta',
52+
'param',
53+
'source',
54+
'track',
55+
'wbr'
56+
];
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function register(): array
62+
{
63+
return [T_INLINE_HTML];
64+
}
65+
66+
/**
67+
* Detect use of self-closing tag with void html element.
68+
*
69+
* @param File $phpcsFile
70+
* @param int $stackPtr
71+
* @return void
72+
*/
73+
public function process(File $phpcsFile, $stackPtr): void
74+
{
75+
if ($stackPtr !== 0) {
76+
return;
77+
}
78+
$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
79+
80+
if (empty($html)) {
81+
return;
82+
}
83+
84+
if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
85+
foreach ($matches as $match) {
86+
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
87+
$phpcsFile->addWarning(
88+
sprintf(self::WARNING_MESSAGE, $match[0]),
89+
null,
90+
self::WARNING_CODE
91+
);
92+
}
93+
}
94+
}
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
</body>
35+
</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 HtmlClosingVoidTagsUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList()
24+
{
25+
return [1 => 15];
26+
}
27+
}

Magento2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
<type>error</type>
139139
<exclude-pattern>*\.xml$</exclude-pattern>
140140
</rule>
141+
<rule ref="Magento2.Html.HtmlClosingVoidTags">
142+
<severity>10</severity>
143+
<type>warning</type>
144+
<exclude-pattern>*\.xml$</exclude-pattern>
145+
</rule>
141146
<rule ref="Magento2.Html.HtmlCollapsibleAttribute">
142147
<include-pattern>*\.html$</include-pattern>
143148
<include-pattern>*\.phtml$</include-pattern>

0 commit comments

Comments
 (0)