Skip to content
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
12 changes: 12 additions & 0 deletions Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ parameters:
count: 1
path: ../../src/Value/CSSFunction.php

-
message: '#^Function preg_match is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
identifier: theCodingMachineSafe.function
count: 2
path: ../../src/Value/CalcFunction.php

-
message: '#^Parameter \#2 \$offset of method Sabberworm\\CSS\\Parsing\\ParserState\:\:peek\(\) expects int\<0, max\>, \-1 given\.$#'
identifier: argument.type
Expand All @@ -102,6 +108,12 @@ parameters:
count: 1
path: ../../src/Value/Size.php

-
message: '#^Function preg_match is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
identifier: theCodingMachineSafe.function
count: 1
path: ../../src/Value/Value.php

-
message: '#^Parameter \#2 \$arguments of class Sabberworm\\CSS\\Value\\CSSFunction constructor expects array\<Sabberworm\\CSS\\Value\\Value\|string\>\|Sabberworm\\CSS\\Value\\RuleValueList, Sabberworm\\CSS\\Value\\Value\|string given\.$#'
identifier: argument.type
Expand Down
11 changes: 5 additions & 6 deletions src/Value/CalcFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

use function Safe\preg_match;

class CalcFunction extends CSSFunction
{
private const T_OPERAND = 1;
Expand Down Expand Up @@ -61,10 +59,11 @@ public static function parse(ParserState $parserState, bool $ignoreCase = false)
} else {
if (\in_array($parserState->peek(), $operators, true)) {
if (($parserState->comes('-') || $parserState->comes('+'))) {
if (
preg_match('/\\s/', $parserState->peek(1, -1)) !== 1
|| preg_match('/\\s/', $parserState->peek(1, 1)) !== 1
) {
$matchResultBefore = \preg_match('/\\s/', $parserState->peek(1, -1));
\assert(\is_int($matchResultBefore));
$matchResultAfter = \preg_match('/\\s/', $parserState->peek(1, 1));
\assert(\is_int($matchResultAfter));
if ($matchResultBefore !== 1 || $matchResultAfter !== 1) {
throw new UnexpectedTokenException(
" {$parserState->peek()} ",
$parserState->peek(1, -1) . $parserState->peek(2),
Expand Down
16 changes: 10 additions & 6 deletions src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Sabberworm\CSS\Position\Positionable;
use Sabberworm\CSS\ShortClassNameProvider;

use function Safe\preg_match;

/**
* Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`, and another
* abstract subclass `ValueList`.
Expand Down Expand Up @@ -214,14 +212,20 @@ private static function parseUnicodeRangeValue(ParserState $parserState): string
$codepointMaxLength = 6; // Code points outside BMP can use up to six digits
$range = '';
$parserState->consume('U+');
do {
while (true) {
if ($parserState->comes('-')) {
$codepointMaxLength = 13; // Max length is 2 six-digit code points + the dash(-) between them
}
$range .= $parserState->consume(1);
} while (
(\strlen($range) < $codepointMaxLength) && (preg_match('/[A-Fa-f0-9\\?-]/', $parserState->peek()) === 1)
);
if (\strlen($range) >= $codepointMaxLength) {
break;
}
$matchResult = \preg_match('/[A-Fa-f0-9\\?-]/', $parserState->peek());
\assert(\is_int($matchResult));
if ($matchResult !== 1) {
break;
}
}

return "U+{$range}";
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Value/CalcFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public function provideInvalidSyntax(): array
return [
'missing space around -' => ['calc(100%-20px)'],
'missing space around +' => ['calc(100%+20px)'],
'missing space before -' => ['calc(100%- 20px)'],
'missing space before +' => ['calc(100%+ 20px)'],
'missing space after -' => ['calc(100% -20px)'],
'missing space after +' => ['calc(100% +20px)'],
'invalid operator' => ['calc(100% ^ 20px)'],
];
}
Expand Down