PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.11
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.11
4.1.20 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 All 174 releases
searchpro / BerqWP / vendor / symfony / css-selector / XPath / Extension / PseudoClassExtension.php

PseudoClassExtension.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.11, at BerqWP/vendor/symfony/css-selector/XPath/Extension/PseudoClassExtension.php

123 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\XPath\XPathExpr;
16
17 /**
18 * XPath expression translator pseudo-class extension.
19 *
20 * This component is a port of the Python cssselect library,
21 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
22 *
23 * @author Jean-François Simon <[email protected]>
24 *
25 * @internal
26 */
27 class PseudoClassExtension extends AbstractExtension
28 {
29 /**
30 * {@inheritdoc}
31 */
32 public function getPseudoClassTranslators(): array
33 {
34 return [
35 'root' => [$this, 'translateRoot'],
36 'first-child' => [$this, 'translateFirstChild'],
37 'last-child' => [$this, 'translateLastChild'],
38 'first-of-type' => [$this, 'translateFirstOfType'],
39 'last-of-type' => [$this, 'translateLastOfType'],
40 'only-child' => [$this, 'translateOnlyChild'],
41 'only-of-type' => [$this, 'translateOnlyOfType'],
42 'empty' => [$this, 'translateEmpty'],
43 ];
44 }
45
46 public function translateRoot(XPathExpr $xpath): XPathExpr
47 {
48 return $xpath->addCondition('not(parent::*)');
49 }
50
51 public function translateFirstChild(XPathExpr $xpath): XPathExpr
52 {
53 return $xpath
54 ->addStarPrefix()
55 ->addNameTest()
56 ->addCondition('position() = 1');
57 }
58
59 public function translateLastChild(XPathExpr $xpath): XPathExpr
60 {
61 return $xpath
62 ->addStarPrefix()
63 ->addNameTest()
64 ->addCondition('position() = last()');
65 }
66
67 /**
68 * @throws ExpressionErrorException
69 */
70 public function translateFirstOfType(XPathExpr $xpath): XPathExpr
71 {
72 if ('*' === $xpath->getElement()) {
73 throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
74 }
75
76 return $xpath
77 ->addStarPrefix()
78 ->addCondition('position() = 1');
79 }
80
81 /**
82 * @throws ExpressionErrorException
83 */
84 public function translateLastOfType(XPathExpr $xpath): XPathExpr
85 {
86 if ('*' === $xpath->getElement()) {
87 throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
88 }
89
90 return $xpath
91 ->addStarPrefix()
92 ->addCondition('position() = last()');
93 }
94
95 public function translateOnlyChild(XPathExpr $xpath): XPathExpr
96 {
97 return $xpath
98 ->addStarPrefix()
99 ->addNameTest()
100 ->addCondition('last() = 1');
101 }
102
103 public function translateOnlyOfType(XPathExpr $xpath): XPathExpr
104 {
105 $element = $xpath->getElement();
106
107 return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
108 }
109
110 public function translateEmpty(XPathExpr $xpath): XPathExpr
111 {
112 return $xpath->addCondition('not(*) and not(string-length())');
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function getName(): string
119 {
120 return 'pseudo-class';
121 }
122 }
123