| @@ 168-212 (lines=45) @@ | ||
| 165 | * |
|
| 166 | * @return SimpleHtmlDomInterface |
|
| 167 | */ |
|
| 168 | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
|
| 169 | { |
|
| 170 | if (!empty($string)) { |
|
| 171 | $newDocument = new HtmlDomParser($string); |
|
| 172 | ||
| 173 | $tmpDomString = $this->normalizeStringForComparision($newDocument); |
|
| 174 | $tmpStr = $this->normalizeStringForComparision($string); |
|
| 175 | if ($tmpDomString !== $tmpStr) { |
|
| 176 | throw new \RuntimeException( |
|
| 177 | 'Not valid HTML fragment!' . "\n" . |
|
| 178 | $tmpDomString . "\n" . |
|
| 179 | $tmpStr |
|
| 180 | ); |
|
| 181 | } |
|
| 182 | } |
|
| 183 | ||
| 184 | /** @var \DOMNode[] $remove_nodes */ |
|
| 185 | $remove_nodes = []; |
|
| 186 | if ($this->node->childNodes->length > 0) { |
|
| 187 | // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
|
| 188 | // if we delete the elements on the fly. |
|
| 189 | foreach ($this->node->childNodes as $node) { |
|
| 190 | $remove_nodes[] = $node; |
|
| 191 | } |
|
| 192 | } |
|
| 193 | foreach ($remove_nodes as $remove_node) { |
|
| 194 | $this->node->removeChild($remove_node); |
|
| 195 | } |
|
| 196 | ||
| 197 | if (!empty($newDocument)) { |
|
| 198 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
| 199 | $ownerDocument = $this->node->ownerDocument; |
|
| 200 | if ( |
|
| 201 | $ownerDocument |
|
| 202 | && |
|
| 203 | $newDocument->getDocument()->documentElement |
|
| 204 | ) { |
|
| 205 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 206 | /** @noinspection UnusedFunctionResultInspection */ |
|
| 207 | $this->node->appendChild($newNode); |
|
| 208 | } |
|
| 209 | } |
|
| 210 | ||
| 211 | return $this; |
|
| 212 | } |
|
| 213 | ||
| 214 | /** |
|
| 215 | * Replace this node. |
|
| @@ 156-199 (lines=44) @@ | ||
| 153 | * |
|
| 154 | * @return SimpleXmlDomInterface |
|
| 155 | */ |
|
| 156 | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
|
| 157 | { |
|
| 158 | if (!empty($string)) { |
|
| 159 | $newDocument = new XmlDomParser($string); |
|
| 160 | ||
| 161 | $tmpDomString = $this->normalizeStringForComparision($newDocument); |
|
| 162 | $tmpStr = $this->normalizeStringForComparision($string); |
|
| 163 | if ($tmpDomString !== $tmpStr) { |
|
| 164 | throw new \RuntimeException( |
|
| 165 | 'Not valid XML fragment!' . "\n" . |
|
| 166 | $tmpDomString . "\n" . |
|
| 167 | $tmpStr |
|
| 168 | ); |
|
| 169 | } |
|
| 170 | } |
|
| 171 | ||
| 172 | /** @var \DOMNode[] $remove_nodes */ |
|
| 173 | $remove_nodes = []; |
|
| 174 | if ($this->node->childNodes->length > 0) { |
|
| 175 | // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
|
| 176 | // if we delete the elements on the fly. |
|
| 177 | foreach ($this->node->childNodes as $node) { |
|
| 178 | $remove_nodes[] = $node; |
|
| 179 | } |
|
| 180 | } |
|
| 181 | foreach ($remove_nodes as $remove_node) { |
|
| 182 | $this->node->removeChild($remove_node); |
|
| 183 | } |
|
| 184 | ||
| 185 | if (!empty($newDocument)) { |
|
| 186 | $ownerDocument = $this->node->ownerDocument; |
|
| 187 | if ( |
|
| 188 | $ownerDocument |
|
| 189 | && |
|
| 190 | $newDocument->getDocument()->documentElement |
|
| 191 | ) { |
|
| 192 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 193 | /** @noinspection UnusedFunctionResultInspection */ |
|
| 194 | $this->node->appendChild($newNode); |
|
| 195 | } |
|
| 196 | } |
|
| 197 | ||
| 198 | return $this; |
|
| 199 | } |
|
| 200 | ||
| 201 | /** |
|
| 202 | * Replace this node. |
|