Skip to content

Commit

Permalink
Handles ERB-tag inside HTML tags
Browse files Browse the repository at this point in the history
```erb
<div <%= "class='foo'" %>>
  <p>hello</p>
</div>
```
  • Loading branch information
davidwessman committed Aug 30, 2023
1 parent d2b7eea commit 9729f2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

- Handle ERB-tags inside HTML-tags, like `<div <%= "class='foo'" %>>`

## [0.10.4] - 2023-08-28

- Avoid grouping single tags
Expand Down
9 changes: 8 additions & 1 deletion lib/syntax_tree/erb/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,17 @@ def parse_until_erb(classes:)
def parse_html_opening_tag
opening = consume(:open)
name = consume(:name)

if name.value =~ /\A[@:#]/
raise ParseError, "Invalid html-tag name #{name}"
end
attributes = many { parse_html_attribute }

attributes =
many do
atleast do
maybe { parse_erb_tag } || maybe { parse_html_attribute }
end
end

closing =
atleast do
Expand Down
7 changes: 7 additions & 0 deletions test/erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def test_if_and_end_in_same_tag
assert_formatting(source, expected)
end

def test_erb_inside_html_tag
source = "<div <% if eeee then \"b\" else c end %>></div>"
expected = "<div <% eeee ? \"b\" : c %>></div>\n"

assert_formatting(source, expected)
end

def test_long_if_statement
source =
"<%=number_to_percentage(@reports&.first&.stability*100,precision: 1) if @reports&.first&.other&.stronger&.longer %>"
Expand Down

0 comments on commit 9729f2f

Please sign in to comment.