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

Do not strip knockout comments from .html files #118

Merged
merged 2 commits into from
Feb 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,51 @@ public function testWebTemplateHtmlVendorFileNonMeaningfulChange()
];
$this->assertEquals($expected, $ignored);
}

/**
*
*/
public function testWebTemplateHtmlKnockout()
{
$this->m2->expects($this->any())
->method('getListOfHtmlFiles')
->willReturn(
[
'app/design/frontend/Ampersand/theme/Magento_Ui/web/templates/grid/knockout.html',
'vendor/magento/module-ui/view/base/web/templates/grid/knockout.html',
]
);

$reader = new Reader(
$this->testResourcesDir . 'vendor.patch'
);

$entries = $reader->getFiles();
$this->assertNotEmpty($entries, 'We should have a patch file to read');

$entry = $entries[3];

$appCodeGetter = new GetAppCodePathFromVendorPath($this->m2, $entry);
$appCodeFilePath = $appCodeGetter->getAppCodePathFromVendorPath();
$this->assertEquals(
'app/code/Magento/Ui/view/base/web/templates/grid/knockout.html',
$appCodeFilePath
);

$warnings = $infos = $ignored = [];

$check = new WebTemplateHtml($this->m2, $entry, $appCodeFilePath, $warnings, $infos, $ignored);
$this->assertTrue($check->canCheck(), 'Check should be checkable');
$check->check();

$this->assertEmpty($ignored, 'We should have no ignore level items');
$this->assertEmpty($infos, 'We should have no info level items');
$this->assertNotEmpty($warnings, 'We should have a warning');
$expectedWarnings = [
'Override (phtml/js/html)' => [
'app/design/frontend/Ampersand/theme/Magento_Ui/web/templates/grid/knockout.html'
]
];
$this->assertEquals($expectedWarnings, $warnings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>some override</h1>
10 changes: 10 additions & 0 deletions dev/phpunit/unit/resources/checks/WebTemplateHtml/vendor.patch
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ diff -ur -N vendor_orig/magento/module-ui/view/base/web/templates/grid/some_noop
-<!-- -->
\ No newline at end of file
+<!-- --> <!-- --> <!-- -->
diff -ur -N vendor_orig/magento/module-ui/view/base/web/templates/grid/knockout.html vendor/magento/module-ui/view/base/web/templates/grid/knockout.html
--- vendor_orig/magento/module-ui/view/base/web/templates/grid/knockout.html 2024-02-08 20:13:23.000000000 +0000
+++ vendor/magento/module-ui/view/base/web/templates/grid/knockout.html 2024-02-08 20:13:23.000000000 +0000
@@ -1,5 +1,5 @@
<h1>hello</h1>
<h1>goodbye</h1>
-<!-- ko foreach: getRegion('captcha') -->
+<!-- ko foreach: getRegion('somethingweird') -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>hello</h1>
<h1>goodbye</h1>
<!-- ko foreach: getRegion('somethingweird') -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>hello</h1>
<h1>goodbye</h1>
<!-- ko foreach: getRegion('captcha') -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
3 changes: 2 additions & 1 deletion src/Ampersand/PatchHelper/Patchfile/Sanitiser.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static function stripCommentsFromHtml($contents)
{
// This regular expression will match and remove both single-line <!-- ... --> comments and multi-line comments
// spanning multiple lines. The s flag is used to make the dot (.) match any character, including newlines.
return preg_replace('/<!--(.*?)-->/s', '', $contents);
// This does not include knockout templates, as logic is embedded in comments
return preg_replace('/<!--(?! *ko)(.*?)-->/s', '', $contents);
}
}