| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* @description     Transformation Style Sheets - Revolutionising PHP templating    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author          Tom Butler [email protected]                                             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @version         1.2                                                             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Transphporm\Property; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ContentReplace { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	private $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	public function __construct(Content $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		$this->content = $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	public function replaceContent($element, $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		if ($element->getAttribute('transphporm') == 'added') return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		if ($element->getAttribute('transphporm')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			$this->replaceCachedContent($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		$this->insertNodes($element, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		//Remove the original element from the final output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$element->setAttribute('transphporm', 'remove'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	private function insertNodes($element, $content) { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		foreach ($this->content->getNode($content, $element->ownerDocument) as $node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 			if ($node instanceof \DomElement && !$node->getAttribute('transphporm'))  $node->setAttribute('transphporm', 'added'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 			$element->parentNode->insertBefore($node, $element); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	private function replaceCachedContent($element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$el = $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		while ($el = $el->previousSibling) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			if ($el->nodeType == 1 && $el->getAttribute('transphporm') != 'remove') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				$el->parentNode->removeChild($el); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$this->fixPreserveWhitespaceRemoveChild($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	// $doc->preserveWhiteSpace = false should fix this but it doesn't | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	// Remove extra whitespace created by removeChild to avoid the cache growing 1 byte every time it's reloaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	// This may need to be moved in future, anywhere elements are being removed and files are cached may need to apply this fix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	// Also remove any comments to avoid the comment being re-added every time the cache is reloaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	private function fixPreserveWhitespaceRemoveChild($element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		if ($element->previousSibling instanceof \DomComment || ($element->previousSibling instanceof \DomText && $element->previousSibling->isElementContentWhiteSpace())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			$element->parentNode->removeChild($element->previousSibling); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 54 |  |  | } |