Skip to content

Commit 47970ab

Browse files
committed
[FEATURE] Do not escape characters that do not need escaping in CSS string
1 parent 0ae1fde commit 47970ab

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/Value/CSSString.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ public function getString(): string
9090
*/
9191
public function render(OutputFormat $outputFormat): string
9292
{
93-
$string = \addslashes($this->string);
93+
$string = $this->escape($this->string, $outputFormat->getStringQuotingType());
9494
$string = \str_replace("\n", '\\A', $string);
9595
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
9696
}
97+
98+
private function escape(string $str, string $quote) : string
99+
{
100+
$str = \addslashes($str);
101+
102+
$replace = $quote === '"' ? ["\\'" => "'"] : ["\\\"" => "\""];
103+
104+
return \str_replace(array_keys($replace), array_values($replace), $str);
105+
}
97106
}

tests/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ public function unicodeParsing(): void
206206
self::assertSame('"\\"\\""', $firstContentRuleAsString);
207207
}
208208
if ($selector === '.test-9') {
209-
self::assertSame('"\\"\\\'"', $firstContentRuleAsString);
209+
self::assertSame('"\\"\'"', $firstContentRuleAsString);
210210
}
211211
if ($selector === '.test-10') {
212-
self::assertSame('"\\\'\\\\"', $firstContentRuleAsString);
212+
self::assertSame('"\'\\\\"', $firstContentRuleAsString);
213213
}
214214
if ($selector === '.test-11') {
215215
self::assertSame('"test"', $firstContentRuleAsString);

tests/fixtures/unicode.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.test-6 { content: "\00A5" } /* Same as "¥" */
66
.test-7 { content: '\a' } /* Same as "\A" (Newline) */
77
.test-8 { content: "\"\22" } /* Same as "\"\"" */
8-
.test-9 { content: "\"\27" } /* Same as ""\"\'"" */
8+
.test-9 { content: "\"\27" } /* Same as ""\"'"" */
99
.test-10 { content: "\'\\" } /* Same as "'\" */
1010
.test-11 { content: "\test" } /* Same as "test" */
1111

0 commit comments

Comments
 (0)