Skip to content

Commit

Permalink
Handles self-closing new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwessman committed Aug 20, 2023
1 parent 0a513a5 commit b5fe79b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
11 changes: 10 additions & 1 deletion lib/syntax_tree/erb/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,12 @@ def test_breakable_on_char_data_white_space

assert_formatting(source, expected)
end

def test_self_closing_group
source = "<link />\n<link />\n<meta />"
expected = "<link />\n<link />\n<meta />\n"

assert_formatting(source, expected)
end
end
end
8 changes: 0 additions & 8 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b5fe79b

Please sign in to comment.