diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 77831fd3..cfc5d7bb 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -74,7 +74,7 @@ public static function parseList(ParserState $parserState, CSSList $list): void if (\is_string($parserState)) { $parserState = new ParserState($parserState, Settings::create()); } - $usesLenientParsing = $parserState->getSettings()->lenientParsing; + $usesLenientParsing = $parserState->getSettings()->usesLenientParsing(); $comments = []; while (!$parserState->isEnd()) { $comments = \array_merge($comments, $parserState->consumeWhiteSpace()); @@ -138,7 +138,7 @@ private static function parseListItem(ParserState $parserState, CSSList $list) return $atRule; } elseif ($parserState->comes('}')) { if ($isRoot) { - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { return DeclarationBlock::parse($parserState); } else { throw new SourceException('Unopened {', $parserState->currentLine()); @@ -215,7 +215,7 @@ private static function parseAtRule(ParserState $parserState) // Unknown other at rule (font-face or such) $arguments = \trim($parserState->consumeUntil('{', false, true)); if (\substr_count($arguments, '(') != \substr_count($arguments, ')')) { - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { return null; } else { throw new SourceException('Unmatched brace count in media query', $parserState->currentLine()); diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 91efa301..b4885c17 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -58,7 +58,7 @@ public function __construct($text, Settings $parserSettings, $lineNumber = 1) $this->parserSettings = $parserSettings; $this->text = $text; $this->lineNumber = $lineNumber; - $this->setCharset($this->parserSettings->defaultCharset); + $this->setCharset($this->parserSettings->getDefaultCharset()); } /** @@ -151,7 +151,7 @@ public function parseCharacter($isForIdentifier) { if ($this->peek() === '\\') { if ( - $isForIdentifier && $this->parserSettings->lenientParsing + $isForIdentifier && $this->parserSettings->usesLenientParsing() && ($this->comes('\\0') || $this->comes('\\9')) ) { // Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing. @@ -215,7 +215,7 @@ public function consumeWhiteSpace(): array while (\preg_match('/\\s/isSu', $this->peek()) === 1) { $this->consume(1); } - if ($this->parserSettings->lenientParsing) { + if ($this->parserSettings->usesLenientParsing()) { try { $comment = $this->consumeComment(); } catch (UnexpectedEOFException $e) { @@ -416,7 +416,7 @@ public function backtrack($numberOfCharacters): void */ public function strlen($string): int { - if ($this->parserSettings->multibyteSupport) { + if ($this->parserSettings->hasMultibyteSupport()) { return \mb_strlen($string, $this->charset); } else { return \strlen($string); @@ -449,7 +449,7 @@ private function substr($offset, $length): string */ private function strtolower($string): string { - if ($this->parserSettings->multibyteSupport) { + if ($this->parserSettings->hasMultibyteSupport()) { return \mb_strtolower($string, $this->charset); } else { return \strtolower($string); @@ -465,7 +465,7 @@ private function strtolower($string): string */ private function strsplit($string) { - if ($this->parserSettings->multibyteSupport) { + if ($this->parserSettings->hasMultibyteSupport()) { if ($this->streql($this->charset, 'utf-8')) { $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); if (!\is_array($result)) { @@ -498,7 +498,7 @@ private function strsplit($string) */ private function strpos($haystack, $needle, $offset) { - if ($this->parserSettings->multibyteSupport) { + if ($this->parserSettings->hasMultibyteSupport()) { return \mb_strpos($haystack, $needle, $offset, $this->charset); } else { return \strpos($haystack, $needle, $offset); diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 4ac75724..45cf24d0 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -91,7 +91,7 @@ public static function parse(ParserState $parserState): Rule $parserState->consume(':'); $value = Value::parseValue($parserState, self::listDelimiterForRule($rule->getRule())); $rule->setValue($value); - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { while ($parserState->comes('\\')) { $parserState->consume('\\'); $rule->addIeHack($parserState->consume()); diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 7e0dfc1b..ff6c2c24 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -61,7 +61,7 @@ public static function parse(ParserState $parserState, $list = null) $parserState->consume(1); } } catch (UnexpectedTokenException $e) { - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { if (!$parserState->comes('}')) { $parserState->consumeUntil('}', false, true); } diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 5364e484..991c5043 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -64,7 +64,7 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet): } while (!$parserState->comes('}')) { $rule = null; - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { try { $rule = Rule::parse($parserState); } catch (UnexpectedTokenException $e) { diff --git a/src/Settings.php b/src/Settings.php index 3bf92bf3..965f430a 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -26,7 +26,7 @@ class Settings /** * The default charset for the CSS if no `@charset` declaration is found. Defaults to utf-8. * - * @var string + * @var non-empty-string * * @internal since 8.8.0, will be made private in 9.0.0 */ @@ -62,17 +62,21 @@ public static function create(): self public function withMultibyteSupport(bool $multibyteSupport = true): self { $this->multibyteSupport = $multibyteSupport; + return $this; } /** * Sets the charset to be used if the CSS does not contain an `@charset` declaration. * + * @param non-empty-string $defaultCharset + * * @return $this fluent interface */ public function withDefaultCharset(string $defaultCharset): self { $this->defaultCharset = $defaultCharset; + return $this; } @@ -84,6 +88,7 @@ public function withDefaultCharset(string $defaultCharset): self public function withLenientParsing(bool $usesLenientParsing = true): self { $this->lenientParsing = $usesLenientParsing; + return $this; } @@ -96,4 +101,30 @@ public function beStrict(): self { return $this->withLenientParsing(false); } + + /** + * @internal + */ + public function hasMultibyteSupport(): bool + { + return $this->multibyteSupport; + } + + /** + * @return non-empty-string + * + * @internal + */ + public function getDefaultCharset(): string + { + return $this->defaultCharset; + } + + /** + * @internal + */ + public function usesLenientParsing(): bool + { + return $this->lenientParsing; + } } diff --git a/src/Value/LineName.php b/src/Value/LineName.php index 9af9149e..379aae87 100644 --- a/src/Value/LineName.php +++ b/src/Value/LineName.php @@ -32,7 +32,7 @@ public static function parse(ParserState $parserState): LineName $parserState->consumeWhiteSpace(); $aNames = []; do { - if ($parserState->getSettings()->lenientParsing) { + if ($parserState->getSettings()->usesLenientParsing()) { try { $aNames[] = $parserState->parseIdentifier(); } catch (UnexpectedTokenException $e) { diff --git a/src/Value/Value.php b/src/Value/Value.php index cfc23ca3..dcd8686a 100644 --- a/src/Value/Value.php +++ b/src/Value/Value.php @@ -166,7 +166,7 @@ public static function parsePrimitiveValue(ParserState $parserState) $oValue = Color::parse($parserState); } elseif ($parserState->comes("'") || $parserState->comes('"')) { $oValue = CSSString::parse($parserState); - } elseif ($parserState->comes('progid:') && $parserState->getSettings()->lenientParsing) { + } elseif ($parserState->comes('progid:') && $parserState->getSettings()->usesLenientParsing()) { $oValue = self::parseMicrosoftFilter($parserState); } elseif ($parserState->comes('[')) { $oValue = LineName::parse($parserState); diff --git a/tests/Unit/SettingsTest.php b/tests/Unit/SettingsTest.php index ed21c9b3..63f94521 100644 --- a/tests/Unit/SettingsTest.php +++ b/tests/Unit/SettingsTest.php @@ -48,7 +48,7 @@ public function createReturnsANewInstanceForEachCall(): void */ public function multibyteSupportByDefaultStateOfMbStringExtension(): void { - self::assertSame(\extension_loaded('mbstring'), $this->subject->multibyteSupport); + self::assertSame(\extension_loaded('mbstring'), $this->subject->hasMultibyteSupport()); } /** @@ -78,7 +78,7 @@ public function withMultibyteSupportSetsMultibyteSupport(bool $value): void { $this->subject->withMultibyteSupport($value); - self::assertSame($value, $this->subject->multibyteSupport); + self::assertSame($value, $this->subject->hasMultibyteSupport()); } /** @@ -86,7 +86,7 @@ public function withMultibyteSupportSetsMultibyteSupport(bool $value): void */ public function defaultCharsetByDefaultIsUtf8(): void { - self::assertSame('utf-8', $this->subject->defaultCharset); + self::assertSame('utf-8', $this->subject->getDefaultCharset()); } /** @@ -105,7 +105,7 @@ public function withDefaultCharsetSetsDefaultCharset(): void $charset = 'ISO-8859-1'; $this->subject->withDefaultCharset($charset); - self::assertSame($charset, $this->subject->defaultCharset); + self::assertSame($charset, $this->subject->getDefaultCharset()); } /** @@ -113,7 +113,7 @@ public function withDefaultCharsetSetsDefaultCharset(): void */ public function lenientParsingByDefaultIsTrue(): void { - self::assertTrue($this->subject->lenientParsing); + self::assertTrue($this->subject->usesLenientParsing()); } /** @@ -132,7 +132,7 @@ public function withLenientParsingSetsLenientParsing(bool $value): void { $this->subject->withLenientParsing($value); - self::assertSame($value, $this->subject->lenientParsing); + self::assertSame($value, $this->subject->usesLenientParsing()); } /** @@ -150,6 +150,6 @@ public function beStrictSetsLenientParsingToFalse(): void { $this->subject->beStrict(); - self::assertFalse($this->subject->lenientParsing); + self::assertFalse($this->subject->usesLenientParsing()); } }