function RssResponseCdata::wrapDescriptionCdata

Converts description node to CDATA RSS markup.

Parameters

string $rss_markup: The RSS markup to update.

Return value

string|false The updated RSS XML or FALSE if there is an error saving the xml.

1 call to RssResponseCdata::wrapDescriptionCdata()
RssResponseCdata::onResponse in core/lib/Drupal/Core/EventSubscriber/RssResponseCdata.php
Wraps RSS descriptions in CDATA.

File

core/lib/Drupal/Core/EventSubscriber/RssResponseCdata.php, line 40

Class

RssResponseCdata
Subscribes to wrap RSS descriptions in CDATA.

Namespace

Drupal\Core\EventSubscriber

Code

protected function wrapDescriptionCdata(string $rss_markup) : string|false {
    $rss_dom = new \DOMDocument();
    // Load the RSS, if there are parsing errors, abort and return the unchanged
    // markup.
    $previous_value = libxml_use_internal_errors(TRUE);
    $rss_dom->loadXML($rss_markup);
    $errors = libxml_get_errors();
    libxml_use_internal_errors($previous_value);
    if ($errors) {
        return $rss_markup;
    }
    foreach ($rss_dom->getElementsByTagName('item') as $item) {
        foreach ($item->getElementsByTagName('description') as $node) {
            $html_markup = $node->nodeValue;
            if (!empty($html_markup)) {
                $html_markup = Xss::filter($html_markup, [
                    'a',
                    'abbr',
                    'acronym',
                    'address',
                    'b',
                    'bdo',
                    'big',
                    'blockquote',
                    'br',
                    'caption',
                    'cite',
                    'code',
                    'col',
                    'colgroup',
                    'dd',
                    'del',
                    'dfn',
                    'dl',
                    'dt',
                    'em',
                    'h1',
                    'h2',
                    'h3',
                    'h4',
                    'h5',
                    'h6',
                    'hr',
                    'i',
                    'ins',
                    'kbd',
                    'li',
                    'ol',
                    'p',
                    'pre',
                    'q',
                    'samp',
                    'small',
                    'span',
                    'strong',
                    'sub',
                    'sup',
                    'table',
                    'tbody',
                    'td',
                    'tfoot',
                    'th',
                    'thead',
                    'tr',
                    'tt',
                    'ul',
                    'var',
                ]);
                $new_node = $rss_dom->createCDATASection($html_markup);
                $node->replaceChild($new_node, $node->firstChild);
            }
        }
    }
    return $rss_dom->saveXML();
}

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