Skip to content

Commit

Permalink
Fix mixed nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Apr 25, 2017
1 parent 3cab87b commit 8f3ac34
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/slime/tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ defmodule Slime.Tree do
|> Enum.map(&to_branch/1)
filter = make_child_filter(indentation)
{children, rem} = Enum.split_while(t, filter)
split_children? = existing == [] && children != [] &&
Application.get_env(:slime, :keep_lines)
sep = if split_children?, do: [%TextNode{content: ""}], else: []
children_tree = existing ++ sep ++ build_tree(children)
children_tree = children |> build_tree |> append_to(existing)
attrs = Keyword.put(attrs, :children, children_tree)
branch = to_branch({tag, attrs})
tree = build_tree(rem)
Expand All @@ -45,6 +42,20 @@ defmodule Slime.Tree do
end
end

defp append_to([], existing), do: existing
defp append_to(children, []), do: sep() ++ children
defp append_to(children, existing = [%TextNode{}]), do: existing ++ children
defp append_to(children, [html_node = %HTMLNode{children: node_children}]) do
case node_children do
[%TextNode{}] -> [html_node] ++ children
_ -> [%{html_node|children: append_to(children, node_children)}]
end
end

defp sep() do
Application.get_env(:slime, :keep_lines) && [%TextNode{content: ""}] || []
end

defp to_branch(%{} = branch), do: branch
defp to_branch(text) when is_binary(text) do
%TextNode{content: text}
Expand Down
25 changes: 25 additions & 0 deletions test/renderer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ defmodule RendererTest do
""" |> String.replace("\n", "")
end

test "render mixed nesting" do
slime = ~s"""
.wrap: .row: .col-lg-12
.box: p One
.box: p Two
p Three
"""

assert render(slime) == """
<div class="wrap">
<div class="row">
<div class="col-lg-12">
<div class="box">
<p>One</p>
</div>
<div class="box">
<p>Two</p>
</div>
</div>
</div>
</div>
<p>Three</p>
""" |> String.replace("\n", "")
end

test "render closed tag (ending with /)" do
assert render(~s(img src="image.png"/)) == ~s(<img src="image.png"/>)
end
Expand Down
52 changes: 52 additions & 0 deletions test/tree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,56 @@ defmodule TreeTest do

assert parsed == expected
end

test "creates tree with mixed nesting" do
expected = [
%HTMLNode{
tag: :div,
attributes: [class: ~w(wrap)],
children: [
%HTMLNode{
tag: :div,
attributes: [class: ~w(row)],
children: [
%HTMLNode{
tag: :div,
attributes: [class: ~w(col-lg-12)],
children: [
%HTMLNode{
tag: :div,
attributes: [class: ~w(box)],
children: [
%HTMLNode{
tag: :p,
attributes: [],
children: [%TextNode{content: "One"}]}]},
%HTMLNode{
tag: :p,
attributes: [],
children: [%TextNode{content: "Two"}]}]}]}]},
%HTMLNode{
tag: :p,
attributes: [],
children: [%TextNode{content: "Three"}]}]

parsed = [
{0, {:div,
attributes: [class: ~w(wrap)],
children: [
{:div,
attributes: [class: ~w(row)],
children: [
{:div,
attributes: [class: ~w(col-lg-12)],
children: []}]}]}},
{2, {:div,
attributes: [class: ~w(box)],
children: [
{:p, attributes: [], children: ~w(One)}]}},
{2, {:p, attributes: [], children: ~w(Two)}},
{0, {:p, attributes: [], children: ~w(Three)}}
] |> Tree.build_tree

assert parsed == expected
end
end

0 comments on commit 8f3ac34

Please sign in to comment.