|
1
|
|
|
<?php |
|
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
|
3
|
|
|
* @author Tom Butler [email protected] * |
|
4
|
|
|
* @copyright 2015 Tom Butler <[email protected]> | https://r.je/ * |
|
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
|
6
|
|
|
* @version 1.0 */ |
|
7
|
|
|
namespace Transphporm; |
|
8
|
|
|
class CssToXpath { |
|
9
|
|
|
private $specialChars = [' ', '.', '>', '~', '#', ':', '[', ']']; |
|
10
|
|
|
private $translators = []; |
|
11
|
|
|
private $css; |
|
12
|
|
|
private $depth; |
|
13
|
|
|
private $valueParser; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($css, ValueParser $valueParser, $prefix = '') { |
|
16
|
|
|
$this->css = str_replace([' >', '> '],['>', '>'], trim($css)); |
|
17
|
|
|
$this->valueParser = $valueParser; |
|
18
|
|
|
$this->translators = [ |
|
19
|
|
|
' ' => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
|
20
|
|
|
'' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
21
|
|
|
'>' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
22
|
|
|
'#' => function($string) { return '[@id=\'' . $string . '\']'; }, |
|
23
|
|
|
'.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
|
24
|
|
|
'[' => function($string) { return '[@' . $this->parseAttr($string) . ']'; }, |
|
25
|
|
|
']' => function() { return ''; } |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function createSelector() { |
|
30
|
|
|
$selector = new \stdclass; |
|
31
|
|
|
$selector->type = ''; |
|
32
|
|
|
$selector->string = ''; |
|
33
|
|
|
return $selector; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function parseAttr($attr) { |
|
37
|
|
|
$comparators = ['!=', '=']; |
|
38
|
|
|
foreach ($comparators as $comparator) { |
|
39
|
|
|
if (strpos($attr, $comparator) !== false) { |
|
40
|
|
|
$parts = explode($comparator, $attr); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($parts as &$part) $part = implode('', $this->valueParser->parse($part)); |
|
43
|
|
|
if (isset($parts[1])) $parts[1] = '"' . $parts[1] . '"'; |
|
44
|
|
|
return implode($comparator, $parts); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $attr; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
//split the css into indivudal functions |
|
52
|
|
|
private function split($css) { |
|
53
|
|
|
$selectors = []; |
|
54
|
|
|
$selector = $this->createSelector(); |
|
55
|
|
|
$selectors[] = $selector; |
|
56
|
|
|
|
|
57
|
|
|
for ($i = 0; $i < strlen($css); $i++) { |
|
58
|
|
|
if (in_array($css[$i], $this->specialChars)) { |
|
59
|
|
|
$selector = $this->createSelector(); |
|
60
|
|
|
$selector->type = $css[$i]; |
|
61
|
|
|
$selectors[] = $selector; |
|
62
|
|
|
} |
|
63
|
|
|
else $selector->string .= $css[$i]; |
|
64
|
|
|
} |
|
65
|
|
|
return $selectors; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getXpath() { |
|
69
|
|
|
$css = explode(':', $this->css)[0]; |
|
70
|
|
|
$selectors = $this->split($css); |
|
71
|
|
|
$this->depth = count($selectors); |
|
72
|
|
|
$xpath = '/'; |
|
73
|
|
|
foreach ($selectors as $selector) { |
|
74
|
|
|
if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$xpath = str_replace('/[', '/*[', $xpath); |
|
78
|
|
|
|
|
79
|
|
|
return $xpath; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getDepth() { |
|
83
|
|
|
return $this->depth; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getPseudo() { |
|
87
|
|
|
$parts = explode(':', $this->css); |
|
88
|
|
|
array_shift($parts); |
|
89
|
|
|
return $parts; |
|
90
|
|
|
} |
|
91
|
|
|
} |