From b5fe79bf2aef18de2d48bae34898c62bf9668927 Mon Sep 17 00:00:00 2001 From: David Wessman Date: Sun, 20 Aug 2023 07:34:42 +0200 Subject: [PATCH] Handles self-closing new lines --- lib/syntax_tree/erb/nodes.rb | 11 ++++++++++- test/html_test.rb | 7 +++++++ test/test_helper.rb | 8 -------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/syntax_tree/erb/nodes.rb b/lib/syntax_tree/erb/nodes.rb index 00e1aa9..ce5c882 100644 --- a/lib/syntax_tree/erb/nodes.rb +++ b/lib/syntax_tree/erb/nodes.rb @@ -164,7 +164,7 @@ def new_line def without_new_line self.class.new( - **deconstruct_keys([]).merge(closing: closing.without_new_line) + **deconstruct_keys([]).merge(closing: closing&.without_new_line) ) end @@ -253,6 +253,15 @@ def deconstruct_keys(keys) end end + def without_new_line + self.class.new( + **deconstruct_keys([]).merge( + opening: closing.nil? ? opening.without_new_line : opening, + closing: closing&.without_new_line + ) + ) + end + # The HTML-closing tag is responsible for new lines after the node. def new_line closing.nil? ? opening.new_line : closing&.new_line diff --git a/test/html_test.rb b/test/html_test.rb index 2f58ade..f97cb36 100644 --- a/test/html_test.rb +++ b/test/html_test.rb @@ -180,5 +180,12 @@ def test_breakable_on_char_data_white_space assert_formatting(source, expected) end + + def test_self_closing_group + source = "\n\n" + expected = "\n\n\n" + + assert_formatting(source, expected) + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 608d233..a846211 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,18 +12,10 @@ class TestCase < Minitest::Test def assert_formatting(source, expected) formatted = SyntaxTree::ERB.format(source) - if (expected != formatted) - binding.irb if debug - end - assert_equal(formatted, expected, "Failed first") formatted_twice = SyntaxTree::ERB.format(formatted) - if (expected != formatted_twice) - binding.irb if debug - end - assert_equal(formatted_twice, expected, "Failed second") # Check that pretty_print works