| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\CssSelector\XPath; |
| 13 |
|
| 14 |
/** |
| 15 |
* XPath expression translator interface. |
| 16 |
* |
| 17 |
* This component is a port of the Python cssselect library, |
| 18 |
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. |
| 19 |
* |
| 20 |
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> |
| 21 |
* |
| 22 |
* @internal |
| 23 |
*/ |
| 24 |
class XPathExpr |
| 25 |
{ |
| 26 |
public function __construct( |
| 27 |
private string $path = '', |
| 28 |
private string $element = '*', |
| 29 |
private string $condition = '', |
| 30 |
bool $starPrefix = false, |
| 31 |
) { |
| 32 |
if ($starPrefix) { |
| 33 |
$this->addStarPrefix(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function getElement(): string |
| 38 |
{ |
| 39 |
return $this->element; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return $this |
| 44 |
*/ |
| 45 |
public function addCondition(string $condition, string $operator = 'and'): static |
| 46 |
{ |
| 47 |
$this->condition = $this->condition ? \sprintf('(%s) %s (%s)', $this->condition, $operator, $condition) : $condition; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function getCondition(): string |
| 53 |
{ |
| 54 |
return $this->condition; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return $this |
| 59 |
*/ |
| 60 |
public function addNameTest(): static |
| 61 |
{ |
| 62 |
if ('*' !== $this->element) { |
| 63 |
$this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); |
| 64 |
$this->element = '*'; |
| 65 |
} |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return $this |
| 72 |
*/ |
| 73 |
public function addStarPrefix(): static |
| 74 |
{ |
| 75 |
$this->path .= '*/'; |
| 76 |
|
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Joins another XPathExpr with a combiner. |
| 82 |
* |
| 83 |
* When $hasInnerConditions is true, $expr->condition is folded into $expr->element as a |
| 84 |
* "[...]" predicate rather than onto $this->condition, so subsequent addCondition() calls |
| 85 |
* on $this are not AND'd with it. Needed for relative selectors inside :has(). |
| 86 |
* |
| 87 |
* @return $this |
| 88 |
*/ |
| 89 |
public function join(string $combiner, self $expr, ?string $closingCombiner = null, bool $hasInnerConditions = false): static |
| 90 |
{ |
| 91 |
$path = $this->__toString().$combiner; |
| 92 |
|
| 93 |
if ('*/' !== $expr->path) { |
| 94 |
$path .= $expr->path; |
| 95 |
} |
| 96 |
|
| 97 |
$this->path = $path; |
| 98 |
|
| 99 |
if (!$hasInnerConditions) { |
| 100 |
$this->element = $expr->element.($closingCombiner ?? ''); |
| 101 |
$this->condition = $expr->condition; |
| 102 |
} else { |
| 103 |
$this->element = $expr->element; |
| 104 |
if ($expr->condition) { |
| 105 |
$this->element .= '['.$expr->condition.']'; |
| 106 |
} |
| 107 |
if ($closingCombiner) { |
| 108 |
$this->element .= $closingCombiner; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
public function __toString(): string |
| 116 |
{ |
| 117 |
$path = $this->path.$this->element; |
| 118 |
$condition = '' === $this->condition ? '' : '['.$this->condition.']'; |
| 119 |
|
| 120 |
return $path.$condition; |
| 121 |
} |
| 122 |
} |
| 123 |
|