From 579b68f37976300f47884d42f075a0e6a3b9337e Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 27 Feb 2017 06:54:19 +0100 Subject: [PATCH 1/4] parse empty elements and comments --- src/Xml/Decode.elm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Xml/Decode.elm b/src/Xml/Decode.elm index 735f055..635a17c 100644 --- a/src/Xml/Decode.elm +++ b/src/Xml/Decode.elm @@ -97,6 +97,10 @@ parseSlice first firstClose trimmed = [] -> if String.startsWith "?" tagName then Ok ( DocType tagName props, firstClose + 1 ) + else if String.endsWith "/" beforeClose then + Ok ( Tag tagName props (Object []), firstClose + 1 ) + else if String.startsWith "!" tagName then + Ok ( Object [], firstClose + 1 ) else "Failed to find close tag for " ++ tagName From 4d394530aacadeac6c2741b7f9af8dc5f32e8271 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 1 Jun 2017 13:40:20 +0200 Subject: [PATCH 2/4] allow for nested nodes --- src/Xml/Decode.elm | 83 ++++++++++++++++++++++++++++++++++++---------- tests/Main.elm | 2 -- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/Xml/Decode.elm b/src/Xml/Decode.elm index 635a17c..bebeadd 100644 --- a/src/Xml/Decode.elm +++ b/src/Xml/Decode.elm @@ -1,17 +1,17 @@ module Xml.Decode exposing (decode, decodeInt, decodeString, decodeFloat, decodeBool, decodeChildren) -{-| - -@docs decode +{-| @docs decode @docs decodeInt, decodeFloat, decodeString, decodeString, decodeBool @docs decodeChildren + -} import Dict import Regex exposing (Regex) import Xml exposing (Value(..)) +import Tuple exposing (second) {-| Try and decode the props from a string @@ -72,12 +72,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 @@ -91,10 +99,47 @@ parseSlice first firstClose trimmed = findProps words closeTag = - "" + "" + + openTag = + "<" + ++ Regex.escape tagName + ++ "[\\s\\n]+[^/]*?>" + |> Regex.regex + + openTags = + 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 = + 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.endsWith "/" beforeClose then @@ -106,7 +151,7 @@ parseSlice first firstClose trimmed = ++ tagName |> Err - firstCloseTag :: _ -> + Just ( _, firstCloseTag ) -> let contents = String.slice (firstClose + 1) (firstCloseTag) trimmed @@ -151,11 +196,11 @@ actualDecode text = {-| Try to decode a string and turn it into an XML value - >>> import Xml exposing(Value(Tag, Object)) - >>> import Xml.Encode exposing (null) - >>> import Dict - >>> decode "" - Ok (Object [Tag "name" Dict.empty null]) +>>> import Xml exposing(Value(Tag, Object)) +>>> import Xml.Encode exposing (null) +>>> import Dict +>>> decode "" +Ok (Object [Tag "name" Dict.empty null]) -} decode : String -> Result String Value decode text = @@ -173,6 +218,7 @@ decode text = >>> import Xml exposing (Value(StrNode)) >>> decodeString "hello" Ok (StrNode "hello") + -} decodeString : String -> Result String Value decodeString str = @@ -181,12 +227,13 @@ decodeString str = {-| Decode a int - >>> import Xml exposing (Value(IntNode)) - >>> decodeInt "hello" - Err "could not convert string 'hello' to an Int" +>>> import Xml exposing (Value(IntNode)) +>>> decodeInt "hello" +Err "could not convert string 'hello' to an Int" >>> decodeInt "5" Ok (IntNode 5) + -} decodeInt : String -> Result String Value decodeInt str = @@ -200,9 +247,9 @@ decodeInt str = {-| Decode a float - >>> import Xml exposing (Value(FloatNode)) - >>> decodeFloat "hello" - Err "could not convert string 'hello' to a Float" +>>> import Xml exposing (Value(FloatNode)) +>>> decodeFloat "hello" +Err "could not convert string 'hello' to a Float" >>> decodeFloat "5" Ok (FloatNode 5.0) diff --git a/tests/Main.elm b/tests/Main.elm index 8e9f5c2..a1b8554 100644 --- a/tests/Main.elm +++ b/tests/Main.elm @@ -2,7 +2,6 @@ port module Main exposing (..) import Test exposing (..) import Tests -import Doc.Tests import Test.Runner.Node exposing (run, TestProgram) import Json.Encode exposing (Value) @@ -11,7 +10,6 @@ main : TestProgram main = describe "Everything" [ Tests.all - , Doc.Tests.all ] |> run emit From 7217aba76c9267f0aab91ea9f47ea43e8a230367 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 1 Jun 2017 14:27:24 +0200 Subject: [PATCH 3/4] reset elm-format changes, reset Doc.Tests thing, bump version --- elm-package.json | 2 +- src/Xml/Decode.elm | 41 +++++++++++++++-------------------------- tests/Main.elm | 2 ++ 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/elm-package.json b/elm-package.json index 4f48f67..211725c 100644 --- a/elm-package.json +++ b/elm-package.json @@ -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", diff --git a/src/Xml/Decode.elm b/src/Xml/Decode.elm index 0f86518..512362d 100644 --- a/src/Xml/Decode.elm +++ b/src/Xml/Decode.elm @@ -1,13 +1,12 @@ module Xml.Decode exposing (decode, decodeInt, decodeString, decodeFloat, decodeBool, decodeChildren) -{-| . +{-| @docs decode @docs decodeInt, decodeFloat, decodeString, decodeString, decodeBool @docs decodeChildren - -} import Dict @@ -101,15 +100,11 @@ parseSlice first firstClose trimmed = findProps words closeTag = - "" + "" openTag = - "<" - ++ Regex.escape tagName - ++ "[\\s\\n]*[^/]*?>" - |> Regex.regex + "<" ++ Regex.escape tagName ++ "[\\s\\n]*[^/]*?>" + |> Regex.regex openTags = Regex.find Regex.All openTag afterOpen @@ -198,13 +193,11 @@ actualDecode text = {-| Try to decode a string and turn it into an XML value - -> > > import Xml exposing(Value(Tag, Object)) -> > > import Xml.Encode exposing (null) -> > > import Dict -> > > decode "" -> > > Ok (Object [Tag "name" Dict.empty null]) - + >>> import Xml exposing(Value(Tag, Object)) + >>> import Xml.Encode exposing (null) + >>> import Dict + >>> decode "" + Ok (Object [Tag "name" Dict.empty null]) -} decode : String -> Result String Value decode text = @@ -222,7 +215,6 @@ decode text = >>> import Xml exposing (Value(StrNode)) >>> decodeString "hello" Ok (StrNode "hello") - -} decodeString : String -> Result String Value decodeString str = @@ -231,14 +223,12 @@ decodeString str = {-| Decode a int - -> > > import Xml exposing (Value(IntNode)) -> > > decodeInt "hello" -> > > Err "could not convert string 'hello' to an Int" + >>> import Xml exposing (Value(IntNode)) + >>> decodeInt "hello" + Err "could not convert string 'hello' to an Int" >>> decodeInt "5" Ok (IntNode 5) - -} decodeInt : String -> Result String Value decodeInt str = @@ -252,10 +242,9 @@ decodeInt str = {-| Decode a float - -> > > import Xml exposing (Value(FloatNode)) -> > > decodeFloat "hello" -> > > Err "could not convert string 'hello' to a Float" + >>> import Xml exposing (Value(FloatNode)) + >>> decodeFloat "hello" + Err "could not convert string 'hello' to a Float" >>> decodeFloat "5" Ok (FloatNode 5.0) diff --git a/tests/Main.elm b/tests/Main.elm index a1b8554..8e9f5c2 100644 --- a/tests/Main.elm +++ b/tests/Main.elm @@ -2,6 +2,7 @@ port module Main exposing (..) import Test exposing (..) import Tests +import Doc.Tests import Test.Runner.Node exposing (run, TestProgram) import Json.Encode exposing (Value) @@ -10,6 +11,7 @@ main : TestProgram main = describe "Everything" [ Tests.all + , Doc.Tests.all ] |> run emit From 95e7b0675be17c04d4e8b0cbbc2f1f5324f9d64d Mon Sep 17 00:00:00 2001 From: "Bill St. Clair" Date: Thu, 23 Nov 2017 20:05:38 -0500 Subject: [PATCH 4/4] Properly parse a self-closing tag with no attributes and no space before '/>'. Add '' example to the test suite. Update the tests for version 4.2 to 5 of elm-test. No more Main.elm. Remove elm-doc-test installation and use. There aren't any doc tests, and it was generating files that didn't compile on my machine. --- .travis.yml | 1 - run_tests.sh | 3 +-- src/Xml/Decode.elm | 7 ++++++- tests/Main.elm | 19 ------------------- tests/Tests.elm | 4 +++- tests/elm-package.json | 5 ++--- 6 files changed, 12 insertions(+), 27 deletions(-) delete mode 100644 tests/Main.elm diff --git a/.travis.yml b/.travis.yml index f75e1f7..30b7125 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,6 @@ install: - npm --version - npm install -g elm@$ELM_VERSION - npm install -g elm-test - - npm install -g elm-doc-test script: diff --git a/run_tests.sh b/run_tests.sh index e4f9b67..04b2a40 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,4 +1,3 @@ #!/bin/bash -elm-doc-test -elm-test \ No newline at end of file +elm-test diff --git a/src/Xml/Decode.elm b/src/Xml/Decode.elm index 512362d..1eb7a4e 100644 --- a/src/Xml/Decode.elm +++ b/src/Xml/Decode.elm @@ -140,7 +140,12 @@ parseSlice first firstClose trimmed = if String.startsWith "?" tagName then Ok ( DocType tagName props, firstClose + 1 ) else if String.endsWith "/" beforeClose then - Ok ( Tag tagName props (Object []), firstClose + 1 ) + let tag = if String.endsWith "/" tagName then + 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 diff --git a/tests/Main.elm b/tests/Main.elm deleted file mode 100644 index 8e9f5c2..0000000 --- a/tests/Main.elm +++ /dev/null @@ -1,19 +0,0 @@ -port module Main exposing (..) - -import Test exposing (..) -import Tests -import Doc.Tests -import Test.Runner.Node exposing (run, TestProgram) -import Json.Encode exposing (Value) - - -main : TestProgram -main = - describe "Everything" - [ Tests.all - , Doc.Tests.all - ] - |> run emit - - -port emit : ( String, Value ) -> Cmd msg diff --git a/tests/Tests.elm b/tests/Tests.elm index c377015..1d9cb01 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -34,6 +34,7 @@ selfClosingExampleAsString = kitofr + kitofr @@ -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" ) ] @@ -237,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 diff --git a/tests/elm-package.json b/tests/elm-package.json index 9310c20..661e5d1 100644 --- a/tests/elm-package.json +++ b/tests/elm-package.json @@ -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" }