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