Updates to vendors etc

This commit is contained in:
Chris Hunt
2025-07-11 15:57:48 +01:00
parent d972cbcd0a
commit 8fb6438254
8043 changed files with 248005 additions and 189479 deletions

View File

@@ -1,8 +1,14 @@
CHANGELOG
=========
7.1
---
* Add support for `:is()`
* Add support for `:where()`
6.3
-----
---
* Add support for `:scope`

View File

@@ -25,17 +25,17 @@ class SyntaxErrorException extends ParseException
{
public static function unexpectedToken(string $expectedValue, Token $foundToken): self
{
return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken));
return new self(\sprintf('Expected %s, but %s found.', $expectedValue, $foundToken));
}
public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation): self
{
return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation));
return new self(\sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation));
}
public static function unclosedString(int $position): self
{
return new self(sprintf('Unclosed/invalid string at %s.', $position));
return new self(\sprintf('Unclosed/invalid string at %s.', $position));
}
public static function nestedNot(): self
@@ -45,7 +45,7 @@ class SyntaxErrorException extends ParseException
public static function notAtTheStartOfASelector(string $pseudoElement): self
{
return new self(sprintf('Got immediate child pseudo-element ":%s" not at the start of a selector', $pseudoElement));
return new self(\sprintf('Got immediate child pseudo-element ":%s" not at the start of a selector', $pseudoElement));
}
public static function stringAsFunctionArgument(): self

View File

@@ -23,19 +23,13 @@ namespace Symfony\Component\CssSelector\Node;
*/
class AttributeNode extends AbstractNode
{
private NodeInterface $selector;
private ?string $namespace;
private string $attribute;
private string $operator;
private ?string $value;
public function __construct(NodeInterface $selector, ?string $namespace, string $attribute, string $operator, ?string $value)
{
$this->selector = $selector;
$this->namespace = $namespace;
$this->attribute = $attribute;
$this->operator = $operator;
$this->value = $value;
public function __construct(
private NodeInterface $selector,
private ?string $namespace,
private string $attribute,
private string $operator,
private ?string $value,
) {
}
public function getSelector(): NodeInterface
@@ -73,7 +67,7 @@ class AttributeNode extends AbstractNode
$attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute;
return 'exists' === $this->operator
? sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute)
: sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value);
? \sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute)
: \sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value);
}
}

View File

