Special characters in RSS item titles are double-encoded

Created on 1 April 2022, about 2 years ago
Updated 12 May 2024, about 1 month ago

Problem/Motivation

This module has code that strips HTML from titles of RSS feed items output via Views:

/**
 * Implements hook_preprocess_HOOK().
 */
function html_title_preprocess_views_view_row_rss(&$variables) {
  $variables['title'] = strip_tags($variables['title']);
}

I guess the intention is that HTML someone added to the title should just be stripped out as RSS feed readers probably won't support rendering it.

The problem is that $variables['title'] is actually an instance of \Drupal\Core\Render\Markup, and performing strip_tags on it converts it to a scalar string. That means that when it's being rendered, Twig will encode it to make sure it's safe, which it normally would not do for a Markup object.

The result is that instead of the title being output as

Student from class of '22 awarded fellowship

we get

Student from class of '22 awarded fellowship

because Twig is escaping the ampersand.

Steps to reproduce

Create a Views RSS feed output using the "Fields" style plugin. Add a node on your site that uses some special character, like an apostrophe '. Enable the HTML Title module. Observe feed output double encodes the apostrophe.

Proposed resolution

Preserve the Markup object.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇺🇸United States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Status changed to Needs review 2 months ago
  • 🇺🇦Ukraine quadrexdev Lutsk

    Added test coverage in an existing merge request, please review

  • Status changed to Needs work about 1 month ago
  • 🇺🇸United States generalredneck

    I imported the view used in the automated test. I don't think this patch fixes the full problem. I used a modified version of the test's content and put in the title of I'm testing special characters day&night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦. It really is just the Ampersand (&) that causes issues. While the "title" is fixed in the view, the other fields that rendered are not.

    Here is the full source code of the view outputed by the test. Note that the link, description, dc:creator, and guid> fields are all double encoding the ampersand.

    
    <?xml version="1.0" encoding="utf-8"?>
    <rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xml:base="http://drupal-10.lndo.site/">
      <channel>
        <title>Rss</title>
        <link>http://drupal-10.lndo.site/</link>
        <description/>
        <language>en</language>
        
        <item>
      <title>Hey Hey</title>
      <link>http://drupal-10.lndo.site/Hey%20%3Csup%3EHey%3C/sup%3E</link>
      <description>Hey &lt;sup&gt;Hey&lt;/sup&gt;</description>
      <pubDate>Hey Hey</pubDate>
        <dc:creator>Hey <sup>Hey</sup></dc:creator>
        <guid isPermaLink="true">http://drupal-10.lndo.site/Hey%20%3Csup%3EHey%3C/sup%3E</guid>
        </item>
    <item>
      <title>I'm testing special characters day&amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</title>
      <link>http://drupal-10.lndo.site/I%26#039;m testing special characters day&amp;amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</link>
      <description>I'm testing special characters day&amp;amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</description>
      <pubDate>I&amp;#039;m testing special characters day&amp;amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</pubDate>
        <dc:creator>I'm testing special characters day&amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</dc:creator>
        <guid isPermaLink="true">http://drupal-10.lndo.site/I%26#039;m testing special characters day&amp;amp;night© ® € ™ ← ↑ → ↓ ♠ ♣ ♥ ♦</guid>
        </item>
    
      </channel>
    </rss>
    
Production build 0.69.0 2024