1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Properly compile single quotes and double quotes

This commit is contained in:
Takashi Kokubun 2015-11-23 13:57:54 +09:00
parent 11ebffa084
commit 601b17bb95
2 changed files with 13 additions and 6 deletions

View file

@ -9,8 +9,6 @@ module Hamlit::StringInterpolation
strip_comment!(tokens)
raise Hamlit::InternalError if tokens.size < 2
strip_quotes!(tokens)
compile_tokens!(exps, tokens)
end
end
@ -24,20 +22,24 @@ module Hamlit::StringInterpolation
end
def strip_quotes!(tokens)
_, type, _ = tokens.shift
_, type, beg_str = tokens.shift
raise Hamlit::InternalError if type != :on_tstring_beg
_, type, _ = tokens.pop
_, type, end_str = tokens.pop
raise Hamlit::InternalError if type != :on_tstring_end
[beg_str, end_str]
end
def compile_tokens!(exps, tokens)
beg_str, end_str = strip_quotes!(tokens)
until tokens.empty?
_, type, str = tokens.shift
case type
when :on_tstring_content
exps << [:static, str]
exps << [:static, eval("#{beg_str}#{str}#{end_str}")]
when :on_embexpr_beg
embedded = shift_balanced_embexpr(tokens)
exps << [:dynamic, embedded] unless embedded.empty?

View file

@ -15,9 +15,14 @@ describe Hamlit::StringInterpolation do
it { assert_compile([[:static, ' '], [:dynamic, %q[ " #{ '#{}' } " ]]], %q|" #{ " #{ '#{}' } " }"|) }
it { assert_compile([[:static, 'a'], [:dynamic, 'b'], [:static, 'c'], [:dynamic, 'd'], [:static, 'e']], %q|%Q[a#{b}c#{d}e]|) }
it { assert_compile([[:static, 'a#{b}c#{d}e']], %q|%q[a#{b}c#{d}e]|) }
it { assert_compile([[:static, '\#{}'], [:dynamic, '123']], %q|"\#{}#{123}"|) }
it { assert_compile([[:static, '#{}'], [:dynamic, '123']], %q|"\#{}#{123}"|) }
it { assert_compile([[:dynamic, " '}' "]], %q|"#{ '}' }"|) }
it { assert_compile([[:static, 'a']], %q| "a" # hello |) }
it { assert_compile([[:static, '"']], %q|"\""|) }
it { assert_compile([[:static, '\\"']], %q|"\\\\\\""|) }
it { assert_compile([[:static, '\"']], %q|'\"'|) }
it { assert_compile([[:static, '\"']], %q|'\\"'|) }
it { assert_compile([[:static, '\\"']], %q|'\\\"'|) }
describe 'invalid argument' do
it 'raises internal error' do