Skip to content

Commit 676dd8c

Browse files
committed
[FEATURE] Do not escape characters that do not need escaping in CSS string
1 parent 2b61cd5 commit 676dd8c

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Please also have a look at our
3939

4040
### Documentation
4141

42+
## 9.2.0:
43+
44+
### Fixed
45+
46+
- Do not escape characters that do not need escaping in CSS string (#1444)
47+
4248
## 9.1.0: Add support for PHP 8.5
4349

4450
### Added

src/Value/CSSString.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getString(): string
9393
*/
9494
public function render(OutputFormat $outputFormat): string
9595
{
96-
$string = \addslashes($this->string);
96+
$string = $this->escape($this->string, $outputFormat->getStringQuotingType());
9797
$string = \str_replace("\n", '\\A', $string);
9898
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
9999
}
@@ -111,4 +111,13 @@ public function getArrayRepresentation(): array
111111
'contents' => $this->string,
112112
];
113113
}
114+
115+
private function escape(string $str, string $quote) : string
116+
{
117+
$str = \addslashes($str);
118+
119+
$replace = $quote === '"' ? ["\\'" => "'"] : ["\\\"" => "\""];
120+
121+
return \str_replace(array_keys($replace), array_values($replace), $str);
122+
}
114123
}

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)