Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,35 @@ public function getString(): string
*/
public function render(OutputFormat $outputFormat): string
{
$string = \addslashes($this->string);
$string = $this->escape($this->string, $outputFormat->getStringQuotingType());
$string = \str_replace("\n", '\\A', $string);
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
}

private function escape(string $str, string $quote) : string
{
$matches = [
0x0, // null
0x5c, // \
];

$matches[] = $quote === '"' ? 0x22 : 0x27;

$length = \strlen($str);
$output = '';
$previous = 0x0;

for ($i = 0; $i < $length; ++$i) {
$current = \ord($str[$i]);

if (\in_array($current, $matches, true) && $previous !== 0x5c) {
$output .= '\\';
}

$output .= $str[$i];
$previous = $current;
}

return $output;
}
}
4 changes: 2 additions & 2 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ public function unicodeParsing(): void
self::assertSame('"\\"\\""', $firstContentRuleAsString);
}
if ($selector === '.test-9') {
self::assertSame('"\\"\\\'"', $firstContentRuleAsString);
self::assertSame('"\\"\'"', $firstContentRuleAsString);
}
if ($selector === '.test-10') {
self::assertSame('"\\\'\\\\"', $firstContentRuleAsString);
self::assertSame('"\'\\\\"', $firstContentRuleAsString);
}
if ($selector === '.test-11') {
self::assertSame('"test"', $firstContentRuleAsString);
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/unicode.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.test-6 { content: "\00A5" } /* Same as "¥" */
.test-7 { content: '\a' } /* Same as "\A" (Newline) */
.test-8 { content: "\"\22" } /* Same as "\"\"" */
.test-9 { content: "\"\27" } /* Same as ""\"\'"" */
.test-9 { content: "\"\27" } /* Same as ""\"'"" */
.test-10 { content: "\'\\" } /* Same as "'\" */
.test-11 { content: "\test" } /* Same as "test" */

Expand Down
Loading