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 1 commit
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
Prev Previous commit
Next Next commit
Add more complex selector inside has node
  • Loading branch information
franckranaivo authored and Franck committed Feb 7, 2025
commit 9358c39071ca98ed89498ea2e0444ef94778f08b
20 changes: 12 additions & 8 deletions src/Symfony/Component/CssSelector/Node/RelationNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Component\CssSelector\Node;

use Symfony\Component\CssSelector\Parser\Token;

/**
* Represents a "<selector>:has(<identifier>)" node.
* Represents a "<selector>:has(<arguments>)" node.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
Expand All @@ -24,31 +26,33 @@
class RelationNode extends AbstractNode
{
private NodeInterface $selector;
private NodeInterface $subSelector;
private array $arguments;

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

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

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

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

public function __toString(): string
{
return sprintf('%s[%s:has(%s)]', $this->getNodeName(), $this->selector, $this->subSelector);
$arguments = implode(', ', array_map(fn (Token $token) => "'".$token->getValue()."'", $this->arguments));

return sprintf('%s[%s:has(%s)]', $this->getNodeName(), $this->selector, $arguments ? '['.$arguments.']' : '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\CssSelector\XPath\Extension;

use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
use Symfony\Component\CssSelector\Node;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;

Expand Down Expand Up @@ -71,7 +73,7 @@ public function getNodeTranslators(): array
'Class' => $this->translateClass(...),
'Hash' => $this->translateHash(...),
'Element' => $this->translateElement(...),
'Relation' => $this->translateRelation(...)
'Relation' => $this->translateRelation(...),
];
}

Expand Down Expand Up @@ -213,11 +215,20 @@ public function translateElement(Node\ElementNode $node): XPathExpr
public function translateRelation(Node\RelationNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
$subXpath = $translator->nodeToXPath($node->getSubSelector());
$subXpath->addNameTest();
$selectors = $node->getArguments();

if ($subXpath->getCondition()) {
return $xpath->addCondition(sprintf('count(descendant-or-self::*[%s]) > 0', $subXpath->getCondition()));
foreach ($selectors as $index => $selector) {
if (null !== $selector->getPseudoElement()) {
throw new ExpressionErrorException('Pseudo-elements are not supported.');
}

$selectors[$index] = $translator->selectorToXPath($selector);
}

$subSelectors = implode(' | ', $selectors);

if ($subSelectors) {
return $xpath->addCondition(sprintf('count(%s) > 0', $subSelectors));
}

return $xpath->addCondition('0');
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy