You are here

protected function AlinkPostRenderer::replaceNodeContent in Alinks 8

Replace the contents of a DOMNode.

Parameters

\DOMNode $node: A DOMNode object.

string $content: The text or HTML that will replace the contents of $node.

1 call to AlinkPostRenderer::replaceNodeContent()
AlinkPostRenderer::replace in src/AlinkPostRenderer.php
Load the content and replace the matched strings with automatic links.

File

src/AlinkPostRenderer.php, line 307

Class

AlinkPostRenderer
Class AlinkPostRenderer.

Namespace

Drupal\alinks

Code

protected function replaceNodeContent(\DOMNode &$node, $content) {
  if (strlen($content)) {

    // Load the content into a new DOMDocument and retrieve the DOM nodes.
    $replacement_nodes = Html::load($content)
      ->getElementsByTagName('body')
      ->item(0)->childNodes;
  }
  else {
    $replacement_nodes = [
      $node->ownerDocument
        ->createTextNode(''),
    ];
  }
  foreach ($replacement_nodes as $replacement_node) {

    // Import the replacement node from the new DOMDocument into the original
    // one, importing also the child nodes of the replacement node.
    $replacement_node = $node->ownerDocument
      ->importNode($replacement_node, TRUE);
    $node->parentNode
      ->insertBefore($replacement_node, $node);
  }
  $node->parentNode
    ->removeChild($node);
}