Skip to content
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
10 changes: 8 additions & 2 deletions src/analyze_conditional_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
if (!is_simple_erb_conditional(open_node)) { continue; }

hb_array_T* open_statements = get_erb_conditional_statements(open_node);
size_t open_tag_count = count_nodes_of_type(open_statements, AST_HTML_OPEN_TAG_NODE);

size_t open_tag_count = count_nodes_of_type(open_statements, AST_HTML_OPEN_TAG_NODE);
if (open_tag_count < 2) { continue; }

size_t open_close_tag_count = count_nodes_of_type(open_statements, AST_HTML_CLOSE_TAG_NODE);
if (open_tag_count <= open_close_tag_count) { continue; }

bool open_is_if;
const char* open_condition = extract_condition_from_erb_content(open_node, &open_is_if);

Expand All @@ -242,10 +245,13 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
if (!is_simple_erb_conditional(close_node)) { continue; }

hb_array_T* close_statements = get_erb_conditional_statements(close_node);
size_t close_tag_count = count_nodes_of_type(close_statements, AST_HTML_CLOSE_TAG_NODE);

size_t close_tag_count = count_nodes_of_type(close_statements, AST_HTML_CLOSE_TAG_NODE);
if (close_tag_count < 2) { continue; }

size_t close_open_tag_count = count_nodes_of_type(close_statements, AST_HTML_OPEN_TAG_NODE);
if (close_tag_count <= close_open_tag_count) { continue; }

bool close_is_if;
const char* close_condition = extract_condition_from_erb_content(close_node, &close_is_if);

Expand Down
8 changes: 0 additions & 8 deletions src/analyze_conditional_open_tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ static single_open_tag_result_T get_single_open_tag_from_statements(hb_array_T*

if (!node) { continue; }

if (node->type == AST_WHITESPACE_NODE) { continue; }
if (node->type == AST_HTML_CLOSE_TAG_NODE) { continue; }
if (node->type == AST_ERB_CONTENT_NODE) { continue; }
if (node->type == AST_HTML_ELEMENT_NODE) { continue; }
if (node->type == AST_ERB_BLOCK_NODE) { continue; }

if (node->type == AST_HTML_TEXT_NODE) {
AST_HTML_TEXT_NODE_T* text = (AST_HTML_TEXT_NODE_T*) node;
bool whitespace_only = true;
Expand Down Expand Up @@ -125,8 +119,6 @@ static single_open_tag_result_T get_single_open_tag_from_statements(hb_array_T*

continue;
}

return result;
}

if (tag_count != 1) {
Expand Down
88 changes: 88 additions & 0 deletions test/analyze/conditional_element_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,93 @@ class ConditionalElementTest < Minitest::Spec
<% end %>
HTML
end

test "complete elements inside matching if blocks should not trigger multiple tags error" do
assert_parsed_snapshot(<<~HTML)
<% if condition %>
<div>
<h2>Title</h2>
<p>Description</p>
</div>
<% end %>

<% if condition %>
<div>
<h2>Title</h2>
<p>Description</p>
</div>
<% end %>
HTML
end

test "complete elements with same condition in nested scope should not trigger error" do
assert_parsed_snapshot(<<~HTML)
<div>
<% if @is_owner %>
<div class="section">
<h2>Title</h2>
<p>Description</p>
</div>
<% end %>

<% if @is_owner %>
<div class="section">
<h2>Title</h2>
<p>Description</p>
</div>
<% end %>
</div>
HTML
end

test "matching unless blocks with balanced tags should not trigger error" do
assert_parsed_snapshot(<<~HTML)
<% unless hidden %>
<div>
<h2>Title</h2>
</div>
<% end %>
<% unless hidden %>
<div>
<span>Content</span>
</div>
<% end %>
HTML
end

test "if blocks with deeply nested balanced tags should not trigger error" do
assert_parsed_snapshot(<<~HTML)
<% if condition %>
<div>
<section>
<h1>Title</h1>
<p>Description</p>
</section>
</div>
<% end %>
<% if condition %>
<div>
<section>
<h1>Title</h1>
<p>Description</p>
</section>
</div>
<% end %>
HTML
end

test "actual unmatched multiple tags in matching conditionals" do
assert_parsed_snapshot(<<~HTML)
<% if condition %>
<div>
<span>
<% end %>
content
<% if condition %>
</span>
</div>
<% end %>
HTML
end
end
end
Loading
Loading