Repository navigation
Preserve void elements before the truncation point in truncatehtml - #1366
Merged
jasmith-hs merged 1 commit intoNov 5, 2026
Merged
Conversation
truncatehtml removed every element whose text was blank, to clean up the empty shells left after truncated text is cleared. Void elements such as <br>, <img> and <hr> never contain text, so they were removed wherever they appeared, even before the truncation point or when no text needed truncating at all. Line breaks and images disappeared from otherwise untouched content. Void elements are now removed only once the truncation point has been reached, and a blank element that still contains a kept void element (e.g. <p><img></p>) is kept. Other blank elements are removed as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cathaloc
marked this pull request as ready for review
November 5, 2026 12:21
jasmith-hs
approved these changes
Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
truncatehtmlremoves elements that end up with blank text, to clean up the empty shells left after truncated text is cleared. That check also matches void elements such as<br>,<img>and<hr>, which never contain text, so they were removed wherever they appeared, including before the truncation point and when no text needed truncating at all.For example,
{{ '<p>Hey,<br>Great to hear!</p><p><img src="a.png"></p>'|truncatehtml(255) }}returned<p>Hey,Great to hear!</p>: the line break and the image were dropped even though the text was well under the limit.With this change:
<p><img></p>) is kept.The visitor keeps a running count of kept void elements and records it when it enters each element, so
tailcan tell whether an element contains one without rescanning its subtree.BRAVE
Backwards Compatibility
Output changes only for input that contains void elements. Line breaks and images before the truncation point now appear in the output; previously they were removed. Truncation itself, the
end/breakwordhandling, and removal of other blank elements are unchanged.Rollout and Rollback Plan
Ships with the next Jinjava release. Roll back by reverting this commit.
Automated Testing
Tests added to
TruncateHtmlFilterTest:<br>,<br/>and<img>(including<img ...></img>) are kept when content is under the limit<br>before the truncation point is kept when later text is truncatedVerification
Run
truncatehtmlon HTML containing<br>and<img>under the length limit and check they're kept; run it with a limit inside the text and check that void elements after the cut are removed.Expect Dependencies to Fail
No new dependencies.
REVIEWERS: Please review both the code changes and the answers above, and validate that they match the expectations for BRAVE
🤖 Generated with Claude Code