@@ -12,6 +12,9 @@ discard block |
||
12 | 12 | private $valueParser; |
13 | 13 | private $xPath; |
14 | 14 | |
15 | + /** |
|
16 | + * @param string $tss |
|
17 | + */ |
|
15 | 18 | public function __construct($tss, $baseDir, CssToXpath $xPath, Value $valueParser) { |
16 | 19 | $this->tss = $this->stripComments($tss, '//', "\n"); |
17 | 20 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -46,6 +49,9 @@ discard block |
||
46 | 49 | return $rules; |
47 | 50 | } |
48 | 51 | |
52 | + /** |
|
53 | + * @param integer $index |
|
54 | + */ |
|
49 | 55 | private function CssToRules($selector, $index, $properties) { |
50 | 56 | //$parts = explode(',', $selector); |
51 | 57 | $parts = $this->splitOnToken($selector, Tokenizer::ARG); |
@@ -70,6 +76,10 @@ discard block |
||
70 | 76 | return $rules; |
71 | 77 | } |
72 | 78 | |
79 | + /** |
|
80 | + * @param integer $key |
|
81 | + * @param integer $indexStart |
|
82 | + */ |
|
73 | 83 | private function processingInstructions($key, $indexStart) { |
74 | 84 | if (isset($this->tss[$key]) && $this->tss[$key]['type'] !== Tokenizer::AT_SIGN) return false; |
75 | 85 | $rules = []; |
@@ -95,6 +105,10 @@ discard block |
||
95 | 105 | return ($a->depth < $b->depth) ? -1 : 1; |
96 | 106 | } |
97 | 107 | |
108 | + /** |
|
109 | + * @param string $open |
|
110 | + * @param string $close |
|
111 | + */ |
|
98 | 112 | private function stripComments($str, $open, $close) { |
99 | 113 | $pos = 0; |
100 | 114 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -84,7 +84,7 @@ |
||
84 | 84 | |
85 | 85 | private function import($args, $indexStart) { |
86 | 86 | $fileName = $args[0]; |
87 | - $sheet = new Sheet(file_get_contents($this->baseDir . $fileName), dirname(realpath($this->baseDir . $fileName)) . DIRECTORY_SEPARATOR, $this->xPath, $this->valueParser); |
|
87 | + $sheet = new Sheet(file_get_contents($this->baseDir.$fileName), dirname(realpath($this->baseDir.$fileName)).DIRECTORY_SEPARATOR, $this->xPath, $this->valueParser); |
|
88 | 88 | return $sheet->parse($indexStart); |
89 | 89 | } |
90 | 90 |
@@ -26,7 +26,9 @@ discard block |
||
26 | 26 | $rules = []; |
27 | 27 | $numOfTokens = count($this->tss); |
28 | 28 | for ($i = 0; isset($this->tss[$i]) && $i <= $numOfTokens; $i++) { |
29 | - if ($this->tss[$i]['type'] === Tokenizer::WHITESPACE) continue; |
|
29 | + if ($this->tss[$i]['type'] === Tokenizer::WHITESPACE) { |
|
30 | + continue; |
|
31 | + } |
|
30 | 32 | if ($processing = $this->processingInstructions($i, count($rules)+$indexStart)) { |
31 | 33 | $i = $processing['endPos']+1; |
32 | 34 | $rules = array_merge($rules, $processing['rules']); |
@@ -35,14 +37,20 @@ discard block |
||
35 | 37 | $tokens = array_slice($this->tss, $i); |
36 | 38 | $selector = $this->splitOnToken($tokens, Tokenizer::OPEN_BRACE)[0]; |
37 | 39 | $i += count($selector); |
38 | - if ($selector[count($selector)-1]['type'] === Tokenizer::WHITESPACE) array_pop($selector); |
|
39 | - if (!isset($this->tss[$i])) break; |
|
40 | + if ($selector[count($selector)-1]['type'] === Tokenizer::WHITESPACE) { |
|
41 | + array_pop($selector); |
|
42 | + } |
|
43 | + if (!isset($this->tss[$i])) { |
|
44 | + break; |
|
45 | + } |
|
40 | 46 | |
41 | 47 | $newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss[$i]['value'])); |
42 | 48 | $rules = $this->writeRule($rules, $newRules); |
43 | 49 | } |
44 | 50 | usort($rules, [$this, 'sortRules']); |
45 | - if (empty($rules) && !empty($this->tss)) throw new \Exception("No TSS rules parsed"); |
|
51 | + if (empty($rules) && !empty($this->tss)) { |
|
52 | + throw new \Exception("No TSS rules parsed"); |
|
53 | + } |
|
46 | 54 | return $rules; |
47 | 55 | } |
48 | 56 | |
@@ -71,7 +79,9 @@ discard block |
||
71 | 79 | } |
72 | 80 | |
73 | 81 | private function processingInstructions($key, $indexStart) { |
74 | - if (isset($this->tss[$key]) && $this->tss[$key]['type'] !== Tokenizer::AT_SIGN) return false; |
|
82 | + if (isset($this->tss[$key]) && $this->tss[$key]['type'] !== Tokenizer::AT_SIGN) { |
|
83 | + return false; |
|
84 | + } |
|
75 | 85 | $rules = []; |
76 | 86 | $tokens = array_slice($this->tss, $key+1); |
77 | 87 | $tokens = $this->splitOnToken($tokens, Tokenizer::SEMI_COLON)[0]; |
@@ -90,7 +100,9 @@ discard block |
||
90 | 100 | |
91 | 101 | private function sortRules($a, $b) { |
92 | 102 | //If they have the same depth, compare on index |
93 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
103 | + if ($a->depth === $b->depth) { |
|
104 | + return $a->index < $b->index ? -1 : 1; |
|
105 | + } |
|
94 | 106 | |
95 | 107 | return ($a->depth < $b->depth) ? -1 : 1; |
96 | 108 | } |
@@ -99,7 +111,9 @@ discard block |
||
99 | 111 | $pos = 0; |
100 | 112 | while (($pos = strpos($str, $open, $pos)) !== false) { |
101 | 113 | $end = strpos($str, $close, $pos); |
102 | - if ($end === false) break; |
|
114 | + if ($end === false) { |
|
115 | + break; |
|
116 | + } |
|
103 | 117 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
104 | 118 | } |
105 | 119 | |
@@ -110,8 +124,11 @@ discard block |
||
110 | 124 | $splitTokens = []; |
111 | 125 | $i = 0; |
112 | 126 | foreach ($tokens as $token) { |
113 | - if ($token['type'] === $splitOn) $i++; |
|
114 | - else $splitTokens[$i][] = $token; |
|
127 | + if ($token['type'] === $splitOn) { |
|
128 | + $i++; |
|
129 | + } else { |
|
130 | + $splitTokens[$i][] = $token; |
|
131 | + } |
|
115 | 132 | } |
116 | 133 | return $splitTokens; |
117 | 134 | } |
@@ -123,13 +140,18 @@ discard block |
||
123 | 140 | $rules = []; |
124 | 141 | $i = 0; |
125 | 142 | foreach ($tokens as $token) { |
126 | - if ($token['type'] === Tokenizer::SEMI_COLON) $i++; |
|
127 | - else if ($token['type'] !== Tokenizer::WHITESPACE) $rules[$i][] = $token; |
|
143 | + if ($token['type'] === Tokenizer::SEMI_COLON) { |
|
144 | + $i++; |
|
145 | + } else if ($token['type'] !== Tokenizer::WHITESPACE) { |
|
146 | + $rules[$i][] = $token; |
|
147 | + } |
|
128 | 148 | } |
129 | 149 | |
130 | 150 | $return = []; |
131 | 151 | foreach ($rules as $rule) { |
132 | - if ($rule[1]['type'] === Tokenizer::COLON) $return[$rule[0]['value']] = array_slice($rule, 2); |
|
152 | + if ($rule[1]['type'] === Tokenizer::COLON) { |
|
153 | + $return[$rule[0]['value']] = array_slice($rule, 2); |
|
154 | + } |
|
133 | 155 | } |
134 | 156 | |
135 | 157 | return $return; |
@@ -19,12 +19,12 @@ |
||
19 | 19 | $this->functionSet = $functionSet; |
20 | 20 | |
21 | 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\', \'' . json_encode($string) . '\', ., "' . $hash . '")' . ']'; } |
|
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\', \''.json_encode($string).'\', ., "'.$hash.'")'.']'; } |
|
28 | 28 | ]; |
29 | 29 | } |
30 | 30 |
@@ -49,7 +49,9 @@ discard block |
||
49 | 49 | |
50 | 50 | $parts = self::$instances[$hash]->splitOnToken($attr, Tokenizer::EQUALS); |
51 | 51 | |
52 | - if ($parts[0] === $attr) return $element[0]->getAttribute($valueParser->parseTokens($attr)[0]) !== ''; |
|
52 | + if ($parts[0] === $attr) { |
|
53 | + return $element[0]->getAttribute($valueParser->parseTokens($attr)[0]) !== ''; |
|
54 | + } |
|
53 | 55 | |
54 | 56 | if ($parts[0][count($parts[0])-1]['type'] === Tokenizer::NOT) { |
55 | 57 | $attr = [ |
@@ -59,8 +61,7 @@ discard block |
||
59 | 61 | ['type' => Tokenizer::EQUALS] |
60 | 62 | ]; |
61 | 63 | $attr = array_merge($attr, $parts[1]); |
62 | - } |
|
63 | - else { |
|
64 | + } else { |
|
64 | 65 | $attr = [ |
65 | 66 | ['type' => Tokenizer::NAME, 'value' => 'attr'], |
66 | 67 | ['type' => Tokenizer::OPEN_BRACKET, 'value' => $parts[0]], |
@@ -75,8 +76,11 @@ discard block |
||
75 | 76 | $splitTokens = []; |
76 | 77 | $i = 0; |
77 | 78 | foreach ($tokens as $token) { |
78 | - if ($token['type'] === $splitOn) $i++; |
|
79 | - else $splitTokens[$i][] = $token; |
|
79 | + if ($token['type'] === $splitOn) { |
|
80 | + $i++; |
|
81 | + } else { |
|
82 | + $splitTokens[$i][] = $token; |
|
83 | + } |
|
80 | 84 | } |
81 | 85 | return $splitTokens; |
82 | 86 | } |
@@ -93,7 +97,9 @@ discard block |
||
93 | 97 | $selector->type = $token['type']; |
94 | 98 | $selectors[] = $selector; |
95 | 99 | } |
96 | - if (isset($token['value'])) $selectors[count($selectors)-1]->string = $token['value']; |
|
100 | + if (isset($token['value'])) { |
|
101 | + $selectors[count($selectors)-1]->string = $token['value']; |
|
102 | + } |
|
97 | 103 | } |
98 | 104 | return $selectors; |
99 | 105 | } |
@@ -101,15 +107,20 @@ discard block |
||
101 | 107 | public function getXpath($css) { |
102 | 108 | foreach ($css as $key => $token) { |
103 | 109 | if ($token['type'] === Tokenizer::WHITESPACE && |
104 | - (isset($css[$key+1]) && $css[$key+1]['type'] === Tokenizer::GREATER_THAN)) unset($css[$key]); |
|
105 | - else if ($token['type'] === Tokenizer::WHITESPACE && |
|
106 | - (isset($css[$key-1]) && $css[$key-1]['type'] === Tokenizer::GREATER_THAN)) unset($css[$key]); |
|
110 | + (isset($css[$key+1]) && $css[$key+1]['type'] === Tokenizer::GREATER_THAN)) { |
|
111 | + unset($css[$key]); |
|
112 | + } else if ($token['type'] === Tokenizer::WHITESPACE && |
|
113 | + (isset($css[$key-1]) && $css[$key-1]['type'] === Tokenizer::GREATER_THAN)) { |
|
114 | + unset($css[$key]); |
|
115 | + } |
|
107 | 116 | } |
108 | 117 | $css = $this->splitOnToken(array_values($css), Tokenizer::COLON)[0]; |
109 | 118 | $selectors = $this->split($css); |
110 | 119 | $xpath = '/'; |
111 | 120 | foreach ($selectors as $selector) { |
112 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
121 | + if (isset($this->translators[$selector->type])) { |
|
122 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
123 | + } |
|
113 | 124 | } |
114 | 125 | |
115 | 126 | $xpath = str_replace('/[', '/*[', $xpath); |