'data' URI Parsing Rules

  1. Let URI be the string representing the data URI.
  2. If URI does not start with a case-insensitive "data:":

    1. Throw a MALFORMED_URI exception.
  3. If URI does not contain a ",":

    1. Throw a MALFORMED_URI exception.
  4. Let supportedContentEncodings be an array of strings representing the supported content encodings. (["base64"] for example)
  5. Let mimeType be a string with the value "text/plain".
  6. Let contentEncoding be an empy string.
  7. Let contentEncodingAlreadySet be a boolean with a value of false.
  8. Let supportedValues be a map of string:string pairs where the first string in each pair represents the name of the supported value and the second string in each pair represents an empty string or default string value. (Example: {"charset" : "", "filename" : "", "content-disposition" : ""})
  9. Let supportedValueSetBits be a map of string:bool pairs representing each of the names in supportedValues with each name set to false.
  10. Let comma be the position of the first "," found in URI.
  11. Let temp be the substring of URI from, and including, position 5 to, and excluding, the comma position. (between "data:" and first ",")
  12. Let headers be an array of strings returned by splitting temp by ";".
  13. For each string s in headers:

    1. Let s equal the lowercase version of s
    2. Let eq be the position result of searching for "=" in s.
    3. Let name and value be empty strings.
    4. If eq is not a valid position in s:

      1. Let name equal the result of percent-decoding s.
      2. Let name equal the result of trimming leading and trailing white-space from name.
    5. Else:

      1. Let name equal the substring of s from position 0 to, but not including, position eq.
      2. Let name equal the result of percent-decoding name.
      3. Let name equal the result of trimmnig leading and trailing white-space from name.
      4. Let value equal the substring of s from position eq + 1 to the end of s.
      5. Let value equal the result of precent-decoding value.
      6. Let value equal the result of trimming leading and trailing white-space from value.
    6. If s is the first element in headers and eq is not a valid position in s and the length of name is greater than 0:

      1. Let mimeType equal name.
    7. Else:

      1. If eq is not a valid position in s:

        1. If name is found case-insensitively in supportedContentEncodings:

          1. If contentEncodingAlreadySet is false:

            1. Let contentEncoding equal name.
            2. Let ContentEncodingAlreadySet equal true.
      2. Else:

        1. If the length of value is greater than 0 and name is found case-insensitively in supportedValues:

          1. If the corresponding value for name found (case-insensitivley) in supportedValueSetBits is false:

            1. Let the corresponding value for name found (case-insensitively) in supportedValues equal value.
            2. Let the corresponding value for name found (case-insensitively) in supportedValueSetBits equal true.
  14. Let data be the substring of URI from position comma + 1 to the end of URI.
  15. Let data be the result of percent-decoding data.
  16. Let dataURIObject be an object consisting of the mimeType, contentEncoding, data and supportedValues objects.
  17. return dataURIObject.

Javascript Example