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
Next Next commit
[CssSelector] Add has relation support
  • Loading branch information
franckranaivo authored and Franck committed Feb 7, 2025
commit 18ad9bbfca8892a55682ad793a34aaf12b204d4e
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 Expand Up @@ -48,6 +48,11 @@ 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));
}

public static function nestedHas(): self
{
return new self('Got nested :has().');
}

public static function stringAsFunctionArgument(): self
{
return new self('String not allowed as function argument.');
Expand Down
54 changes: 54 additions & 0 deletions src/Symfony/Component/CssSelector/Node/RelationNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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(<identifier>)" node.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class RelationNode extends AbstractNode
{
private NodeInterface $selector;
private NodeInterface $subSelector;

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

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

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);
}
}
Original file line number Diff line number Diff line change
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,19 @@ public function translateElement(Node\ElementNode $node): XPathExpr
return $xpath;
}

public function translateRelation(Node\RelationNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
$subXpath = $translator->nodeToXPath($node->getSubSelector());
$subXpath->addNameTest();

if ($subXpath->getCondition()) {
return $xpath->addCondition(sprintf('count(descendant-or-self::*[%s]) > 0', $subXpath->getCondition()));
}

return $xpath->addCondition('0');
}

public function getName(): string
{
return 'node';
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