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
6 changes: 3 additions & 3 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 7 additions & 7 deletions src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 32 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -84,6 +88,7 @@ public function withDefaultCharset(string $defaultCharset): self
public function withLenientParsing(bool $usesLenientParsing = true): self
{
$this->lenientParsing = $usesLenientParsing;

return $this;
}

Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Value/LineName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -78,15 +78,15 @@ public function withMultibyteSupportSetsMultibyteSupport(bool $value): void
{
$this->subject->withMultibyteSupport($value);

self::assertSame($value, $this->subject->multibyteSupport);
self::assertSame($value, $this->subject->hasMultibyteSupport());
}

/**
* @test
*/
public function defaultCharsetByDefaultIsUtf8(): void
{
self::assertSame('utf-8', $this->subject->defaultCharset);
self::assertSame('utf-8', $this->subject->getDefaultCharset());
}

/**
Expand All @@ -105,15 +105,15 @@ public function withDefaultCharsetSetsDefaultCharset(): void
$charset = 'ISO-8859-1';
$this->subject->withDefaultCharset($charset);

self::assertSame($charset, $this->subject->defaultCharset);
self::assertSame($charset, $this->subject->getDefaultCharset());
}

/**
* @test
*/
public function lenientParsingByDefaultIsTrue(): void
{
self::assertTrue($this->subject->lenientParsing);
self::assertTrue($this->subject->usesLenientParsing());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -150,6 +150,6 @@ public function beStrictSetsLenientParsingToFalse(): void
{
$this->subject->beStrict();

self::assertFalse($this->subject->lenientParsing);
self::assertFalse($this->subject->usesLenientParsing());
}
}