Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\CssSelector;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
|
Chris@0
|
15 use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
|
Chris@0
|
16 use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
|
Chris@0
|
17 use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
|
Chris@0
|
18 use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
|
Chris@0
|
19 use Symfony\Component\CssSelector\XPath\Translator;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * CssSelectorConverter is the main entry point of the component and can convert CSS
|
Chris@0
|
23 * selectors to XPath expressions.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Christophe Coevoet <stof@notk.org>
|
Chris@0
|
26 */
|
Chris@0
|
27 class CssSelectorConverter
|
Chris@0
|
28 {
|
Chris@0
|
29 private $translator;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @param bool $html Whether HTML support should be enabled. Disable it for XML documents
|
Chris@0
|
33 */
|
Chris@0
|
34 public function __construct($html = true)
|
Chris@0
|
35 {
|
Chris@0
|
36 $this->translator = new Translator();
|
Chris@0
|
37
|
Chris@0
|
38 if ($html) {
|
Chris@0
|
39 $this->translator->registerExtension(new HtmlExtension($this->translator));
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 $this->translator
|
Chris@0
|
43 ->registerParserShortcut(new EmptyStringParser())
|
Chris@0
|
44 ->registerParserShortcut(new ElementParser())
|
Chris@0
|
45 ->registerParserShortcut(new ClassParser())
|
Chris@0
|
46 ->registerParserShortcut(new HashParser())
|
Chris@0
|
47 ;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Translates a CSS expression to its XPath equivalent.
|
Chris@0
|
52 *
|
Chris@0
|
53 * Optionally, a prefix can be added to the resulting XPath
|
Chris@0
|
54 * expression with the $prefix parameter.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param string $cssExpr The CSS expression
|
Chris@0
|
57 * @param string $prefix An optional prefix for the XPath expression
|
Chris@0
|
58 *
|
Chris@0
|
59 * @return string
|
Chris@0
|
60 */
|
Chris@0
|
61 public function toXPath($cssExpr, $prefix = 'descendant-or-self::')
|
Chris@0
|
62 {
|
Chris@0
|
63 return $this->translator->cssToXPath($cssExpr, $prefix);
|
Chris@0
|
64 }
|
Chris@0
|
65 }
|