Skip to content

Commit

Permalink
Merge pull request #44 from phil-janeapp/issue_42_double_quotes
Browse files Browse the repository at this point in the history
Fix for long attribute values containing = sign
  • Loading branch information
elia authored Feb 26, 2024
2 parents 149a0a2 + c5d3476 commit a5d44b0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def format(q)

class Error < StandardError; end

SPACES = /\s+/m

# https://stackoverflow.com/a/317081
ATTR_NAME = %r{[^\r\n\t\f\v= '"<>]*[^\r\n\t\f\v= '"<>/]} # not ending with a slash
UNQUOTED_VALUE = ATTR_NAME
UNQUOTED_VALUE = %r{[^<>'"\s]+}
UNQUOTED_ATTR = %r{#{ATTR_NAME}=#{UNQUOTED_VALUE}}
SINGLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}='[^']*?')}m
DOUBLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}="[^"]*?")}m
Expand Down Expand Up @@ -136,17 +138,18 @@ def format_attributes(tag_name, attrs, tag_closing)
attrs.scan(ATTR).flatten.each do |attr|
attr.strip!
name, value = attr.split('=', 2)
if UNQUOTED_ATTR =~ attr
attr_html << indented("#{name}=\"#{value}\"")
next
end

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

value_parts = value[1...-1].strip.split(/\s+/)
if /\A#{UNQUOTED_VALUE}\z/o.match?(value)
attr_html << indented("#{name}=\"#{value}\"")
next
end

value_parts = value[1...-1].strip.split(SPACES)
value_parts.sort_by!(&@css_class_sorter) if name == 'class' && @css_class_sorter

full_attr = "#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}"
Expand Down Expand Up @@ -231,7 +234,7 @@ def format_text(text)

return if text.match?(/\A\s*\z/m) # empty

text = text.gsub(/\s+/m, ' ').strip
text = text.gsub(SPACES, ' ').strip

offset = indented("").size
# Restore full line width if there are less than 40 columns available
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/attributes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
sizes="(max-width: 600px) 480px,
(max-width: 1000px) 800px,
1200px"
data-autocomplete-min-length-value=2>
data-autocomplete-min-length-value=2
data-short-url="//test.com/?q=v"
data-long-url="https://google.ca/this-is-a-long-url-with-a-query-string?query=something"
data-long-url-single='https://google.ca/this-is-a-long-url-with-a-query-string?query=something'>
3 changes: 3 additions & 0 deletions test/fixtures/attributes.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
srcset="image-480w.jpg 480w, image-800w.jpg 800w, image-1200w.jpg 1200w"
sizes="(max-width: 600px) 480px, (max-width: 1000px) 800px, 1200px"
data-autocomplete-min-length-value="2"
data-short-url="//test.com/?q=v"
data-long-url="https://google.ca/this-is-a-long-url-with-a-query-string?query=something"
data-long-url-single='https://google.ca/this-is-a-long-url-with-a-query-string?query=something'
>
6 changes: 6 additions & 0 deletions test/fixtures/multiline_attributes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ aria-disabled:text-gray-300 aria-disabled:bg-transparent aria-disabled:border-gr

<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>

<p
single-double-quotes='"double" quotes'
double-single-quotes="'single' quotes"
escaped-quotes=escaped&quote;quotes
></p>
6 changes: 6 additions & 0 deletions test/fixtures/multiline_attributes.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@
>
Foooo
</nav>

<p
single-double-quotes='"double" quotes'
double-single-quotes="'single' quotes"
escaped-quotes="escaped&quote;quotes"
></p>

0 comments on commit a5d44b0

Please sign in to comment.