Skip to content

Commit

Permalink
Fixes #281;
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Sep 23, 2019
1 parent 518ded0 commit ad4955f
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 248 deletions.
4 changes: 1 addition & 3 deletions lib/earmark/ast/inline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ defmodule Earmark.Ast.Inline do
link = if String.at(link, 6) == ":", do: behead(link, 7), else: link
text = link
href = "mailto:" <> text
{encode(href), text}
{href, text}
end
defp convert_autolink(link, _separator) do
link = encode(link)
{link, link}
end

Expand Down Expand Up @@ -301,7 +300,6 @@ defmodule Earmark.Ast.Inline do
end

defp output_link(context, text, href, title, lnb) do
href = encode(href, false)
context1 = %{context | options: %{context.options | pure_links: false}}

context2 = _convert(text, lnb, set_value(context1, []), false)
Expand Down
13 changes: 0 additions & 13 deletions lib/earmark/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ defmodule Earmark.Helpers do
Regex.replace(regex, text, replacement, options)
end

@doc false
# Encode URIs to be included in the `<a>` elements.

# Percent-escapes a URI, and after that escapes any
# `&`, `<`, `>`, `"`, `'`.
def encode(html, escape \\ true)
def encode(html, true) do
html |> URI.encode |> escape(true)
end
def encode(html, false) do
URI.encode(html)
end

@doc """
Replace <, >, and quotes with the corresponding entities. If
`encode` is true, convert ampersands, too, otherwise only
Expand Down
1 change: 0 additions & 1 deletion lib/earmark/helpers/ast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ defmodule Earmark.Helpers.AstHelpers do
@remove_escapes ~r{ \\ (?! \\ ) }x
@doc false
def render_image(text, href, title) do
href = encode(href, false)
alt = text |> escape() |> String.replace(@remove_escapes, "")

if title do
Expand Down
9 changes: 2 additions & 7 deletions lib/earmark/inline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,10 @@ defmodule Earmark.Inline do
link = if String.at(link, 6) == ":", do: behead(link, 7), else: link
text = mangle_link(link)
href = mangle_link("mailto:") <> text
{encode(href), escape(text)}
{href, escape(text)}
end

defp convert_autolink(link, _separator) do
link = encode(link)
{link, link}
end

Expand All @@ -301,16 +300,15 @@ defmodule Earmark.Inline do
link
end

defp output_image_or_link(context, img_or_link, text, href, title, lnb)
defp output_image_or_link(context, "!" <> _, text, href, title, _lnb) do
output_image(context.options.renderer, text, href, title)
end

defp output_image_or_link(context, _, text, href, title, lnb) do
output_link(context, text, href, title, lnb)
end

defp output_link(context, text, href, title, lnb) do
href = encode(href)
title = if title, do: escape(title), else: nil

context1 = %{context | options: %{context.options | pure_links: false}}
Expand All @@ -324,14 +322,11 @@ defmodule Earmark.Inline do
end

defp output_footnote_link(context, ref, back_ref, number) do
ref = encode(ref)
back_ref = encode(back_ref)
context.options.renderer.footnote_link(ref, back_ref, number)
end

@remove_escapes ~r{ \\ (?! \\ ) }x
defp output_image(renderer, text, href, title) do
href = encode(href)
title = if title, do: escape(title), else: nil
alt = text |> escape() |> String.replace(@remove_escapes, "")

Expand Down
7 changes: 1 addition & 6 deletions lib/earmark/transform.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Earmark.Transform do

import Earmark.Helpers, only: [escape: 1, replace: 3]
import Earmark.Helpers, only: [replace: 3]

@moduledoc """
Public Interface to functions operating on the AST
Expand Down Expand Up @@ -91,14 +91,9 @@ defmodule Earmark.Transform do
end

defp make_att(name_value_pair, tag)
defp make_att({"src", value}, "img"), do: _make_encoded_attr("src", value)
defp make_att({"href", value}, "a"), do: _make_encoded_attr("href", value)
defp make_att({name, value}, _) do
[" ", name, "=\"", value, "\""]
end
defp _make_encoded_attr(name, value) do
[" ", name, "=\"", escape(value), "\""]
end

defp make_indent(%{indent: indent}, level) do
Stream.cycle([" "])
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/ast/footnotes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Acceptance.Ast.FootnotesTest do
messages = []

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

test "undefined footnotes" do
markdown = "foo[^1]\nhello\n\n[^2]: bar baz"
Expand Down
18 changes: 13 additions & 5 deletions test/acceptance/ast/links_images/img_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ defmodule Acceptance.Ast.LinkImages.ImgTest do
assert as_ast(markdown) == {:ok, ["", ast], messages}
end

