Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-fix Magento2.Legacy.PhtmlTemplate.TextJavascriptTypeFound #448

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PhtmlTemplateSniff implements Sniff
{
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound';
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess';

/**
* @inheritdoc
*/
Expand All @@ -40,7 +40,7 @@ public function process(File $phpcsFile, $stackPtr)
$this->checkHtml($phpcsFile, $stackPtr);
}
}

/**
* Check access to protected and private members of Block
*
Expand Down Expand Up @@ -74,13 +74,19 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
private function checkHtml(File $phpcsFile, int $stackPtr): void
{
$content = $phpcsFile->getTokensAsString($stackPtr, 1);

if (preg_match('/type="text\/javascript"/', $content)) {
$phpcsFile->addWarning(
$pattern = '_(<script[^>]*?)\s+type=(["\'])text/javascript\2_i';

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, $matches[1], $content);
$phpcsFile->fixer->replaceToken($stackPtr, $content);
}
}
}
}
34 changes: 16 additions & 18 deletions Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function getTestFiles($testFileBase): array

// Put them in order.
sort($testFiles);

return $testFiles;
}

Expand All @@ -54,22 +54,20 @@ public function getErrorList()
*/
public function getWarningList($testFile = '')
{
if ($testFile === 'PhtmlTemplateUnitTest.1.phtml' || $testFile === 'PhtmlTemplateUnitTest.2.phtml') {
return [
9 => 1,
20 => 1,
23 => 1,
27 => 1
];
}
if ($testFile === 'PhtmlTemplateUnitTest.3.phtml') {
return [
9 => 1,
20 => 1,
23 => 1,
27 => 1,
];
}
return [];
return [
9 => 1,
20 => 1,
23 => 1,
27 => 1,
44 => 1,
45 => 1,
46 => 1,
47 => 1,
48 => 1,
49 => 1,
50 => 1,
51 => 1,
52 => 1,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ $this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script type='text/javascript'></script>
<script type="TEXT/JAVASCRIPT"></script>
<SCRIPT TYPE="text/javascript"></SCRIPT>
<script type="text/javascript"></script>
<script async type="text/javascript"></script>
<script defer type="text/javascript"></script>
<script type="text/javascript" defer></script>
<script crossorigin="anonymous" async type="text/javascript" src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer type="text/javascript"></script>
<script>
const type="text/javascript";
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script></script>
<script></script>
<SCRIPT></SCRIPT>
<script></script>
<script async></script>
<script defer></script>
<script defer></script>
<script crossorigin="anonymous" async src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer></script>
<script>
const type="text/javascript";
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ $this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script type='text/javascript'></script>
<script type="TEXT/JAVASCRIPT"></script>
<SCRIPT TYPE="text/javascript"></SCRIPT>
<script type="text/javascript"></script>
<script async type="text/javascript"></script>
<script defer type="text/javascript"></script>
<script type="text/javascript" defer></script>
<script crossorigin="anonymous" async type="text/javascript" src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer type="text/javascript"></script>
<script>
const type="text/javascript";
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script></script>
<script></script>
<SCRIPT></SCRIPT>
<script></script>
<script async></script>
<script defer></script>
<script defer></script>
<script crossorigin="anonymous" async src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer></script>
<script>
const type="text/javascript";
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ $this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script type='text/javascript'></script>
<script type="TEXT/JAVASCRIPT"></script>
<SCRIPT TYPE="text/javascript"></SCRIPT>
<script type="text/javascript"></script>
<script async type="text/javascript"></script>
<script defer type="text/javascript"></script>
<script type="text/javascript" defer></script>
<script crossorigin="anonymous" async type="text/javascript" src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer type="text/javascript"></script>
<script>
const type="text/javascript";
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>

<p>This sniff should remove the any <code> type="text/javascript" </code> attributes from &lt;script&gt; tags, but not from elsewhere in the document.</p>

<script></script>
<script></script>
<SCRIPT></SCRIPT>
<script></script>
<script async></script>
<script defer></script>
<script defer></script>
<script crossorigin="anonymous" async src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://unpkg.com/alpinejs" defer></script>
<script>
const type="text/javascript";
</script>