Skip to content

Commit 98227e3

Browse files
authored
Format empty html-tags on one line if possible (#42)
1 parent 3dbc3d9 commit 98227e3

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
## [0.9.3] - 2023-06-30
10+
11+
- Print empty html-tags on one line if possible
12+
913
## [0.9.2] - 2023-06-30
1014

1115
- Handle whitespace in HTML-strings using ERB-tags

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
w_syntax_tree-erb (0.9.2)
4+
w_syntax_tree-erb (0.9.3)
55
prettier_print (>= 1.2.0)
66
syntax_tree (>= 6.1.1)
77

lib/syntax_tree/erb/format.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ def visit_block(node)
4949
end
5050

5151
def visit_html(node)
52-
visit_block(node)
52+
# Make sure to group the tags together if there is no child nodes.
53+
if node.elements.size == 0
54+
q.group { visit_block(node) }
55+
else
56+
visit_block(node)
57+
end
5358
end
5459

5560
def visit_erb_block(node)

lib/syntax_tree/erb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module SyntaxTree
44
module ERB
5-
VERSION = "0.9.2"
5+
VERSION = "0.9.3"
66
end
77
end

test/fixture/erb_syntax_formatted.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@
4242
assign_c ||= "c"
4343
%>
4444

45-
<div class="card mb-2 <%= condition ? "pb-3" : "pb-0" %> mt-<%= 5 * 5 %>">
46-
</div>
45+
<div class="card mb-2 <%= condition ? "pb-3" : "pb-0" %> mt-<%= 5 * 5 %>"></div>

test/html_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,13 @@ def test_html_attribute_without_quotes
8989
formatted = ERB.format(source)
9090
assert_equal("<div class=\"card\">\n Hello World\n</div>\n", formatted)
9191
end
92+
93+
def test_html_attribute_without_content
94+
source = "<component-without-content>\n</component-without-content>\n"
95+
expected = "<component-without-content></component-without-content>\n"
96+
97+
formatted = ERB.format(source)
98+
assert_equal(expected, formatted)
99+
end
92100
end
93101
end

0 commit comments

Comments
 (0)