Skip to content

Commit ca0ebae

Browse files
committed
Always fix closing HTML tags
1 parent 321d698 commit ca0ebae

4 files changed

+28
-52
lines changed

Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php

+11-22
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,18 @@ public function process(File $phpcsFile, $stackPtr): void
8383
foreach ($matches as $match) {
8484
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
8585
$ptr = $this->findPointer($phpcsFile, $match[0]);
86-
if (!str_contains($match[0], "\n")) {
87-
$fix = $phpcsFile->addFixableWarning(
88-
sprintf(self::WARNING_MESSAGE, $match[0]),
89-
$ptr,
90-
self::WARNING_CODE
91-
);
86+
$fix = $phpcsFile->addFixableWarning(
87+
sprintf(self::WARNING_MESSAGE, $match[0]),
88+
$ptr,
89+
self::WARNING_CODE
90+
);
9291

93-
if ($fix) {
94-
$token = $phpcsFile->getTokens()[$ptr];
95-
$original = $match[0];
96-
$replacement = str_replace(' />', '>', $original);
97-
$replacement = str_replace('/>', '>', $replacement);
98-
$phpcsFile->fixer->replaceToken(
99-
$ptr,
100-
str_replace($original, $replacement, $token['content'])
101-
);
102-
}
103-
} else {
104-
$phpcsFile->addWarning(
105-
sprintf(self::WARNING_MESSAGE, $match[0]),
106-
$ptr,
107-
self::WARNING_CODE
108-
);
92+
if ($fix) {
93+
$token = $phpcsFile->getTokens()[$ptr];
94+
$original = $token['content'];
95+
$replacement = str_replace(' />', '>', $original);
96+
$replacement = str_replace('/>', '>', $replacement);
97+
$phpcsFile->fixer->replaceToken($ptr, $replacement);
10998
}
11099
}
111100
}

Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php

+13-26
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,19 @@ public function process(File $phpcsFile, $stackPtr)
7272
foreach ($matches as $match) {
7373
if (!in_array($match[1], self::HTML_VOID_ELEMENTS)) {
7474
$ptr = $this->findPointer($phpcsFile, $match[0]);
75-
if (!str_contains($match[0], "\n")) {
76-
$fix = $phpcsFile->addFixableError(
77-
'Avoid using self-closing tag with non-void html element'
78-
. ' - "' . $match[0] . PHP_EOL,
79-
$ptr,
80-
'HtmlSelfClosingNonVoidTag'
81-
);
82-
83-
if ($fix) {
84-
$token = $phpcsFile->getTokens()[$ptr];
85-
$original = $match[0];
86-
$replacement = str_replace(' />', '></' . $match[1] . '>', $original);
87-
$replacement = str_replace('/>', '></' . $match[1] . '>', $replacement);
88-
$phpcsFile->fixer->replaceToken(
89-
$ptr,
90-
str_replace($original, $replacement, $token['content'])
91-
);
92-
93-
}
94-
} else {
95-
$phpcsFile->addError(
96-
'Avoid using self-closing tag with non-void html element'
97-
. ' - "' . $match[0] . PHP_EOL,
98-
$ptr,
99-
'HtmlSelfClosingNonVoidTag'
100-
);
75+
$fix = $phpcsFile->addFixableError(
76+
'Avoid using self-closing tag with non-void html element'
77+
. ' - "' . $match[0] . PHP_EOL,
78+
$ptr,
79+
'HtmlSelfClosingNonVoidTag'
80+
);
81+
82+
if ($fix) {
83+
$token = $phpcsFile->getTokens()[$ptr];
84+
$original = $token['content'];
85+
$replacement = str_replace(' />', '></' . $match[1] . '>', $original);
86+
$replacement = str_replace('/>', '></' . $match[1] . '>', $replacement);
87+
$phpcsFile->fixer->replaceToken($ptr, $replacement);
10188
}
10289
}
10390
}

Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<input type="text"
2626
id="multi-line-input"
2727
placeholder="Alert should be on last line"
28-
/>
28+
>
2929
<input type="text"
3030
id="multi-line-input2"
31-
placeholder="Alert should be on last line" />
31+
placeholder="Alert should be on last line">
3232
<link>
3333
<meta>
3434
<video>

Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
<label for="test_input"></label>
4141
<label
4242
for="multi-line-input"
43-
/>
43+
></label>
4444
<label
45-
for="multi-line-input2" />
45+
for="multi-line-input2"></label>
4646
<style type="text/css"></style>
4747
<div></div>
4848
<span></span>

0 commit comments

Comments
 (0)