From 6f35ad804d63bb1371b9f95dd86fa5510032646c Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 25 Apr 2023 17:57:47 +0100 Subject: [PATCH 1/2] Also lint HTML when not at start of file --- .../Sniffs/Html/HtmlClosingVoidTagsSniff.php | 7 ++- .../Html/HtmlCollapsibleAttributeSniff.php | 7 ++- Magento2/Sniffs/Html/HtmlDirectiveSniff.php | 7 ++- .../Sniffs/Html/HtmlSelfClosingTagsSniff.php | 7 ++- ....inc => HtmlClosingVoidTagsUnitTest.1.inc} | 0 .../Html/HtmlClosingVoidTagsUnitTest.2.inc | 35 +++++++++++++++ .../HtmlCollapsibleAttributeUnitTest.2.inc | 35 +++++++++++++++ .../Html/HtmlSelfClosingTagsUnitTest.2.inc | 45 +++++++++++++++++++ 8 files changed, 135 insertions(+), 8 deletions(-) rename Magento2/Tests/Html/{HtmlClosingVoidTagsUnitTest.inc => HtmlClosingVoidTagsUnitTest.1.inc} (100%) create mode 100644 Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.2.inc create mode 100644 Magento2/Tests/Html/HtmlCollapsibleAttributeUnitTest.2.inc create mode 100644 Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.2.inc diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 5ce7c20d..e2005fd3 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -72,10 +72,13 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr): void { - if ($stackPtr !== 0) { + static $lastFile; + if ($lastFile === $phpcsFile->getFilename()) { return; } - $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens())); + $lastFile = $phpcsFile->getFilename(); + + $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr); if (empty($html)) { return; diff --git a/Magento2/Sniffs/Html/HtmlCollapsibleAttributeSniff.php b/Magento2/Sniffs/Html/HtmlCollapsibleAttributeSniff.php index 8798c416..79fdd93b 100644 --- a/Magento2/Sniffs/Html/HtmlCollapsibleAttributeSniff.php +++ b/Magento2/Sniffs/Html/HtmlCollapsibleAttributeSniff.php @@ -33,10 +33,13 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - if ($stackPtr !== 0) { + static $lastFile; + if ($lastFile === $phpcsFile->getFilename()) { return; } - $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens())); + $lastFile = $phpcsFile->getFilename(); + + $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr); if (empty($html)) { return; diff --git a/Magento2/Sniffs/Html/HtmlDirectiveSniff.php b/Magento2/Sniffs/Html/HtmlDirectiveSniff.php index 2fdc0518..82ddae5d 100644 --- a/Magento2/Sniffs/Html/HtmlDirectiveSniff.php +++ b/Magento2/Sniffs/Html/HtmlDirectiveSniff.php @@ -50,11 +50,14 @@ public function process(File $phpcsFile, $stackPtr) { $this->usedVariables = []; $this->unfilteredVariables = []; - if ($stackPtr !== 0) { + + static $lastFile; + if ($lastFile === $phpcsFile->getFilename()) { return; } + $lastFile = $phpcsFile->getFilename(); - $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens())); + $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr); if (empty($html)) { return; diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index ecbdca44..574b2b20 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -59,10 +59,13 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - if ($stackPtr !== 0) { + static $lastFile; + if ($lastFile === $phpcsFile->getFilename()) { return; } - $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens())); + $lastFile = $phpcsFile->getFilename(); + + $html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr); if (empty($html)) { return; diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.1.inc similarity index 100% rename from Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.inc rename to Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.1.inc diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.2.inc b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.2.inc new file mode 100644 index 00000000..336c75a1 --- /dev/null +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.2.inc @@ -0,0 +1,35 @@ + + + + + + + + + +
+ + + + +
+ +
+ + + + + + + + + + diff --git a/Magento2/Tests/Html/HtmlCollapsibleAttributeUnitTest.2.inc b/Magento2/Tests/Html/HtmlCollapsibleAttributeUnitTest.2.inc new file mode 100644 index 00000000..6551f9ff --- /dev/null +++ b/Magento2/Tests/Html/HtmlCollapsibleAttributeUnitTest.2.inc @@ -0,0 +1,35 @@ + + + + + + + + +
+ +
+
+ + + +
+
+ Test +
+ > + escapeHtml($element->getLegend()) ?> + + + diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.2.inc b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.2.inc new file mode 100644 index 00000000..70d92444 --- /dev/null +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.2.inc @@ -0,0 +1,45 @@ + + + + + + + + + +
+ + + + +
+ +
+ + + + + + + + + +