Skip to content

Commit

Permalink
Generating void elements correctly on DocAST.to_string (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst authored Apr 6, 2021
1 parent 609d976 commit dcf303d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ex_doc/doc_ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ defmodule ExDoc.DocAST do
raise "content type #{inspect(other)} is not supported"
end

# https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html#void-element
@void_elements ~W(area base br col command embed hr img input keygen link
meta param source track wbr)a

@doc """
Transform AST into string.
"""
Expand All @@ -35,6 +39,11 @@ defmodule ExDoc.DocAST do
fun.(list, result)
end

def to_string({tag, attrs, _inner, _meta} = ast, fun) when tag in @void_elements do
result = "<#{tag}#{ast_attributes_to_string(attrs)}/>"
fun.(ast, result)
end

def to_string({tag, attrs, inner, %{verbatim: true}} = ast, fun) do
inner = Enum.join(inner, "")
result = "<#{tag}#{ast_attributes_to_string(attrs)}>" <> inner <> "</#{tag}>"
Expand Down
11 changes: 11 additions & 0 deletions test/ex_doc/doc_ast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,16 @@ defmodule ExDoc.DocASTTest do
assert DocAST.to_string(ast, f) ==
"<p>foo <STRONG>BAR</STRONG> baz</p>"
end

test "void elements" do
markdown = """
foo
bar
"""

ast = DocAST.parse!(markdown, "text/markdown")

assert DocAST.to_string(ast) == ~s{<p>foo<br/>bar</p>}
end
end
end

0 comments on commit dcf303d

Please sign in to comment.