Engine: Strip whitespace between consecutive end tags after expression blocks#1492
Merged
marcoroth merged 11 commits intomarcoroth:mainfrom Mar 27, 2026
Merged
Engine: Strip whitespace between consecutive end tags after expression blocks#1492marcoroth merged 11 commits intomarcoroth:mainfrom
marcoroth merged 11 commits intomarcoroth:mainfrom
Conversation
marcoroth
reviewed
Mar 24, 2026
Contributor
Author
|
@marcoroth green! |
marcoroth
added a commit
that referenced
this pull request
Mar 26, 2026
This pull request adds a new `--trim` CLI flag for the `compile` and `render` commands in the `herb` Ruby CLI. When invoked with the `--trim` flag, the CLI passes `trim: true` to the `Herb::Engine.new` call when compiling and rendering a template. Discovered while reviewing #1492.
marcoroth
added a commit
that referenced
this pull request
Mar 26, 2026
This pull request introduces a new `enforce_actionview_erubi_equality`
keyword argument for the `assert_evaluated_snapshot` test helper. This
option makes sure the `Herb::Engine` evaluated output matches the
`Erubi::Engine` evaluated output through ActionView.
The `enforce_actionview_erubi_equality` is automatically enabled when
`enforce_erubi_equality` is being passed, so this now runs automatically
on all existing test that have `enforce_erubi_equality: true`.
If, for some reason, the output should differ we can opt-out of
`enforce_actionview_erubi_equality` by doing the following:
```ruby
assert_evaluated_snapshot(template, {}, enforce_erubi_equality: true, enforce_actionview_erubi_equality: false)
```
Obviously, just passing `enforce_actionview_erubi_equality: true` also
works. In that case we only enforcing the equality of the ActionView
evaluated engine output:
```ruby
assert_evaluated_snapshot(template, {},
enforce_actionview_erubi_equality: true)
```
Introducing this option allows us to properly test #1492.
marcoroth
reviewed
Mar 26, 2026
Co-authored-by: Marco Roth <marco.roth@intergga.ch> Signed-off-by: Joel Hawksley <joelhawksley@github.com>
Owner
|
Thank you @joelhawksley! 🙏🏼 |
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.
Fix: Strip whitespace between consecutive
<% end %>tags after expression blocksCo-written with Claude Opus 4.6 - I've reviewed the fix and generally think it's right, but there might be a simpler solution?
Problem
Herb emits
@output_buffer.safe_append=for whitespace between consecutive<% end %>tags that Erubi (withtrim: true) strips. This breaksCaptureHelper#capturein Rails 8.2 for helpers likeform_tagthat return HTML via the block's return value rather than callingconcat.When Herb adds
@output_buffer.safe_append=' 'betweenendstatements:@raw_buffer(the return ofsafe_append=)" ")buffer.presencereturnsnil(whitespace is.blank?)valuefallback is just@raw_buffer(which only contains whitespace)Triggering template:
Herb compiled (before fix):
Erubi compiled (and Herb after fix):
Root Cause
In preceding_token_ends_with_newline?,
:expr_block_endwas grouped with expression tokens in a blanket return false list. This prevented at_line_start? from recognizing that a<% end %>tag following an expression block's closingendis at line start — so the subsequent code tag was treated as inline, and the whitespace between them was emitted as buffer appends instead of being trimmed.Fix
Remove
:expr_block_endfrom the blanket return false list and instead check whether theexpr_block_endtoken's value actually ends with"\n"(which it does when the end tag was on its own line). This lets at_line_start? correctly trigger trim behavior for subsequent code-only tags.