@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | $this->save = function($content = null) { |
38 | 38 | return $this->document->saveHtml($content); |
39 | 39 | }; |
40 | - $this->document->loadHtml('<' . '?xml encoding="UTF-8">' .$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
40 | + $this->document->loadHtml('<'.'?xml encoding="UTF-8">'.$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
41 | 41 | |
42 | 42 | if (strpos($doc, '<!') !== 0) { |
43 | 43 | $templateNode = $this->document->getElementsByTagName('template')[0]; |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | //Either return a whole DomDocument or return the output HTML |
89 | 89 | if ($document) return $this->document; |
90 | 90 | |
91 | - $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype) . "\n" : ''; |
|
91 | + $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype)."\n" : ''; |
|
92 | 92 | |
93 | 93 | if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
94 | 94 | else $output = $this->printDocument(); |
@@ -66,7 +66,9 @@ discard block |
||
66 | 66 | /** Loops through all assigned hooks, runs the Xpath query and calls the hook */ |
67 | 67 | private function processHooks() { |
68 | 68 | foreach ($this->hooks as list($query, $hook)) { |
69 | - foreach ($this->xpath->query($query) as $element) $hook->run($element); |
|
69 | + foreach ($this->xpath->query($query) as $element) { |
|
70 | + $hook->run($element); |
|
71 | + } |
|
70 | 72 | } |
71 | 73 | $this->hooks = []; |
72 | 74 | } |
@@ -74,7 +76,9 @@ discard block |
||
74 | 76 | /** Prints out the current DomDocument as HTML */ |
75 | 77 | private function printDocument() { |
76 | 78 | $output = ''; |
77 | - foreach ($this->document->documentElement->childNodes as $node) $output .= call_user_func($this->save, $node); |
|
79 | + foreach ($this->document->documentElement->childNodes as $node) { |
|
80 | + $output .= call_user_func($this->save, $node); |
|
81 | + } |
|
78 | 82 | return $output; |
79 | 83 | } |
80 | 84 | |
@@ -86,12 +90,17 @@ discard block |
||
86 | 90 | //Generate the document by taking only the childnodes of the template, ignoring the <template> and </template> tags |
87 | 91 | //TODO: Is there a faster way of doing this without string manipulation on the output or this loop through childnodes? |
88 | 92 | //Either return a whole DomDocument or return the output HTML |
89 | - if ($document) return $this->document; |
|
93 | + if ($document) { |
|
94 | + return $this->document; |
|
95 | + } |
|
90 | 96 | |
91 | 97 | $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype) . "\n" : ''; |
92 | 98 | |
93 | - if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
|
94 | - else $output = $this->printDocument(); |
|
99 | + if ($this->document->documentElement->tagName !== 'template') { |
|
100 | + $output .= call_user_func($this->save, $this->document->documentElement); |
|
101 | + } else { |
|
102 | + $output = $this->printDocument(); |
|
103 | + } |
|
95 | 104 | |
96 | 105 | //repair empty tags. Browsers break on <script /> and <div /> so can't avoid LIBXML_NOEMPTYTAG but they also break on <base></base> so repair them |
97 | 106 | $output = str_replace(['></img>', '></br>', '></meta>', '></base>', '></link>', '></hr>', '></input>'], ' />', $output); |
@@ -11,25 +11,33 @@ |
||
11 | 11 | private $lastParentNode; |
12 | 12 | |
13 | 13 | public function match($name, $args, \DomElement $element) { |
14 | - if ($element->parentNode !== $this->lastParentNode) $this->count = 0; |
|
14 | + if ($element->parentNode !== $this->lastParentNode) { |
|
15 | + $this->count = 0; |
|
16 | + } |
|
15 | 17 | |
16 | 18 | $this->lastParentNode = $element->parentNode; |
17 | 19 | |
18 | 20 | |
19 | 21 | |
20 | - if ($name !== 'nth-child') return true; |
|
22 | + if ($name !== 'nth-child') { |
|
23 | + return true; |
|
24 | + } |
|
21 | 25 | |
22 | 26 | $this->count++; |
23 | 27 | $criteria = $args[0]; |
24 | 28 | |
25 | - if (is_callable([$this, $criteria])) return $this->$criteria($this->count); |
|
29 | + if (is_callable([$this, $criteria])) { |
|
30 | + return $this->$criteria($this->count); |
|
31 | + } |
|
26 | 32 | $this->assert(is_numeric($criteria), "Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
27 | 33 | return $this->count == $criteria; |
28 | 34 | } |
29 | 35 | |
30 | 36 | //TODO: Abstract assertions throughout |
31 | 37 | private function assert($condition, $error) { |
32 | - if (!$condition) throw new \Exception($error); |
|
38 | + if (!$condition) { |
|
39 | + throw new \Exception($error); |
|
40 | + } |
|
33 | 41 | } |
34 | 42 | |
35 | 43 | private function odd($num) { |
@@ -94,7 +94,7 @@ |
||
94 | 94 | if ($lastResult) { |
95 | 95 | $this->traversing = true; |
96 | 96 | } |
97 | - else if ($this->last === null) { |
|
97 | + else if ($this->last === null) { |
|
98 | 98 | $this->processString(['value' => '.']); |
99 | 99 | $this->result->setMode(Tokenizer::CONCAT); |
100 | 100 | } |
@@ -56,7 +56,9 @@ discard block |
||
56 | 56 | $this->last = null; |
57 | 57 | $this->traversing = false; |
58 | 58 | |
59 | - if (count($tokens) <= 0) return [$data]; |
|
59 | + if (count($tokens) <= 0) { |
|
60 | + return [$data]; |
|
61 | + } |
|
60 | 62 | |
61 | 63 | foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { |
62 | 64 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -84,8 +86,7 @@ discard block |
||
84 | 86 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
85 | 87 | if ($lastResult) { |
86 | 88 | $this->traversing = true; |
87 | - } |
|
88 | - else if ($this->last === null) { |
|
89 | + } else if ($this->last === null) { |
|
89 | 90 | $this->processString(['value' => '.']); |
90 | 91 | $this->result->setMode(Tokenizer::CONCAT); |
91 | 92 | } |
@@ -101,11 +102,12 @@ discard block |
||
101 | 102 | $parser = new Value($this->baseData, $this->autoLookup); |
102 | 103 | if ($this->hasFunction($this->last)) { |
103 | 104 | $this->callTransphpormFunctions($token); |
104 | - } |
|
105 | - else { |
|
105 | + } else { |
|
106 | 106 | $this->data->traverse($this->last, $this->result); |
107 | 107 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
108 | - if (!is_bool($this->last)) $this->traversing = true; |
|
108 | + if (!is_bool($this->last)) { |
|
109 | + $this->traversing = true; |
|
110 | + } |
|
109 | 111 | } |
110 | 112 | } |
111 | 113 | |
@@ -125,8 +127,7 @@ discard block |
||
125 | 127 | if ($this->hasFunction($this->last) |
126 | 128 | && !$this->data->methodExists($this->last)) { |
127 | 129 | $this->callTransphpormFunctions($token); |
128 | - } |
|
129 | - else { |
|
130 | + } else { |
|
130 | 131 | $this->processNested($token); |
131 | 132 | } |
132 | 133 | } |
@@ -156,8 +157,7 @@ discard block |
||
156 | 157 | try { |
157 | 158 | $value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
158 | 159 | $this->result->processValue($value); |
159 | - } |
|
160 | - catch (\UnexpectedValueException $e) { |
|
160 | + } catch (\UnexpectedValueException $e) { |
|
161 | 161 | $this->processLastUnexpected(); |
162 | 162 | } |
163 | 163 | } |
@@ -166,8 +166,7 @@ discard block |
||
166 | 166 | private function processLastUnexpected() { |
167 | 167 | if (!($this->autoLookup || $this->traversing)) { |
168 | 168 | $this->result->processValue($this->last); |
169 | - } |
|
170 | - else { |
|
169 | + } else { |
|
171 | 170 | $this->result->clear(); |
172 | 171 | $this->result->write(0, false); |
173 | 172 | } |
@@ -25,7 +25,9 @@ discard block |
||
25 | 25 | foreach ($this->pseudo as $tokens) { |
26 | 26 | foreach ($this->functions as $function) { |
27 | 27 | $matches = $this->match($tokens, $function, $element); |
28 | - if ($matches === false) return false; |
|
28 | + if ($matches === false) { |
|
29 | + return false; |
|
30 | + } |
|
29 | 31 | } |
30 | 32 | } |
31 | 33 | return true; |
@@ -35,9 +37,10 @@ discard block |
||
35 | 37 | try { |
36 | 38 | $parts = $this->getFuncParts($tokens); |
37 | 39 | $matches = $function->match($parts['name'], $parts['args'], $element); |
38 | - if ($matches === false) return false; |
|
39 | - } |
|
40 | - catch (\Exception $e) { |
|
40 | + if ($matches === false) { |
|
41 | + return false; |
|
42 | + } |
|
43 | + } catch (\Exception $e) { |
|
41 | 44 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
42 | 45 | } |
43 | 46 | } |
@@ -46,31 +49,37 @@ discard block |
||
46 | 49 | $parts['name'] = $this->getFuncName($tokens); |
47 | 50 | if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) { |
48 | 51 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
49 | - } |
|
50 | - else if (count($tokens) > 1) { |
|
52 | + } else if (count($tokens) > 1) { |
|
51 | 53 | $tokens->rewind(); |
52 | 54 | $tokens->next(); |
53 | 55 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
56 | + } else { |
|
57 | + $parts['args'] = [['']]; |
|
54 | 58 | } |
55 | - else $parts['args'] = [['']]; |
|
56 | 59 | return $parts; |
57 | 60 | } |
58 | 61 | |
59 | 62 | private function getFuncName($tokens) { |
60 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
63 | + if ($tokens->type() === Tokenizer::NAME) { |
|
64 | + return $tokens->read(); |
|
65 | + } |
|
61 | 66 | return null; |
62 | 67 | } |
63 | 68 | |
64 | 69 | public function hasFunction($name) { |
65 | 70 | foreach ($this->pseudo as $tokens) { |
66 | - if ($name === $this->getFuncName($tokens)) return true; |
|
71 | + if ($name === $this->getFuncName($tokens)) { |
|
72 | + return true; |
|
73 | + } |
|
67 | 74 | } |
68 | 75 | } |
69 | 76 | |
70 | 77 | public function getFuncArgs($name) { |
71 | 78 | foreach ($this->pseudo as $tokens) { |
72 | 79 | $parts = $this->getFuncParts($tokens); |
73 | - if ($name === $parts['name']) return $parts['args']; |
|
80 | + if ($name === $parts['name']) { |
|
81 | + return $parts['args']; |
|
82 | + } |
|
74 | 83 | } |
75 | 84 | } |
76 | 85 | } |
@@ -11,6 +11,9 @@ discard block |
||
11 | 11 | private $line; |
12 | 12 | private $filePath; |
13 | 13 | |
14 | + /** |
|
15 | + * @param integer $line |
|
16 | + */ |
|
14 | 17 | public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) { |
15 | 18 | $this->functionSet = $functionSet; |
16 | 19 | $this->elementData = $elementData; |
@@ -58,6 +61,12 @@ discard block |
||
58 | 61 | return $value; |
59 | 62 | } |
60 | 63 | |
64 | + /** |
|
65 | + * @param \DOMElement $element |
|
66 | + * @param integer $count |
|
67 | + * |
|
68 | + * @return \DOMElement |
|
69 | + */ |
|
61 | 70 | private function cloneElement($element, $iteration, $key, $count) { |
62 | 71 | $clone = $element->cloneNode(true); |
63 | 72 | $this->tagElement($clone, $count); |
@@ -77,6 +86,9 @@ discard block |
||
77 | 86 | return isset($values[1]) ? $values[1] : PHP_INT_MAX; |
78 | 87 | } |
79 | 88 | |
89 | + /** |
|
90 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
91 | + */ |
|
80 | 92 | private function createHook($newRules, $pseudoMatcher, $properties) { |
81 | 93 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath); |
82 | 94 | foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
@@ -9,13 +9,13 @@ |
||
9 | 9 | private $functionSet; |
10 | 10 | private $elementData; |
11 | 11 | private $line; |
12 | - private $filePath; |
|
12 | + private $filePath; |
|
13 | 13 | |
14 | 14 | public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) { |
15 | 15 | $this->functionSet = $functionSet; |
16 | 16 | $this->elementData = $elementData; |
17 | 17 | $this->line = &$line; |
18 | - $this->filePath = $filePath; |
|
18 | + $this->filePath = $filePath; |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
@@ -20,7 +20,9 @@ discard block |
||
20 | 20 | |
21 | 21 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
22 | 22 | $values = $this->fixEmpty($values); |
23 | - if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
|
23 | + if ($element->getAttribute('transphporm') === 'added') { |
|
24 | + return $element->parentNode->removeChild($element); |
|
25 | + } |
|
24 | 26 | $max = $this->getMax($values); |
25 | 27 | $count = 0; |
26 | 28 | $repeat = $this->getRepeatValue($values, $max); |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | $hook = $this->createHook($rules, $pseudoMatcher, $properties); |
30 | 32 | |
31 | 33 | foreach ($repeat as $key => $iteration) { |
32 | - if ($count+1 > $max) break; |
|
34 | + if ($count+1 > $max) { |
|
35 | + break; |
|
36 | + } |
|
33 | 37 | $clone = $this->cloneElement($element, $iteration, $key, $count++); |
34 | 38 | //Re-run the hook on the new element, but use the iterated data |
35 | 39 | $hook->run($clone); |
@@ -41,8 +45,9 @@ discard block |
||
41 | 45 | |
42 | 46 | private function getRepeatValue($values, &$max) { |
43 | 47 | $mode = $this->getMode($values); |
44 | - if ($mode === 'each') $repeat = $values[0]; |
|
45 | - else if ($mode === 'loop') { |
|
48 | + if ($mode === 'each') { |
|
49 | + $repeat = $values[0]; |
|
50 | + } else if ($mode === 'loop') { |
|
46 | 51 | $repeat = range($values[0], $max); |
47 | 52 | $max++; |
48 | 53 | } |
@@ -54,7 +59,9 @@ discard block |
||
54 | 59 | } |
55 | 60 | |
56 | 61 | private function fixEmpty($value) { |
57 | - if (empty($value[0])) $value[0] = []; |
|
62 | + if (empty($value[0])) { |
|
63 | + $value[0] = []; |
|
64 | + } |
|
58 | 65 | return $value; |
59 | 66 | } |
60 | 67 | |
@@ -70,7 +77,9 @@ discard block |
||
70 | 77 | |
71 | 78 | private function tagElement($element, $count) { |
72 | 79 | //Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
73 | - if ($count > 0) $element->setAttribute('transphporm', 'added'); |
|
80 | + if ($count > 0) { |
|
81 | + $element->setAttribute('transphporm', 'added'); |
|
82 | + } |
|
74 | 83 | } |
75 | 84 | |
76 | 85 | private function getMax($values) { |
@@ -79,7 +88,9 @@ discard block |
||
79 | 88 | |
80 | 89 | private function createHook($newRules, $pseudoMatcher, $properties) { |
81 | 90 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath); |
82 | - foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
|
91 | + foreach ($properties as $name => $property) { |
|
92 | + $hook->registerProperty($name, $property); |
|
93 | + } |
|
83 | 94 | return $hook; |
84 | 95 | } |
85 | 96 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | private $functionSet; |
18 | 18 | |
19 | 19 | public function __construct(array $rules, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, |
20 | - \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) { |
|
20 | + \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) { |
|
21 | 21 | $this->rules = $rules; |
22 | 22 | $this->configLine = &$configLine; |
23 | 23 | $this->file = $file; |
@@ -25,7 +25,7 @@ |
||
25 | 25 | $this->valueParser = $valueParser; |
26 | 26 | $this->pseudoMatcher = $pseudoMatcher; |
27 | 27 | $this->functionSet = $functionSet; |
28 | - if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
28 | + if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)).DIRECTORY_SEPARATOR); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public function run(\DomElement $element) { |
@@ -25,7 +25,9 @@ discard block |
||
25 | 25 | $this->valueParser = $valueParser; |
26 | 26 | $this->pseudoMatcher = $pseudoMatcher; |
27 | 27 | $this->functionSet = $functionSet; |
28 | - if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
28 | + if ($this->file !== null) { |
|
29 | + $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
30 | + } |
|
29 | 31 | } |
30 | 32 | |
31 | 33 | public function run(\DomElement $element) { |
@@ -33,10 +35,11 @@ discard block |
||
33 | 35 | $this->configLine = $this->line; |
34 | 36 | try { |
35 | 37 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
36 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
38 | + if (!$this->pseudoMatcher->matches($element)) { |
|
39 | + return; |
|
40 | + } |
|
37 | 41 | $this->callProperties($element); |
38 | - } |
|
39 | - catch (\Transphporm\RunException $e) { |
|
42 | + } catch (\Transphporm\RunException $e) { |
|
40 | 43 | throw new \Transphporm\Exception($e, $this->file, $this->line); |
41 | 44 | } |
42 | 45 | } |
@@ -47,7 +50,9 @@ discard block |
||
47 | 50 | private function callProperties($element) { |
48 | 51 | foreach ($this->rules as $name => $value) { |
49 | 52 | $result = $this->callProperty($name, $element, $this->getArgs($value)); |
50 | - if ($result === false) break; |
|
53 | + if ($result === false) { |
|
54 | + break; |
|
55 | + } |
|
51 | 56 | } |
52 | 57 | } |
53 | 58 | private function getArgs($value) { |
@@ -62,9 +67,10 @@ discard block |
||
62 | 67 | if (isset($this->properties[$name])) { |
63 | 68 | try { |
64 | 69 | return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
65 | - } |
|
66 | - catch (\Exception $e) { |
|
67 | - if ($e instanceof \Transphporm\RunException) throw $e; |
|
70 | + } catch (\Exception $e) { |
|
71 | + if ($e instanceof \Transphporm\RunException) { |
|
72 | + throw $e; |
|
73 | + } |
|
68 | 74 | throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e); |
69 | 75 | } |
70 | 76 | } |
@@ -28,8 +28,9 @@ discard block |
||
28 | 28 | |
29 | 29 | |
30 | 30 | public function traverse() { |
31 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
32 | - else { |
|
31 | + if ($this->last !== null) { |
|
32 | + $this->data->traverse($this->last); |
|
33 | + } else { |
|
33 | 34 | $lastResult = $this->result->pop(); |
34 | 35 | if ($lastResult) { |
35 | 36 | $this->data = new ValueData($lastResult); |
@@ -67,8 +68,7 @@ discard block |
||
67 | 68 | try { |
68 | 69 | $value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
69 | 70 | $this->result->processValue($value); |
70 | - } |
|
71 | - catch (\UnexpectedValueException $e) { |
|
71 | + } catch (\UnexpectedValueException $e) { |
|
72 | 72 | $this->processLastUnexpected(); |
73 | 73 | } |
74 | 74 | } |
@@ -77,8 +77,7 @@ discard block |
||
77 | 77 | private function processLastUnexpected() { |
78 | 78 | if (!($this->autoLookup || $this->traversing)) { |
79 | 79 | $this->result->processValue($this->last); |
80 | - } |
|
81 | - else { |
|
80 | + } else { |
|
82 | 81 | $this->result->clear(); |
83 | 82 | $this->result[0] = false; |
84 | 83 | } |