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