Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/surface/compiler/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ defmodule Surface.Compiler.Converter do
[{:unquoted_string, meta} | acc]
end

defp extract_meta({:string, _name, %{delimiter: ?"} = meta}, acc) do
meta = %{meta | column: meta.column - 1, column_end: meta.column_end + 1}
[{:double_quoted_string, meta} | acc]
end

defp extract_meta(_node, acc) do
acc
end
Expand Down
10 changes: 10 additions & 0 deletions lib/surface/compiler/converter_0_5.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ defmodule Surface.Compiler.Converter_0_5 do
"{#{value}}"
end

def convert(:double_quoted_string, value, _state, _opts) do
new_value = Regex.replace(~r/{{(.+?)}}/, value, "\#{\\1}")

if new_value != value do
"{#{new_value}}"
else
value
end
end

def convert(:tag_name, "template", _state, _opts) do
"#template"
end
Expand Down
14 changes: 12 additions & 2 deletions lib/surface/compiler/tokenizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ defmodule Surface.Compiler.Tokenizer do
end

defp handle_attr_value_begin("\"" <> rest, line, column, acc, state) do
acc = put_attr_value(acc, {:string, nil, %{line: line, column: column + 1, delimiter: ?"}})
handle_attr_value_double_quote(rest, line, column + 1, [], acc, state)
end

defp handle_attr_value_begin("'" <> rest, line, column, acc, state) do
acc = put_attr_value(acc, {:string, nil, %{line: line, column: column + 1, delimiter: ?'}})
handle_attr_value_single_quote(rest, line, column + 1, [], acc, state)
end

Expand Down Expand Up @@ -388,7 +390,11 @@ defmodule Surface.Compiler.Tokenizer do

defp handle_attr_value_double_quote("\"" <> rest, line, column, buffer, acc, state) do
value = buffer_to_string(buffer)
acc = put_attr_value(acc, {:string, value, %{delimiter: ?"}})

acc =
update_attr_value(acc, fn {type, _old_value, meta} ->
{type, value, Map.merge(meta, %{line_end: line, column_end: column})}
end)

handle_maybe_tag_open_end(rest, line, column + 1, acc, state)
end
Expand All @@ -415,7 +421,11 @@ defmodule Surface.Compiler.Tokenizer do

defp handle_attr_value_single_quote("'" <> rest, line, column, buffer, acc, state) do
value = buffer_to_string(buffer)
acc = put_attr_value(acc, {:string, value, %{delimiter: ?'}})

acc =
update_attr_value(acc, fn {type, _old_value, meta} ->
{type, value, Map.merge(meta, %{line_end: line, column_end: column})}
end)

handle_maybe_tag_open_end(rest, line, column + 1, acc, state)
end
Expand Down
23 changes: 23 additions & 0 deletions test/compiler/converter_0_5_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,29 @@ defmodule Surface.Compiler.Converter_0_5Test do
"""
end

test "convert literal strings with embedded interpolation" do
expected =
convert("""
<div id="id_{{@id}}">
<div id=
"
id_{{@id}}
">
</div>
</div>
""")

assert expected == """
<div id={"id_\#{@id}"}>
<div id=
{"
id_\#{@id}
"}>
</div>
</div>
"""
end

## Planned changes. Uncomment as the related implementation gets merged

# test "convert <For> into <#For>" do
Expand Down
28 changes: 28 additions & 0 deletions test/compiler/tokenizer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ defmodule Surface.Compiler.TokenizerTest do
] = tokens
end

test "compute line and columns" do
attrs =
tokenize_attrs("""
<div
title="first
second
third">\
""")

assert [
{"title", {:string, _, %{line: 2, column: 10, line_end: 4, column_end: 6}}, %{}}
] = attrs
end

test "raise on incomplete attribute value (EOF)" do
assert_raise ParseError, "nofile:2:15: expected closing `\"` for attribute value", fn ->
tokenize!("""
Expand Down Expand Up @@ -380,6 +394,20 @@ defmodule Surface.Compiler.TokenizerTest do
] = tokens
end

test "compute line and columns" do
attrs =
tokenize_attrs("""
<div
title='first
second
third'>\
""")

assert [
{"title", {:string, _, %{line: 2, column: 10, line_end: 4, column_end: 6}}, %{}}
] = attrs
end

test "raise on incomplete attribute value (EOF)" do
assert_raise ParseError, "nofile:2:15: expected closing `\'` for attribute value", fn ->
tokenize!("""
Expand Down