Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for long attribute values containing = sign #44

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading