1 | <?php |
||
8 | class CssToXpath { |
||
9 | private $specialChars = [Tokenizer::WHITESPACE, Tokenizer::DOT, Tokenizer::GREATER_THAN, |
||
10 | '~', Tokenizer::NUM_SIGN, Tokenizer::COLON, Tokenizer::OPEN_SQUARE_BRACKET]; |
||
11 | private $translators = []; |
||
12 | private static $instances = []; |
||
13 | private $functionSet; |
||
14 | |||
15 | |||
16 | public function __construct(\Transphporm\FunctionSet $functionSet, $prefix = '') { |
||
17 | $hash = count(self::$instances); |
||
18 | self::$instances[$hash] = $this; |
||
19 | $this->functionSet = $functionSet; |
||
20 | |||
21 | $this->translators = [ |
||
22 | Tokenizer::WHITESPACE => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
||
23 | '' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
||
24 | Tokenizer::GREATER_THAN => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
||
25 | Tokenizer::NUM_SIGN => function($string) { return '[@id=\'' . $string . '\']'; }, |
||
26 | Tokenizer::DOT => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
||
27 | Tokenizer::OPEN_SQUARE_BRACKET => function($string) use ($hash) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . base64_encode(serialize($string)) . '\', ., "' . $hash . '")' . ']'; } |
||
28 | ]; |
||
29 | } |
||
30 | |||
31 | private function createSelector() { |
||
32 | $selector = new \stdclass; |
||
33 | $selector->type = ''; |
||
34 | $selector->string = ''; |
||
35 | return $selector; |
||
36 | } |
||
37 | |||
38 | //XPath only allows registering of static functions... this is a hacky workaround for that |
||
39 | public static function processAttr($attr, $element, $hash) { |
||
40 | $attr = unserialize(base64_decode($attr)); |
||
41 | |||
42 | $functionSet = self::$instances[$hash]->functionSet; |
||
43 | $functionSet->setElement($element[0]); |
||
44 | |||
45 | $attributes = array(); |
||
46 | foreach($element[0]->attributes as $attribute_name => $attribute_node) { |
||
47 | $attributes[$attribute_name] = $attribute_node->nodeValue; |
||
48 | } |
||
49 | |||
50 | $parser = new \Transphporm\Parser\Value($functionSet, true); |
||
51 | $return = $parser->parseTokens($attr, $attributes); |
||
52 | |||
53 | return $return[0] === '' ? false : $return[0]; |
||
54 | } |
||
55 | |||
56 | public static function cleanup() { |
||
57 | self::$instances = []; |
||
58 | } |
||
59 | |||
60 | //split the css into indivudal functions |
||
61 | private function split($css) { |
||
76 | |||
77 | public function getXpath($css) { |
||
89 | |||
90 | private function removeSpacesFromDirectDecend($css) { |
||
98 | |||
99 | |||
100 | public function getDepth($css) { |
||
103 | |||
104 | public function getPseudo($css) { |
||
109 | } |
||
110 |