| 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\Extension; |
| 13 |
|
| 14 |
use Symfony\Component\CssSelector\Exception\ExpressionErrorException; |
| 15 |
use Symfony\Component\CssSelector\Node\FunctionNode; |
| 16 |
use Symfony\Component\CssSelector\XPath\Translator; |
| 17 |
use Symfony\Component\CssSelector\XPath\XPathExpr; |
| 18 |
|
| 19 |
/** |
| 20 |
* XPath expression translator HTML extension. |
| 21 |
* |
| 22 |
* This component is a port of the Python cssselect library, |
| 23 |
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. |
| 24 |
* |
| 25 |
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> |
| 26 |
* |
| 27 |
* @internal |
| 28 |
*/ |
| 29 |
class HtmlExtension extends AbstractExtension |
| 30 |
{ |
| 31 |
// Each disabled fieldset ancestor disables the element, except the one whose first legend |
| 32 |
// child the element sits in. Those excepted fieldsets are exactly the parents of the |
| 33 |
// ancestors-or-self that are a first legend child of a disabled fieldset, one for one, so |
| 34 |
// the element is disabled as soon as the first count exceeds the second one. Counting keeps |
| 35 |
// this to two cheap ancestor walks instead of a predicate run on every ancestor. |
| 36 |
private const DISABLING_FIELDSET = 'count(ancestor::fieldset[@disabled]) > count(ancestor-or-self::legend[not(preceding-sibling::legend)][parent::fieldset[@disabled]])'; |
| 37 |
|
| 38 |
public function __construct(Translator $translator) |
| 39 |
{ |
| 40 |
$translator |
| 41 |
->getExtension('node') |
| 42 |
->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true) |
| 43 |
->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true); |
| 44 |
} |
| 45 |
|
| 46 |
public function getPseudoClassTranslators(): array |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'checked' => $this->translateChecked(...), |
| 50 |
'link' => $this->translateLink(...), |
| 51 |
'disabled' => $this->translateDisabled(...), |
| 52 |
'enabled' => $this->translateEnabled(...), |
| 53 |
'selected' => $this->translateSelected(...), |
| 54 |
'invalid' => $this->translateInvalid(...), |
| 55 |
'hover' => $this->translateHover(...), |
| 56 |
'visited' => $this->translateVisited(...), |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function getFunctionTranslators(): array |
| 61 |
{ |
| 62 |
return [ |
| 63 |
'lang' => $this->translateLang(...), |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
public function translateChecked(XPathExpr $xpath): XPathExpr |
| 68 |
{ |
| 69 |
return $xpath->addCondition( |
| 70 |
'(@checked ' |
| 71 |
."and (name(.) = 'input' or name(.) = 'command')" |
| 72 |
."and (@type = 'checkbox' or @type = 'radio'))" |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function translateLink(XPathExpr $xpath): XPathExpr |
| 77 |
{ |
| 78 |
return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')"); |
| 79 |
} |
| 80 |
|
| 81 |
public function translateDisabled(XPathExpr $xpath): XPathExpr |
| 82 |
{ |
| 83 |
return $xpath->addCondition( |
| 84 |
'(' |
| 85 |
.'@disabled and' |
| 86 |
.'(' |
| 87 |
."(name(.) = 'input' and @type != 'hidden')" |
| 88 |
." or name(.) = 'button'" |
| 89 |
." or name(.) = 'select'" |
| 90 |
." or name(.) = 'textarea'" |
| 91 |
." or name(.) = 'command'" |
| 92 |
." or name(.) = 'fieldset'" |
| 93 |
." or name(.) = 'optgroup'" |
| 94 |
." or name(.) = 'option'" |
| 95 |
.')' |
| 96 |
.') or (' |
| 97 |
."(name(.) = 'input' and @type != 'hidden')" |
| 98 |
." or name(.) = 'button'" |
| 99 |
." or name(.) = 'select'" |
| 100 |
." or name(.) = 'textarea'" |
| 101 |
." or name(.) = 'fieldset'" |
| 102 |
.')' |
| 103 |
.' and '.self::DISABLING_FIELDSET |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function translateEnabled(XPathExpr $xpath): XPathExpr |
| 108 |
{ |
| 109 |
return $xpath->addCondition( |
| 110 |
'(' |
| 111 |
.'@href and (' |
| 112 |
."name(.) = 'a'" |
| 113 |
." or name(.) = 'link'" |
| 114 |
." or name(.) = 'area'" |
| 115 |
.')' |
| 116 |
.') or (' |
| 117 |
.'(' |
| 118 |
."name(.) = 'command'" |
| 119 |
." or name(.) = 'optgroup'" |
| 120 |
.')' |
| 121 |
.' and not(@disabled)' |
| 122 |
.') or (' |
| 123 |
.'(' |
| 124 |
."(name(.) = 'input' and @type != 'hidden')" |
| 125 |
." or name(.) = 'button'" |
| 126 |
." or name(.) = 'select'" |
| 127 |
." or name(.) = 'textarea'" |
| 128 |
." or name(.) = 'keygen'" |
| 129 |
." or name(.) = 'fieldset'" |
| 130 |
.')' |
| 131 |
.' and not(@disabled or '.self::DISABLING_FIELDSET.')' |
| 132 |
.') or (' |
| 133 |
."name(.) = 'option' and not(" |
| 134 |
.'@disabled or ancestor::optgroup[@disabled]' |
| 135 |
.')' |
| 136 |
.')' |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @throws ExpressionErrorException |
| 142 |
*/ |
| 143 |
public function translateLang(XPathExpr $xpath, FunctionNode $function): XPathExpr |
| 144 |
{ |
| 145 |
$arguments = $function->getArguments(); |
| 146 |
foreach ($arguments as $token) { |
| 147 |
if (!($token->isString() || $token->isIdentifier())) { |
| 148 |
throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments)); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $xpath->addCondition(\sprintf( |
| 153 |
'ancestor-or-self::*[@lang][1][starts-with(concat(' |
| 154 |
."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')" |
| 155 |
.', %s)]', |
| 156 |
'lang', |
| 157 |
Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-') |
| 158 |
)); |
| 159 |
} |
| 160 |
|
| 161 |
public function translateSelected(XPathExpr $xpath): XPathExpr |
| 162 |
{ |
| 163 |
return $xpath->addCondition("(@selected and name(.) = 'option')"); |
| 164 |
} |
| 165 |
|
| 166 |
public function translateInvalid(XPathExpr $xpath): XPathExpr |
| 167 |
{ |
| 168 |
return $xpath->addCondition('0'); |
| 169 |
} |
| 170 |
|
| 171 |
public function translateHover(XPathExpr $xpath): XPathExpr |
| 172 |
{ |
| 173 |
return $xpath->addCondition('0'); |
| 174 |
} |
| 175 |
|
| 176 |
public function translateVisited(XPathExpr $xpath): XPathExpr |
| 177 |
{ |
| 178 |
return $xpath->addCondition('0'); |
| 179 |
} |
| 180 |
|
| 181 |
public function getName(): string |
| 182 |
{ |
| 183 |
return 'html'; |
| 184 |
} |
| 185 |
} |
| 186 |
|