Skip to content

Commit

Permalink
[+]: fix "$htmlCommentWithoutConditionalCommentRegEx" is too greedy +…
Browse files Browse the repository at this point in the history
… more tests

thx @haet -> d6a10ad
  • Loading branch information
voku committed Oct 18, 2017
1 parent a8d1480 commit 5967655
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CssToInlineStyles.php
Expand Up @@ -52,7 +52,7 @@ class CssToInlineStyles
*
* @var string
*/
private static $htmlCommentWithoutConditionalCommentRegEx = '|<!--(?!\[if).*?-->|isU';
private static $htmlCommentWithoutConditionalCommentRegEx = '|<!--(?!\[if).*-->|isU';

/**
* regular expression: style-tag with 'cleanup'-css-class
Expand Down
42 changes: 42 additions & 0 deletions tests/CssToInlineStylesTest.php
Expand Up @@ -206,6 +206,15 @@ public function testCssWithComments()
self::assertSame('<h1 style="color: rebeccapurple;"><a style="display: block; padding: 5px;">foo</a></h1>', $result);
}

public function testCssWithCommentsInOneLine()
{
$css = <<<CSS
a { padding: 5px; display: block; } /* style the titles */ h1 { color: rebeccapurple; } /* end of title styles */
CSS;
$result = $this->cssToInlineStyles->setHTML('<h1><a>foo</a></h1>')->setCSS($css)->convert();
self::assertSame('<h1 style="color: rebeccapurple;"><a style="display: block; padding: 5px;">foo</a></h1>', $result);
}

public function testCssWithMediaQueries()
{
$css = <<<EOF
Expand Down Expand Up @@ -717,6 +726,17 @@ public function testExcludeConditionalInlineStylesBlock()
$this->cssToInlineStyles->setStripOriginalStyleTags(true);
$this->cssToInlineStyles->setExcludeMediaQueries(true);
$this->runHTMLToCSS($html, $css, $expected);

// ---

$html = '<html><body><!--[if gte mso 9]><style>.test { top: 1em; } </style><![endif]--><style>div { width: 200px; width: 222px; }</style><!-- <style> .test { width: 0 !important; } </style> --><div class="test"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$css = '';
$expected = '<html><body><!--[if gte mso 9]><style>.test { top: 1em; } </style><![endif]--><style></style><!-- <style> .test { width: 0 !important; } </style> --><div class="test" style="width: 200px; width: 222px;"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$this->cssToInlineStyles->setUseInlineStylesBlock(true);
$this->cssToInlineStyles->setExcludeConditionalInlineStylesBlock(true);
$this->cssToInlineStyles->setStripOriginalStyleTags(true);
$this->cssToInlineStyles->setExcludeMediaQueries(true);
$this->runHTMLToCSS($html, $css, $expected);
}

public function testExcludeConditionalInlineStylesBlockFalse()
Expand All @@ -740,6 +760,28 @@ public function testExcludeConditionalInlineStylesBlockFalse()
$this->cssToInlineStyles->setStripOriginalStyleTags(true);
$this->cssToInlineStyles->setExcludeMediaQueries(true);
$this->runHTMLToCSS($html, $css, $expected);

// ---

$html = '<html><body><!--[if gte mso 9]><style>.test { top: 1em; } </style><![endif]--><style>div { width: 200px; width: 222px; }</style><!-- <style> .test { width: 0 !important; } </style> --><div class="test"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$css = '';
$expected = '<html><body><!--[if gte mso 9]><style>.test { top: 1em; } </style><![endif]--><style></style><!-- <style> .test { width: 0 !important; } </style> --><div class="test" style="width: 222px; top: 1em;"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$this->cssToInlineStyles->setUseInlineStylesBlock(true);
$this->cssToInlineStyles->setExcludeConditionalInlineStylesBlock(false);
$this->cssToInlineStyles->setStripOriginalStyleTags(true);
$this->cssToInlineStyles->setExcludeMediaQueries(true);
$this->runHTMLToCSS($html, $css, $expected);
}

public function testBugHtmlCommentWithoutConditionalCommentRegExTooGreedy()
{
$html = '<html><body><style>div { width: 200px; <!-- width: 220px; --> width: 222px; <!-- width: 224px; --> }</style><div class="test"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$css = '';
$expected = '<html><body><style></style><div class="test" style="width: 200px; width: 222px;"><h1>foo</h1><h1>foo2</h1></div></body></html>';
$this->cssToInlineStyles->setUseInlineStylesBlock(true);
$this->cssToInlineStyles->setStripOriginalStyleTags(true);
$this->cssToInlineStyles->setExcludeMediaQueries(true);
$this->runHTMLToCSS($html, $css, $expected);
}

public function testBug83()
Expand Down

0 comments on commit 5967655

Please sign in to comment.