function filter_dom_serialize_escape_cdata_element

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 drupal_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.

1 call to filter_dom_serialize_escape_cdata_element()
filter_dom_serialize in modules/filter/filter.module
Converts a DOM object back to an HTML snippet.

File

modules/filter/filter.module, line 1172

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 drupal_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);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.