Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve developer experience with closing tags #435

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Next Next commit
Update list of void elements
  • Loading branch information
fredden committed Jan 31, 2023
commit 875262961aaa8ce7d34512131d7ce8556dfe3585
6 changes: 2 additions & 4 deletions Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php
Original file line number Diff line number Diff line change
@@ -44,15 +44,13 @@ class HtmlClosingVoidTagsSniff implements Sniff
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr'
'wbr',
];

/**
11 changes: 4 additions & 7 deletions Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php
Original file line number Diff line number Diff line change
@@ -17,13 +17,13 @@
class HtmlSelfClosingTagsSniff implements Sniff
{
/**
* List of void elements
* List of void elements.
*
* https://www.w3.org/TR/html51/syntax.html#writing-html-documents-elements
* https://html.spec.whatwg.org/multipage/syntax.html#void-elements
*
* @var string[]
*/
private $voidElements = [
private const HTML_VOID_ELEMENTS = [
'area',
'base',
'br',
@@ -32,11 +32,8 @@ class HtmlSelfClosingTagsSniff implements Sniff
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr',
@@ -70,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr)

if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (!in_array($match[1], $this->voidElements)) {
if (!in_array($match[1], self::HTML_VOID_ELEMENTS)) {
$phpcsFile->addError(
'Avoid using self-closing tag with non-void html element'
. ' - "' . $match[0] . PHP_EOL,
2 changes: 0 additions & 2 deletions Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc
Original file line number Diff line number Diff line change
@@ -22,10 +22,8 @@
<hr/>
<img src="" alt=""/>
<input type="text" id="test_input"/>
<keygen/>
<link/>
<meta/>
<param name="" value=""/>
<video>
<source/>
<track src=""/>
4 changes: 3 additions & 1 deletion Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ public function getErrorList()
*/
public function getWarningList()
{
return [1 => 15];
return [
1 => 14,
];
}
}
2 changes: 0 additions & 2 deletions Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -22,10 +22,8 @@
<hr/>
<img src="" alt=""/>
<input type="text" id="test_input"/>
<keygen/>
<link/>
<meta/>
<param name="" value=""/>
<video>
<source/>
<track src=""/>
4 changes: 3 additions & 1 deletion Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,9 @@ class HtmlSelfClosingTagsUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList()
{
return [1 => 9];
return [
1 => 9,
];
}

/**