| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * @package   s9e\SweetDOM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * @copyright Copyright (c) The s9e authors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * @license   http://www.opensource.org/licenses/mit-license.php The MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace s9e\SweetDOM\NodeTraits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use DOMNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function in_array, is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | trait ChildNodeWorkarounds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	// https://github.com/php/php-src/pull/11768 - fixed in ~8.1.23, ^8.2.10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	// https://github.com/php/php-src/pull/11905 - behaviour changed in 8.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	public function after(...$nodes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$this->replaceWith($this, ...$nodes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	// https://github.com/php/php-src/pull/11905 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public function before(...$nodes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		if (!in_array($this, $nodes, true)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			$nodes[] = $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$this->replaceWith(...$nodes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	// https://github.com/php/php-src/issues/11289 - introduced in 8.1.18, 8.2.6 - fixed in ~8.1.10, ^8.2.8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	// https://github.com/php/php-src/pull/11888 - introduced in 8.2.8 - fixed in ^8.2.10 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  | 	// https://github.com/php/php-src/pull/11905 | 
            
                                                        
            
                                    
            
            
                | 35 |  |  | 	public function replaceWith(...$nodes): void | 
            
                                                        
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                        
            
                                    
            
            
                | 37 |  |  | 		if (!isset($this->parentNode)) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  | 		{ | 
            
                                                        
            
                                    
            
            
                | 39 |  |  | 			return; | 
            
                                                        
            
                                    
            
            
                | 40 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  | 		$contextNode = $this->ownerDocument->createTextNode(''); | 
            
                                                        
            
                                    
            
            
                | 43 |  |  | 		$parentNode  = $this->parentNode; | 
            
                                                        
            
                                    
            
            
                | 44 |  |  | 		$parentNode->replaceChild($contextNode, $this); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  | 		foreach ($nodes as $node) | 
            
                                                        
            
                                    
            
            
                | 46 |  |  | 		{ | 
            
                                                        
            
                                    
            
            
                | 47 |  |  | 			if (is_string($node)) | 
            
                                                        
            
                                    
            
            
                | 48 |  |  | 			{ | 
            
                                                        
            
                                    
            
            
                | 49 |  |  | 				$node = $this->ownerDocument->createTextNode($node); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  | 			} | 
            
                                                        
            
                                    
            
            
                | 51 |  |  | 			$parentNode->insertBefore($node, $contextNode); | 
            
                                                        
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 53 |  |  | 		$contextNode->remove(); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 55 |  |  | } |