test "url encoding is **not** our job" do
markdown = "[foo]: /url?é=42 \"title\"\n\n![foo]\n"
html = "<p><img src=\"/url?é=42\" alt=\"foo\" title=\"title\" /></p>\n"
ast = parse_html(html)
messages = []

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


test "this ain't no img (and no link)" do
markdown = "[foo]: /url \"title\"\n\n![bar]\n"
html = "<p>![bar]</p>\n"
Expand Down Expand Up @@ -114,17 +124,15 @@ defmodule Acceptance.Ast.LinkImages.ImgTest do

test "alt goes crazy, with deprecation warnings" do
markdown = "\n![foo[([])]](/url 'title\")\n"
html = "<p><img src=\"/url%20&#39;title%22\" alt=\"foo[([])]\"/></p>\n"
ast = parse_html(html)

ast = [{"p", [], [{"img", [{"src", "/url 'title\""}, {"alt", "foo[([])]"}], []}]}]
messages = []

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

test "url escapes of course" do
markdown = "![foo](/url no title)\n"
html = "<p><img src=\"/url%20no%20title\" alt=\"foo\"/></p>\n"
html = "<p><img src=\"/url no title\" alt=\"foo\"/></p>\n"
ast = parse_html(html)
messages = []

Expand Down
10 changes: 10 additions & 0 deletions test/acceptance/ast/links_images/link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ defmodule Acceptance.Ast.LinkImages.LinkTest do
assert as_ast(markdown) == {:ok, [ast], messages}
end

test "still no uri encoding here" do
markdown = "<http://foo.bar.baz?url=é>\n"
html = "<p><a href=\"http://foo.bar.baz?url=é\">http://foo.bar.baz?url=é</a></p>\n"
ast = parse_html(html)
messages = []

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


test "as was this" do
markdown = "<irc://foo.bar:2233/baz>\n"
html = "<p><a href=\"irc://foo.bar:2233/baz\">irc://foo.bar:2233/baz</a></p>\n"
Expand Down
3 changes: 1 addition & 2 deletions test/acceptance/ast/links_images/titles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ defmodule Acceptance.Ast.LinksImages.TitlesTest do

test "titled link, with deprecated quote mismatch" do
markdown = "[link](/uri \"title')\n"
html = "<p><a href=\"/uri%20%22title&#39;\">link</a></p>\n"
ast = parse_html(html)
ast = {"p", [], [{"a", [{"href", "/uri \"title'"}], ["link"]}]}
messages = []

assert as_ast(markdown) == {:ok, [ast], messages}
Expand Down
12 changes: 10 additions & 2 deletions test/acceptance/html/links_images/img_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ defmodule Acceptance.Html.LinkImages.ImgTest do
assert as_html(markdown) == {:ok, html, messages}
end

test "url encoding is **not** our job" do
markdown = "[foo]: /url?é=42 \"title\"\n\n![foo]\n"
html = "<p><img src=\"/url?é=42\" alt=\"foo\" title=\"title\" /></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end

test "this ain't no img (and no link)" do
markdown = "[foo]: /url \"title\"\n\n![bar]\n"
html = "<p>![bar]</p>\n"
Expand Down Expand Up @@ -115,7 +123,7 @@ defmodule Acceptance.Html.LinkImages.ImgTest do

test "alt goes crazy, with deprecation warnings" do
markdown = "\n![foo[([])]](/url 'title\")\n"
html = "<p><img src=\"/url%20&#39;title%22\" alt=\"foo[([])]\" /></p>\n"
html = "<p><img src=\"/url 'title\"\" alt=\"foo[([])]\" /></p>\n"

messages = []

Expand All @@ -124,7 +132,7 @@ defmodule Acceptance.Html.LinkImages.ImgTest do

test "url escapes of course" do
markdown = "![foo](/url no title)\n"
html = "<p><img src=\"/url%20no%20title\" alt=\"foo\" /></p>\n"
html = "<p><img src=\"/url no title\" alt=\"foo\" /></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
Expand Down
24 changes: 24 additions & 0 deletions test/acceptance/html/links_images/link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ defmodule Acceptance.Html.LinkImages.LinkTest do

assert as_html(markdown) == {:ok, html, messages}
end
test "assure url encoding is not done here" do
markdown = "[foo]: /url?url=https%3A%2F%2Fsomewhere \"title\"\n\n[foo]\n"
html = "<p><a href=\"/url?url=https%3A%2F%2Fsomewhere\" title=\"title\">foo</a></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end

test "link with utf8 title" do
markdown = "[foo]: /url \"Überschrift\"\n\n[foo]\n"
Expand Down Expand Up @@ -120,6 +127,14 @@ defmodule Acceptance.Html.LinkImages.LinkTest do
assert as_html(markdown) == {:ok, html, messages}
end

