arowM / elm-data-url / DataUrl

A module to handle data URLs (IETF RFC 2397) in type safe manner. This library is useful to handle strings generated by File.toUrl of elm/file.

Types


type alias DataUrl =
Internal.DataUrl

An opaque type representing data url


type alias Data =
Data

Reexport Data type from DataUrl.Data for convenience.


type alias MediaType =
MediaType

Reexport MediaType type from DataUrl.MediaType for convenience.

Constructors

fromString : String -> Maybe DataUrl

A standard way to construct DataUrl value. It takes a String representation of data URL and parses it as a DataUrl value. If the provided String argument is illigal as a data URL, it returns Nothing. Note that it does not accept raw HTML string, though data URLs page of MDN has such an example.

fromString "data:text/html,<script>alert('hi');</script>"
--> Nothing

fromStringFuzzy : String -> Maybe DataUrl

Yet another way to construct DataUrl value. It is much faster than fromString, but it does not validate data part.

import DataUrl.Data exposing (Data(..))

Maybe.map data <| fromStringFuzzy "data:text/html,<script>alert('hi');</script>"
--> Just <| Textual "<script>alert('hi');</script>"

Getters

mediaType : DataUrl -> Maybe MediaType

Take MediaType value from DataUrl value. As data URLs page of MDN says, it should be assumed that text/plain;charset=US-ASCII is set when the value is Nothing.

data : DataUrl -> Data

Take data part from DataUrl. It is guaranteed that the data URL has base64 token when the value is Base64 someString, but the someString could be illegal base64 string as follows.

import DataUrl.Data exposing(Data(..))

Maybe.map data <| fromString "data:text/plain;base64,This-is?illegal%base64_string"
--> Just <| Base64 "This-is?illegal%base64_string"

The someString is only guaranteed to meet uric of RFC 2396.

Convert functions

toString : DataUrl -> String

Convert DataUrl values to String.

Maybe.map DataUrl.toString <|
    fromString "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D"
--> Just "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D"

Lower level functions

parser : Parser DataUrl

A parser for DataUrl. Use fromString for simple usage.