Skip to content

Commit

Permalink
Only detect and remove <script> attributes
Browse files Browse the repository at this point in the history
Before this change, the sniff would alert on (and remove) any non-PHP text
which matched, not only <script> attributes.
  • Loading branch information
fredden committed Jun 26, 2023
1 parent 1681210 commit 1eebe6c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
private function checkHtml(File $phpcsFile, int $stackPtr): void
{
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
$pattern = '_\s+type=(["\'])text/javascript\1_i';
$pattern = '_(<script[^>\s]*)\stype=(["\'])text/javascript\2_i';

if (preg_match($pattern, $content)) {
if (preg_match($pattern, $content, $matches)) {
$fix = $phpcsFile->addFixableWarning(
'Please do not use "text/javascript" type attribute.',
$stackPtr,
self::WARNING_CODE_TEXT_JAVASCRIPT
);

if ($fix) {
$content = preg_replace($pattern, '', $content);
$content = preg_replace($pattern, $matches[1], $content);
$phpcsFile->fixer->replaceToken($stackPtr, $content);
}
}
Expand Down

0 comments on commit 1eebe6c

Please sign in to comment.