test "let us not uri encode the url" do
markdown = "[link](x?//)\n"
html = "<p><a href=\"x?//\">link</a></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end

test "nowhere in a bottle" do
markdown = "[link](())\n"
html = "<p><a href=\"()\">link</a></p>\n"
Expand Down Expand Up @@ -151,6 +166,15 @@ defmodule Acceptance.Html.LinkImages.LinkTest do
assert as_html(markdown) == {:ok, html, messages}
end

test "still no uri encoding here" do
markdown = "<http://foo.bar.baz?url=é>\n"
html = "<p><a href=\"http://foo.bar.baz?url=é\">http://foo.bar.baz?url=é</a></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end


test "as was this" do
markdown = "<irc://foo.bar:2233/baz>\n"
html = "<p><a href=\"irc://foo.bar:2233/baz\">irc://foo.bar:2233/baz</a></p>\n"
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/html/links_images/titles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Acceptance.Html.LinksImages.TitlesTest do

test "titled link, with deprecated quote mismatch" do
markdown = "[link](/uri \"title')\n"
html = "<p><a href=\"/uri%20%22title&#39;\">link</a></p>\n"
html = "<p><a href=\"/uri \"title'\">link</a></p>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/html1/links_images/img_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Acceptance.Html1.LinkImages.ImgTest do

test "alt goes crazy, with deprecation warnings" do
markdown = "\n![foo[([])]](/url 'title\")\n"
html = "<p>\n <img src=\"/url%20&#39;title%22\" alt=\"foo[([])]\" />\n</p>\n"
html = "<p>\n <img src=\"/url 'title\"\" alt=\"foo[([])]\" />\n</p>\n"

messages = []

Expand All @@ -129,7 +129,7 @@ defmodule Acceptance.Html1.LinkImages.ImgTest do

test "url escapes of course" do
markdown = "![foo](/url no title)\n"
html = "<p>\n <img src=\"/url%20no%20title\" alt=\"foo\" />\n</p>\n"
html = "<p>\n <img src=\"/url no title\" alt=\"foo\" />\n</p>\n"
messages = []

assert to_html1(markdown) == {:ok, html, messages}
Expand Down
24 changes: 24 additions & 0 deletions test/acceptance/html1/links_images/link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ defmodule Acceptance.Html1.LinkImages.LinkTest do
assert to_html1(markdown) == {:ok, html, messages}
end

test "assure url encoding is not done here" do
markdown = "[foo]: /url?url=https%3A%2F%2Fsomewhere \"title\"\n\n[foo]\n"
html = para( {:a, ~s{href="/url?url=https%3A%2F%2Fsomewhere" title="title"}, "foo"})
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end

test "link with utf8 title" do
markdown = "[foo]: /url \"Überschrift\"\n\n[foo]\n"
html = "<p>\n <a href=\"/url\" title=\"Überschrift\">\n foo\n </a>\n</p>\n"
Expand Down Expand Up @@ -154,6 +162,14 @@ defmodule Acceptance.Html1.LinkImages.LinkTest do
assert to_html1(markdown) == {:ok, html, messages}
end

test "let us not uri encode the url" do
markdown = "[link](x?//)\n"
html = para( {:a, ~s{href="x?//"}, "link"} )
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end

test "nowhere in a bottle" do
markdown = "[link](())\n"
html = "<p>\n <a href=\"()\">\n link\n </a>\n</p>\n"
Expand Down Expand Up @@ -195,6 +211,14 @@ defmodule Acceptance.Html1.LinkImages.LinkTest do
assert to_html1(markdown) == {:ok, html, messages}
end

test "still no uri encoding here" do
markdown = "<http://foo.bar.baz?url=http://>\n"
html = para({:a, ~s{href="http://foo.bar.baz?url=http://"}, "http://foo.bar.baz?url=http://"} )
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end

test "as was this" do
markdown = "<irc://foo.bar:2233/baz>\n"
html = "<p>\n <a href=\"irc://foo.bar:2233/baz\">\n irc://foo.bar:2233/baz\n </a>\n</p>\n"
Expand Down
5 changes: 1 addition & 4 deletions test/acceptance/html1/links_images/titles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ defmodule Acceptance.Html1.LinksImages.TitlesTest do

test "titled link, with deprecated quote mismatch" do
markdown = "[link](/uri \"title')\n"
html = para([
{:a, "href=\"/uri%20%22title&#39;\""},
"link"
])
html = para( {:a, ~s{href="/uri \"title'"}, "link"} )
messages = []

assert to_html1(markdown) == {:ok, html, messages}
Expand Down
Loading

0 comments on commit ad4955f

Please sign in to comment.