Code Monkey home page Code Monkey logo

elm-xml-parser's Introduction

XmlParser

Build Status

XML Parser for Elm

How to use

There is only two functions available.

parse : String -> Result Parser.Error Xml
format : Xml -> String

Typically, you'll use parse function, get the root node and traverse it.

> import XmlParser
> XmlParser.parse """<a name="value">foo</a>"""
Ok { processingInstructions = [], docType = Nothing, root = Element "a" ([{ name = "name", value = "value" }]) ([Text "foo"]) }

I'm not going to make decoder and encoder right now. Please let me know if you are interested :)

LICENSE

BSD-3-Clause

elm-xml-parser's People

Contributors

aratama avatar jinjor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

elm-xml-parser's Issues

Stack overflow with large number of child nodes

If an XML node has a large number of child nodes, the children function will overflow the stack because it cannot be tail-call optimized as written.

$ elm repl
---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> import XmlParser
> XmlParser.parse ("<parent>" ++ String.repeat 2000 "<child></child>" ++ "</parent>")
RangeError: Maximum call stack size exceeded

formatでProcessingInstructionのvalueをescapeしてしまっていている

"""<?xml-stylesheet type="text/xsl" href="style.xsl"?><node />"""
    |> XmlParser.parse
    |> Result.map XmlParser.format

Ok ("<?xml-stylesheet type="text/xsl" href="style.xsl"?><node />")

ではなく

Ok ("<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;style.xsl&quot;?><node />")

になってしまっている

RangeError: Maximum call stack size exceeded

Trying to parse https://www.reddit.com/.rss with jinjor/elm-xml-parser version 2.0.0 in elm repl, I get the following error:

> import XmlParser

> XmlParser.parse Test.reddit
RangeError: Maximum call stack size exceeded

It seems to be due to elements with a high number of escaped characters (typically encoded HTML).

Is there a way to make the parser reasonably stack safe?

Edit: this also happens with Chrome 73, but not with firefox 66.

Handle attribute's namespaces

Hi,
I’m using your lib (thanks!) to (in pseudo pipeline) :

string svg
 |> parse
 |> do stuff with the nodes (add attribute, reorder, etc)
 |> render the svg to the dom via the elm-lang/Svg  + elm-lang/virtual-dom

My problem lies in the third pipe ”render the svg to the dom via the elm-lang/Svg + elm-lang/virtual-dom”: To create namespaced attributes correctly in dom elements correctly, I have to use VirtualDom.attributeNS.

Say a tag like: <use xlink:href="#great" … />. the xlink:href is a namespaced attribute, the namespace is declared in the svg tag like <svg xmlns:xlink="http://www.w3.org/1999/xlink">.

Once parsed with elm-xml-parser, to render this node in view, I would love to do it like this:

{-| Render a SVG node 
-}
displayNode : Node -> Svg msg
displayNode pnode =
    case pnode of
        XmlParser.Element name attrs child ->
            node name (attrs |> List.map convertToAttributes) (child |> List.map displayNode)

        XmlParser.Text leaf ->
            text leaf

{-| Convert parsed SVG attributes to DOM SVG attributes
-}
convertToAttributes : XmlParser.Attribute -> Svg.Attribute msg
convertToAttributes attr =

     -- Today, this is not possible: we only have the name and the value of the attribute, not the namespace
    case attr of
         Attribute name value ->
             attribute name value
         NamespacedAttribute namespace name value ->
             attributeNS namespace name value

I know this would be a BC break, since it would leads to changing the type of Attribute from type alias Attribute = { name : String, value : String } to something like maybe:

type XMLAttribute
        = Attribute { name: String, value : String }
        | AttributeNS { name: String, value: String, namespace: String }

type Node
    = Element String (List XMLAttribute) (List Node)
    | Text String

or just adding a namespace: Maybe String to the existing Attribute record.

If you are interested in this feature, I could give it a try. Just let me know which solution you prefer.

Refs

Fails to parse trivial svg

This is a simple icon I found on the internet and it fails to parse

<?xml version="1.0" ?><!DOCTYPE svg  PUBLIC '-//W3C//DTD SVG 1.0//EN'  'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'><svg enable-background="new 0 0 24 24" id="Layer_1" version="1.0" viewBox="0 0 24 24" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M21,11v8c0,1.1-0.9,2-2,2H5c-1.1,0-2-0.9-2-2V5  c0-1.1,0.9-2,2-2h9" fill="none" stroke="#000000" stroke-miterlimit="10" stroke-width="2"/><polyline clip-rule="evenodd" fill="none" fill-rule="evenodd" id="Done__x2014__Displayed_on_the_left_side_of_a_contextual_action_bar__x28_CAB_x29__to_allow_the_user_to_dismiss_it._3_" points="  21.2,4.6 11.2,15.2 6.8,10.8 " stroke="#000000" stroke-miterlimit="10" stroke-width="2"/></svg>

Wrong parse of attributes containing ampersands

The attribute attr gets cut:

$ elm repl
> import XmlParser
> XmlParser.parse "<root attr=\"a&amp;b\"></root>"
Ok { docType = Nothing, processingInstructions = [], root = Element "root" [{ name = "attr", value = "&b" }] [] }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.