Leniently parse html5 documents and fragments and then render them into strings or Elm's virtual dom nodes.
An html document has a <!doctype> and then a root html node.
Parse an html fragment into a list of html nodes.
The html fragment can have multiple top-level nodes.
run "<div>hi</div><div>bye</div>"
== Ok
[ Element "div" [] [ Text "hi" ]
, Element "div" [] [ Text "bye" ]
]Like run except it only succeeds when the html input is a
single top-level element, and it always returns a single node.
Parses <!doctype html> and any html nodes after.
Always returns a single root node. Wraps nodes in a root <html> node if one is not present.
Caveat: If there are multiple top-level nodes and one of them is <html>, then this
function will wrap them all in another <html> node.
Turn a single node into an Elm html node that Elm can render.
Turn a multiple html nodes into Elm html that Elm can render.
view : Html Msg
view =
Html.div
[]
("<p>hello world</p>"
|> Html.Parser.run
|> Result.map Html.Parser.nodesToHtml
|> Result.withDefault [ Html.text "parse error" ]
)Convert an html node into a non-pretty string.
nodeToString (Element "a" [] [ Text "hi" ])
== "<a>hi</a>"Convert multiple html nodes into a non-pretty string.
nodesToString
[ Element "a" [] [ Text "hi" ]
, Element "div" [] [ Element "span" [] [] ]
]
== "<a>hi</a><div><span></span></div>"Generate a pretty string for a single html node.
Turn a node tree into a pretty-printed, indented html string.
("<a><b><c>hello</c></b></a>"
|> Html.Parser.run
|> Result.map nodesToPrettyString
)
== Ok """<a>
<b>
<c>
hello
</c>
</b>
</a>"""Convert a document into a string starting with <!doctype html> followed by the root html node.
Convert a document into a pretty, indented string.
An html node is tree of text, comments, and element nodes.
An element (e.g.
<div foo="bar">hello</div>) can have attributes and child nodes.