diff options
author | Michael Große <mic.grosse@googlemail.com> | 2020-01-19 23:24:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-19 23:24:19 +0100 |
commit | 65fda485d44f1cb0f57c3adeacc0426d3f18d6d7 (patch) | |
tree | df182b182e6595e5916f1d4bc2eff36ab1d43545 | |
parent | 546e4fa1a966866d8357d585a86872ceba169e8c (diff) | |
parent | 2f5645efb6338abcacae559d6f5d3eeeca978051 (diff) | |
download | dokuwiki-65fda485d44f1cb0f57c3adeacc0426d3f18d6d7.tar.gz dokuwiki-65fda485d44f1cb0f57c3adeacc0426d3f18d6d7.zip |
css_compress: use placeholder to keep quoted strings (#2670)
css_compress: use placeholder to keep quoted strings
-rw-r--r-- | _test/tests/lib/exe/css_css_compress.test.php | 15 | ||||
-rw-r--r-- | lib/exe/css.php | 39 |
2 files changed, 34 insertions, 20 deletions
diff --git a/_test/tests/lib/exe/css_css_compress.test.php b/_test/tests/lib/exe/css_css_compress.test.php index 807317ca6..35654ec0c 100644 --- a/_test/tests/lib/exe/css_css_compress.test.php +++ b/_test/tests/lib/exe/css_css_compress.test.php @@ -71,7 +71,7 @@ class css_css_compress_test extends DokuWikiTest { function test_hack(){ $text = '/* Mac IE will not see this and continue with inline-block */ /* \\*/ - display: inline; + display: inline; /* */'; $this->assertEquals('/* \\*/display:inline;/* */', css_compress($text)); } @@ -138,6 +138,19 @@ class css_css_compress_test extends DokuWikiTest { $this->assertEquals($expect, css_compress($input)); } + function test_quotes() { + $input = '/* "blockcomment" */ content: "/* STR2 : STR1 */ thisis : inquote"; STR1: 10px; STR2:"STR1"; STR3:\'STR1\';'; + $expect = 'content:"/* STR2 : STR1 */ thisis : inquote";STR1:10px;STR2:"STR1";STR3:\'STR1\';'; + + $this->assertEquals($expect, css_compress($input)); + } + + function test_escapedQuotes() { + $inputEscapedQuote = 'content:"one quote visible: \\" "; foo: bar;//"'; + $expectedOutput = 'content:"one quote visible: \\" ";foo:bar;'; + + $this->assertEquals($expectedOutput, css_compress($inputEscapedQuote)); + } } //Setup VIM: ex: et ts=4 : diff --git a/lib/exe/css.php b/lib/exe/css.php index 50ddc335c..2ea2c0963 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -543,10 +543,20 @@ function css_pluginstyles($mediatype='screen'){ * @return string */ function css_compress($css){ - //strip comments through a callback + // replace quoted strings with placeholder + $quote_storage = []; + + $quote_cb = function ($match) use (&$quote_storage) { + $quote_storage[] = $match[0]; + return '"STR'.(count($quote_storage)-1).'"'; + }; + + $css = preg_replace_callback('/(([\'"]).*?(?<!\\\\)\2)/', $quote_cb, $css); + + // strip comments through a callback $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); - //strip (incorrect but common) one line comments + // strip (incorrect but common) one line comments $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css); // strip whitespaces @@ -600,6 +610,14 @@ function css_compress($css){ $css ); + // replace back protected strings + $quote_back_cb = function ($match) use (&$quote_storage) { + return $quote_storage[$match[1]]; + }; + + $css = preg_replace_callback('/"STR(\d+)"/', $quote_back_cb, $css); + $css = trim($css); + return $css; } @@ -642,23 +660,6 @@ function css_onelinecomment_cb($matches) { break; } - // keep any quoted string that starts before a comment - $nextsqt = strpos($line, "'", $i); - $nextdqt = strpos($line, '"', $i); - if(min($nextsqt, $nextdqt) < $nextcom) { - $skipto = false; - if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) { - $skipto = strpos($line, "'", $nextsqt+1) +1; - } else if ($nextdqt !== false) { - $skipto = strpos($line, '"', $nextdqt+1) +1; - } - - if($skipto !== false) { - $i = $skipto; - continue; - } - } - if($nexturl === false || $nextcom < $nexturl) { // no url anymore, strip comment and be done $i = $nextcom; |