src/Drupal/Component/Utility/Html.php
public static function escapeCdataElement(\DOMNode $node, $comment_start = '//', $comment_end = '') {
    foreach ($node->childNodes as $child_node) {
      if ($child_node instanceof \DOMCdataSection) {
        $embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n";
        $embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n";

        // Prevent invalid cdata escaping as this would throw a DOM error.
        // This is the same behavior as found in libxml2.
        // Related W3C standard: http://www.w3.org/TR/REC-xml/#dt-cdsection
        // Fix explanation: http://en.wikipedia.org/wiki/CDATA#Nesting
        $data = str_replace(']]>', ']]]]><![CDATA[>', $child_node->data);

        $fragment = $node->ownerDocument->createDocumentFragment();
        $fragment->appendXML($embed_prefix . $data . $embed_suffix);
        $node->appendChild($fragment);
        $node->removeChild($child_node);
      }
    }
  }
public static function escapeCdataElement(\DOMNode $node, $comment_start = '//', $comment_end = '') {
    foreach ($node->childNodes as $child_node) {
      if ($child_node instanceof \DOMCdataSection) {
        $embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n";
        $embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n";

        // Prevent invalid cdata escaping as this would throw a DOM error.
        // This is the same behavior as found in libxml2.
        // Related W3C standard: http://www.w3.org/TR/REC-xml/#dt-cdsection
        // Fix explanation: http://en.wikipedia.org/wiki/CDATA#Nesting
        $data = str_replace(']]>', ']]]]><![CDATA[>', $child_node->data);

        $fragment = $node->ownerDocument->createDocumentFragment();
        $fragment->appendXML($embed_prefix . $data . $embed_suffix);
        $node->appendChild($fragment);
        $node->removeChild($child_node);
      }
    }
  }
public static function escapeCdataElement ( \ DOMNode $ node , $ comment_start = '//' , $ comment_end = '' ) { foreach ( $ node -> childNodes as $ child_node ) { if ( $ child_node instanceof \ DOMCdataSection ) { $ embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n" ; $ embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n" ; // Prevent invalid cdata escaping as this would throw a DOM error. // This is the same behavior as found in libxml2. // Related W3C standard: http://www.w3.org/TR/REC-xml/#dt-cdsection // Fix explanation: http://en.wikipedia.org/wiki/CDATA#Nesting $ data = str_replace ( ']]>' , ']]]]><![CDATA[>' , $ child_node -> data ) ; $ fragment = $ node -> ownerDocument -> createDocumentFragment ( ) ; $ fragment -> appendXML ( $ embed_prefix . $ data . $ embed_suffix ) ; $ node -> appendChild ( $ fragment ) ; $ node -> removeChild ( $ child_node ) ; } } }
Adds comments around a <!CDATA section in a \DOMNode.

\DOMDocument::loadHTML() in \Drupal\Component\Utility\Html::load() makes
CDATA sections from the contents of inline script and style tags. This can
cause HTML4 browsers to throw exceptions.

This function attempts to solve the problem by creating a
\DOMDocumentFragment to comment the CDATA tag.

@param \DOMNode $node
The element potentially containing a CDATA node.
@param string $comment_start
(optional) A string to use as a comment start marker to escape the CDATA
declaration. Defaults to '//'.
@param string $comment_end
(optional) A string to use as a comment end marker to escape the CDATA
declaration. Defaults to an empty string.
Adds comments around a <!CDATA section in a \ DOMNode .