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:
parent
11ebffa084
commit
601b17bb95
2 changed files with 13 additions and 6 deletions
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue