Skip to content

Commit

Permalink
Merge pull request #146 from stof/pcre_jit_error
Browse files Browse the repository at this point in the history
Use possessive quantifiers to avoid reaching PCRE limits
  • Loading branch information
tijsverkoyen committed Sep 6, 2016
2 parents e4ba269 + a6c086a commit bb379f4
Show file tree
Hide file tree
Showing 3 changed files with 1,173 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Css/Processor.php
Expand Up @@ -3,6 +3,7 @@
namespace TijsVerkoyen\CssToInlineStyles\Css;

use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor as RuleProcessor;
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Rule;

class Processor
{
Expand Down Expand Up @@ -50,15 +51,15 @@ public function getCssFromStyleTags($html)
private function doCleanup($css)
{
// remove charset
$css = preg_replace('/@charset "[^"]+";/', '', $css);
$css = preg_replace('/@charset "[^"]++";/', '', $css);
// remove media queries
$css = preg_replace('/@media [^{]*{([^{}]|{[^{}]*})*}/', '', $css);
$css = preg_replace('/@media [^{]*+{([^{}]++|{[^{}]*+})*+}/', '', $css);

$css = str_replace(array("\r", "\n"), '', $css);
$css = str_replace(array("\t"), ' ', $css);
$css = str_replace('"', '\'', $css);
$css = preg_replace('|/\*.*?\*/|', '', $css);
$css = preg_replace('/\s\s+/', ' ', $css);
$css = preg_replace('/\s\s++/', ' ', $css);
$css = trim($css);

return $css;
Expand Down
7 changes: 7 additions & 0 deletions tests/Css/ProcessorTest.php
Expand Up @@ -94,6 +94,13 @@ public function testCssWithMediaQueries()
$this->assertEquals(1, $rules[0]->getOrder());
}

public function testCssWithBigMediaQueries()
{
$rules = $this->processor->getRules(file_get_contents(__DIR__.'/test.css'));

$this->assertCount(414, $rules);
}

public function testMakeSureMediaQueriesAreRemoved()
{
$css = '@media tv and (min-width: 700px) and (orientation: landscape) {.foo {display: none;}}';
Expand Down

0 comments on commit bb379f4

Please sign in to comment.