Skip to content

Preserve void elements before the truncation point in truncatehtml - #1366

Merged
jasmith-hs merged 1 commit into
HubSpot:masterfrom
cathaloc:truncatehtml-preserve-void-elements
Nov 5, 2026
Merged

jasmith-hs merged 1 commit into
HubSpot:masterfrom
cathaloc:truncatehtml-preserve-void-elements

Conversation

@cathaloc

@cathaloc cathaloc commented Nov 5, 2026

Copy link
Copy Markdown

Description

truncatehtml removes 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:

  • Void elements are removed only once the truncation point has been reached.
  • A blank element that still contains a kept void element (e.g. <p><img></p>) is kept.
  • Other blank elements are removed as before.

The visitor keeps a running count of kept void elements and records it when it enters each element, so tail can 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/breakword handling, 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
  • a <br> before the truncation point is kept when later text is truncated
  • void elements, and blank elements that only contain them, are removed after the truncation point

Verification

Run truncatehtml on 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

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
cathaloc marked this pull request as ready for review November 5, 2026 12:21
@jasmith-hs
jasmith-hs merged commit ffdb648 into HubSpot:master Nov 5, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants