Skip to content

Commit

Permalink
Strip and rebuild newlines from inside class attributes
Browse files Browse the repository at this point in the history
Also explicitly take care of attributes without a value.
  • Loading branch information
elia committed Dec 1, 2023
1 parent 6a4077c commit a51dc71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,21 @@ def format_attributes(tag_name, attrs, tag_closing)

attrs.scan(ATTR).flatten.each do |attr|
attr.strip!
full_attr = indented(attr)
name, value = attr.split('=', 2)

if value.nil?
attr_html << indented("#{name}")
next
end

value_parts = value[1...-1].strip.split(/\s+/)

full_attr = indented("#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}")

if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR)
attr_html << indented("#{name}=#{value[0]}")
tag_stack_push('attr"', value)

value_parts = value[1...-1].strip.split(/\s+/)

if !@single_class_per_line && name == 'class'
line = value_parts.shift
value_parts.each do |value_part|
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/multiline_attributes.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<button class="text-gray-700 bg-transparent hover:bg-gray-50 active:bg-gray-100 focus:bg-gray-50 focus:ring-gray-300 focus:ring-2
disabled:text-gray-300 disabled:bg-transparent disabled:border-gray-300 disabled:cursor-not-allowed
aria-disabled:text-gray-300 aria-disabled:bg-transparent aria-disabled:border-gray-300 aria-disabled:cursor-not-allowed">Button</button>

<nav class="
flex flex-col bg-gray-15 p-4 w-full " data-controller="<%= stimulus_id %>" data-<%= stimulus_id %>-cookie-value="solidus_admin"> Foooo </nav>
8 changes: 8 additions & 0 deletions test/fixtures/multiline_attributes.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
aria-disabled:border-gray-300 aria-disabled:cursor-not-allowed
"
>Button</button>

<nav
class="flex flex-col bg-gray-15 p-4 w-full"
data-controller="<%= stimulus_id %>"
data-<%= stimulus_id %>-cookie-value="solidus_admin"
>
Foooo
</nav>

0 comments on commit a51dc71

Please sign in to comment.