Skip to content

[CssSelector] Add :has() support #49388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: 7.4
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/CssSelector/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
---

* Add support for `:scope`
* Add support for `*:has`

4.4.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* ParseException is thrown when a CSS selector syntax is not valid.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
Expand Down
61 changes: 61 additions & 0 deletions src/Symfony/Component/CssSelector/Node/RelationNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>:has(<subselector>)" node.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Franck Ranaivo-Harisoa <franckranaivo@gmail.com>
*
* @internal
*/
class RelationNode extends AbstractNode
{
private NodeInterface $selector;
private NodeInterface $subSelector;
private string $combinator;

public function __construct(NodeInterface $selector, string $combinator, NodeInterface $subSelector)
{
$this->selector = $selector;
$this->combinator = $combinator;
$this->subSelector = $subSelector;
}

public function getSelector(): NodeInterface
{
return $this->selector;
}

public function getCombinator(): string
{
return $this->combinator;
}

public function getSubSelector(): NodeInterface
{
return $this->subSelector;
}

public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus($this->subSelector->getSpecificity());
}

public function __toString(): string
{
return sprintf('%s[%s:has(%s)]', $this->getNodeName(), $this->selector, $this->subSelector);
}
}
38 changes: 37 additions & 1 deletion src/Symfony/Component/CssSelector/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\CssSelector\Parser;

use Symfony\Component\CssSelector\Exception\InternalErrorException;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Node;
use Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
Expand Down Expand Up @@ -144,10 +145,42 @@ private function parserSelectorNode(TokenStream $stream, bool $isArgument = fals
return new Node\SelectorNode($result, $pseudoElement);
}

/**
* @throws InternalErrorException
* @throws SyntaxErrorException
*/
function parseRelativeSelector(TokenStream $stream): array
{
$stream->skipWhitespace();
$subSelector = '';
$next = $stream->getNext();

if ($next->isDelimiter(['-', '+', '>', '~'])) {
$combinator = $next->getValue();
$stream->skipWhitespace();
$next = $stream->getNext();
} else {
$combinator = new Token(Token::TYPE_DELIMITER, ' ', 0);
}

while(true){
if ($next->isString() || $next->isIdentifier() || $next->isNumber()
|| $next->isDelimiter(['.', '*'])) {
$subSelector .= $next->getValue();
} elseif ($next->isDelimiter([')'])) {
$result = $this->parse($subSelector);
return [$combinator, $result[0]];
} else {
throw SyntaxErrorException::unexpectedToken('an argument', $next);
}
$next = $stream->getNext();
}
}

/**
* Parses next simple node (hash, class, pseudo, negation).
*
* @throws SyntaxErrorException
* @throws SyntaxErrorException|InternalErrorException
*/
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false, bool $isArgument = false): array
{
Expand Down Expand Up @@ -253,6 +286,9 @@ private function parseSimpleSelector(TokenStream $stream, bool $insideNegation =
}

$result = new Node\SpecificityAdjustmentNode($result, $selectors);
} elseif('has' === strtolower($identifier)) {
[$combinator, $arguments] = $this->parseRelativeSelector($stream);
$result = new Node\RelationNode($result, $combinator ,$arguments);
} else {
$arguments = [];
$next = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public static function getParserTestData()
['div:contains("foo")', ["Function[Element[div]:contains(['foo'])]"]],
['div#foobar', ['Hash[Element[div]#foobar]']],
['div:not(div.foo)', ['Negation[Element[div]:not(Class[Element[div].foo])]']],
['div:has(div.foo)', ['Relation[Element[div]:has(Selector[Class[Element[div].foo]])]']],
['td ~ th', ['CombinedSelector[Element[td] ~ Element[th]]']],
['.foo[data-bar][data-baz=0]', ["Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]"]],
['div#foo\.bar', ['Hash[Element[div]#foo.bar]']],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public static function getCssToXPathTestData()
[':scope', '*[1]'],
['e:is(section, article) h1', "e[(name() = 'section') or (name() = 'article')]/descendant-or-self::*/h1"],
['e:where(section, article) h1', "e[(name() = 'section') or (name() = 'article')]/descendant-or-self::*/h1"],
['div:has(> .foo)', "div[./*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]]"],
['div:has(~ .foo)', "div[following-sibling::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]]"],
['div:has(+ .foo)', "div[following-sibling::*[(@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')) and (position() = 1)]]"],
['div:has(+ .foo)', "div[following-sibling::*[(@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')) and (position() = 1)]]"],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* XPath expression translator abstract extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
Expand Down Expand Up @@ -47,4 +47,9 @@ public function getAttributeMatchingTranslators(): array
{
return [];
}

public function getRelativeCombinationTranslators(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
* XPath expression translator extension interface.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @method array<string, callable(XPathExpr, XPathExpr): XPathExpr> getRelativeCombinationTranslators() Returns combination translators found inside ":has()" relation.
*
* @internal
*/
interface ExtensionInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* XPath expression translator node extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
Expand Down Expand Up @@ -71,6 +71,7 @@ public function getNodeTranslators(): array
'Class' => $this->translateClass(...),
'Hash' => $this->translateHash(...),
'Element' => $this->translateElement(...),
'Relation' => $this->translateRelation(...),
];
}

