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

Parse nested tags with equal name (solves #3) #8

Open
wants to merge 6 commits 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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ install:
- npm --version
- npm install -g elm@$ELM_VERSION
- npm install -g elm-test
- npm install -g elm-doc-test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this removed? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't working for me, likely due to an elm-doc-test bug when there aren't any doc tests. It's a good template, but wasn't used by this package. I'd prefer a solution that made it work, but I was too lazy at the time. Will work on a proper solution if you want.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was used by this package. There are multiple doc tests. They likely need to be upgraded to use elm-verify-examples: but they should be upgraded, not dropped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that elm-doc-test has been renamed to elm-verify-examples.

elm-doc-test 4.0.0 creates elm files that don't compile, when there are no doc tests.

The latest version of elm-verify-examples does not have that problem. I'll push a change to use that instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Copy link
Owner

@eeue56 eeue56 Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Perhaps at one time ">>>" was the magic prefix for a doc test. Now it is "-->". I've never used them, so I have no memory of when that changed.

https://github.com/stoeffel/elm-verify-examples

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. So in order to switch, you'll need to change the syntax of the examples

Copy link
Contributor

@billstclair billstclair Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured out what happened. Referencing https://github.com/stoeffel/elm-verify-examples/commits/master :

Jun 14, "Proposal new dsl" changed the syntax.
Jun 16, "Version 4.0.0" (of elm-doc-test) released.
Jun 16, "Renaming to elm-verify-examples".
Jun 17, "Version 1.0.1" (of elm-verify-examples) released.

The Travis script was getting elm-doc-test version 4.0.0, with the new syntax, hence failing.

I added a PR to @myrho's fork to update all the comment tests to the new syntax. Travis is happy in Linux, but still waiting to run the macOS test.

Hopefully, @myrho will check in soon, fix the other issues, and commit to add his and my fixes to this PR.

I also added syconfcpus to the Travis script, to speed "elm make" from minutes to seconds. Yes. It's that amazing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just open a new PR against here, no need to merge into the fork



script:
Expand Down
2 changes: 1 addition & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.2.2",
"version": "2.2.3",
"summary": "XML parser, encoder/decoder and queries in Elm",
"repository": "https://github.com/eeue56/elm-xml.git",
"license": "BSD3",
Expand Down
3 changes: 1 addition & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

elm-doc-test
elm-test
elm-test
59 changes: 54 additions & 5 deletions src/Xml/Decode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Xml.Decode exposing (decode, decodeInt, decodeString, decodeFloat, decode
import Dict
import Regex exposing (Regex)
import Xml exposing (Value(..))
import Tuple exposing (second)


{-| Try and decode the props from a string
Expand Down Expand Up @@ -72,12 +73,20 @@ findProps =
>> Dict.fromList


type Tag
= OpenTag
| CloseTag


parseSlice : Int -> Int -> String -> Result String ( Value, Int )
parseSlice first firstClose trimmed =
let
beforeClose =
String.slice (first + 1) firstClose trimmed

afterOpen =
String.slice (first + 1) (String.length trimmed) trimmed

words =
beforeClose
|> String.words
Expand All @@ -92,19 +101,59 @@ parseSlice first firstClose trimmed =

closeTag =
"</" ++ tagName ++ ">"

openTag =
"<" ++ Regex.escape tagName ++ "[\\s\\n]*[^/]*?>"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining regexes at runtime like this can lead to runtime errors, they should instead be defined as flatly as possible, see propRegex. In the cases where they can't, I'd rather not use a regex for that.

|> Regex.regex

openTags =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type signatures here would help make the code readable

Regex.find Regex.All openTag afterOpen
|> List.map (.index >> (,) OpenTag)

closeTags =
String.indexes closeTag trimmed
|> List.map ((,) CloseTag)

allTags =
openTags
++ closeTags
|> List.sortBy second

reduce list =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name doesn't make it obvious to me what this function is meant to do

case list of
( OpenTag, i1 ) :: ( CloseTag, i2 ) :: rest ->
reduce rest

( OpenTag, i1 ) :: rest ->
case reduce rest of
( CloseTag, i2 ) :: rest ->
reduce rest

rest ->
rest

_ ->
list
in
case String.indexes closeTag trimmed of
[] ->
case List.head <| reduce allTags of
Nothing ->
if String.startsWith "?" tagName then
Ok ( DocType tagName props, firstClose + 1 )
else if (String.contains "/>" trimmed) then
Ok ( Tag tagName props (Object []), firstClose + 1 )
else if String.endsWith "/" beforeClose then
let tag = if String.endsWith "/" tagName then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be on a newline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I just started using elm-format. Was naughty about such things in the past. Will fix. By saving in my editor. :)

String.slice 0 -1 tagName
else
tagName
in
Ok ( Tag tag props (Object []), firstClose + 1 )
else if String.startsWith "!" tagName then
Ok ( Object [], firstClose + 1 )
else
"Failed to find close tag for "
++ tagName
|> Err

firstCloseTag :: _ ->
Just ( _, firstCloseTag ) ->
let
contents =
String.slice (firstClose + 1) (firstCloseTag) trimmed
Expand Down
19 changes: 0 additions & 19 deletions tests/Main.elm

This file was deleted.

54 changes: 53 additions & 1 deletion tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ selfClosingExampleAsString =
<person>
<name is="me">kitofr</name>
<here is="false" />
<there/>
<here is="true"/>
<name is="me">kitofr</name>
</person>
Expand All @@ -48,6 +49,7 @@ selfClosingExample =
, object
[ ( "name", Dict.fromList [ ( "is", string "me" ) ], string "kitofr" )
, ( "here", Dict.fromList [ ( "is", bool False ) ], null )
, ( "there", Dict.fromList [], null )
, ( "here", Dict.fromList [ ( "is", bool True ) ], null )
, ( "name", Dict.fromList [ ( "is", string "me" ) ], string "kitofr" )
]
Expand Down Expand Up @@ -128,6 +130,50 @@ nestedExampleAsString =
|> String.trim


nestedSelfExample : Value
nestedSelfExample =
object
[ ( "g"
, Dict.singleton "id" (string "foo")
, object
[ ( "g", Dict.singleton "id" (string "bla"), string "group" )
]
)
]


nestedSelfExampleAsString : String
nestedSelfExampleAsString =
"""
<g id="foo">
<g id="bla">group</g>
</g>
"""
|> String.trim


nestedSelfExampleWithoutProps : Value
nestedSelfExampleWithoutProps =
object
[ ( "g"
, Dict.empty
, object
[ ( "g", Dict.empty, string "group" )
]
)
]


nestedSelfExampleWithoutPropsAsString : String
nestedSelfExampleWithoutPropsAsString =
"""
<g>
<g>group</g>
</g>
"""
|> String.trim


decodedExampleStuff : Result String Value
decodedExampleStuff =
decode ExampleStuff.stuff
Expand All @@ -154,6 +200,12 @@ all =
, test "a nested tag is decoded properly" <|
\_ ->
Expect.equal (decode nestedExampleAsString) (Ok nestedExample)
, test "a self nested tag is encoded properly" <|
\_ ->
Expect.equal nestedSelfExampleAsString (encode 4 nestedSelfExample)
, test "a self nested tag is decoded properly" <|
\_ ->
Expect.equal (decode nestedSelfExampleAsString) (Ok nestedSelfExample)
, test "a self closing tag is decoded properly" <|
\_ ->
Expect.equal (decode selfClosingExampleAsString) (Ok selfClosingExample)
Expand Down Expand Up @@ -187,7 +239,7 @@ all =
|> List.length
)
1
, test "the XML contains a node we expect" <|
, test "the other XML contains a node we expect" <|
\_ ->
Expect.equal
(decodedExampleStuff
Expand Down
5 changes: 2 additions & 3 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
],
"exposed-modules": [],
"dependencies": {
"elm-community/elm-test": "3.0.0 <= v < 4.0.0",
"elm-community/elm-test": "4.2.0 <= v < 5.0.0",
"elm-community/json-extra": "2.0.0 <= v < 3.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"justinmimbs/elm-date-extra": "2.0.3 <= v < 3.0.0",
"mgold/elm-random-pcg": "4.0.2 <= v < 5.0.0",
"rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"
"mgold/elm-random-pcg": "4.0.2 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}