Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xml/Encode: ensure closing tags are properly indented #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Xml/Encode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,31 @@ valueToString level indent value =
case value of
Tag name props nextValue ->
let
indentString =
if needsIndent nextValue then
indentation =
String.repeat (indent * level // 2) " "

newlineNecessary =
needsIndent nextValue

maybeNewline =
if newlineNecessary then
"\n"
else
""
in
"<"
indentation
++ "<"
++ name
++ (propsToString props)
++ ">"
++ indentString
++ maybeNewline
++ (valueToString (level + 1) indent nextValue)
++ indentString
++ maybeNewline
++ (if newlineNecessary then
indentation
else
""
)
++ "</"
++ name
++ ">"
Expand All @@ -99,7 +111,6 @@ valueToString level indent value =

Object xs ->
List.map (valueToString (level + 1) indent) xs
|> List.map ((++) (String.repeat (level * indent) " "))
|> String.join "\n"

DocType name props ->
Expand Down
38 changes: 38 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ decodedExampleStuff =
decode ExampleStuff.stuff


threeLayersAsString : String
threeLayersAsString =
"""
<person>
<name>
<firstname>Chris</firstname>
<lastname>topher</lastname>
<meta>
<suffix>Jr</suffix>
</meta>
</name>
</person>
"""
|> String.trim


threeLayers : Value
threeLayers =
Tag "person" Dict.empty <|
Tag "name" Dict.empty <|
list
[ Tag "firstname" Dict.empty <| StrNode "Chris"
, Tag "lastname" Dict.empty <| StrNode "topher"
, Tag "meta" Dict.empty <|
list
[ Tag "suffix" Dict.empty <| StrNode "Jr"
]
]


encodedThreeLayers : String
encodedThreeLayers =
encode 2 threeLayers


all : Test
all =
describe "Encode test"
Expand Down Expand Up @@ -209,4 +244,7 @@ all =
Expect.equal
(List.length <| Result.withDefault [] <| ExampleStuff.fromXML <| ExampleStuff.stuff)
100
, test "three layers of tags are properly indented" <|
\_ ->
Expect.equal encodedThreeLayers threeLayersAsString
]