diff --git a/src/template_compiler_scanner.erl b/src/template_compiler_scanner.erl index 6596c8a..cf953e2 100644 --- a/src/template_compiler_scanner.erl +++ b/src/template_compiler_scanner.erl @@ -286,41 +286,30 @@ scan(<<"_\'", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_code, Closer}) scan(<<$', T/binary>>, Scanned, {SourceRef, Row, Column}, {in_identifier, Closer}) -> scan(T, [{string_literal, {SourceRef, Row, Column}, <<>>} | Scanned], {SourceRef, Row, Column + 1}, {in_single_quote, Closer}); -scan(<<"`", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_code, Closer}) -> +scan(<<$`, T/binary>>, Scanned, {SourceRef, Row, Column}, {in_code, Closer}) -> scan(T, [{atom_literal, {SourceRef, Row, Column}, <<>>} | Scanned], {SourceRef, Row, Column + 1}, {in_back_quote, Closer}); scan(<<$`, T/binary>>, Scanned, {SourceRef, Row, Column}, {in_identifier, Closer}) -> scan(T, [{atom_literal, {SourceRef, Row, Column}, <<>>} | Scanned], {SourceRef, Row, Column + 1}, {in_back_quote, Closer}); scan(<<$\\, $", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_double_quote, Closer}) -> - scan(T, append_char(Scanned, $"), {SourceRef, Row, Column + 1}, {in_double_quote_slash, Closer}); - -scan(<>, Scanned, {SourceRef, Row, Column}, {in_double_quote_slash, Closer}) -> - scan(T, append_char(Scanned, H), {SourceRef, Row, Column + 1}, {in_double_quote, Closer}); + scan(T, append_char(append_char(Scanned, $\\), $"), {SourceRef, Row, Column + 2}, {in_double_quote, Closer}); scan(<<$\\, $', T/binary>>, Scanned, {SourceRef, Row, Column}, {in_single_quote, Closer}) -> - scan(T, append_char(Scanned, $'), {SourceRef, Row, Column + 1}, {in_single_quote_slash, Closer}); - -scan(<>, Scanned, {SourceRef, Row, Column}, {in_single_quote_slash, Closer}) -> - scan(T, append_char(Scanned, H), {SourceRef, Row, Column + 1}, {in_single_quote, Closer}); - -scan(<<$\\, T/binary>>, Scanned, {SourceRef, Row, Column}, {in_back_quote, Closer}) -> - scan(T, append_char(Scanned, $\\), {SourceRef, Row, Column + 1}, {in_back_quote_slash, Closer}); - -scan(<>, Scanned, {SourceRef, Row, Column}, {in_back_quote_slash, Closer}) -> - scan(T, append_char(Scanned, H), {SourceRef, Row, Column + 1}, {in_back_quote, Closer}); - + scan(T, append_char(append_char(Scanned, $\\), $'), {SourceRef, Row, Column + 2}, {in_single_quote, Closer}); +scan(<<$\\, $`, T/binary>>, Scanned, {SourceRef, Row, Column}, {in_back_quote, Closer}) -> + scan(T, append_char(append_char(Scanned, $\\), $`), {SourceRef, Row, Column + 2}, {in_back_quote, Closer}); % end quote -scan(<<"\"", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_double_quote, Closer}) -> +scan(<<$", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_double_quote, Closer}) -> scan(T, Scanned, {SourceRef, Row, Column + 1}, {in_code, Closer}); % treat single quotes the same as double quotes -scan(<<"\'", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_single_quote, Closer}) -> +scan(<<$', T/binary>>, Scanned, {SourceRef, Row, Column}, {in_single_quote, Closer}) -> scan(T, Scanned, {SourceRef, Row, Column + 1}, {in_code, Closer}); -scan(<<"`", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_back_quote, Closer}) -> +scan(<<$`, T/binary>>, Scanned, {SourceRef, Row, Column}, {in_back_quote, Closer}) -> scan(T, Scanned, {SourceRef, Row, Column + 1}, {in_code, Closer}); scan(<>, Scanned, {SourceRef, Row, Column}, {in_double_quote, Closer}) -> diff --git a/test/template_compiler_quote_SUITE.erl b/test/template_compiler_quote_SUITE.erl new file mode 100644 index 0000000..9c51c2f --- /dev/null +++ b/test/template_compiler_quote_SUITE.erl @@ -0,0 +1,75 @@ +-module(template_compiler_quote_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-compile(export_all). + + +suite() -> + [ + {timetrap, {seconds, 30}} + ]. + +all() -> + [ + {group, basic} + ]. + +groups() -> + [{basic, [], + [single_quote_test + ,single_quote_slash_test + ,double_quote_test + ,double_quote_slash_test + ,back_quote_test + ,back_quote_slash_test + ]}]. + +init_per_suite(Config) -> + {ok, _} = application:ensure_all_started(template_compiler), + application:set_env(template_compiler, template_dir, test_data_dir(Config)), + Config. + +end_per_suite(_Config) -> + ok. + +init_per_group(basic, Config) -> + Config. + +end_per_group(basic, _Config) -> + ok. + +single_quote_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_single.tpl", #{}, [], undefined), + <<"
<<"foo">>
'bar'">> = iolist_to_binary(Bin), + ok. + +single_quote_slash_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_single_slash.tpl", #{}, [], undefined), + <<"
<<"'foo'">>
\\'bar\\'">> = iolist_to_binary(Bin), + ok. + +double_quote_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_double.tpl", #{}, [], undefined), + <<"
<<"foo">>
\"bar\"">> = iolist_to_binary(Bin), + ok. + +double_quote_slash_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_double_slash.tpl", #{}, [], undefined), + <<"
<<"\\"foo\\"">>
\\\"bar\\\"">> = iolist_to_binary(Bin), + ok. + +back_quote_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_back.tpl", #{}, [], undefined), + <<"
foo
`bar`">> = iolist_to_binary(Bin), + ok. + +back_quote_slash_test(_Config) -> + {ok, Bin} = template_compiler:render("quote_back_slash.tpl", #{}, [], undefined), + <<"
'\\\\`foo\\\\`'
\\`bar\\`">> = iolist_to_binary(Bin), + ok. + +test_data_dir(Config) -> + filename:join([ + filename:dirname(filename:dirname(?config(data_dir, Config))), + "test-data"]). diff --git a/test/test-data/quote_back.tpl b/test/test-data/quote_back.tpl new file mode 100644 index 0000000..91118ef --- /dev/null +++ b/test/test-data/quote_back.tpl @@ -0,0 +1 @@ +{% print `foo` %}`bar` \ No newline at end of file diff --git a/test/test-data/quote_back_slash.tpl b/test/test-data/quote_back_slash.tpl new file mode 100644 index 0000000..28b513b --- /dev/null +++ b/test/test-data/quote_back_slash.tpl @@ -0,0 +1 @@ +{% print `\`foo\`` %}\`bar\` \ No newline at end of file diff --git a/test/test-data/quote_double.tpl b/test/test-data/quote_double.tpl new file mode 100644 index 0000000..8e56c88 --- /dev/null +++ b/test/test-data/quote_double.tpl @@ -0,0 +1 @@ +{% print "foo" %}"bar" \ No newline at end of file diff --git a/test/test-data/quote_double_slash.tpl b/test/test-data/quote_double_slash.tpl new file mode 100644 index 0000000..37323f0 --- /dev/null +++ b/test/test-data/quote_double_slash.tpl @@ -0,0 +1 @@ +{% print "\"foo\"" %}\"bar\" \ No newline at end of file diff --git a/test/test-data/quote_single.tpl b/test/test-data/quote_single.tpl new file mode 100644 index 0000000..7e0bdb0 --- /dev/null +++ b/test/test-data/quote_single.tpl @@ -0,0 +1 @@ +{% print 'foo' %}'bar' \ No newline at end of file diff --git a/test/test-data/quote_single_slash.tpl b/test/test-data/quote_single_slash.tpl new file mode 100644 index 0000000..6b79ac3 --- /dev/null +++ b/test/test-data/quote_single_slash.tpl @@ -0,0 +1 @@ +{% print '\'foo\'' %}\'bar\' \ No newline at end of file