From 875262961aaa8ce7d34512131d7ce8556dfe3585 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 31 Jan 2023 17:17:20 +0000 Subject: [PATCH 01/12] Update list of void elements --- Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php | 6 ++---- Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php | 11 ++++------- Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc | 2 -- Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php | 4 +++- Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc | 2 -- Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php | 4 +++- 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 5ce7c20d..c92f3742 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -44,15 +44,13 @@ class HtmlClosingVoidTagsSniff implements Sniff 'col', 'embed', 'hr', + 'img', 'input', - 'keygen', 'link', - 'menuitem', 'meta', - 'param', 'source', 'track', - 'wbr' + 'wbr', ]; /** diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index ecbdca44..e2527bb2 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -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, diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc index dd701d03..68cf5f01 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc @@ -22,10 +22,8 @@
- - +
diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php index 87103b9a..54ef3da8 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php @@ -23,7 +23,21 @@ public function getErrorList() public function getWarningList() { return [ - 1 => 14, + 10 => 1, + 11 => 1, + 14 => 1, + 15 => 1, + 18 => 1, + 21 => 1, + 22 => 1, + 23 => 1, + 24 => 1, + 25 => 1, + 26 => 1, + 28 => 1, + 29 => 1, + 31 => 1, + 32 => 1, ]; } } diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc index b6ebd936..4a412822 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc @@ -39,5 +39,6 @@ + diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed index 60d65c44..2d348c55 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed @@ -39,5 +39,6 @@ + diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php index b7287c87..316c63ea 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php @@ -15,7 +15,16 @@ class HtmlSelfClosingTagsUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 1 => 9, + 33 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 40 => 1, + 41 => 1, + 42 => 1, ]; } From 381242e15c2251adbae60160ce7ada936fde8342 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 31 Jan 2023 19:07:47 +0000 Subject: [PATCH 04/12] Increase minimum polyfill version This is an attempt to fix a test failure which seems unrelated to this change, which seems to require str_ends_with() somewhere. --- composer.json | 2 +- composer.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3624e916..49b3f7e2 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpcompatibility/php-compatibility": "^9.3", "squizlabs/php_codesniffer": "^3.6.1", "rector/rector": "^0.14.8", - "symfony/polyfill": "^1.15" + "symfony/polyfill": "^1.16" }, "require-dev": { "phpunit/phpunit": "^9.5.8" diff --git a/composer.lock b/composer.lock index fc4ed920..b4a64057 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6e4991bd93ecef257345b98d603f57d0", + "content-hash": "0c1e598f16594c56f36b7da4e389d3f9", "packages": [ { "name": "phpcompatibility/php-compatibility", From 809cfabe1928ad5accabd0e5e6bda2e036bade3d Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 2 Feb 2023 13:06:45 +0000 Subject: [PATCH 05/12] Remove space before self-close when auto-fixed --- Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php | 3 ++- Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php | 3 ++- Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc | 1 + Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed | 1 + Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php | 1 + Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc | 1 + Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed | 1 + Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php | 1 + 8 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 3d895bff..5e9655d7 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -96,7 +96,8 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $token = $phpcsFile->getTokens()[$ptr]; $original = $match[0]; - $replacement = str_replace('/>', '>', $original); + $replacement = str_replace(' />', '>', $original); + $replacement = str_replace('/>', '>', $replacement); $phpcsFile->fixer->replaceToken( $ptr, str_replace($original, $replacement, $token['content']) diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index f068627f..e02a25a5 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -83,7 +83,8 @@ public function process(File $phpcsFile, $stackPtr) if ($fix) { $token = $phpcsFile->getTokens()[$ptr]; $original = $match[0]; - $replacement = str_replace('/>', '>', $original); + $replacement = str_replace(' />', '>', $original); + $replacement = str_replace('/>', '>', $replacement); $phpcsFile->fixer->replaceToken( $ptr, str_replace($original, $replacement, $token['content']) diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc index 5c6c021a..336ff618 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc @@ -30,5 +30,6 @@
+
diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed index 0ad134a8..6898c43b 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc.fixed @@ -30,5 +30,6 @@
+
diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php index 54ef3da8..477e945a 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php @@ -38,6 +38,7 @@ public function getWarningList() 29 => 1, 31 => 1, 32 => 1, + 33 => 1, ]; } } diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc index 4a412822..ada76221 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc @@ -40,5 +40,6 @@ + diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed index 2d348c55..8c37b48d 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.1.inc.fixed @@ -40,5 +40,6 @@ + diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php index 316c63ea..4946fd0e 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php @@ -25,6 +25,7 @@ public function getErrorList() 40 => 1, 41 => 1, 42 => 1, + 43 => 1, ]; } From 33585e045dabfe148a68892a747046959527cd1e Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 11:35:13 +0100 Subject: [PATCH 06/12] Correct return type --- Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index e02a25a5..5342272a 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -55,7 +55,7 @@ public function register() * * @param File $phpcsFile * @param int $stackPtr - * @return int|void + * @return void */ public function process(File $phpcsFile, $stackPtr) { From 42bc067b9bff33060bd6193d390b018a4802a1bb Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 11:35:34 +0100 Subject: [PATCH 07/12] Reduce duplicate code --- .../Sniffs/Html/HtmlClosingVoidTagsSniff.php | 32 +------------------ .../Sniffs/Html/HtmlSelfClosingTagsSniff.php | 2 +- 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 5e9655d7..81d048f3 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -13,7 +13,7 @@ /** * Sniff for void closing tags. */ -class HtmlClosingVoidTagsSniff implements Sniff +class HtmlClosingVoidTagsSniff extends HtmlSelfClosingTagsSniff implements Sniff { /** * String representation of warning. @@ -53,9 +53,6 @@ class HtmlClosingVoidTagsSniff implements Sniff 'wbr', ]; - /** @var int */ - private int $lastPointer = 0; - /** * @inheritdoc */ @@ -114,31 +111,4 @@ public function process(File $phpcsFile, $stackPtr): void } } } - - /** - * Apply a fix for the detected issue - * - * @param File $phpcsFile - * @param string $needle - * @return int|null - */ - public function findPointer(File $phpcsFile, string $needle): ?int - { - foreach ($phpcsFile->getTokens() as $ptr => $token) { - if ($ptr < $this->lastPointer) { - continue; - } - - if ($token['code'] !== T_INLINE_HTML) { - continue; - } - - if (str_contains($token['content'], $needle)) { - $this->lastPointer = $ptr; - return $ptr; - } - } - - return null; - } } diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index 5342272a..f6b5dff8 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -111,7 +111,7 @@ public function process(File $phpcsFile, $stackPtr) * @param string $needle * @return int|null */ - private function findPointer(File $phpcsFile, string $needle): ?int + protected function findPointer(File $phpcsFile, string $needle): ?int { foreach ($phpcsFile->getTokens() as $ptr => $token) { if ($ptr < $this->lastPointer) { From 48ff5f2a52cccdec90bd78287ffe7a69c28f61c0 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 11:50:33 +0100 Subject: [PATCH 08/12] Improve test cases --- .../Html/HtmlClosingVoidTagsUnitTest.inc | 7 +++++++ .../HtmlClosingVoidTagsUnitTest.inc.fixed | 7 +++++++ .../Html/HtmlClosingVoidTagsUnitTest.php | 11 +++++----- .../Html/HtmlSelfClosingTagsUnitTest.1.inc | 12 +++++++++++ .../HtmlSelfClosingTagsUnitTest.1.inc.fixed | 12 +++++++++++ .../Html/HtmlSelfClosingTagsUnitTest.php | 21 ++++++++++--------- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc index 336ff618..68f40388 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc @@ -22,6 +22,13 @@
+ +