Skip to content

Commit

Permalink
Merge pull request #355 from pragdave/i353-oneline-tags-broken
Browse files Browse the repository at this point in the history
Refs: #353; Setting up tests WIP [skip-ci]
  • Loading branch information
RobertDober authored Jun 21, 2020
2 parents 45130b7 + a2a85bc commit 49842a5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
9 changes: 4 additions & 5 deletions lib/earmark/helpers/html_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ defmodule Earmark.Helpers.HtmlParser do

@tag_tail ~r{\A.*?(/?)>\s*(.*)\z}
defp _parse_tag_tail(string, tag, atts) do
case Regex.run(@tag_tail, string) do
case Regex.run(@tag_tail, string) do
[_, closing, suffix] ->
_close_tag_tail(tag, atts, closing != "", suffix)
suffix1 = String.replace(suffix, ~r{\s*</#{tag}>.*}, "")
_close_tag_tail(tag, atts, closing != "", suffix1)
# [_, _, ""] -> {:ok, {tag, Enum.reverse(atts)} }
# [_, "", suffix] -> {:ok, {tag, Enum.reverse(atts)}, suffix }
# [_, _closing, suffix] -> {:ext, {tag, Enum.reverse(atts)}, suffix }
Expand All @@ -61,10 +62,8 @@ defmodule Earmark.Helpers.HtmlParser do

@verbatim %{meta: %{verbatim: true}}
defp _parse_rest(rest, tag_tpl, lines)
# Hu? That should never have happened but let us return some
# sensible data, allowing rendering after the corruped block
defp _parse_rest([], tag_tpl, lines) do
tag_tpl |> Tuple.append(Enum.reverse(lines))
tag_tpl |> Tuple.append(Enum.reverse(lines)) |> Tuple.append(@verbatim)
end
defp _parse_rest([last_line], {tag, _}=tag_tpl, lines) do
case Regex.run(~r{\A</#{tag}>\s*(.*)}, last_line) do
Expand Down
12 changes: 12 additions & 0 deletions lib/earmark/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ defmodule Earmark.Transform do
[make_indent(options, level), element]
end
# Void tags: `area`, `br`, `hr`, `img`, and `wbr` are rendered slightly differently
# TODO: Remove 3 tuple matches in Release 1.4.6
defp _to_html({"area", _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"area", _, _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"br", _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"br", _, _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"hr", _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"hr", _, _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"img", _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"img", _, _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"wbr", _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({"wbr", _, _, _}=tag, options, level, _verbatim), do: void_tag(tag, options, level)
defp _to_html({:comment, children}, options, level, _verbatim) do
indent = make_indent(options, level)
[ indent,
Expand Down Expand Up @@ -161,9 +167,15 @@ defmodule Earmark.Transform do
end
defp smartypants(text, _options), do: text

# TODO: Remove 3 tuple matches in Release 1.4.6
defp void_tag({tag, atts, []}, options, level) do
[ make_indent(options, level),
open_tag(tag, atts, true),
"\n" ]
end
defp void_tag({tag, atts, [], _}, options, level) do
[ make_indent(options, level),
open_tag(tag, atts, true),
"\n" ]
end
end
36 changes: 18 additions & 18 deletions test/acceptance/ast/html/block_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Acceptance.Ast.Html.BlockTest do
use ExUnit.Case, async: true
import Support.Helpers, only: [as_ast: 1, parse_html: 1]
import Support.AstHelpers, only: [p: 1, void_tag: 1]
import Support.Helpers, only: [as_ast: 1]
import Support.AstHelpers, only: [p: 1, tag: 2, verb_tag: 1, verb_tag: 3]

@verbatim %{meta: %{verbatim: true}}

Expand Down Expand Up @@ -44,44 +44,44 @@ defmodule Acceptance.Ast.Html.BlockTest do
describe "HTML void elements" do
test "area" do
markdown = "<area shape=\"rect\" coords=\"0,0,1,1\" href=\"xxx\" alt=\"yyy\">\n**emphasized** text"
html = "<area shape=\"rect\" coords=\"0,0,1,1\" href=\"xxx\" alt=\"yyy\"><p><strong>emphasized</strong> text</p>\n"
ast = parse_html(html)
ast = [
verb_tag("area", nil, shape: "rect", coords: "0,0,1,1", href: "xxx", alt: "yyy"),
p([tag("strong", "emphasized"), " text"])]

messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "we are outside the void now (lucky us)" do
markdown = "<br>\n**emphasized** text"
html = "<br><p><strong>emphasized</strong> text</p>\n"
ast = parse_html(html)
ast = [
verb_tag("br"), p([tag("strong", "emphasized"), " text"])]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "high regards???" do
markdown = "<hr>\n**emphasized** text"
html = "<hr><p><strong>emphasized</strong> text</p>\n"
ast = parse_html(html)
ast = [verb_tag("hr"), p([tag("strong", "emphasized"), " text"])]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "img (a punless test)" do
markdown = "<img src=\"hello\">\n**emphasized** text"
html = "<img src=\"hello\"><p><strong>emphasized</strong> text</p>\n"
ast = parse_html(html)
ast = [verb_tag("img", [], src: "hello"), p([tag("strong", "emphasized"), " text"])]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "not everybody knows this one (hint: take a break)" do
markdown = "<wbr>\n**emphasized** text"
html = "<wbr><p><strong>emphasized</strong> text</p>\n"
ast = parse_html(html)
ast = [
verb_tag("wbr"), p([tag("strong", "emphasized"), " text"])]
messages = []
assert as_ast(markdown) == {:ok, ast, messages}
end
Expand All @@ -91,7 +91,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "void elements close para" do
markdown = "alpha\n<hr>beta"
# We ignore beta now shall we deprecate for HTML????
ast = [p("alpha"), void_tag("hr"), "beta"]
ast = [p("alpha"), verb_tag("hr"), "beta"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand All @@ -108,7 +108,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "self closing block elements close para" do
markdown = "alpha\n<div/>beta"
# We ignore beta now shall we deprecate for HTML????
ast =[{"p", [], ["alpha"]}, {"div", [], []}, "beta"]
ast =[{"p", [], ["alpha"]}, {"div", [], [], @verbatim}, "beta"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand All @@ -117,7 +117,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "self closing block elements close para, atts do not matter" do
markdown = "alpha\n<div class=\"first\"/>beta"
# We ignore beta now shall we deprecate for HTML????
ast = [{"p", [], ["alpha"]}, {"div", [{"class", "first"}], []}, "beta"]
ast = [{"p", [], ["alpha"]}, {"div", [{"class", "first"}], [], @verbatim}, "beta"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand All @@ -126,7 +126,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "self closing block elements close para, atts and spaces do not matter" do
markdown = "alpha\n<div class=\"first\" />beta\ngamma"
# We ignore beta now shall we deprecate for HTML????
ast = [{"p", [], ["alpha"]}, {"div", [{"class", "first"}], []}, "beta", {"p", [], ["gamma"]}]
ast = [{"p", [], ["alpha"]}, {"div", [{"class", "first"}], [], @verbatim}, "beta", {"p", [], ["gamma"]}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand All @@ -153,7 +153,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "block elements close para" do
markdown = "alpha\n<div></div>beta"
# SIC just do not write that markup
ast = [{"p", [], ["alpha"]}, {"div", [], ["</div>beta"]}]
ast = [{"p", [], ["alpha"]}, {"div", [], [], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand All @@ -162,7 +162,7 @@ defmodule Acceptance.Ast.Html.BlockTest do
test "block elements close para, atts do not matter" do
markdown = "alpha\n<div class=\"first\"></div>beta"
# SIC just do not write that markup
ast = [{"p", [], ["alpha"]}, {"div", [{"class", "first"}], ["</div>beta"]}]
ast = [p("alpha"), verb_tag("div", [], class: "first")]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand Down
24 changes: 24 additions & 0 deletions test/acceptance/ast/html/oneline_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Acceptance.Ast.Html.OnelineTest do
use ExUnit.Case, async: true
import Support.Helpers, only: [as_ast: 1]
import Support.AstHelpers, only: [verb_tag: 2, verb_tag: 3]

describe "oneline tags" do
test "really simple" do
markdown = "<h1>Headline</h1>"
ast = [verb_tag("h1", "Headline")]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "a little bit more complicated" do
markdown = ~s{<p align="center"><img src="image.svg"/></p>}
ast = [verb_tag("p", ["<img src=\"image.svg\"/>"], align: "center")]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
end
end

3 changes: 2 additions & 1 deletion test/acceptance/html/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ defmodule Acceptance.Html.HtmlBlocksTest do
assert as_html(markdown) == {:ok, html, messages}
end

@tag :wip
test "self closing block elements close para, atts and spaces do not matter" do
markdown = "alpha\n<div class=\"first\" />beta\ngamma"
html = gen([
{:p, "alpha"},
{:div, [class: "first"], []},
{:div, [class: "first"], [""]},
"beta",
{:p, "gamma"}])
messages = []
Expand Down
18 changes: 18 additions & 0 deletions test/support/ast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ defmodule Support.AstHelpers do
def p(content, atts),
do: {"p", atts, content}

def tag(name, content \\ nil, atts \\ []) do
{to_string(name), _atts(atts), _content(content)}
end

def verb_tag(tag, content \\ nil, atts \\ []) do
{to_string(tag), _atts(atts), _content(content), _verbatim()}
end
def void_tag(tag, atts \\ []) do
{to_string(tag), atts, []}
end


defp _atts(atts) do
atts |> Enum.into(Keyword.new) |> Enum.map(fn {x, y} -> {to_string(x), to_string(y)} end)
end

defp _content(c)
defp _content(nil), do: []
defp _content(s) when is_binary(s), do: [s]
defp _content(c), do: c

defp _verbatim, do: %{meta: %{verbatim: true}}
end

0 comments on commit 49842a5

Please sign in to comment.