@@ -23,13 +23,10 @@ namespace Symfony\Component\CssSelector\Node;
*/
class ClassNode extends AbstractNode
{
private NodeInterface $selector;
private string $name;
public function __construct(NodeInterface $selector, string $name)
{
$this->selector = $selector;
$this->name = $name;
public function __construct(
private NodeInterface $selector,
private string $name,
) {
}
public function getSelector(): NodeInterface
@@ -49,6 +46,6 @@ class ClassNode extends AbstractNode
public function __toString(): string
{
return sprintf('%s[%s.%s]', $this->getNodeName(), $this->selector, $this->name);
return \sprintf('%s[%s.%s]', $this->getNodeName(), $this->selector, $this->name);
}
}

View File

@@ -23,15 +23,11 @@ namespace Symfony\Component\CssSelector\Node;
*/
class CombinedSelectorNode extends AbstractNode
{
private NodeInterface $selector;
private string $combinator;
private NodeInterface $subSelector;
public function __construct(NodeInterface $selector, string $combinator, NodeInterface $subSelector)
{
$this->selector = $selector;
$this->combinator = $combinator;
$this->subSelector = $subSelector;
public function __construct(
private NodeInterface $selector,
private string $combinator,
private NodeInterface $subSelector,
) {
}
public function getSelector(): NodeInterface
@@ -58,6 +54,6 @@ class CombinedSelectorNode extends AbstractNode
{
$combinator = ' ' === $this->combinator ? '<followed>' : $this->combinator;
return sprintf('%s[%s %s %s]', $this->getNodeName(), $this->selector, $combinator, $this->subSelector);
return \sprintf('%s[%s %s %s]', $this->getNodeName(), $this->selector, $combinator, $this->subSelector);
}
}

View File

@@ -23,13 +23,10 @@ namespace Symfony\Component\CssSelector\Node;
*/
class ElementNode extends AbstractNode
{
private ?string $namespace;
private ?string $element;
public function __construct(?string $namespace = null, ?string $element = null)
{
$this->namespace = $namespace;
$this->element = $element;
public function __construct(
private ?string $namespace = null,
private ?string $element = null,
) {
}
public function getNamespace(): ?string
@@ -51,6 +48,6 @@ class ElementNode extends AbstractNode
{
$element = $this->element ?: '*';
return sprintf('%s[%s]', $this->getNodeName(), $this->namespace ? $this->namespace.'|'.$element : $element);
return \sprintf('%s[%s]', $this->getNodeName(), $this->namespace ? $this->namespace.'|'.$element : $element);
}
}

View File

@@ -25,18 +25,17 @@ use Symfony\Component\CssSelector\Parser\Token;
*/
class FunctionNode extends AbstractNode
{
private NodeInterface $selector;
private string $name;
private array $arguments;
/**
* @param Token[] $arguments
*/
public function __construct(NodeInterface $selector, string $name, array $arguments = [])
{
$this->selector = $selector;
public function __construct(
private NodeInterface $selector,
string $name,
private array $arguments = [],
) {
$this->name = strtolower($name);
$this->arguments = $arguments;
}
public function getSelector(): NodeInterface
@@ -66,6 +65,6 @@ class FunctionNode extends AbstractNode
{
$arguments = implode(', ', array_map(fn (Token $token) => "'".$token->getValue()."'", $this->arguments));
return sprintf('%s[%s:%s(%s)]', $this->getNodeName(), $this->selector, $this->name, $arguments ? '['.$arguments.']' : '');
return \sprintf('%s[%s:%s(%s)]', $this->getNodeName(), $this->selector, $this->name, $arguments ? '['.$arguments.']' : '');
}
}

View File

@@ -23,13 +23,10 @@ namespace Symfony\Component\CssSelector\Node;
*/
class HashNode extends AbstractNode
{
private NodeInterface $selector;
private string $id;
public function __construct(NodeInterface $selector, string $id)
{
$this->selector = $selector;
$this->id = $id;
public function __construct(
private NodeInterface $selector,
private string $id,
) {
}
public function getSelector(): NodeInterface
@@ -49,6 +46,6 @@ class HashNode extends AbstractNode
public function __toString(): string
{
return sprintf('%s[%s#%s]', $this->getNodeName(), $this->selector, $this->id);
return \sprintf('%s[%s#%s]', $this->getNodeName(), $this->selector, $this->id);
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Node;
/**
* Represents a "<selector>:is(<subSelectorList>)" node.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Hubert Lenoir <lenoir.hubert@gmail.com>
*
* @internal
*/
class MatchingNode extends AbstractNode
{
/**
* @param array<NodeInterface> $arguments
*/
public function __construct(
public readonly NodeInterface $selector,
public readonly array $arguments = [],
) {
}
public function getSpecificity(): Specificity
{
$argumentsSpecificity = array_reduce(
$this->arguments,
fn ($c, $n) => 1 === $n->getSpecificity()->compareTo($c) ? $n->getSpecificity() : $c,
new Specificity(0, 0, 0),
);
return $this->selector->getSpecificity()->plus($argumentsSpecificity);
}
public function __toString(): string
{
$selectorArguments = array_map(
fn ($n): string => ltrim((string) $n, '*'),
$this->arguments,
);
return \sprintf('%s[%s:is(%s)]', $this->getNodeName(), $this->selector, implode(', ', $selectorArguments));
}
}

View File

@@ -23,13 +23,10 @@ namespace Symfony\Component\CssSelector\Node;
*/
class NegationNode extends AbstractNode
{
private NodeInterface $selector;
private NodeInterface $subSelector;
public function __construct(NodeInterface $selector, NodeInterface $subSelector)
{
$this->selector = $selector;
$this->subSelector = $subSelector;
public function __construct(
private NodeInterface $selector,
private NodeInterface $subSelector,
) {
}
public function getSelector(): NodeInterface
@@ -49,6 +46,6 @@ class NegationNode extends AbstractNode
public function __toString(): string
{
return sprintf('%s[%s:not(%s)]', $this->getNodeName(), $this->selector, $this->subSelector);
return \sprintf('%s[%s:not(%s)]', $this->getNodeName(), $this->selector, $this->subSelector);
}
}

View File

@@ -23,12 +23,12 @@ namespace Symfony\Component\CssSelector\Node;
*/
class PseudoNode extends AbstractNode
{
private NodeInterface $selector;
private string $identifier;
public function __construct(NodeInterface $selector, string $identifier)
{
$this->selector = $selector;
public function __construct(
private NodeInterface $selector,
string $identifier,
) {
$this->identifier = strtolower($identifier);
}
@@ -49,6 +49,6 @@ class PseudoNode extends AbstractNode
public function __toString(): string
{
return sprintf('%s[%s:%s]', $this->getNodeName(), $this->selector, $this->identifier);
return \sprintf('%s[%s:%s]', $this->getNodeName(), $this->selector, $this->identifier);
}
}

View File

@@ -23,12 +23,12 @@ namespace Symfony\Component\CssSelector\Node;
*/
class SelectorNode extends AbstractNode
{
private NodeInterface $tree;
private ?string $pseudoElement;
public function __construct(NodeInterface $tree, ?string $pseudoElement = null)
{
$this->tree = $tree;
public function __construct(
private NodeInterface $tree,
?string $pseudoElement = null,
) {
$this->pseudoElement = $pseudoElement ? strtolower($pseudoElement) : null;
}
@@ -49,6 +49,6 @@ class SelectorNode extends AbstractNode
public function __toString(): string
{
return sprintf('%s[%s%s]', $this->getNodeName(), $this->tree, $this->pseudoElement ? '::'.$this->pseudoElement : '');
return \sprintf('%s[%s%s]', $this->getNodeName(), $this->tree, $this->pseudoElement ? '::'.$this->pseudoElement : '');
}
}

View File

@@ -29,15 +29,11 @@ class Specificity
public const B_FACTOR = 10;
public const C_FACTOR = 1;
private int $a;
private int $b;
private int $c;
public function __construct(int $a, int $b, int $c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
public function __construct(
private int $a,
private int $b,
private int $c,
) {
}
public function plus(self $specificity): self

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Node;
/**
* Represents a "<selector>:where(<subSelectorList>)" node.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Hubert Lenoir <lenoir.hubert@gmail.com>
*
* @internal
*/
class SpecificityAdjustmentNode extends AbstractNode
{
/**
* @param array<NodeInterface> $arguments
*/
public function __construct(
public readonly NodeInterface $selector,
public readonly array $arguments = [],
) {
}
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity();
}
public function __toString(): string
{
$selectorArguments = array_map(
fn ($n) => ltrim((string) $n, '*'),
$this->arguments,
);
return \sprintf('%s[%s:where(%s)]', $this->getNodeName(), $this->selector, implode(', ', $selectorArguments));
}
}

View File

@@ -29,13 +29,10 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
class HashHandler implements HandlerInterface
{
private TokenizerPatterns $patterns;
private TokenizerEscaping $escaping;
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
$this->escaping = $escaping;
public function __construct(
private TokenizerPatterns $patterns,
private TokenizerEscaping $escaping,
) {
}
public function handle(Reader $reader, TokenStream $stream): bool

View File

@@ -29,13 +29,10 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
class IdentifierHandler implements HandlerInterface
{
private TokenizerPatterns $patterns;
private TokenizerEscaping $escaping;
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
$this->escaping = $escaping;
public function __construct(
private TokenizerPatterns $patterns,
private TokenizerEscaping $escaping,
) {
}
public function handle(Reader $reader, TokenStream $stream): bool

View File

@@ -28,11 +28,9 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
class NumberHandler implements HandlerInterface
{
private TokenizerPatterns $patterns;
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
public function __construct(
private TokenizerPatterns $patterns,
) {
}
public function handle(Reader $reader, TokenStream $stream): bool

View File

@@ -31,13 +31,10 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
class StringHandler implements HandlerInterface
{
private TokenizerPatterns $patterns;
private TokenizerEscaping $escaping;
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
$this->escaping = $escaping;
public function __construct(
private TokenizerPatterns $patterns,
private TokenizerEscaping $escaping,
) {
}
public function handle(Reader $reader, TokenStream $stream): bool
@@ -52,7 +49,7 @@ class StringHandler implements HandlerInterface
$match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
if (!$match) {
throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
throw new InternalErrorException(\sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
}
// check unclosed strings

View File

@@ -87,13 +87,17 @@ class Parser implements ParserInterface
];
}
private function parseSelectorList(TokenStream $stream): array
private function parseSelectorList(TokenStream $stream, bool $isArgument = false): array
{
$stream->skipWhitespace();
$selectors = [];
while (true) {
$selectors[] = $this->parserSelectorNode($stream);
if ($isArgument && $stream->getPeek()->isDelimiter([')'])) {
break;
}
$selectors[] = $this->parserSelectorNode($stream, $isArgument);
if ($stream->getPeek()->isDelimiter([','])) {
$stream->getNext();
@@ -106,15 +110,19 @@ class Parser implements ParserInterface
return $selectors;
}
private function parserSelectorNode(TokenStream $stream): Node\SelectorNode
private function parserSelectorNode(TokenStream $stream, bool $isArgument = false): Node\SelectorNode
{
[$result, $pseudoElement] = $this->parseSimpleSelector($stream);
[$result, $pseudoElement] = $this->parseSimpleSelector($stream, false, $isArgument);
while (true) {
$stream->skipWhitespace();
$peek = $stream->getPeek();
if ($peek->isFileEnd() || $peek->isDelimiter([','])) {
if (
$peek->isFileEnd()
|| $peek->isDelimiter([','])
|| ($isArgument && $peek->isDelimiter([')']))
) {
break;
}
@@ -129,7 +137,7 @@ class Parser implements ParserInterface
$combinator = ' ';
}
[$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream);
[$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream, false, $isArgument);
$result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
}
@@ -141,7 +149,7 @@ class Parser implements ParserInterface
*
* @throws SyntaxErrorException
*/
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false, bool $isArgument = false): array
{
$stream->skipWhitespace();
@@ -154,7 +162,7 @@ class Parser implements ParserInterface
if ($peek->isWhitespace()
|| $peek->isFileEnd()
|| $peek->isDelimiter([',', '+', '>', '~'])
|| ($insideNegation && $peek->isDelimiter([')']))
|| ($isArgument && $peek->isDelimiter([')']))
) {
break;
}
@@ -215,7 +223,7 @@ class Parser implements ParserInterface
throw SyntaxErrorException::nestedNot();
}
[$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true);
[$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true, true);
$next = $stream->getNext();
if (null !== $argumentPseudoElement) {
@@ -227,6 +235,24 @@ class Parser implements ParserInterface
}
$result = new Node\NegationNode($result, $argument);
} elseif ('is' === strtolower($identifier)) {
$selectors = $this->parseSelectorList($stream, true);
$next = $stream->getNext();
if (!$next->isDelimiter([')'])) {
throw SyntaxErrorException::unexpectedToken('")"', $next);
}
$result = new Node\MatchingNode($result, $selectors);
} elseif ('where' === strtolower($identifier)) {
$selectors = $this->parseSelectorList($stream, true);
$next = $stream->getNext();
if (!$next->isDelimiter([')'])) {
throw SyntaxErrorException::unexpectedToken('")"', $next);
}
$result = new Node\SpecificityAdjustmentNode($result, $selectors);
} else {
$arguments = [];
$next = null;

View File

@@ -23,13 +23,12 @@ namespace Symfony\Component\CssSelector\Parser;
*/
class Reader
{
private string $source;
private int $length;
private int $position = 0;
public function __construct(string $source)
{
$this->source = $source;
public function __construct(
private string $source,
) {
$this->length = \strlen($source);
}

View File

@@ -31,15 +31,11 @@ class Token
public const TYPE_NUMBER = 'number';
public const TYPE_STRING = 'string';
private ?string $type;
private ?string $value;
private ?int $position;
public function __construct(?string $type, ?string $value, ?int $position)
{
$this->type = $type;
$this->value = $value;
$this->position = $position;
public function __construct(
private ?string $type,
private ?string $value,
private ?int $position,
) {
}
public function getType(): ?int
@@ -72,7 +68,7 @@ class Token
return true;
}
return \in_array($this->value, $values);
return \in_array($this->value, $values, true);
}
public function isWhitespace(): bool
@@ -103,9 +99,9 @@ class Token
public function __toString(): string
{
if ($this->value) {
return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);
return \sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);
}
return sprintf('<%s at %s>', $this->type, $this->position);
return \sprintf('<%s at %s>', $this->type, $this->position);
}
}

View File

@@ -23,11 +23,9 @@ namespace Symfony\Component\CssSelector\Parser\Tokenizer;
*/
class TokenizerEscaping
{
private TokenizerPatterns $patterns;
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
public function __construct(
private TokenizerPatterns $patterns,
) {
}
public function escapeUnicode(string $value): string

View File

@@ -84,6 +84,6 @@ class TokenizerPatterns
public function getQuotedStringPattern(string $quote): string
{
return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';
return '~^'.\sprintf($this->quotedStringPattern, $quote).'~i';
}
}

View File

@@ -47,12 +47,12 @@ class AttributeMatchingExtension extends AbstractExtension
public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
return $xpath->addCondition(\sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
}
public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
return $xpath->addCondition($value ? \sprintf(
'%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
$attribute,
Translator::getXpathLiteral(' '.$value.' ')
@@ -61,7 +61,7 @@ class AttributeMatchingExtension extends AbstractExtension
public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf(
return $xpath->addCondition(\sprintf(
'%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
$attribute,
Translator::getXpathLiteral($value),
@@ -71,7 +71,7 @@ class AttributeMatchingExtension extends AbstractExtension
public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
return $xpath->addCondition($value ? \sprintf(
'%1$s and starts-with(%1$s, %2$s)',
$attribute,
Translator::getXpathLiteral($value)
@@ -80,7 +80,7 @@ class AttributeMatchingExtension extends AbstractExtension
public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
return $xpath->addCondition($value ? \sprintf(
'%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
$attribute,
\strlen($value) - 1,
@@ -90,7 +90,7 @@ class AttributeMatchingExtension extends AbstractExtension
public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
return $xpath->addCondition($value ? \sprintf(
'%1$s and contains(%1$s, %2$s)',
$attribute,
Translator::getXpathLiteral($value)
@@ -99,7 +99,7 @@ class AttributeMatchingExtension extends AbstractExtension
public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf(
return $xpath->addCondition(\sprintf(
$value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
$attribute,
Translator::getXpathLiteral($value)

View File

@@ -50,7 +50,7 @@ class FunctionExtension extends AbstractExtension
try {
[$a, $b] = Parser::parseSeries($function->getArguments());
} catch (SyntaxErrorException $e) {
throw new ExpressionErrorException(sprintf('Invalid series: "%s".', implode('", "', $function->getArguments())), 0, $e);
throw new ExpressionErrorException(\sprintf('Invalid series: "%s".', implode('", "', $function->getArguments())), 0, $e);
}
$xpath->addStarPrefix();
@@ -83,10 +83,10 @@ class FunctionExtension extends AbstractExtension
$expr .= ' - '.$b;
}
$conditions = [sprintf('%s %s 0', $expr, $sign)];
$conditions = [\sprintf('%s %s 0', $expr, $sign)];
if (1 !== $a && -1 !== $a) {
$conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
$conditions[] = \sprintf('(%s) mod %d = 0', $expr, $a);
}
return $xpath->addCondition(implode(' and ', $conditions));
@@ -134,7 +134,7 @@ class FunctionExtension extends AbstractExtension
}
}
return $xpath->addCondition(sprintf(
return $xpath->addCondition(\sprintf(
'contains(string(.), %s)',
Translator::getXpathLiteral($arguments[0]->getValue())
));
@@ -152,7 +152,7 @@ class FunctionExtension extends AbstractExtension
}
}
return $xpath->addCondition(sprintf(
return $xpath->addCondition(\sprintf(
'lang(%s)',
Translator::getXpathLiteral($arguments[0]->getValue())
));

View File

@@ -142,7 +142,7 @@ class HtmlExtension extends AbstractExtension
}
}
return $xpath->addCondition(sprintf(
return $xpath->addCondition(\sprintf(
'ancestor-or-self::*[@lang][1][starts-with(concat('
."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')"
.', %s)]',

View File

@@ -31,11 +31,9 @@ class NodeExtension extends AbstractExtension
public const ATTRIBUTE_NAME_IN_LOWER_CASE = 2;
public const ATTRIBUTE_VALUE_IN_LOWER_CASE = 4;
private int $flags;
public function __construct(int $flags = 0)
{
$this->flags = $flags;
public function __construct(
private int $flags = 0,
) {
}
/**
@@ -65,6 +63,8 @@ class NodeExtension extends AbstractExtension
'Selector' => $this->translateSelector(...),
'CombinedSelector' => $this->translateCombinedSelector(...),
'Negation' => $this->translateNegation(...),
'Matching' => $this->translateMatching(...),
'SpecificityAdjustment' => $this->translateSpecificityAdjustment(...),
'Function' => $this->translateFunction(...),
'Pseudo' => $this->translatePseudo(...),
'Attribute' => $this->translateAttribute(...),
@@ -91,12 +91,42 @@ class NodeExtension extends AbstractExtension
$subXpath->addNameTest();
if ($subXpath->getCondition()) {
return $xpath->addCondition(sprintf('not(%s)', $subXpath->getCondition()));
return $xpath->addCondition(\sprintf('not(%s)', $subXpath->getCondition()));
}
return $xpath->addCondition('0');
}
public function translateMatching(Node\MatchingNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->selector);
foreach ($node->arguments as $argument) {
$expr = $translator->nodeToXPath($argument);
$expr->addNameTest();
if ($condition = $expr->getCondition()) {
$xpath->addCondition($condition, 'or');
}
}
return $xpath;
}
public function translateSpecificityAdjustment(Node\SpecificityAdjustmentNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->selector);
foreach ($node->arguments as $argument) {
$expr = $translator->nodeToXPath($argument);
$expr->addNameTest();
if ($condition = $expr->getCondition()) {
$xpath->addCondition($condition, 'or');
}
}
return $xpath;
}
public function translateFunction(Node\FunctionNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
@@ -121,11 +151,11 @@ class NodeExtension extends AbstractExtension
}
if ($node->getNamespace()) {
$name = sprintf('%s:%s', $node->getNamespace(), $name);
$name = \sprintf('%s:%s', $node->getNamespace(), $name);
$safe = $safe && $this->isSafeName($node->getNamespace());
}
$attribute = $safe ? '@'.$name : sprintf('attribute::*[name() = %s]', Translator::getXpathLiteral($name));
$attribute = $safe ? '@'.$name : \sprintf('attribute::*[name() = %s]', Translator::getXpathLiteral($name));
$value = $node->getValue();
$xpath = $translator->nodeToXPath($node->getSelector());
@@ -166,7 +196,7 @@ class NodeExtension extends AbstractExtension
}
if ($node->getNamespace()) {
$element = sprintf('%s:%s', $node->getNamespace(), $element);
$element = \sprintf('%s:%s', $node->getNamespace(), $element);
$safe = $safe && $this->isSafeName($node->getNamespace());
}

View File

@@ -107,7 +107,7 @@ class PseudoClassExtension extends AbstractExtension
{
$element = $xpath->getElement();
return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
return $xpath->addCondition(\sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
}
public function translateEmpty(XPathExpr $xpath): XPathExpr

View File

@@ -75,7 +75,7 @@ class Translator implements TranslatorInterface
$parts = [];
while (true) {
if (false !== $pos = strpos($string, "'")) {
$parts[] = sprintf("'%s'", substr($string, 0, $pos));
$parts[] = \sprintf("'%s'", substr($string, 0, $pos));
$parts[] = "\"'\"";
$string = substr($string, $pos + 1);
} else {
@@ -84,7 +84,7 @@ class Translator implements TranslatorInterface
}
}
return sprintf('concat(%s)', implode(', ', $parts));
return \sprintf('concat(%s)', implode(', ', $parts));
}
public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string
@@ -130,7 +130,7 @@ class Translator implements TranslatorInterface
public function getExtension(string $name): Extension\ExtensionInterface
{
if (!isset($this->extensions[$name])) {
throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
throw new ExpressionErrorException(\sprintf('Extension "%s" not registered.', $name));
}
return $this->extensions[$name];
@@ -152,7 +152,7 @@ class Translator implements TranslatorInterface
public function nodeToXPath(NodeInterface $node): XPathExpr
{
if (!isset($this->nodeTranslators[$node->getNodeName()])) {
throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
throw new ExpressionErrorException(\sprintf('Node "%s" not supported.', $node->getNodeName()));
}
return $this->nodeTranslators[$node->getNodeName()]($node, $this);
@@ -164,7 +164,7 @@ class Translator implements TranslatorInterface
public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr
{
if (!isset($this->combinationTranslators[$combiner])) {
throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
throw new ExpressionErrorException(\sprintf('Combiner "%s" not supported.', $combiner));
}
return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
@@ -176,7 +176,7 @@ class Translator implements TranslatorInterface
public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
if (!isset($this->functionTranslators[$function->getName()])) {
throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
throw new ExpressionErrorException(\sprintf('Function "%s" not supported.', $function->getName()));
}
return $this->functionTranslators[$function->getName()]($xpath, $function);
@@ -188,7 +188,7 @@ class Translator implements TranslatorInterface
public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr
{
if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
throw new ExpressionErrorException(\sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
}
return $this->pseudoClassTranslators[$pseudoClass]($xpath);
@@ -200,7 +200,7 @@ class Translator implements TranslatorInterface
public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, ?string $value): XPathExpr
{
if (!isset($this->attributeMatchingTranslators[$operator])) {
throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
throw new ExpressionErrorException(\sprintf('Attribute matcher operator "%s" not supported.', $operator));
}
return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value);

View File

@@ -23,16 +23,12 @@ namespace Symfony\Component\CssSelector\XPath;
*/
class XPathExpr
{
private string $path;
private string $element;
private string $condition;
public function __construct(string $path = '', string $element = '*', string $condition = '', bool $starPrefix = false)
{
$this->path = $path;
$this->element = $element;
$this->condition = $condition;
public function __construct(
private string $path = '',
private string $element = '*',
private string $condition = '',
bool $starPrefix = false,
) {
if ($starPrefix) {
$this->addStarPrefix();
}
@@ -46,9 +42,9 @@ class XPathExpr
/**
* @return $this
*/
public function addCondition(string $condition): static
public function addCondition(string $condition, string $operator = 'and'): static
{
$this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition;
$this->condition = $this->condition ? \sprintf('(%s) %s (%s)', $this->condition, $operator, $condition) : $condition;
return $this;
}
@@ -104,7 +100,7 @@ class XPathExpr
public function __toString(): string
{
$path = $this->path.$this->element;
$condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
$condition = '' === $this->condition ? '' : '['.$this->condition.']';
return $path.$condition;
}