Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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\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 <jeanfrancois.simon@sensiolabs.com>
24
 *
25
 * @internal
26
 */
27
class PseudoClassExtension extends AbstractExtension
28
{
29
    public function getPseudoClassTranslators(): array
30
    {
31
        return [
32
            'root' => $this->translateRoot(...),
33
            'first-child' => $this->translateFirstChild(...),
34
            'last-child' => $this->translateLastChild(...),
35
            'first-of-type' => $this->translateFirstOfType(...),
36
            'last-of-type' => $this->translateLastOfType(...),
37
            'only-child' => $this->translateOnlyChild(...),
38
            'only-of-type' => $this->translateOnlyOfType(...),
39
            'empty' => $this->translateEmpty(...),
40
        ];
41
    }
42
 
43
    public function translateRoot(XPathExpr $xpath): XPathExpr
44
    {
45
        return $xpath->addCondition('not(parent::*)');
46
    }
47
 
48
    public function translateFirstChild(XPathExpr $xpath): XPathExpr
49
    {
50
        return $xpath
51
            ->addStarPrefix()
52
            ->addNameTest()
53
            ->addCondition('position() = 1');
54
    }
55
 
56
    public function translateLastChild(XPathExpr $xpath): XPathExpr
57
    {
58
        return $xpath
59
            ->addStarPrefix()
60
            ->addNameTest()
61
            ->addCondition('position() = last()');
62
    }
63
 
64
    /**
65
     * @throws ExpressionErrorException
66
     */
67
    public function translateFirstOfType(XPathExpr $xpath): XPathExpr
68
    {
69
        if ('*' === $xpath->getElement()) {
70
            throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
71
        }
72
 
73
        return $xpath
74
            ->addStarPrefix()
75
            ->addCondition('position() = 1');
76
    }
77
 
78
    /**
79
     * @throws ExpressionErrorException
80
     */
81
    public function translateLastOfType(XPathExpr $xpath): XPathExpr
82
    {
83
        if ('*' === $xpath->getElement()) {
84
            throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
85
        }
86
 
87
        return $xpath
88
            ->addStarPrefix()
89
            ->addCondition('position() = last()');
90
    }
91
 
92
    public function translateOnlyChild(XPathExpr $xpath): XPathExpr
93
    {
94
        return $xpath
95
            ->addStarPrefix()
96
            ->addNameTest()
97
            ->addCondition('last() = 1');
98
    }
99
 
100
    public function translateOnlyOfType(XPathExpr $xpath): XPathExpr
101
    {
102
        $element = $xpath->getElement();
103
 
104
        return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
105
    }
106
 
107
    public function translateEmpty(XPathExpr $xpath): XPathExpr
108
    {
109
        return $xpath->addCondition('not(*) and not(string-length())');
110
    }
111
 
112
    public function getName(): string
113
    {
114
        return 'pseudo-class';
115
    }
116
}