Skip to content

Commit aa12f6f

Browse files
authored
Engine: <%= -%> does not trim trailing newline (#1361)
### Description The `-%>` trim modifier on `<%= %>` (expression) tags does not suppress the trailing newline. Standard ERB trims the trailing newline when `-%>` is used, but Herb preserves it. ### Reproduction Template: ```erb hello<%= -%> world ``` Expected output (standard ERB): `helloworld` Actual output (Herb): ``` hello world ``` ### Notes <% -%> (statement tags) trim correctly β€” the issue is specific to <%= -%> (expression tags). This is a common Rails idiom for controlling whitespace in email and other templates.
1 parent 3fb47ca commit aa12f6f

9 files changed

+60
-3
lines changed

β€Žlib/herb/engine/compiler.rbβ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def process_erb_tag(node, skip_comment_check: false)
372372
code = node.content.value.strip
373373

374374
if erb_output?(opening)
375-
process_erb_output(opening, code)
375+
process_erb_output(node, opening, code)
376376
else
377377
apply_trim(node, code)
378378
end
@@ -487,9 +487,11 @@ def find_token_before_code_sequence(tokens, whitespace_index)
487487
search_index >= 0 ? tokens[search_index] : nil
488488
end
489489

490-
def process_erb_output(opening, code)
490+
def process_erb_output(node, opening, code)
491+
has_right_trim = node.tag_closing&.value == "-%>"
491492
should_escape = should_escape_output?(opening)
492493
add_expression_with_escaping(code, should_escape)
494+
@trim_next_whitespace = true if has_right_trim
493495
end
494496

495497
def should_escape_output?(opening)

β€Žsig/herb/engine/compiler.rbsβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/engine/whitespace_trimming_test.rbβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,26 @@ class WhitespaceTrimmingTest < Minitest::Spec
148148
assert_compiled_snapshot(template)
149149
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
150150
end
151+
152+
test "expression tag with right trim removes following newline" do
153+
template = "<%= \"hello\" -%>\nworld"
154+
155+
assert_compiled_snapshot(template)
156+
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
157+
end
158+
159+
test "expression tag with right trim inline" do
160+
template = "<%= \"hello\" -%> world"
161+
162+
assert_compiled_snapshot(template)
163+
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
164+
end
165+
166+
test "expression tag with right trim as no-op for newline control" do
167+
template = "2024-07-09 with the filters: \"archived:false\"<%= -%>.\n"
168+
169+
assert_compiled_snapshot(template)
170+
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
171+
end
151172
end
152173
end

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0016_expression_tag_with_right_trim_removes_following_newline_5d3c15a8c5a086378193a71c41d8b925.txtβ€Ž

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0016_expression_tag_with_right_trim_removes_following_newline_b01f02b254597ef585e83f992dbbf2f5.txtβ€Ž

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0017_expression_tag_with_right_trim_inline_ba87a8097255ef70a622322d32d15e1d.txtβ€Ž

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0017_expression_tag_with_right_trim_inline_e9676bc505311d417e489db5dc998443.txtβ€Ž

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0018_expression_tag_with_right_trim_as_no-op_for_newline_control_a6f21b3c4464ca4ee6530fc903ec429e.txtβ€Ž

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/engine/whitespace_trimming_test/test_0018_expression_tag_with_right_trim_as_no-op_for_newline_control_c2919e976aa627ae45a2ff623d284c5b.txtβ€Ž

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)