Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ static AST_HTML_ATTRIBUTE_NAME_NODE_T* parser_parse_html_attribute_name(parser_T
TOKEN_EOF
)) {
if (token_is(parser, TOKEN_ERB_START)) {
const char* tag = parser->current_token->value;
bool is_output_tag = (tag[2] == '=');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: What if the tag length is less than 3?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Remove the braces


if (!is_output_tag) { break; }

parser_append_literal_node_from_buffer(parser, &buffer, children, start);

AST_ERB_CONTENT_NODE_T* erb_node = parser_parse_erb_tag(parser);
Expand Down
61 changes: 61 additions & 0 deletions test/parser/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,66 @@ class AttributesTest < Minitest::Spec
test "Empty attribute value with closing bracket immediatly following it" do
assert_parsed_snapshot(%(<div attribute-name=>div-content</div>))
end

test "Conditional attribute with ERB control flow and no surrounding spaces" do
assert_parsed_snapshot(%(<dialog data-controller="dialog" <% if local_assigns[:permanent] %>data-turbo-permanent<% end %>></dialog>))
end

test "Conditional attribute with ERB control flow with surrounding spaces" do
assert_parsed_snapshot(%(<dialog data-controller="dialog" <% if local_assigns[:permanent] %> data-turbo-permanent <% end %>></dialog>))
end

test "Conditional attribute with ERB on separate line" do
assert_parsed_snapshot <<~HTML
<span
<% if @replaceable %>data-menu-button<% end %>
class="css-truncate css-truncate-target"
></span>
HTML
end

test "Conditional attribute with value" do
assert_parsed_snapshot(%(<div <% if enabled? %>data-enabled="true"<% end %>></div>))
end

test "Multiple conditional attributes" do
assert_parsed_snapshot(%(<div <% if a? %>data-a<% end %> <% if b? %>data-b<% end %>></div>))
end

test "Conditional attribute with elsif and else" do
assert_parsed_snapshot(%(<div <% if primary? %>data-primary<% elsif secondary? %>data-secondary<% else %>data-default<% end %>></div>))
end

test "Conditional attribute with unless" do
assert_parsed_snapshot(%(<div <% unless disabled? %>data-enabled<% end %>></div>))
end

test "Multiple attributes in one conditional block" do
assert_parsed_snapshot(%(<div <% if admin? %>data-admin data-role="admin"<% end %>></div>))
end

test "Conditional boolean attribute" do
assert_parsed_snapshot(%(<input <% if should_disable? %>disabled<% end %> type="text">))
end

test "Conditional attribute with output tag inside" do
assert_parsed_snapshot(%(<div <% if user %>data-user="<%= user.id %>"<% end %>></div>))
end

test "ERB comment between attributes" do
assert_parsed_snapshot(%(<div data-foo="bar" <%# This is a comment %> data-baz="qux"></div>))
end

test "Conditional attribute with trimming tags" do
assert_parsed_snapshot(%(<div <%- if condition -%>data-conditional<%- end -%>></div>))
end

test "Empty conditional block in attributes" do
assert_parsed_snapshot(%(<div data-static <% if false %><% end %> data-other="value"></div>))
end

test "Nested conditional attributes" do
assert_parsed_snapshot(%(<div <% if outer? %><% if inner? %>data-inner<% end %><% end %>></div>))
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading