Twig trim filter currently breaks Drupal markup

Created on 18 August 2025, 5 days ago

Problem/Motivation

Running the trim filter against a piece of markup string unescapes it.

This is because the default trim Twig filter only has special handling for \Twig\Markup, whereas Drupal uses \Drupal\Component\Render\MarkupInterface.

So support for safely trimming MarkupInterface should also be supported.

Steps to reproduce

1. Attempt to run the trim filter on a piece of safe markup like <p>content</p>
2. The content is now rendered as an escaped html markup: &lt;p&gt;content&lt;/p&gt;.

Proposed resolution

Reimplement the original trim filter but with support for Drupal markups.

If the trim is a non-standard whitespace trim, then the markup will be considered unsafe.

Remaining tasks

Provide MR with tests.

User interface changes

N/A

API changes

The default trim Twig filter will now be implemented in Drupal.

Release notes snippet

TBD

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

render system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024