Skip to content

Commit

Permalink
Always fix closing HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed May 26, 2023
1 parent 321d698 commit ca0ebae
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 52 deletions.
33 changes: 11 additions & 22 deletions Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,18 @@ public function process(File $phpcsFile, $stackPtr): void
foreach ($matches as $match) {
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
$ptr = $this->findPointer($phpcsFile, $match[0]);
if (!str_contains($match[0], "\n")) {
$fix = $phpcsFile->addFixableWarning(
sprintf(self::WARNING_MESSAGE, $match[0]),
$ptr,
self::WARNING_CODE
);
$fix = $phpcsFile->addFixableWarning(
sprintf(self::WARNING_MESSAGE, $match[0]),
$ptr,
self::WARNING_CODE
);

if ($fix) {
$token = $phpcsFile->getTokens()[$ptr];
$original = $match[0];
$replacement = str_replace(' />', '>', $original);
$replacement = str_replace('/>', '>', $replacement);
$phpcsFile->fixer->replaceToken(
$ptr,
str_replace($original, $replacement, $token['content'])
);
}
} else {
$phpcsFile->addWarning(
sprintf(self::WARNING_MESSAGE, $match[0]),
$ptr,
self::WARNING_CODE
);
if ($fix) {
$token = $phpcsFile->getTokens()[$ptr];
$original = $token['content'];
$replacement = str_replace(' />', '>', $original);
$replacement = str_replace('/>', '>', $replacement);
$phpcsFile->fixer->replaceToken($ptr, $replacement);
}
}
}
Expand Down
39 changes: 13 additions & 26 deletions Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,19 @@ public function process(File $phpcsFile, $stackPtr)
foreach ($matches as $match) {
if (!in_array($match[1], self::HTML_VOID_ELEMENTS)) {
$ptr = $this->findPointer($phpcsFile, $match[0]);
if (!str_contains($match[0], "\n")) {
$fix = $phpcsFile->addFixableError(
'Avoid using self-closing tag with non-void html element'
. ' - "' . $match[0] . PHP_EOL,
$ptr,
'HtmlSelfClosingNonVoidTag'
);

if ($fix) {
$token = $phpcsFile->getTokens()[$ptr];
$original = $match[0];
$replacement = str_replace(' />', '></' . $match[1] . '>', $original);
$replacement = str_replace('/>', '></' . $match[1] . '>', $replacement);
$phpcsFile->fixer->replaceToken(
$ptr,
str_replace($original, $replacement, $token['content'])
);

}
} else {
$phpcsFile->addError(
'Avoid using self-closing tag with non-void html element'
. ' - "' . $match[0] . PHP_EOL,
$ptr,
'HtmlSelfClosingNonVoidTag'
);
$fix = $phpcsFile->addFixableError(
'Avoid using self-closing tag with non-void html element'
. ' - "' . $match[0] . PHP_EOL,
$ptr,
'HtmlSelfClosingNonVoidTag'
);

if ($fix) {
$token = $phpcsFile->getTokens()[$ptr];
$original = $token['content'];
$replacement = str_replace(' />', '></' . $match[1] . '>', $original);
$replacement = str_replace('/>', '></' . $match[1] . '>', $replacement);
$phpcsFile->fixer->replaceToken($ptr, $replacement);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<input type="text"
id="multi-line-input"
placeholder="Alert should be on last line"
/>
>
<input type="text"
id="multi-line-input2"
placeholder="Alert should be on last line" />
placeholder="Alert should be on last line">
<link>
<meta>
<video>
Expand Down
4 changes: 2 additions & 2 deletions Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
<label for="test_input"></label>
<label
for="multi-line-input"
/>
></label>
<label
for="multi-line-input2" />
for="multi-line-input2"></label>
<style type="text/css"></style>
<div></div>
<span></span>
Expand Down

0 comments on commit ca0ebae

Please sign in to comment.