1 filter.module filter_dom_serialize_escape_cdata_element($dom_document, $dom_element, $comment_start = '//', $comment_end = '')

Adds comments around the <!CDATA section in a dom element.

DOMDocument::loadHTML in filter_dom_load() makes CDATA sections from the contents of inline script and style tags. This can cause HTML 4 browsers to throw exceptions.

This function attempts to solve the problem by creating a DocumentFragment and imitating the behavior in backdrop_get_js(), commenting the CDATA tag.

Parameters

$dom_document: The DOMDocument containing the $dom_element.

$dom_element: The element potentially containing a CDATA node.

$comment_start: (optional) A string to use as a comment start marker to escape the CDATA declaration. Defaults to '//'.

$comment_end: (optional) A string to use as a comment end marker to escape the CDATA declaration. Defaults to an empty string.

File

core/modules/filter/filter.module, line 1699
Framework for handling the filtering of content.

Code

function filter_dom_serialize_escape_cdata_element($dom_document, $dom_element, $comment_start = '//', $comment_end = '') {
  foreach ($dom_element->childNodes as $node) {
    if (get_class($node) == 'DOMCdataSection') {
      // See backdrop_get_js().  This code is more or less duplicated there.
      $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[>', $node->data);

      $fragment = $dom_document->createDocumentFragment();
      $fragment->appendXML($embed_prefix . $data . $embed_suffix);
      $dom_element->appendChild($fragment);
      $dom_element->removeChild($node);
    }
  }
}