Skip to content

Commit

Permalink
[+]: allow to configure special comments via "setSpecialHtmlComments()"
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Aug 5, 2020
1 parent 835fac6 commit 5efab31
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
59 changes: 55 additions & 4 deletions src/voku/helper/HtmlMin.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,23 @@ class HtmlMin implements HtmlMinInterface
private $localDomains = [];

/**
* @var array
* @var string[]
*/
private $domainsToRemoveHttpPrefixFromAttributes = [
'google.com',
'google.de',
];

/**
* @var string[]
*/
private $specialHtmlCommentsStaringWith = [];

/**
* @var string[]
*/
private $specialHtmlCommentsEndingWith = [];

/**
* @var bool
*/
Expand Down Expand Up @@ -1517,6 +1527,30 @@ private function isConditionalComment($comment): bool
return false;
}

/**
* Check if the current string is an special comment.
*
* @param string $comment
*
* @return bool
*/
private function isSpecialComment($comment): bool
{
foreach ($this->specialHtmlCommentsStaringWith as $search) {
if (\strpos($comment, $search) === 0) {
return true;
}
}

foreach ($this->specialHtmlCommentsEndingWith as $search) {
if (\substr($comment, -\strlen($search)) === $search) {
return true;
}
}

return false;
}

/**
* @param string $html
* @param bool $multiDecodeNewHtmlEntity
Expand Down Expand Up @@ -1698,8 +1732,11 @@ private function protectTags(HtmlDomParser $dom): HtmlDomParser

$text = $element->text();

// skip normal comments
if (!$this->isConditionalComment($text)) {
if (
!$this->isConditionalComment($text)
&&
!$this->isSpecialComment($text)
) {
continue;
}

Expand Down Expand Up @@ -1796,7 +1833,7 @@ private function restoreProtectedHtml($matches): string
}

/**
* @param array $domainsToRemoveHttpPrefixFromAttributes
* @param string[] $domainsToRemoveHttpPrefixFromAttributes
*
* @return $this
*/
Expand All @@ -1807,6 +1844,20 @@ public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpP
return $this;
}

/**
* @param string[] $startingWith
* @param string[] $endingWith
*
* @return $this
*/
public function setSpecialHtmlComments(array $startingWith, array $endingWith = []): self
{
$this->specialHtmlCommentsStaringWith = $startingWith;
$this->specialHtmlCommentsEndingWith = $endingWith;

return $this;
}

/**
* Sum-up extra whitespace from dom-nodes.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/HtmlMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,19 @@ public function testHtmlAndCssEdgeCase()
static::assertSame($expected, $htmlMin->minify($html));
}

public function testHtmlWithSpecialHtmlComment()
{
// init
$htmlMin = new HtmlMin();
$htmlMin->setSpecialHtmlComments(['INT_SCRIPT'], ['END_INI_SCRIPT']);

$html = '<p><!--INT_SCRIPT test1 --> lall <!-- test2 --></p> <!-- test2 END_INI_SCRIPT-->';

$expected = '<p><!--INT_SCRIPT test1--> lall <!--test2 END_INI_SCRIPT-->';

static::assertSame($expected, $htmlMin->minify($html));
}

public function testMultipleHorizontalWhitespaceCharactersCollaps()
{
// init
Expand Down

0 comments on commit 5efab31

Please sign in to comment.