@@ -13,6 +13,9 @@ |
||
13 | 13 | private $functionSet; |
14 | 14 | private $id; |
15 | 15 | |
16 | + /** |
|
17 | + * @param string $id |
|
18 | + */ |
|
16 | 19 | public function __construct(\Transphporm\FunctionSet $functionSet, $prefix = '', $id = null) { |
17 | 20 | $this->id = $id; |
18 | 21 | self::$instances[$this->id] = $this; |
@@ -70,7 +70,9 @@ discard block |
||
70 | 70 | $selector->type = $token['type']; |
71 | 71 | $selectors[] = $selector; |
72 | 72 | } |
73 | - if (isset($token['value'])) $selectors[count($selectors)-1]->string = $token['value']; |
|
73 | + if (isset($token['value'])) { |
|
74 | + $selectors[count($selectors)-1]->string = $token['value']; |
|
75 | + } |
|
74 | 76 | } |
75 | 77 | return $selectors; |
76 | 78 | } |
@@ -80,7 +82,9 @@ discard block |
||
80 | 82 | $selectors = $this->split($css); |
81 | 83 | $xpath = '/'; |
82 | 84 | foreach ($selectors as $selector) { |
83 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
85 | + if (isset($this->translators[$selector->type])) { |
|
86 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
87 | + } |
|
84 | 88 | } |
85 | 89 | |
86 | 90 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -92,11 +96,15 @@ discard block |
||
92 | 96 | $tokens = new Tokens; |
93 | 97 | $split = $css->splitOnToken(Tokenizer::GREATER_THAN); |
94 | 98 | |
95 | - if (count($split) <= 1) return $css; |
|
99 | + if (count($split) <= 1) { |
|
100 | + return $css; |
|
101 | + } |
|
96 | 102 | |
97 | 103 | for ($i = 0; $i < count($split); $i++) { |
98 | 104 | $tokens->add($split[$i]->trim()); |
99 | - if (isset($split[$i+1])) $tokens->add(['type' => Tokenizer::GREATER_THAN]); |
|
105 | + if (isset($split[$i+1])) { |
|
106 | + $tokens->add(['type' => Tokenizer::GREATER_THAN]); |
|
107 | + } |
|
100 | 108 | } |
101 | 109 | |
102 | 110 | return $tokens; |
@@ -19,13 +19,13 @@ discard block |
||
19 | 19 | $this->functionSet = $functionSet; |
20 | 20 | |
21 | 21 | $this->translators = [ |
22 | - Tokenizer::WHITESPACE => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
|
23 | - Tokenizer::MULTIPLY => function () { return '*'; }, |
|
24 | - '' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
25 | - Tokenizer::GREATER_THAN => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
26 | - Tokenizer::NUM_SIGN => function($string) { return '[@id=\'' . $string . '\']'; }, |
|
27 | - Tokenizer::DOT => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
|
28 | - Tokenizer::OPEN_SQUARE_BRACKET => function($string) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . base64_encode(serialize($string)) . '\', ., "' . $this->id . '")' . ']'; } |
|
22 | + Tokenizer::WHITESPACE => function($string) use ($prefix) { return '//'.$prefix.$string; }, |
|
23 | + Tokenizer::MULTIPLY => function() { return '*'; }, |
|
24 | + '' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
25 | + Tokenizer::GREATER_THAN => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
26 | + Tokenizer::NUM_SIGN => function($string) { return '[@id=\''.$string.'\']'; }, |
|
27 | + Tokenizer::DOT => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' '.$string.' \')]'; }, |
|
28 | + Tokenizer::OPEN_SQUARE_BRACKET => function($string) { return '['.'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \''.base64_encode(serialize($string)).'\', ., "'.$this->id.'")'.']'; } |
|
29 | 29 | ]; |
30 | 30 | } |
31 | 31 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | $functionSet->setElement($element[0]); |
45 | 45 | |
46 | 46 | $attributes = []; |
47 | - foreach($element[0]->attributes as $name => $node) { |
|
47 | + foreach ($element[0]->attributes as $name => $node) { |
|
48 | 48 | $attributes[$name] = $node->nodeValue; |
49 | 49 | } |
50 | 50 |
@@ -25,14 +25,19 @@ discard block |
||
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | 27 | $this->filePath->addPath(dirname(realpath($tss))); |
28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
29 | - else return; |
|
28 | + if (empty($this->rules)) { |
|
29 | + $tss = file_get_contents($tss); |
|
30 | + } else { |
|
31 | + return; |
|
32 | + } |
|
30 | 33 | } |
31 | 34 | $this->tss = (new Tokenizer($tss))->getTokens(); |
32 | 35 | } |
33 | 36 | |
34 | 37 | public function parse($indexStart = 0) { |
35 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
38 | + if (!empty($this->rules)) { |
|
39 | + return $this->rules['rules']; |
|
40 | + } |
|
36 | 41 | $rules = $this->parseTokens($indexStart); |
37 | 42 | usort($rules, [$this, 'sortRules']); |
38 | 43 | $this->checkError($rules); |
@@ -44,8 +49,9 @@ discard block |
||
44 | 49 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
45 | 50 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
46 | 51 | $this->rules = array_merge($this->rules, $processing); |
52 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
53 | + $this->addRules($token, $indexStart); |
|
47 | 54 | } |
48 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
49 | 55 | } |
50 | 56 | |
51 | 57 | return $this->rules; |
@@ -55,14 +61,18 @@ discard block |
||
55 | 61 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
56 | 62 | |
57 | 63 | $this->tss->skip(count($selector)); |
58 | - if (count($selector) === 0) return; |
|
64 | + if (count($selector) === 0) { |
|
65 | + return; |
|
66 | + } |
|
59 | 67 | |
60 | 68 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
61 | 69 | $this->rules = $this->writeRule($this->rules, $newRules); |
62 | 70 | } |
63 | 71 | |
64 | 72 | private function checkError($rules) { |
65 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
73 | + if (empty($rules) && count($this->tss) > 0) { |
|
74 | + throw new \Exception('No TSS rules parsed'); |
|
75 | + } |
|
66 | 76 | } |
67 | 77 | |
68 | 78 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -89,7 +99,9 @@ discard block |
||
89 | 99 | } |
90 | 100 | |
91 | 101 | private function processingInstructions($token, $indexStart) { |
92 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
102 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
103 | + return false; |
|
104 | + } |
|
93 | 105 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
94 | 106 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
95 | 107 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -109,7 +121,9 @@ discard block |
||
109 | 121 | |
110 | 122 | private function sortRules($a, $b) { |
111 | 123 | //If they have the same depth, compare on index |
112 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
124 | + if ($a->depth === $b->depth) { |
|
125 | + return $a->index < $b->index ? -1 : 1; |
|
126 | + } |
|
113 | 127 | |
114 | 128 | return ($a->depth < $b->depth) ? -1 : 1; |
115 | 129 | } |
@@ -39,6 +39,9 @@ discard block |
||
39 | 39 | return $this->cache->write($this->file, $rules, $this->import); |
40 | 40 | } |
41 | 41 | |
42 | + /** |
|
43 | + * @param integer $indexStart |
|
44 | + */ |
|
42 | 45 | private function parseTokens($indexStart) { |
43 | 46 | $this->rules = []; |
44 | 47 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -65,6 +68,10 @@ discard block |
||
65 | 68 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
66 | 69 | } |
67 | 70 | |
71 | + /** |
|
72 | + * @param Tokens $selector |
|
73 | + * @param integer $index |
|
74 | + */ |
|
68 | 75 | private function CssToRules($selector, $index, $properties, $line) { |
69 | 76 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
70 | 77 | $rules = []; |
@@ -88,6 +95,9 @@ discard block |
||
88 | 95 | return $rules; |
89 | 96 | } |
90 | 97 | |
98 | + /** |
|
99 | + * @param integer $indexStart |
|
100 | + */ |
|
91 | 101 | private function processingInstructions($token, $indexStart) { |
92 | 102 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
93 | 103 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -87,7 +87,7 @@ |
||
87 | 87 | if ($token['type'] === $tokenType) $i++; |
88 | 88 | else $splitTokens[$i][] = $token; |
89 | 89 | } |
90 | - return array_map(function ($tokens) { |
|
90 | + return array_map(function($tokens) { |
|
91 | 91 | return new Tokens($tokens); |
92 | 92 | }, $splitTokens); |
93 | 93 | //return $splitTokens; |
@@ -39,8 +39,11 @@ discard block |
||
39 | 39 | } |
40 | 40 | |
41 | 41 | public function add($token) { |
42 | - if ($token instanceof Tokens) $this->tokens = array_merge($token->tokens); |
|
43 | - else $this->tokens[] = $token; |
|
42 | + if ($token instanceof Tokens) { |
|
43 | + $this->tokens = array_merge($token->tokens); |
|
44 | + } else { |
|
45 | + $this->tokens[] = $token; |
|
46 | + } |
|
44 | 47 | } |
45 | 48 | |
46 | 49 | |
@@ -54,9 +57,13 @@ discard block |
||
54 | 57 | |
55 | 58 | private function getKeyToSlice($tokenType) { |
56 | 59 | $keys = $this->getKeysOfTokenType($tokenType); |
57 | - if (empty($keys)) return false; |
|
60 | + if (empty($keys)) { |
|
61 | + return false; |
|
62 | + } |
|
58 | 63 | $key = $keys[0]; |
59 | - for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
|
64 | + for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) { |
|
65 | + $key = $keys[$i]; |
|
66 | + } |
|
60 | 67 | return $key; |
61 | 68 | } |
62 | 69 | |
@@ -70,10 +77,17 @@ discard block |
||
70 | 77 | |
71 | 78 | private function sliceTokens($tokenType, $type, $increment = false) { |
72 | 79 | $key = $this->getKeyToSlice($tokenType); |
73 | - if ($key === false) return new Tokens([]); |
|
74 | - if ($increment) $key++; |
|
75 | - if ($type === "from") return new Tokens(array_slice($this->tokens, $key)); |
|
76 | - else return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
80 | + if ($key === false) { |
|
81 | + return new Tokens([]); |
|
82 | + } |
|
83 | + if ($increment) { |
|
84 | + $key++; |
|
85 | + } |
|
86 | + if ($type === "from") { |
|
87 | + return new Tokens(array_slice($this->tokens, $key)); |
|
88 | + } else { |
|
89 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
90 | + } |
|
77 | 91 | } |
78 | 92 | |
79 | 93 | public function skip($count) { |
@@ -84,8 +98,11 @@ discard block |
||
84 | 98 | $splitTokens = []; |
85 | 99 | $i = 0; |
86 | 100 | foreach ($this->tokens as $token) { |
87 | - if ($token['type'] === $tokenType) $i++; |
|
88 | - else $splitTokens[$i][] = $token; |
|
101 | + if ($token['type'] === $tokenType) { |
|
102 | + $i++; |
|
103 | + } else { |
|
104 | + $splitTokens[$i][] = $token; |
|
105 | + } |
|
89 | 106 | } |
90 | 107 | return array_map(function ($tokens) { |
91 | 108 | return new Tokens($tokens); |
@@ -109,7 +126,9 @@ discard block |
||
109 | 126 | |
110 | 127 | public function removeLine() { |
111 | 128 | $tokens = $this->tokens; |
112 | - foreach ($tokens as &$token) unset($token['line']); |
|
129 | + foreach ($tokens as &$token) { |
|
130 | + unset($token['line']); |
|
131 | + } |
|
113 | 132 | return new Tokens($tokens); |
114 | 133 | } |
115 | 134 |
@@ -55,21 +55,21 @@ discard block |
||
55 | 55 | } |
56 | 56 | |
57 | 57 | public function read($offset = 0) { |
58 | - return $this->str[$this->pos + $offset]; |
|
58 | + return $this->str[$this->pos+$offset]; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | public function identifyChar($offset = 0) { |
62 | - $chr = $this->str[$this->pos + $offset]; |
|
62 | + $chr = $this->str[$this->pos+$offset]; |
|
63 | 63 | if (!empty($this->chars[$chr])) return $this->chars[$chr]; |
64 | 64 | else return Tokenizer::NAME; |
65 | 65 | } |
66 | 66 | |
67 | 67 | public function has($offset = 0) { |
68 | - return isset($this->str[$this->pos + $offset]); |
|
68 | + return isset($this->str[$this->pos+$offset]); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | public function pos($str) { |
72 | - $pos = strpos($this->str, $str, $this->pos); |
|
72 | + $pos = strpos($this->str, $str, $this->pos); |
|
73 | 73 | return $pos ? $pos-$this->pos : false; |
74 | 74 | } |
75 | 75 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | } |
83 | 83 | |
84 | 84 | public function extractString($offset = 0) { |
85 | - $pos = $this->pos + $offset; |
|
85 | + $pos = $this->pos+$offset; |
|
86 | 86 | $char = $this->str[$pos]; |
87 | 87 | $end = strpos($this->str, $char, $pos+1); |
88 | 88 | while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
@@ -40,8 +40,11 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | public function move($n) { |
43 | - if ($n === false) $this->pos = strlen($this->str)-1; |
|
44 | - else $this->pos += $n; |
|
43 | + if ($n === false) { |
|
44 | + $this->pos = strlen($this->str)-1; |
|
45 | + } else { |
|
46 | + $this->pos += $n; |
|
47 | + } |
|
45 | 48 | } |
46 | 49 | |
47 | 50 | public function next() { |
@@ -60,8 +63,11 @@ discard block |
||
60 | 63 | |
61 | 64 | public function identifyChar($offset = 0) { |
62 | 65 | $chr = $this->str[$this->pos + $offset]; |
63 | - if (!empty($this->chars[$chr])) return $this->chars[$chr]; |
|
64 | - else return Tokenizer::NAME; |
|
66 | + if (!empty($this->chars[$chr])) { |
|
67 | + return $this->chars[$chr]; |
|
68 | + } else { |
|
69 | + return Tokenizer::NAME; |
|
70 | + } |
|
65 | 71 | } |
66 | 72 | |
67 | 73 | public function has($offset = 0) { |
@@ -85,7 +91,9 @@ discard block |
||
85 | 91 | $pos = $this->pos + $offset; |
86 | 92 | $char = $this->str[$pos]; |
87 | 93 | $end = strpos($this->str, $char, $pos+1); |
88 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
94 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
95 | + $end = strpos($this->str, $char, $end+1); |
|
96 | + } |
|
89 | 97 | |
90 | 98 | return substr($this->str, $pos+1, $end-$pos-1); |
91 | 99 | } |
@@ -95,7 +103,9 @@ discard block |
||
95 | 103 | $close = strpos($this->str, $closeBracket, $open); |
96 | 104 | |
97 | 105 | $cPos = $open+1; |
98 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
106 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
107 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
108 | + } |
|
99 | 109 | return substr($this->str, $open+1, $close-$open-1); |
100 | 110 | } |
101 | 111 |
@@ -5,7 +5,7 @@ |
||
5 | 5 | |
6 | 6 | class Brackets implements \Transphporm\Parser\Tokenize { |
7 | 7 | |
8 | - private $types = [ |
|
8 | + private $types = [ |
|
9 | 9 | Tokenizer::OPEN_BRACKET => ['(', ')'], |
10 | 10 | Tokenizer::OPEN_BRACE => ['{', '}'], |
11 | 11 | Tokenizer::OPEN_SQUARE_BRACKET => ['[', ']'] |
@@ -32,9 +32,14 @@ |
||
32 | 32 | } |
33 | 33 | |
34 | 34 | private function processLiterals($tokens, $name, $str) { |
35 | - if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
36 | - else if ($name == 'true') $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]); |
|
37 | - else if ($name == 'false') $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]); |
|
38 | - else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
35 | + if (is_numeric($name)) { |
|
36 | + $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
37 | + } else if ($name == 'true') { |
|
38 | + $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]); |
|
39 | + } else if ($name == 'false') { |
|
40 | + $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]); |
|
41 | + } else { |
|
42 | + $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
43 | + } |
|
39 | 44 | } |
40 | 45 | } |
41 | 46 | \ No newline at end of file |
@@ -10,7 +10,7 @@ |
||
10 | 10 | $chr = $str->read(); |
11 | 11 | $string = $str->extractString(); |
12 | 12 | $length = strlen($string)+1; |
13 | - $string = str_replace('\\' . $chr, $chr, $string); |
|
13 | + $string = str_replace('\\'.$chr, $chr, $string); |
|
14 | 14 | $tokens->add(['type' => Tokenizer::STRING, 'value' => $string, 'line' => $str->lineNo()]); |
15 | 15 | $str->move($length); |
16 | 16 | } |