@@ -26,11 +26,12 @@ |
||
| 26 | 26 | private function getArgs0($name, $args) { |
| 27 | 27 | if (isset($this->functions[$name]) && !($this->functions[$name] instanceof TSSFunction\Data)) { |
| 28 | 28 | $tokens = $args[0]; |
| 29 | - if ($tokens->count() == 0) return []; |
|
| 29 | + if ($tokens->count() == 0) { |
|
| 30 | + return []; |
|
| 31 | + } |
|
| 30 | 32 | $parser = new \Transphporm\Parser\Value($this); |
| 31 | 33 | return $parser->parseTokens($tokens, $this->elementData->getData($this->element)); |
| 32 | - } |
|
| 33 | - else { |
|
| 34 | + } else { |
|
| 34 | 35 | return iterator_to_array($args[0]); |
| 35 | 36 | } |
| 36 | 37 | |
@@ -85,6 +85,10 @@ |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param Template $template |
|
| 91 | + */ |
|
| 88 | 92 | private function doPostProcessing($template) { |
| 89 | 93 | $template->addHook('//*[@transphporm]', new Hook\PostProcess()); |
| 90 | 94 | return $template; |
@@ -27,7 +27,9 @@ discard block |
||
| 27 | 27 | $this->cache = new Cache(new \ArrayObject()); |
| 28 | 28 | $this->filePath = new FilePath(); |
| 29 | 29 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
| 30 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
| 30 | + foreach ($modules as $module) { |
|
| 31 | + $this->loadModule(new $module); |
|
| 32 | + } |
|
| 31 | 33 | } |
| 32 | 34 | |
| 33 | 35 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -80,7 +82,9 @@ discard block |
||
| 80 | 82 | $valueParser = new Parser\Value($functionSet); |
| 81 | 83 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
| 82 | 84 | |
| 83 | - foreach ($this->modules as $module) $module->load($this->config); |
|
| 85 | + foreach ($this->modules as $module) { |
|
| 86 | + $module->load($this->config); |
|
| 87 | + } |
|
| 84 | 88 | return $template; |
| 85 | 89 | } |
| 86 | 90 | |
@@ -94,7 +98,9 @@ discard block |
||
| 94 | 98 | //Load a template, firstly check if it's a file or a valid string |
| 95 | 99 | private function loadTemplate() { |
| 96 | 100 | $result = ['cache' => $this->template, 'headers' => []]; |
| 97 | - if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
|
| 101 | + if (strpos($this->template, "\n") === false && is_file($this->template)) { |
|
| 102 | + $result = $this->loadTemplateFromFile($this->template); |
|
| 103 | + } |
|
| 98 | 104 | return $result; |
| 99 | 105 | } |
| 100 | 106 | |
@@ -113,6 +119,8 @@ discard block |
||
| 113 | 119 | |
| 114 | 120 | public function __destruct() { |
| 115 | 121 | //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks |
| 116 | - if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
|
| 122 | + if (isset($this->config)) { |
|
| 123 | + $this->config->getCssToXpath()->cleanup(); |
|
| 124 | + } |
|
| 117 | 125 | } |
| 118 | 126 | } |
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function setLocale($locale) { |
| 45 | - $format = new \Transphporm\Module\Format($locale); |
|
| 46 | - $this->modules[get_class($format)] = $format; |
|
| 47 | - } |
|
| 45 | + $format = new \Transphporm\Module\Format($locale); |
|
| 46 | + $this->modules[get_class($format)] = $format; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | public function addPath($dir) { |
| 50 | 50 | $this->filePath->addPath($dir); |
@@ -97,15 +97,15 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | //Load a template, firstly check if it's a file or a valid string |
| 99 | 99 | private function loadTemplate() { |
| 100 | - $result = ['cache' => $this->template, 'headers' => []]; |
|
| 100 | + $result = ['cache' => $this->template, 'headers' => []]; |
|
| 101 | 101 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
| 102 | 102 | return $result; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - private function loadTemplateFromFile($file) { |
|
| 106 | - $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 107 | - return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
| 108 | - } |
|
| 105 | + private function loadTemplateFromFile($file) { |
|
| 106 | + $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 107 | + return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | public function setCache(\ArrayAccess $cache) { |
| 111 | 111 | $this->cache = new Cache($cache); |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | 'body' => $this->doPostProcessing($template)->output($document) |
| 70 | 70 | ]; |
| 71 | 71 | |
| 72 | - $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); |
|
| 72 | + $this->cache->write($tssCache->getCacheKey($data).$this->template, $result); |
|
| 73 | 73 | } |
| 74 | 74 | unset($result['cache'], $result['renderTime']); |
| 75 | 75 | return (object) $result; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
| 80 | 80 | $functionSet = new FunctionSet($elementData); |
| 81 | 81 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
| 82 | - $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); |
|
| 82 | + $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>'); |
|
| 83 | 83 | |
| 84 | 84 | $valueParser = new Parser\Value($functionSet); |
| 85 | 85 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | private function loadTemplateFromFile($file) { |
| 106 | - $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 106 | + $xml = $this->cache->load($this->cacheKey.$this->template, filemtime($this->template)); |
|
| 107 | 107 | return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -15,6 +15,9 @@ discard block |
||
| 15 | 15 | private $rules; |
| 16 | 16 | private $cacheName; |
| 17 | 17 | |
| 18 | + /** |
|
| 19 | + * @param string $tss |
|
| 20 | + */ |
|
| 18 | 21 | public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
| 19 | 22 | $this->cache = $cache; |
| 20 | 23 | $this->filePath = $filePath; |
@@ -99,6 +102,9 @@ discard block |
||
| 99 | 102 | return $rules; |
| 100 | 103 | } |
| 101 | 104 | |
| 105 | + /** |
|
| 106 | + * @param Template $template |
|
| 107 | + */ |
|
| 102 | 108 | public function processRules($template, \Transphporm\Config $config) { |
| 103 | 109 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
| 104 | 110 | |
@@ -113,6 +119,11 @@ discard block |
||
| 113 | 119 | } |
| 114 | 120 | |
| 115 | 121 | //Load the TSS |
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param Parser\CssToXpath $cssToXpath |
|
| 125 | + * @param Parser\Value $valueParser |
|
| 126 | + */ |
|
| 116 | 127 | public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) { |
| 117 | 128 | if (is_file($tss)) { |
| 118 | 129 | //$rules = $this->cache->load($tss); |
@@ -124,6 +135,10 @@ discard block |
||
| 124 | 135 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
| 125 | 136 | } |
| 126 | 137 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param Config $config |
|
| 141 | + */ |
|
| 127 | 142 | private function executeTssRule($rule, $template, $config) { |
| 128 | 143 | $rule->touch(); |
| 129 | 144 | |
@@ -7,21 +7,21 @@ discard block |
||
| 7 | 7 | namespace Transphporm; |
| 8 | 8 | //Separates out TSS file loading/caching from parsing |
| 9 | 9 | class SheetLoader { |
| 10 | - private $cache; |
|
| 11 | - private $sheet; |
|
| 12 | - private $time; |
|
| 13 | - private $import = []; |
|
| 14 | - private $cacheKey; |
|
| 15 | - private $rules; |
|
| 16 | - private $cacheName; |
|
| 17 | - |
|
| 18 | - public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
| 19 | - $this->cache = $cache; |
|
| 20 | - $this->filePath = $filePath; |
|
| 21 | - $this->tss = $tss; |
|
| 22 | - $this->time = $time ?? time(); |
|
| 23 | - $this->cacheName = $tss; |
|
| 24 | - } |
|
| 10 | + private $cache; |
|
| 11 | + private $sheet; |
|
| 12 | + private $time; |
|
| 13 | + private $import = []; |
|
| 14 | + private $cacheKey; |
|
| 15 | + private $rules; |
|
| 16 | + private $cacheName; |
|
| 17 | + |
|
| 18 | + public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
| 19 | + $this->cache = $cache; |
|
| 20 | + $this->filePath = $filePath; |
|
| 21 | + $this->tss = $tss; |
|
| 22 | + $this->time = $time ?? time(); |
|
| 23 | + $this->cacheName = $tss; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | 26 | private function getRulesFromCache($file) { |
| 27 | 27 | //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | //write the sheet to cache |
| 93 | - public function write($file, $rules, $imports = []) { |
|
| 93 | + public function write($file, $rules, $imports = []) { |
|
| 94 | 94 | if (is_file($file)) { |
| 95 | 95 | $existing = $this->cache->load($file, filemtime($file)); |
| 96 | 96 | if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
| 97 | 97 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
| 98 | 98 | } |
| 99 | 99 | return $rules; |
| 100 | - } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | public function processRules($template, \Transphporm\Config $config) { |
| 103 | 103 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -115,12 +115,12 @@ discard block |
||
| 115 | 115 | //Load the TSS |
| 116 | 116 | public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) { |
| 117 | 117 | if (is_file($tss)) { |
| 118 | - //$rules = $this->cache->load($tss); |
|
| 119 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 118 | + //$rules = $this->cache->load($tss); |
|
| 119 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 120 | 120 | $this->filePath->addPath(dirname(realpath($tss))); |
| 121 | 121 | if (empty($rules)) $tss = file_get_contents($tss); |
| 122 | 122 | else return $rules; |
| 123 | - } |
|
| 123 | + } |
|
| 124 | 124 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
| 125 | 125 | } |
| 126 | 126 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | public function updateRequired($data) { |
| 47 | 47 | if (!is_file($this->tss)) return true; |
| 48 | 48 | |
| 49 | - $this->cacheName = $this->getCacheKey($data) . $this->tss; |
|
| 49 | + $this->cacheName = $this->getCacheKey($data).$this->tss; |
|
| 50 | 50 | |
| 51 | 51 | $rules = $this->getRulesFromCache($this->tss, $data); |
| 52 | 52 | //Nothing was cached or the TSS file has changed, update is required |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | if (is_file($this->tss)) $this->getRulesFromCache($this->tss); |
| 83 | 83 | if ($this->cacheKey) { |
| 84 | 84 | $parser = new Parser\Value($data); |
| 85 | - $x= $parser->parseTokens($this->cacheKey)[0]; |
|
| 86 | - $this->cacheName = $x . $this->tss; |
|
| 85 | + $x = $parser->parseTokens($this->cacheKey)[0]; |
|
| 86 | + $this->cacheName = $x.$this->tss; |
|
| 87 | 87 | return $x; |
| 88 | 88 | } |
| 89 | 89 | else return ''; |
@@ -146,6 +146,6 @@ discard block |
||
| 146 | 146 | |
| 147 | 147 | |
| 148 | 148 | private function sortPseudo($a, $b) { |
| 149 | - return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
| 149 | + return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
| 150 | 150 | } |
| 151 | 151 | } |
@@ -32,7 +32,9 @@ discard block |
||
| 32 | 32 | if ($rules) { |
| 33 | 33 | foreach ($rules['import'] as $file) { |
| 34 | 34 | //Check that the import file hasn't been changed since the cache was written |
| 35 | - if (filemtime($file) > $rules['ctime']) return false; |
|
| 35 | + if (filemtime($file) > $rules['ctime']) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 36 | 38 | } |
| 37 | 39 | } |
| 38 | 40 | |
@@ -44,16 +46,22 @@ discard block |
||
| 44 | 46 | // 2. If the data hasn't changed since the last run |
| 45 | 47 | //If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code |
| 46 | 48 | public function updateRequired($data) { |
| 47 | - if (!is_file($this->tss)) return true; |
|
| 49 | + if (!is_file($this->tss)) { |
|
| 50 | + return true; |
|
| 51 | + } |
|
| 48 | 52 | |
| 49 | 53 | $this->cacheName = $this->getCacheKey($data) . $this->tss; |
| 50 | 54 | |
| 51 | 55 | $rules = $this->getRulesFromCache($this->tss, $data); |
| 52 | 56 | //Nothing was cached or the TSS file has changed, update is required |
| 53 | - if (empty($rules)) return true; |
|
| 57 | + if (empty($rules)) { |
|
| 58 | + return true; |
|
| 59 | + } |
|
| 54 | 60 | |
| 55 | 61 | //Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required |
| 56 | - if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true; |
|
| 62 | + if ($rules['ctime']+$rules['minFreq'] <= $this->time) { |
|
| 63 | + return true; |
|
| 64 | + } |
|
| 57 | 65 | |
| 58 | 66 | return false; |
| 59 | 67 | } |
@@ -64,7 +72,9 @@ discard block |
||
| 64 | 72 | |
| 65 | 73 | foreach ($rules as $rule) { |
| 66 | 74 | $ruleFreq = $rule->getUpdateFrequency(); |
| 67 | - if ($ruleFreq < $min) $min = $ruleFreq; |
|
| 75 | + if ($ruleFreq < $min) { |
|
| 76 | + $min = $ruleFreq; |
|
| 77 | + } |
|
| 68 | 78 | } |
| 69 | 79 | |
| 70 | 80 | return $min; |
@@ -79,21 +89,26 @@ discard block |
||
| 79 | 89 | } |
| 80 | 90 | |
| 81 | 91 | public function getCacheKey($data) { |
| 82 | - if (is_file($this->tss)) $this->getRulesFromCache($this->tss); |
|
| 92 | + if (is_file($this->tss)) { |
|
| 93 | + $this->getRulesFromCache($this->tss); |
|
| 94 | + } |
|
| 83 | 95 | if ($this->cacheKey) { |
| 84 | 96 | $parser = new Parser\Value($data); |
| 85 | 97 | $x= $parser->parseTokens($this->cacheKey)[0]; |
| 86 | 98 | $this->cacheName = $x . $this->tss; |
| 87 | 99 | return $x; |
| 100 | + } else { |
|
| 101 | + return ''; |
|
| 88 | 102 | } |
| 89 | - else return ''; |
|
| 90 | 103 | } |
| 91 | 104 | |
| 92 | 105 | //write the sheet to cache |
| 93 | 106 | public function write($file, $rules, $imports = []) { |
| 94 | 107 | if (is_file($file)) { |
| 95 | 108 | $existing = $this->cache->load($file, filemtime($file)); |
| 96 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
| 109 | + if (isset($existing['import']) && empty($imports)) { |
|
| 110 | + $imports = $existing['import']; |
|
| 111 | + } |
|
| 97 | 112 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
| 98 | 113 | } |
| 99 | 114 | return $rules; |
@@ -106,10 +121,14 @@ discard block |
||
| 106 | 121 | usort($rules, [$this, 'sortRules']); |
| 107 | 122 | |
| 108 | 123 | foreach ($rules as $rule) { |
| 109 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
| 124 | + if ($rule->shouldRun($this->time)) { |
|
| 125 | + $this->executeTssRule($rule, $template, $config); |
|
| 126 | + } |
|
| 110 | 127 | } |
| 111 | 128 | |
| 112 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
| 129 | + if (is_file($this->tss)) { |
|
| 130 | + $this->write($this->tss, $rules, $this->import); |
|
| 131 | + } |
|
| 113 | 132 | } |
| 114 | 133 | |
| 115 | 134 | //Load the TSS |
@@ -118,8 +137,11 @@ discard block |
||
| 118 | 137 | //$rules = $this->cache->load($tss); |
| 119 | 138 | $rules = $this->getRulesFromCache($tss)['rules']; |
| 120 | 139 | $this->filePath->addPath(dirname(realpath($tss))); |
| 121 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
| 122 | - else return $rules; |
|
| 140 | + if (empty($rules)) { |
|
| 141 | + $tss = file_get_contents($tss); |
|
| 142 | + } else { |
|
| 143 | + return $rules; |
|
| 144 | + } |
|
| 123 | 145 | } |
| 124 | 146 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
| 125 | 147 | } |
@@ -136,10 +158,15 @@ discard block |
||
| 136 | 158 | |
| 137 | 159 | private function sortRules($a, $b) { |
| 138 | 160 | //If they have the same depth, compare on index |
| 139 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
| 161 | + if ($a->query === $b->query) { |
|
| 162 | + return $this->sortPseudo($a, $b); |
|
| 163 | + } |
|
| 140 | 164 | |
| 141 | - if ($a->depth === $b->depth) $property = 'index'; |
|
| 142 | - else $property = 'depth'; |
|
| 165 | + if ($a->depth === $b->depth) { |
|
| 166 | + $property = 'index'; |
|
| 167 | + } else { |
|
| 168 | + $property = 'depth'; |
|
| 169 | + } |
|
| 143 | 170 | |
| 144 | 171 | return ($a->$property < $b->$property) ? -1 : 1; |
| 145 | 172 | } |
@@ -14,7 +14,9 @@ discard block |
||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 17 | - if (!$this->shouldRun($element)) return false; |
|
| 17 | + if (!$this->shouldRun($element)) { |
|
| 18 | + return false; |
|
| 19 | + } |
|
| 18 | 20 | |
| 19 | 21 | $values = $this->formatter->format($values, $rules); |
| 20 | 22 | |
@@ -25,14 +27,17 @@ discard block |
||
| 25 | 27 | if ($this->getContentMode($rules) === 'replace') { |
| 26 | 28 | $contentReplace = new ContentReplace($this); |
| 27 | 29 | $contentReplace->replaceContent($element, $values); |
| 30 | + } else { |
|
| 31 | + $this->appendContent($element, $values); |
|
| 28 | 32 | } |
| 29 | - else $this->appendContent($element, $values); |
|
| 30 | 33 | } |
| 31 | 34 | } |
| 32 | 35 | |
| 33 | 36 | private function shouldRun($element) { |
| 34 | 37 | do { |
| 35 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
| 38 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
| 39 | + return false; |
|
| 40 | + } |
|
| 36 | 41 | } |
| 37 | 42 | while (($element = $element->parentNode) instanceof \DomElement); |
| 38 | 43 | return true; |
@@ -59,9 +64,10 @@ discard block |
||
| 59 | 64 | public function getNode($node, $document) { |
| 60 | 65 | foreach ($node as $n) { |
| 61 | 66 | if (is_array($n)) { |
| 62 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
| 63 | - } |
|
| 64 | - else { |
|
| 67 | + foreach ($this->getNode($n, $document) as $new) { |
|
| 68 | + yield $new; |
|
| 69 | + } |
|
| 70 | + } else { |
|
| 65 | 71 | yield $this->convertNode($n, $document); |
| 66 | 72 | } |
| 67 | 73 | } |
@@ -70,9 +76,10 @@ discard block |
||
| 70 | 76 | private function convertNode($node, $document) { |
| 71 | 77 | if ($node instanceof \DomElement || $node instanceof \DOMComment) { |
| 72 | 78 | $new = $document->importNode($node, true); |
| 73 | - } |
|
| 74 | - else { |
|
| 75 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
| 79 | + } else { |
|
| 80 | + if ($node instanceof \DomText) { |
|
| 81 | + $node = $node->nodeValue; |
|
| 82 | + } |
|
| 76 | 83 | $new = $document->createElement('text'); |
| 77 | 84 | |
| 78 | 85 | $new->appendChild($document->createTextNode($node)); |
@@ -89,6 +96,8 @@ discard block |
||
| 89 | 96 | } |
| 90 | 97 | |
| 91 | 98 | private function removeAllChildren($element) { |
| 92 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
| 99 | + while ($element->hasChildNodes()) { |
|
| 100 | + $element->removeChild($element->firstChild); |
|
| 101 | + } |
|
| 93 | 102 | } |
| 94 | 103 | } |
| 95 | 104 | \ No newline at end of file |
@@ -13,7 +13,9 @@ discard block |
||
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | public function replaceContent($element, $content) { |
| 16 | - if ($element->getAttribute('transphporm') == 'added') return; |
|
| 16 | + if ($element->getAttribute('transphporm') == 'added') { |
|
| 17 | + return; |
|
| 18 | + } |
|
| 17 | 19 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
| 18 | 20 | if ($element->getAttribute('transphporm')) { |
| 19 | 21 | $this->replaceCachedContent($element); |
@@ -27,7 +29,9 @@ discard block |
||
| 27 | 29 | |
| 28 | 30 | private function insertNodes($element, $content) { |
| 29 | 31 | foreach ($this->content->getNode($content, $element->ownerDocument) as $node) { |
| 30 | - if ($node instanceof \DomElement && !$node->getAttribute('transphporm')) $node->setAttribute('transphporm', 'added'); |
|
| 32 | + if ($node instanceof \DomElement && !$node->getAttribute('transphporm')) { |
|
| 33 | + $node->setAttribute('transphporm', 'added'); |
|
| 34 | + } |
|
| 31 | 35 | $element->parentNode->insertBefore($node, $element); |
| 32 | 36 | } |
| 33 | 37 | } |