@@ -9,7 +9,9 @@ discard block |
||
| 9 | 9 | class Nth implements \Transphporm\Pseudo { |
| 10 | 10 | |
| 11 | 11 | public function match($name, $args, \DomElement $element) { |
| 12 | - if ($name !== 'nth-child') return true; |
|
| 12 | + if ($name !== 'nth-child') { |
|
| 13 | + return true; |
|
| 14 | + } |
|
| 13 | 15 | |
| 14 | 16 | $criteria = $args[0]; |
| 15 | 17 | |
@@ -18,8 +20,11 @@ discard block |
||
| 18 | 20 | $pseudo = $tokenizer->getTokens(); |
| 19 | 21 | $num = end($pseudo)['value'][0]['value']; |
| 20 | 22 | |
| 21 | - if (is_callable([$this, $criteria])) return $this->$criteria($num); |
|
| 22 | - else return $num == $criteria; |
|
| 23 | + if (is_callable([$this, $criteria])) { |
|
| 24 | + return $this->$criteria($num); |
|
| 25 | + } else { |
|
| 26 | + return $num == $criteria; |
|
| 27 | + } |
|
| 23 | 28 | } |
| 24 | 29 | |
| 25 | 30 | private function odd($num) { |
@@ -16,7 +16,9 @@ discard block |
||
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function match($name, $args, \DomElement $element) { |
| 19 | - if ($name !== 'not') return true; |
|
| 19 | + if ($name !== 'not') { |
|
| 20 | + return true; |
|
| 21 | + } |
|
| 20 | 22 | |
| 21 | 23 | $xpath = new \DomXpath($element->ownerDocument); |
| 22 | 24 | return $this->notElement($args, $xpath, $element); |
@@ -29,7 +31,9 @@ discard block |
||
| 29 | 31 | //Find all nodes matched by the expressions in the brackets :not(EXPR) |
| 30 | 32 | foreach ($xpath->query($xpathString) as $matchedElement) { |
| 31 | 33 | //Check to see whether this node was matched by the not query |
| 32 | - if ($element->isSameNode($matchedElement)) return false; |
|
| 34 | + if ($element->isSameNode($matchedElement)) { |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 33 | 37 | } |
| 34 | 38 | } |
| 35 | 39 | return true; |
@@ -7,8 +7,12 @@ |
||
| 7 | 7 | namespace Transphporm\Property; |
| 8 | 8 | class Display implements \Transphporm\Property { |
| 9 | 9 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 10 | - if ($pseudoMatcher->hasFunction('attr')) $element->removeAttribute($pseudoMatcher->getFuncArgs('attr', $element)[0]); |
|
| 11 | - else if (strtolower($values[0]) === 'none') $element->setAttribute('transphporm', 'remove'); |
|
| 12 | - else $element->setAttribute('transphporm', 'show'); |
|
| 10 | + if ($pseudoMatcher->hasFunction('attr')) { |
|
| 11 | + $element->removeAttribute($pseudoMatcher->getFuncArgs('attr', $element)[0]); |
|
| 12 | + } else if (strtolower($values[0]) === 'none') { |
|
| 13 | + $element->setAttribute('transphporm', 'remove'); |
|
| 14 | + } else { |
|
| 15 | + $element->setAttribute('transphporm', 'show'); |
|
| 16 | + } |
|
| 13 | 17 | } |
| 14 | 18 | } |
@@ -14,7 +14,9 @@ |
||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function match($name, $args, \DomElement $element) { |
| 17 | - if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) return true; |
|
| 17 | + if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) { |
|
| 18 | + return true; |
|
| 19 | + } |
|
| 18 | 20 | return $args[0]; |
| 19 | 21 | } |
| 20 | 22 | } |
@@ -28,6 +28,9 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | + /** |
|
| 32 | + * @param \DOMElement $element |
|
| 33 | + */ |
|
| 31 | 34 | private function shouldRun($element) { |
| 32 | 35 | do { |
| 33 | 36 | if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
@@ -40,6 +43,10 @@ discard block |
||
| 40 | 43 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
| 41 | 44 | } |
| 42 | 45 | |
| 46 | + /** |
|
| 47 | + * @param \DOMElement $element |
|
| 48 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
| 49 | + */ |
|
| 43 | 50 | private function processPseudo($value, $element, $pseudoMatcher) { |
| 44 | 51 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
| 45 | 52 | foreach ($pseudoContent as $pseudo) { |
@@ -96,6 +103,9 @@ discard block |
||
| 96 | 103 | } |
| 97 | 104 | } |
| 98 | 105 | |
| 106 | + /** |
|
| 107 | + * @param \DOMElement $element |
|
| 108 | + */ |
|
| 99 | 109 | private function replaceContent($element, $content) { |
| 100 | 110 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
| 101 | 111 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
@@ -104,12 +114,18 @@ discard block |
||
| 104 | 114 | $element->setAttribute('transphporm', 'remove'); |
| 105 | 115 | } |
| 106 | 116 | |
| 117 | + /** |
|
| 118 | + * @param \DOMElement $element |
|
| 119 | + */ |
|
| 107 | 120 | private function appendContent($element, $content) { |
| 108 | 121 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
| 109 | 122 | $element->appendChild($node); |
| 110 | 123 | } |
| 111 | 124 | } |
| 112 | 125 | |
| 126 | + /** |
|
| 127 | + * @param \DOMElement $element |
|
| 128 | + */ |
|
| 113 | 129 | private function removeAllChildren($element) { |
| 114 | 130 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
| 115 | 131 | } |
@@ -16,21 +16,28 @@ discard block |
||
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 19 | - if (!$this->shouldRun($element)) return false; |
|
| 19 | + if (!$this->shouldRun($element)) { |
|
| 20 | + return false; |
|
| 21 | + } |
|
| 20 | 22 | $values = $this->formatter->format($values, $rules); |
| 21 | 23 | |
| 22 | 24 | if (!$this->processPseudo($values, $element, $pseudoMatcher)) { |
| 23 | 25 | //Remove the current contents |
| 24 | 26 | $this->removeAllChildren($element); |
| 25 | 27 | //Now make a text node |
| 26 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
|
| 27 | - else $this->appendContent($element, $values); |
|
| 28 | + if ($this->getContentMode($rules) === 'replace') { |
|
| 29 | + $this->replaceContent($element, $values); |
|
| 30 | + } else { |
|
| 31 | + $this->appendContent($element, $values); |
|
| 32 | + } |
|
| 28 | 33 | } |
| 29 | 34 | } |
| 30 | 35 | |
| 31 | 36 | private function shouldRun($element) { |
| 32 | 37 | do { |
| 33 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
| 38 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
| 39 | + return false; |
|
| 40 | + } |
|
| 34 | 41 | } |
| 35 | 42 | while (($element = $element->parentNode) instanceof \DomElement); |
| 36 | 43 | return true; |
@@ -54,16 +61,18 @@ discard block |
||
| 54 | 61 | private function getNode($node, $document) { |
| 55 | 62 | foreach ($node as $n) { |
| 56 | 63 | if (is_array($n)) { |
| 57 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
| 58 | - } |
|
| 59 | - else { |
|
| 64 | + foreach ($this->getNode($n, $document) as $new) { |
|
| 65 | + yield $new; |
|
| 66 | + } |
|
| 67 | + } else { |
|
| 60 | 68 | if ($n instanceof \DomElement) { |
| 61 | 69 | $new = $document->importNode($n, true); |
| 62 | 70 | //Removing this might cause problems with caching... |
| 63 | 71 | //$new->setAttribute('transphporm', 'added'); |
| 64 | - } |
|
| 65 | - else { |
|
| 66 | - if ($n instanceof \DomText) $n = $n->nodeValue; |
|
| 72 | + } else { |
|
| 73 | + if ($n instanceof \DomText) { |
|
| 74 | + $n = $n->nodeValue; |
|
| 75 | + } |
|
| 67 | 76 | $new = $document->createElement('text'); |
| 68 | 77 | |
| 69 | 78 | $new->appendChild($document->createTextNode($n)); |
@@ -111,6 +120,8 @@ discard block |
||
| 111 | 120 | } |
| 112 | 121 | |
| 113 | 122 | private function removeAllChildren($element) { |
| 114 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
| 123 | + while ($element->hasChildNodes()) { |
|
| 124 | + $element->removeChild($element->firstChild); |
|
| 125 | + } |
|
| 115 | 126 | } |
| 116 | 127 | } |
@@ -39,27 +39,35 @@ |
||
| 39 | 39 | $parts['name'] = $this->getFuncName($tokens); |
| 40 | 40 | if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) { |
| 41 | 41 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
| 42 | + } elseif (isset($tokens[1])) { |
|
| 43 | + $parts['args'] = $this->valueParser->parseTokens($tokens[1]['value']); |
|
| 44 | + } else { |
|
| 45 | + $parts['args'] = [['']]; |
|
| 42 | 46 | } |
| 43 | - elseif (isset($tokens[1])) $parts['args'] = $this->valueParser->parseTokens($tokens[1]['value']); |
|
| 44 | - else $parts['args'] = [['']]; |
|
| 45 | 47 | return $parts; |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | 50 | private function getFuncName($tokens) { |
| 49 | - if ($tokens[0]['type'] === Tokenizer::NAME) return $tokens[0]['value']; |
|
| 51 | + if ($tokens[0]['type'] === Tokenizer::NAME) { |
|
| 52 | + return $tokens[0]['value']; |
|
| 53 | + } |
|
| 50 | 54 | return null; |
| 51 | 55 | } |
| 52 | 56 | |
| 53 | 57 | public function hasFunction($name) { |
| 54 | 58 | foreach ($this->pseudo as $tokens) { |
| 55 | - if ($name === $this->getFuncName($tokens)) return true; |
|
| 59 | + if ($name === $this->getFuncName($tokens)) { |
|
| 60 | + return true; |
|
| 61 | + } |
|
| 56 | 62 | } |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | 65 | public function getFuncArgs($name) { |
| 60 | 66 | foreach ($this->pseudo as $tokens) { |
| 61 | 67 | $parts = $this->getFuncParts($tokens); |
| 62 | - if ($name === $parts['name']) return $parts['args']; |
|
| 68 | + if ($name === $parts['name']) { |
|
| 69 | + return $parts['args']; |
|
| 70 | + } |
|
| 63 | 71 | } |
| 64 | 72 | } |
| 65 | 73 | } |
@@ -20,12 +20,12 @@ discard block |
||
| 20 | 20 | $this->functionSet = $functionSet; |
| 21 | 21 | |
| 22 | 22 | $this->translators = [ |
| 23 | - ' ' => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
|
| 24 | - '' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
| 25 | - '>' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
| 26 | - '#' => function($string) { return '[@id=\'' . $string . '\']'; }, |
|
| 27 | - '.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
|
| 28 | - '[' => function($string) use ($hash) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . $string . '\', ., "' . $hash . '")' . ']'; }, |
|
| 23 | + ' ' => function($string) use ($prefix) { return '//'.$prefix.$string; }, |
|
| 24 | + '' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
| 25 | + '>' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
| 26 | + '#' => function($string) { return '[@id=\''.$string.'\']'; }, |
|
| 27 | + '.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' '.$string.' \')]'; }, |
|
| 28 | + '[' => function($string) use ($hash) { return '['.'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \''.$string.'\', ., "'.$hash.'")'.']'; }, |
|
| 29 | 29 | ']' => function() { return ''; } |
| 30 | 30 | ]; |
| 31 | 31 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | if (strpos($attr, $comparator) !== false) { |
| 54 | 54 | $parts = explode($comparator, $attr); |
| 55 | 55 | |
| 56 | - return $valueParser->parse('attr(' . $parts[0] . ')' . $comparator . $parts[1])[0]; |
|
| 56 | + return $valueParser->parse('attr('.$parts[0].')'.$comparator.$parts[1])[0]; |
|
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | return $element[0]->getAttribute($attr) !== ''; |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | public function getXpath($css) { |
| 80 | - $this->css = str_replace([' >', '> '],['>', '>'], trim($css)); |
|
| 80 | + $this->css = str_replace([' >', '> '], ['>', '>'], trim($css)); |
|
| 81 | 81 | $css = explode(':', $this->css)[0]; |
| 82 | 82 | $selectors = $this->split($css); |
| 83 | 83 | $xpath = '/'; |
@@ -70,8 +70,9 @@ discard block |
||
| 70 | 70 | $selector = $this->createSelector(); |
| 71 | 71 | $selector->type = $css[$i]; |
| 72 | 72 | $selectors[] = $selector; |
| 73 | + } else { |
|
| 74 | + $selector->string .= $css[$i]; |
|
| 73 | 75 | } |
| 74 | - else $selector->string .= $css[$i]; |
|
| 75 | 76 | } |
| 76 | 77 | return $selectors; |
| 77 | 78 | } |
@@ -82,7 +83,9 @@ discard block |
||
| 82 | 83 | $selectors = $this->split($css); |
| 83 | 84 | $xpath = '/'; |
| 84 | 85 | foreach ($selectors as $selector) { |
| 85 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 86 | + if (isset($this->translators[$selector->type])) { |
|
| 87 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 88 | + } |
|
| 86 | 89 | } |
| 87 | 90 | |
| 88 | 91 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -50,7 +50,9 @@ discard block |
||
| 50 | 50 | $this->data = new ValueData($data ? $data : $this->baseData); |
| 51 | 51 | $this->last = null; |
| 52 | 52 | |
| 53 | - if (empty($tokens)) return [$data]; |
|
| 53 | + if (empty($tokens)) { |
|
| 54 | + return [$data]; |
|
| 55 | + } |
|
| 54 | 56 | |
| 55 | 57 | foreach ($tokens as $token) { |
| 56 | 58 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -72,13 +74,15 @@ discard block |
||
| 72 | 74 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
| 73 | 75 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
| 74 | 76 | private function processDot($token) { |
| 75 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
| 76 | - else { |
|
| 77 | + if ($this->last !== null) { |
|
| 78 | + $this->data->traverse($this->last); |
|
| 79 | + } else { |
|
| 77 | 80 | //When . is not preceeded by anything, treat it as part of the string instead of an operator |
| 78 | 81 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
| 79 | 82 | $lastResult = $this->result->pop(); |
| 80 | - if ($lastResult) $this->data = new ValueData($lastResult); |
|
| 81 | - else { |
|
| 83 | + if ($lastResult) { |
|
| 84 | + $this->data = new ValueData($lastResult); |
|
| 85 | + } else { |
|
| 82 | 86 | $this->processString(['value' => '.']); |
| 83 | 87 | $this->result->setMode(Tokenizer::CONCAT); |
| 84 | 88 | } |
@@ -91,9 +95,10 @@ discard block |
||
| 91 | 95 | $parser = new Value($this->baseData, $this->autoLookup); |
| 92 | 96 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
| 93 | 97 | $this->callTransphpormFunctions($token); |
| 94 | - } |
|
| 95 | - else { |
|
| 96 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
| 98 | + } else { |
|
| 99 | + if ($this->last !== null) { |
|
| 100 | + $this->data->traverse($this->last); |
|
| 101 | + } |
|
| 97 | 102 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
| 98 | 103 | } |
| 99 | 104 | } |
@@ -113,8 +118,7 @@ discard block |
||
| 113 | 118 | private function processBrackets($token) { |
| 114 | 119 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
| 115 | 120 | $this->callTransphpormFunctions($token); |
| 116 | - } |
|
| 117 | - else { |
|
| 121 | + } else { |
|
| 118 | 122 | $this->processNested($token); |
| 119 | 123 | } |
| 120 | 124 | } |
@@ -131,7 +135,9 @@ discard block |
||
| 131 | 135 | foreach ($this->result->getResult() as $i => $value) { |
| 132 | 136 | if (is_scalar($value)) { |
| 133 | 137 | $val = $this->data->read($value); |
| 134 | - if ($val) $this->result[$i] = $val; |
|
| 138 | + if ($val) { |
|
| 139 | + $this->result[$i] = $val; |
|
| 140 | + } |
|
| 135 | 141 | } |
| 136 | 142 | } |
| 137 | 143 | $this->last = null; |
@@ -143,12 +149,10 @@ discard block |
||
| 143 | 149 | try { |
| 144 | 150 | $value = $this->data->extract($this->last, $this->autoLookup); |
| 145 | 151 | $this->result->processValue($value); |
| 146 | - } |
|
| 147 | - catch (\UnexpectedValueException $e) { |
|
| 152 | + } catch (\UnexpectedValueException $e) { |
|
| 148 | 153 | if (!$this->autoLookup) { |
| 149 | 154 | $this->result->processValue($this->last); |
| 150 | - } |
|
| 151 | - else { |
|
| 155 | + } else { |
|
| 152 | 156 | $this->result->clear(); |
| 153 | 157 | $this->result[0] = false; |
| 154 | 158 | } |