Skip to content

Commit

Permalink
Parse string interpolation in attribute value
Browse files Browse the repository at this point in the history
Fix #917
  • Loading branch information
k0kubun committed May 1, 2017
1 parent 35f131f commit c67e672
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/haml/attribute_parser.rb
Expand Up @@ -131,6 +131,10 @@ def each_balaned_tokens(tokens, &block)
open_tokens[:paren] += 1
when :on_rparen
open_tokens[:paren] -= 1
when :on_embexpr_beg
open_tokens[:embexpr] += 1
when :on_embexpr_end
open_tokens[:embexpr] -= 1
when *IGNORED_TYPES
next if attr_tokens.empty?
end
Expand Down
4 changes: 4 additions & 0 deletions test/attribute_parser_test.rb
Expand Up @@ -65,6 +65,10 @@ def assert_parse(expected, haml)
it { assert_parse(nil, '%Q[f#{o}o] => bar ') }
end

describe 'string interpolation' do
it { assert_parse({ 'foo' => %q["#{bar baz, '/abc/'}"] }, %q[{foo: "#{bar baz, '/abc/'}"}]) }
end

describe 'multi lines' do
it { assert_parse({ 'a' => 'b', 'c' => 'd' }, "{a: b,\nc: d}") }
end
Expand Down
7 changes: 7 additions & 0 deletions test/engine_test.rb
Expand Up @@ -151,6 +151,13 @@ def test_ruby_code_should_work_inside_attributes
assert_equal("<p class='3'>foo</p>", render("%p{:class => 1+2} foo").chomp)
end

def test_attributes_with_interpolation
assert_equal("<iframe id='test' src='http://www.google.com/abc/'></iframe>\n", render(<<-'HAML'))
- base_url = "http://www.google.com"
%iframe#test{src: "#{File.join base_url, '/abc/'}"}
HAML
end

def test_class_attr_with_array
assert_equal("<p class='a b'>foo</p>\n", render("%p{:class => %w[a b]} foo")) # basic
assert_equal("<p class='a b css'>foo</p>\n", render("%p.css{:class => %w[a b]} foo")) # merge with css
Expand Down

0 comments on commit c67e672

Please sign in to comment.