Skip to content

Commit

Permalink
Implement Html.Parser.nodeToString.
Browse files Browse the repository at this point in the history
Fixes #7.
  • Loading branch information
hecrj committed Mar 10, 2019
1 parent 1e45c61 commit 43c641a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.3.0] - 2019-03-10
### Added
- `Html.Parser.nodeToString` to turn parser nodes back into its HTML representation.

## [2.2.0] - 2019-02-27
### Added
- Expose `Html.Parser.node` to allow other folks to build different parsers on top of this one!
Expand Down Expand Up @@ -38,7 +42,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

[named-character-references]: https://www.w3.org/TR/html5/syntax.html#named-character-references

[Unreleased]: https://github.com/hecrj/html-parser/compare/2.2.0...HEAD
[Unreleased]: https://github.com/hecrj/html-parser/compare/2.3.0...HEAD
[2.3.0]: https://github.com/hecrj/html-parser/compare/2.2.0...2.3.0
[2.2.0]: https://github.com/hecrj/html-parser/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/hecrj/html-parser/compare/2.0.1...2.1.0
[2.0.1]: https://github.com/hecrj/html-parser/compare/2.0.0...2.0.1
Expand Down
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "hecrj/html-parser",
"summary": "Parse HTML 5 in Elm",
"license": "BSD-3-Clause",
"version": "2.2.0",
"version": "2.3.0",
"exposed-modules": [
"Html.Parser",
"Html.Parser.Util"
Expand Down
64 changes: 60 additions & 4 deletions src/Html/Parser.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Html.Parser exposing
( run, Node(..), Attribute
, node
, node, nodeToString
)

{-| Parse HTML 5 in Elm.
Expand All @@ -9,14 +9,14 @@ See <https://www.w3.org/TR/html5/syntax.html>
@docs run, Node, Attribute
# Parser internals
# Internals
If you are building a parser of your own using [`elm/parser`][elm-parser] and
you need to parse HTML... This section is for you!
[elm-parser]: https://package.elm-lang.org/packages/elm/parser/latest
@docs node
@docs node, nodeToString
-}

Expand Down Expand Up @@ -67,7 +67,7 @@ type alias Attribute =
( String, String )


{-| Parses an HTML node.
{-| Parse an HTML node.
You can use this in your own parser to add support for HTML 5.
Expand All @@ -81,6 +81,62 @@ node =
]


{-| Turn a parser node back into its HTML string.
For instance:
Element "a"
[ ( "href", "https://elm-lang.org" ) ]
[ Text "Elm" ]
|> toString
Produces `<a href="https://elm-lang.org">Elm</a>`.
-}
nodeToString : Node -> String
nodeToString node_ =
let
attributeToString ( attr, value ) =
attr ++ "=\"" ++ value ++ "\""
in
case node_ of
Text text_ ->
text_

Element name attributes children ->
let
maybeAttributes =
case attributes of
[] ->
""

_ ->
" " ++ String.join "" (List.map attributeToString attributes)
in
if isVoidElement name then
String.concat
[ "<"
, name
, maybeAttributes
, ">"
]

else
String.concat
[ "<"
, name
, maybeAttributes
, ">"
, String.join "" (List.map nodeToString children)
, "</"
, name
, ">"
]

Comment comment_ ->
"<!-- " ++ comment_ ++ " -->"



-- Text

Expand Down
36 changes: 36 additions & 0 deletions tests/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ nodeTests =
]


nodeToStringTests : Test
nodeToStringTests =
describe "nodeToString"
[ test "simple link" <|
\_ ->
Element "a" [ ( "href", "https://elm-lang.org" ) ] [ Text "Elm" ]
|> Html.Parser.nodeToString
|> Expect.equal "<a href=\"https://elm-lang.org\">Elm</a>"
, test "container" <|
\_ ->
Element "div"
[]
[ Element "p" [] [ Text "Hello," ]
, Element "p" [] [ Text "World!" ]
]
|> Html.Parser.nodeToString
|> Expect.equal "<div><p>Hello,</p><p>World!</p></div>"
, test "void element" <|
\_ ->
Element "br" [] [ Element "a" [] [ Text "should be ignored" ] ]
|> Html.Parser.nodeToString
|> Expect.equal "<br>"
, test "comment" <|
\_ ->
Comment "This is a comment"
|> Html.Parser.nodeToString
|> Expect.equal "<!-- This is a comment -->"
, test "text" <|
\_ ->
Text "Hello, world!"
|> Html.Parser.nodeToString
|> Expect.equal "Hello, world!"
]


scriptTests : Test
scriptTests =
describe "Script"
Expand Down Expand Up @@ -148,6 +183,7 @@ suite =
describe "HtmlParser"
[ textNodeTests
, nodeTests
, nodeToStringTests
, commentTests
, attributeTests
, errorTests
Expand Down

0 comments on commit 43c641a

Please sign in to comment.