Skip to content

Commit

Permalink
Merge pull request #118 from convenient/dont-strip-knockout-comments
Browse files Browse the repository at this point in the history
Do not strip knockout comments from `.html` files
  • Loading branch information
tr33m4n authored Feb 9, 2024
2 parents a7cc0c6 + db9743a commit 544fff0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
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);
}
}

0 comments on commit 544fff0

Please sign in to comment.