@@ -42,6 +42,9 @@ discard block |
||
| 42 | 42 | return $this->cache->write($this->file, $rules, $this->import); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | + /** |
|
| 46 | + * @param integer $indexStart |
|
| 47 | + */ |
|
| 45 | 48 | private function parseTokens($indexStart) { |
| 46 | 49 | $this->rules = []; |
| 47 | 50 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -66,6 +69,9 @@ discard block |
||
| 66 | 69 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
| 67 | 70 | } |
| 68 | 71 | |
| 72 | + /** |
|
| 73 | + * @param integer $index |
|
| 74 | + */ |
|
| 69 | 75 | private function CssToRules($selector, $index, $properties, $line) { |
| 70 | 76 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
| 71 | 77 | $rules = []; |
@@ -89,6 +95,9 @@ discard block |
||
| 89 | 95 | return $rules; |
| 90 | 96 | } |
| 91 | 97 | |
| 98 | + /** |
|
| 99 | + * @param integer $indexStart |
|
| 100 | + */ |
|
| 92 | 101 | private function processingInstructions($token, $indexStart) { |
| 93 | 102 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
| 94 | 103 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -119,6 +128,10 @@ discard block |
||
| 119 | 128 | return ($a->depth < $b->depth) ? -1 : 1; |
| 120 | 129 | } |
| 121 | 130 | |
| 131 | + /** |
|
| 132 | + * @param string $open |
|
| 133 | + * @param string $close |
|
| 134 | + */ |
|
| 122 | 135 | private function stripComments($str, $open, $close) { |
| 123 | 136 | $pos = 0; |
| 124 | 137 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -25,8 +25,11 @@ discard block |
||
| 25 | 25 | $this->file = $tss; |
| 26 | 26 | $this->rules = $this->cache->load($tss); |
| 27 | 27 | $this->filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
| 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 = $this->stripComments($tss, '//', "\n"); |
| 32 | 35 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -35,7 +38,9 @@ discard block |
||
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | public function parse($indexStart = 0) { |
| 38 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
| 41 | + if (!empty($this->rules)) { |
|
| 42 | + return $this->rules['rules']; |
|
| 43 | + } |
|
| 39 | 44 | $rules = $this->parseTokens($indexStart); |
| 40 | 45 | usort($rules, [$this, 'sortRules']); |
| 41 | 46 | $this->checkError($rules); |
@@ -47,8 +52,9 @@ discard block |
||
| 47 | 52 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
| 48 | 53 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
| 49 | 54 | $this->rules = array_merge($this->rules, $processing); |
| 55 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
| 56 | + $this->addRules($token, $indexStart); |
|
| 50 | 57 | } |
| 51 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
| 52 | 58 | } |
| 53 | 59 | return $this->rules; |
| 54 | 60 | } |
@@ -56,14 +62,18 @@ discard block |
||
| 56 | 62 | private function addRules($token, $indexStart) { |
| 57 | 63 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 58 | 64 | $this->tss->skip(count($selector)); |
| 59 | - if (count($selector) === 0) return; |
|
| 65 | + if (count($selector) === 0) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 60 | 68 | |
| 61 | 69 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
| 62 | 70 | $this->rules = $this->writeRule($this->rules, $newRules); |
| 63 | 71 | } |
| 64 | 72 | |
| 65 | 73 | private function checkError($rules) { |
| 66 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
| 74 | + if (empty($rules) && count($this->tss) > 0) { |
|
| 75 | + throw new \Exception('No TSS rules parsed'); |
|
| 76 | + } |
|
| 67 | 77 | } |
| 68 | 78 | |
| 69 | 79 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -90,7 +100,9 @@ discard block |
||
| 90 | 100 | } |
| 91 | 101 | |
| 92 | 102 | private function processingInstructions($token, $indexStart) { |
| 93 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
| 103 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 94 | 106 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
| 95 | 107 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
| 96 | 108 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -102,8 +114,11 @@ discard block |
||
| 102 | 114 | } |
| 103 | 115 | |
| 104 | 116 | private function import($args, $indexStart) { |
| 105 | - if ($this->file !== null) $fileName = $this->filePath->getFilePath($args[0]); |
|
| 106 | - else $fileName = $args[0]; |
|
| 117 | + if ($this->file !== null) { |
|
| 118 | + $fileName = $this->filePath->getFilePath($args[0]); |
|
| 119 | + } else { |
|
| 120 | + $fileName = $args[0]; |
|
| 121 | + } |
|
| 107 | 122 | |
| 108 | 123 | $this->import[] = $fileName; |
| 109 | 124 | $baseDirTemp = $this->filePath->getFilePath(); |
@@ -114,7 +129,9 @@ discard block |
||
| 114 | 129 | |
| 115 | 130 | private function sortRules($a, $b) { |
| 116 | 131 | //If they have the same depth, compare on index |
| 117 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
| 132 | + if ($a->depth === $b->depth) { |
|
| 133 | + return $a->index < $b->index ? -1 : 1; |
|
| 134 | + } |
|
| 118 | 135 | |
| 119 | 136 | return ($a->depth < $b->depth) ? -1 : 1; |
| 120 | 137 | } |
@@ -123,7 +140,9 @@ discard block |
||
| 123 | 140 | $pos = 0; |
| 124 | 141 | while (($pos = strpos($str, $open, $pos)) !== false) { |
| 125 | 142 | $end = strpos($str, $close, $pos); |
| 126 | - if ($end === false) break; |
|
| 143 | + if ($end === false) { |
|
| 144 | + break; |
|
| 145 | + } |
|
| 127 | 146 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
| 128 | 147 | } |
| 129 | 148 | |