Skip to content

[CLEANUP] Always escape backslashes in strings #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2024
Merged
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
8 changes: 4 additions & 4 deletions src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function parseIdentifier($bIgnoreCase = true)
}
$sCharacter = null;
while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
if (\preg_match('/[a-zA-Z0-9\x{00A0}-\x{FFFF}_-]/Sux', $sCharacter)) {
if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $sCharacter)) {
$sResult .= $sCharacter;
} else {
$sResult .= '\\' . $sCharacter;
Expand All @@ -169,13 +169,13 @@ public function parseCharacter($bIsForIdentifier)
if ($this->peek() === '\\') {
if (
$bIsForIdentifier && $this->oParserSettings->bLenientParsing
&& ($this->comes('\0') || $this->comes('\9'))
&& ($this->comes('\\0') || $this->comes('\\9'))
) {
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
return null;
}
$this->consume('\\');
if ($this->comes('\n') || $this->comes('\r')) {
if ($this->comes('\\n') || $this->comes('\\r')) {
return '';
}
if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
Expand All @@ -185,7 +185,7 @@ public function parseCharacter($bIsForIdentifier)
if ($this->strlen($sUnicode) < 6) {
// Consume whitespace after incomplete unicode escape
if (\preg_match('/\\s/isSu', $this->peek())) {
if ($this->comes('\r\n')) {
if ($this->comes('\\r\\n')) {
$this->consume(2);
} else {
$this->consume(1);
Expand Down
8 changes: 4 additions & 4 deletions src/Property/KeyframeSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class KeyframeSelector extends Selector
public const SELECTOR_VALIDATION_RX = '/
^(
(?:
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters
(?:\\\\.)? # a single escaped character
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"]
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters
(?:\\\\.)? # a single escaped character
(?:([\'"]).*?(?<!\\\\)\\2)? # a quoted text like [id="example"]
)*
)|
(\d+%) # keyframe animation progress percentage (e.g. 50%)
(\\d+%) # keyframe animation progress percentage (e.g. 50%)
$
/ux';
}
16 changes: 8 additions & 8 deletions src/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Selector
* @var string
*/
private const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
(\.[\w]+) # classes
(\\.[\\w]+) # classes
|
\[(\w+) # attributes
\\[(\\w+) # attributes
|
(\:( # pseudo classes
(\\:( # pseudo classes
link|visited|active
|hover|focus
|lang
Expand All @@ -38,9 +38,9 @@ class Selector
* @var string
*/
private const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
((^|[\s\+\>\~]+)[\w]+ # elements
((^|[\\s\\+\\>\\~]+)[\\w]+ # elements
|
\:{1,2}( # pseudo-elements
\\:{1,2}( # pseudo-elements
after|before|first-letter|first-line|selection
))
/ix';
Expand All @@ -55,9 +55,9 @@ class Selector
public const SELECTOR_VALIDATION_RX = '/
^(
(?:
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters
(?:\\\\.)? # a single escaped character
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"]
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters
(?:\\\\.)? # a single escaped character
(?:([\'"]).*?(?<!\\\\)\\2)? # a quoted text like [id="example"]
)*
)$
/ux';
Expand Down
2 changes: 1 addition & 1 deletion src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __toString(): string
public function render(OutputFormat $oOutputFormat): string
{
$sString = \addslashes($this->sString);
$sString = \str_replace("\n", '\A', $sString);
$sString = \str_replace("\n", '\\A', $sString);
return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType();
}
}
4 changes: 2 additions & 2 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ public function render(OutputFormat $oOutputFormat): string
{
$l = \localeconv();
$sPoint = \preg_quote($l['decimal_point'], '/');
$sSize = \preg_match("/[\d\.]+e[+-]?\d+/i", (string) $this->fSize)
$sSize = \preg_match("/[\\d\\.]+e[+-]?\\d+/i", (string) $this->fSize)
? \preg_replace("/$sPoint?0+$/", "", \sprintf("%f", $this->fSize)) : $this->fSize;
return \preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize)
return \preg_replace(["/$sPoint/", "/^(-?)0\\./"], ['.', '$1.'], $sSize)
. ($this->sUnit ?? '');
}
}
2 changes: 1 addition & 1 deletion src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static function parseUnicodeRangeValue(ParserState $oParserState): strin
$iCodepointMaxLength = 13; // Max length is 2 six digit code points + the dash(-) between them
}
$sRange .= $oParserState->consume(1);
} while (\strlen($sRange) < $iCodepointMaxLength && \preg_match("/[A-Fa-f0-9\?-]/", $oParserState->peek()));
} while (\strlen($sRange) < $iCodepointMaxLength && \preg_match("/[A-Fa-f0-9\\?-]/", $oParserState->peek()));
return "U+{$sRange}";
}

Expand Down
22 changes: 11 additions & 11 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ public function unicodeParsing(): void
self::assertSame('"¥"', $sString);
}
if ($sSelector == '.test-7') {
self::assertSame('"\A"', $sString);
self::assertSame('"\\A"', $sString);
}
if ($sSelector == '.test-8') {
self::assertSame('"\"\""', $sString);
self::assertSame('"\\"\\""', $sString);
}
if ($sSelector == '.test-9') {
self::assertSame('"\"\\\'"', $sString);
self::assertSame('"\\"\\\'"', $sString);
}
if ($sSelector == '.test-10') {
self::assertSame('"\\\'\\\\"', $sString);
Expand Down Expand Up @@ -795,8 +795,8 @@ public function invalidSelectorsInFile(): void
public function selectorEscapesInFile(): void
{
$oDoc = self::parsedStructureForFile('selector-escapes', Settings::create()->withMultibyteSupport(true));
$sExpected = '#\# {color: red;}
.col-sm-1\/5 {width: 20%;}';
$sExpected = '#\\# {color: red;}
.col-sm-1\\/5 {width: 20%;}';
self::assertSame($sExpected, $oDoc->render());

$oDoc = self::parsedStructureForFile('invalid-selectors-2', Settings::create()->withMultibyteSupport(true));
Expand All @@ -815,8 +815,8 @@ public function selectorEscapesInFile(): void
public function identifierEscapesInFile(): void
{
$oDoc = self::parsedStructureForFile('identifier-escapes', Settings::create()->withMultibyteSupport(true));
$sExpected = 'div {font: 14px Font Awesome\ 5 Pro;font: 14px Font Awesome\} 5 Pro;'
. 'font: 14px Font Awesome\; 5 Pro;f\;ont: 14px Font Awesome\; 5 Pro;}';
$sExpected = 'div {font: 14px Font Awesome\\ 5 Pro;font: 14px Font Awesome\\} 5 Pro;'
. 'font: 14px Font Awesome\\; 5 Pro;f\\;ont: 14px Font Awesome\\; 5 Pro;}';
self::assertSame($sExpected, $oDoc->render());
}

Expand Down Expand Up @@ -1103,8 +1103,8 @@ public function ieHacksStrictParsing(): void
public function ieHacksParsing(): void
{
$oDoc = self::parsedStructureForFile('ie-hacks', Settings::create()->withLenientParsing(true));
$sExpected = 'p {padding-right: .75rem \9;background-image: none \9;color: red \9\0;'
. 'background-color: red \9\0;background-color: red \9\0 !important;content: "red \0";content: "red઼";}';
$sExpected = 'p {padding-right: .75rem \\9;background-image: none \\9;color: red \\9\\0;'
. 'background-color: red \\9\\0;background-color: red \\9\\0 !important;content: "red \\0";content: "red઼";}';
self::assertSame($sExpected, $oDoc->render());
}

Expand Down Expand Up @@ -1246,7 +1246,7 @@ public function escapedSpecialCaseTokens(): void
$rules = $contents[0]->getRules();
$urlRule = $rules[0];
$calcRule = $rules[1];
self::assertTrue(\is_a($urlRule->getValue(), '\Sabberworm\CSS\Value\URL'));
self::assertTrue(\is_a($calcRule->getValue(), '\Sabberworm\CSS\Value\CalcFunction'));
self::assertTrue(\is_a($urlRule->getValue(), '\\Sabberworm\\CSS\\Value\\URL'));
self::assertTrue(\is_a($calcRule->getValue(), '\\Sabberworm\\CSS\\Value\\CalcFunction'));
}
}