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

Allow string interpolation in attribute names #7

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 6 additions & 7 deletions lib/erb/linter/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,21 @@ def erb2html(source)
attributes.map! do |attribute|
if attribute.match?(erb_tags_matcher)
space, attribute = attribute.scan(/\A(\s+)(.*)\z/m).flatten
attribute.gsub!(erb_tags_matcher, erb_tags)

attribute =
case attribute
when /\A#{ERB_TAG}\z/
%{data-erb-#{i += 1}="#{CGI.escapeHTML(attribute)}"}
when /\A#{ATTR_NAME}=/
name, value = attribute.split("=", 2)
case
when /\A#{ERB_TAG}\z/ === attribute.gsub(erb_tags_matcher, erb_tags)
%{data-erb-#{i += 1}="#{CGI.escapeHTML(attribute.gsub(erb_tags_matcher, erb_tags))}"}
when /\A#{ATTR_NAME}=/ === attribute
name, value = attribute.split("=", 2).map { _1.gsub(erb_tags_matcher, erb_tags) }
quote = '"'
if value.match(/\A['"]/)
quote = value[0]
value = value[1...-1]
end
%{data-erb-#{name}=#{quote}#{CGI.escapeHTML(value)}#{quote}}
else
raise "Don't know how to process attribute: #{attribute.inspect}"
raise "Don't know how to process attribute: #{attribute.gsub(erb_tags_matcher, erb_tags).inspect}"
end

"#{space}#{attribute}"
Expand Down
2 changes: 2 additions & 0 deletions test/erb/lint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<div>
<span data-action="foo->#bar" <%= :bar if bar %>></span>
<span data-foo="<%= :foo %>" <%= :bar if bar %>></span>
<span data-<% identifier %>="bar" data-<% identifier %>-foo="<%= 'bar' %>"></span>
<span <%= :foo if foo %> <%= :bar if bar %>></span>
<span data-foo="<%= "foo" %>" <%= "bar" if bar %>></span>
<input <%= :foo if foo %> <%= :bar if bar %>/>
Expand Down Expand Up @@ -72,6 +73,7 @@
<div>
<span data-action="foo->#bar" data-erb-0="&lt;%= :bar if bar %&gt;"></span>
<span data-erb-data-foo="&lt;%= :foo %&gt;" data-erb-0="&lt;%= :bar if bar %&gt;"></span>
<span data-erb-data-<erb silent erb-code=\" identifier \"></erb>=\"bar\" data-erb-data-<erb silent erb-code=\" identifier \"></erb>-foo=\"&lt;%= &#39;bar&#39; %&gt;\"></span>
<span data-erb-0="&lt;%= :foo if foo %&gt;" data-erb-1="&lt;%= :bar if bar %&gt;"></span>
<span data-erb-data-foo="&lt;%= &quot;foo&quot; %&gt;" data-erb-0="&lt;%= &quot;bar&quot; if bar %&gt;"></span>
<input data-erb-0="&lt;%= :foo if foo %&gt;" data-erb-1="&lt;%= :bar if bar %&gt;"/>
Expand Down