Skip to content

Commit 19fa907

Browse files
committed
[CLEANUP] Always escape backslashes in strings
This was done using the `string_implicit_backslashes` PHP-CS-Fixer rule.
1 parent 9c42211 commit 19fa907

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

src/Parsing/ParserState.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function parseIdentifier($bIgnoreCase = true)
144144
}
145145
$sCharacter = null;
146146
while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
147-
if (\preg_match('/[a-zA-Z0-9\x{00A0}-\x{FFFF}_-]/Sux', $sCharacter)) {
147+
if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $sCharacter)) {
148148
$sResult .= $sCharacter;
149149
} else {
150150
$sResult .= '\\' . $sCharacter;
@@ -169,13 +169,13 @@ public function parseCharacter($bIsForIdentifier)
169169
if ($this->peek() === '\\') {
170170
if (
171171
$bIsForIdentifier && $this->oParserSettings->bLenientParsing
172-
&& ($this->comes('\0') || $this->comes('\9'))
172+
&& ($this->comes('\\0') || $this->comes('\\9'))
173173
) {
174174
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
175175
return null;
176176
}
177177
$this->consume('\\');
178-
if ($this->comes('\n') || $this->comes('\r')) {
178+
if ($this->comes('\\n') || $this->comes('\\r')) {
179179
return '';
180180
}
181181
if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
@@ -185,7 +185,7 @@ public function parseCharacter($bIsForIdentifier)
185185
if ($this->strlen($sUnicode) < 6) {
186186
// Consume whitespace after incomplete unicode escape
187187
if (\preg_match('/\\s/isSu', $this->peek())) {
188-
if ($this->comes('\r\n')) {
188+
if ($this->comes('\\r\\n')) {
189189
$this->consume(2);
190190
} else {
191191
$this->consume(1);

src/Property/KeyframeSelector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class KeyframeSelector extends Selector
1414
public const SELECTOR_VALIDATION_RX = '/
1515
^(
1616
(?:
17-
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters
18-
(?:\\\\.)? # a single escaped character
19-
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"]
17+
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters
18+
(?:\\\\.)? # a single escaped character
19+
(?:([\'"]).*?(?<!\\\\)\\2)? # a quoted text like [id="example"]
2020
)*
2121
)|
22-
(\d+%) # keyframe animation progress percentage (e.g. 50%)
22+
(\\d+%) # keyframe animation progress percentage (e.g. 50%)
2323
$
2424
/ux';
2525
}

src/Property/Selector.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Selector
1414
* @var string
1515
*/
1616
private const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
17-
(\.[\w]+) # classes
17+
(\\.[\\w]+) # classes
1818
|
19-
\[(\w+) # attributes
19+
\\[(\\w+) # attributes
2020
|
21-
(\:( # pseudo classes
21+
(\\:( # pseudo classes
2222
link|visited|active
2323
|hover|focus
2424
|lang
@@ -38,9 +38,9 @@ class Selector
3838
* @var string
3939
*/
4040
private const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
41-
((^|[\s\+\>\~]+)[\w]+ # elements
41+
((^|[\\s\\+\\>\\~]+)[\\w]+ # elements
4242
|
43-
\:{1,2}( # pseudo-elements
43+
\\:{1,2}( # pseudo-elements
4444
after|before|first-letter|first-line|selection
4545
))
4646
/ix';
@@ -55,9 +55,9 @@ class Selector
5555
public const SELECTOR_VALIDATION_RX = '/
5656
^(
5757
(?:
58-
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters
59-
(?:\\\\.)? # a single escaped character
60-
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"]
58+
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters
59+
(?:\\\\.)? # a single escaped character
60+
(?:([\'"]).*?(?<!\\\\)\\2)? # a quoted text like [id="example"]
6161
)*
6262
)$
6363
/ux';

src/Value/CSSString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function __toString(): string
9494
public function render(OutputFormat $oOutputFormat): string
9595
{
9696
$sString = \addslashes($this->sString);
97-
$sString = \str_replace("\n", '\A', $sString);
97+
$sString = \str_replace("\n", '\\A', $sString);
9898
return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType();
9999
}
100100
}

src/Value/Size.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ public function render(OutputFormat $oOutputFormat): string
203203
{
204204
$l = \localeconv();
205205
$sPoint = \preg_quote($l['decimal_point'], '/');
206-
$sSize = \preg_match("/[\d\.]+e[+-]?\d+/i", (string) $this->fSize)
206+
$sSize = \preg_match("/[\\d\\.]+e[+-]?\\d+/i", (string) $this->fSize)
207207
? \preg_replace("/$sPoint?0+$/", "", \sprintf("%f", $this->fSize)) : $this->fSize;
208-
return \preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize)
208+
return \preg_replace(["/$sPoint/", "/^(-?)0\\./"], ['.', '$1.'], $sSize)
209209
. ($this->sUnit ?? '');
210210
}
211211
}

src/Value/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private static function parseUnicodeRangeValue(ParserState $oParserState): strin
203203
$iCodepointMaxLength = 13; // Max length is 2 six digit code points + the dash(-) between them
204204
}
205205
$sRange .= $oParserState->consume(1);
206-
} while (\strlen($sRange) < $iCodepointMaxLength && \preg_match("/[A-Fa-f0-9\?-]/", $oParserState->peek()));
206+
} while (\strlen($sRange) < $iCodepointMaxLength && \preg_match("/[A-Fa-f0-9\\?-]/", $oParserState->peek()));
207207
return "U+{$sRange}";
208208
}
209209

tests/ParserTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ public function unicodeParsing(): void
202202
self::assertSame('"¥"', $sString);
203203
}
204204
if ($sSelector == '.test-7') {
205-
self::assertSame('"\A"', $sString);
205+
self::assertSame('"\\A"', $sString);
206206
}
207207
if ($sSelector == '.test-8') {
208-
self::assertSame('"\"\""', $sString);
208+
self::assertSame('"\\"\\""', $sString);
209209
}
210210
if ($sSelector == '.test-9') {
211-
self::assertSame('"\"\\\'"', $sString);
211+
self::assertSame('"\\"\\\'"', $sString);
212212
}
213213
if ($sSelector == '.test-10') {
214214
self::assertSame('"\\\'\\\\"', $sString);
@@ -795,8 +795,8 @@ public function invalidSelectorsInFile(): void
795795
public function selectorEscapesInFile(): void
796796
{
797797
$oDoc = self::parsedStructureForFile('selector-escapes', Settings::create()->withMultibyteSupport(true));
798-
$sExpected = '#\# {color: red;}
799-
.col-sm-1\/5 {width: 20%;}';
798+
$sExpected = '#\\# {color: red;}
799+
.col-sm-1\\/5 {width: 20%;}';
800800
self::assertSame($sExpected, $oDoc->render());
801801

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

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

@@ -1246,7 +1246,7 @@ public function escapedSpecialCaseTokens(): void
12461246
$rules = $contents[0]->getRules();
12471247
$urlRule = $rules[0];
12481248
$calcRule = $rules[1];
1249-
self::assertTrue(\is_a($urlRule->getValue(), '\Sabberworm\CSS\Value\URL'));
1250-
self::assertTrue(\is_a($calcRule->getValue(), '\Sabberworm\CSS\Value\CalcFunction'));
1249+
self::assertTrue(\is_a($urlRule->getValue(), '\\Sabberworm\\CSS\\Value\\URL'));
1250+
self::assertTrue(\is_a($calcRule->getValue(), '\\Sabberworm\\CSS\\Value\\CalcFunction'));
12511251
}
12521252
}

0 commit comments

Comments
 (0)