Expand Down Expand Up @@ -209,6 +210,13 @@ public function translateElement(Node\ElementNode $node): XPathExpr
return $xpath;
}

public function translateRelation(Node\RelationNode $node, Translator $translator): XPathExpr
{
$combinator = $node->getCombinator();

return $translator->addRelativeCombination($combinator, $node->getSelector(), $node->getSubSelector());
}

public function getName(): string
{
return 'node';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\XPath\Extension;

use Symfony\Component\CssSelector\XPath\XPathExpr;

/**
* XPath expression translator combination extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect.
*
* @author Franck Ranaivo-Harisoa <franckranaivo@gmail.com>
*
* @internal
*/
class RelationExtension extends AbstractExtension
{
public function getRelativeCombinationTranslators(): array
{
return [
' ' => $this->translateRelationDescendant(...),
'>' => $this->translateRelationChild(...),
'+' => $this->translateRelationDirectAdjacent(...),
'~' => $this->translateRelationIndirectAdjacent(...),
];
}

public function translateRelationDescendant(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('[descendant-or-self::', $combinedXpath, ']', true);
}

public function translateRelationChild(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('[./', $combinedXpath, ']', true);
}

public function translateRelationDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
$combinedXpath
->addNameTest()
->addCondition('position() = 1');

return $xpath
->join('[following-sibling::', $combinedXpath, ']', true);
}

public function translateRelationIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('[following-sibling::', $combinedXpath, ']', true);
}

public function getName(): string
{
return 'relation';
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/CssSelector/XPath/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Translator implements TranslatorInterface

private array $nodeTranslators = [];
private array $combinationTranslators = [];
private array $relativeCombinationTranslators = [];
private array $functionTranslators = [];
private array $pseudoClassTranslators = [];
private array $attributeMatchingTranslators = [];
Expand All @@ -58,6 +59,7 @@ public function __construct(?ParserInterface $parser = null)
->registerExtension(new Extension\FunctionExtension())
->registerExtension(new Extension\PseudoClassExtension())
->registerExtension(new Extension\AttributeMatchingExtension())
->registerExtension(new Extension\RelationExtension())
;
}

Expand Down Expand Up @@ -120,6 +122,7 @@ public function registerExtension(Extension\ExtensionInterface $extension): stat
$this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
$this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
$this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
$this->relativeCombinationTranslators = array_merge($this->relativeCombinationTranslators, $extension->getRelativeCombinationTranslators());

return $this;
}
Expand Down Expand Up @@ -170,6 +173,18 @@ public function addCombination(string $combiner, NodeInterface $xpath, NodeInter
return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
}

/**
* @throws ExpressionErrorException
*/
public function addRelativeCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr
{
if (!isset($this->relativeCombinationTranslators[$combiner])) {
throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
}

return $this->relativeCombinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
}

/**
* @throws ExpressionErrorException
*/
Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Component/CssSelector/XPath/XPathExpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function addStarPrefix(): static
*
* @return $this
*/
public function join(string $combiner, self $expr): static
public function join(string $combiner, self $expr, string $closingCombiner = null, bool $hasInnerConditions = false): static
{
$path = $this->__toString().$combiner;

Expand All @@ -91,8 +91,19 @@ public function join(string $combiner, self $expr): static
}

$this->path = $path;
$this->element = $expr->element;
$this->condition = $expr->condition;

if (!$hasInnerConditions) {
$this->element = $expr->element.($closingCombiner ?? '');
$this->condition = $expr->condition;
} else {
$this->element = $expr->element;
if ($expr->condition) {
$this->element .= '['.$expr->condition.']';
}
if ($closingCombiner) {
$this->element .= $closingCombiner;
}
}

return $this;
}
Expand Down
Loading