Garados007 / elm-svg-parser / SvgParser

String to SVG parser

Primitives


type SvgNode
    = SvgElement Element
    | SvgText String
    | SvgComment String

A SVG node can be one of the three: SvgElement, SvgText or SvgComment.


type alias Element =
{ name : String
, attributes : List SvgAttribute
, children : List SvgNode 
}

An Element consists of a tag name, a list of attributes, a list of children nodes.

<svg xmlns="http://www.w3.org/2000/svg"></svg>

will be parsed as

Element "svg" [ ( "xmlns", "http://www.w3.org/2000/svg" ) ] []


type alias SvgAttribute =
( String, String )

A name/value pair to denote attribute.

Parsing

parse : String -> Result String (Html msg)

Parses String to Html msg. This function filters top level comments to find the first <svg> element. Additional <svg> elements are ignored.

parseToNode : String -> Result String SvgNode

Parses String to SvgNode. Normally you will use parse instead of this.

parse "<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>"
    == Ok (SvgElement (Element "svg" [ ( "xmlns", "http://www.w3.org/2000/svg" ) ] []))

parseToNodes : String -> Result String (List SvgNode)

Same as parseToNode, but returns a list of all the nodes in the string.

nodeToSvg : SvgNode -> Svg msg

Convert SvgNode to Svg msg. This is useful when you want to manipulate SvgNode before conveting to Html msg.

toAttribute : SvgAttribute -> Svg.Attribute msg

Convert SvgAttribute to Attribute msg. This is useful when you want to manipulate SvgAttribute before converting to Html msg.

This function also automatically handles the deprecated namespace attributes such as xlink:href'. See MDN Web Docs.