Skip to content

Commit 444ed31

Browse files
authored
Merge pull request #66 from hryvinskyi/patch-1
Overwrite special script tags
2 parents 2247ffa + 70d1ccb commit 444ed31

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/voku/helper/HtmlMin.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ class HtmlMin implements HtmlMinInterface
327327
*/
328328
private $templateLogicSyntaxInSpecialScriptTags;
329329

330+
/**
331+
* @var string[]|null
332+
*/
333+
private $specialScriptTags;
334+
330335
/**
331336
* HtmlMin constructor.
332337
*/
@@ -1577,6 +1582,10 @@ private function minifyHtmlDom($html, $multiDecodeNewHtmlEntity): string
15771582
if ($this->templateLogicSyntaxInSpecialScriptTags !== null) {
15781583
$dom->overwriteTemplateLogicSyntaxInSpecialScriptTags($this->templateLogicSyntaxInSpecialScriptTags);
15791584
}
1585+
1586+
if ($this->specialScriptTags !== null) {
1587+
$dom->overwriteSpecialScriptTags($this->specialScriptTags);
1588+
}
15801589

15811590
$dom->getDocument()->preserveWhiteSpace = false; // remove redundant white space
15821591
$dom->getDocument()->formatOutput = false; // do not formats output with indentation
@@ -1951,4 +1960,23 @@ public function overwriteTemplateLogicSyntaxInSpecialScriptTags(array $templateL
19511960

19521961
return $this;
19531962
}
1963+
1964+
1965+
/**
1966+
* @param string[] $specialScriptTags
1967+
*
1968+
* @return HtmlDomParser
1969+
*/
1970+
public function overwriteSpecialScriptTags(array $specialScriptTags): self
1971+
{
1972+
foreach ($specialScriptTags as $tag) {
1973+
if (!\is_string($tag)) {
1974+
throw new \InvalidArgumentException('SpecialScriptTags only allows string[]');
1975+
}
1976+
}
1977+
1978+
$this->specialScriptTags = $specialScriptTags;
1979+
1980+
return $this;
1981+
}
19541982
}

tests/HtmlMinTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,64 @@ public function testHtmlInsideJavaScriptTemplates()
812812
static::assertSame($expected, $htmlMin->minify($html));
813813
}
814814

815+
public function testOverwriteSpecialScriptTags()
816+
{
817+
$html = <<<HTML
818+
<!doctype html>
819+
<html lang="nl">
820+
<head></head>
821+
<body>
822+
<script type="text/x-custom">
823+
<ul class="prices-tier items">
824+
<% _.each(tierPrices, function(item, key) { %>
825+
<% var priceStr = '<span class="price-container price-tier_price">'
826+
+ '<span data-price-amount="' + priceUtils.formatPrice(item.price, currencyFormat) + '"'
827+
+ ' data-price-type=""' + ' class="price-wrapper ">'
828+
+ '<span class="price">' + priceUtils.formatPrice(item.price, currencyFormat) + '</span>'
829+
+ '</span>'
830+
+ '</span>'; %>
831+
<li class="item">
832+
<%= 'some text %1 %2'.replace('%1', item.qty).replace('%2', priceStr) %>
833+
<strong class="benefit">
834+
save <span class="percent tier-<%= key %>">&nbsp;<%= item.percentage %></span>%
835+
</strong>
836+
</li>
837+
<% }); %>
838+
</ul>
839+
</script>
840+
<div data-role="tier-price-block">
841+
<div> Some Content </div>
842+
</div>
843+
</body>
844+
</html>
845+
HTML;
846+
$htmlMin = new voku\helper\HtmlMin();
847+
$htmlMin->overwriteSpecialScriptTags(['text/x-custom']);
848+
$expected = <<<HTML
849+
<!DOCTYPE html><html lang=nl><head> <body><script type=text/x-custom>
850+
<ul class="prices-tier items">
851+
<% _.each(tierPrices, function(item, key) { %>
852+
<% var priceStr = '<span class="price-container price-tier_price">'
853+
+ '<span data-price-amount="' + priceUtils.formatPrice(item.price, currencyFormat) + '"'
854+
+ ' data-price-type=""' + ' class="price-wrapper ">'
855+
+ '<span class="price">' + priceUtils.formatPrice(item.price, currencyFormat) + '</span>'
856+
+ '</span>'
857+
+ '</span>'; %>
858+
<li class="item">
859+
<%= 'some text %1 %2'.replace('%1', item.qty).replace('%2', priceStr) %>
860+
<strong class="benefit">
861+
save <span class="percent tier-<%= key %>">&nbsp;<%= item.percentage %></span>%
862+
</strong>
863+
</li>
864+
<% }); %>
865+
</ul>
866+
</script> <div data-role=tier-price-block><div> Some Content </div> </div>
867+
HTML;
868+
869+
870+
static::assertSame($expected, $htmlMin->minify($html));
871+
}
872+
815873
public function testHtmlClosingTagInSpecialScript()
816874
{
817875
$htmlMin = new \voku\helper\HtmlMin();

0 commit comments

Comments
 (0)