Liking cljdoc? Tell your friends :D

dk.simongray.whatwg-mime

Parse MIME types and sniff resources the way browsers do, by the MIME Sniffing Standard.

A MIME type is a plain map:

  • :type and :subtype, in lower case, e.g. "text" and "html"
  • :parameters, when there are any, a map of lower-case names to values in the order that they're written

The parameters can also be a vector of name-value pairs. Unlike the map, it keeps its order when you add a ninth parameter.

Every function that takes a MIME type also takes its text, and treats text that doesn't parse as nil.

Parse MIME types and sniff resources the way browsers do, by the MIME
Sniffing Standard.

A MIME type is a plain map:

- :type and :subtype, in lower case, e.g. "text" and "html"
- :parameters, when there are any, a map of lower-case names to values
  in the order that they're written

The parameters can also be a vector of name-value pairs. Unlike the map,
it keeps its order when you add a ninth parameter.

Every function that takes a MIME type also takes its text, and treats
text that doesn't parse as nil.
raw docstring

archive?clj/s

(archive? m)

Whether the MIME type m is an archive type: ZIP, gzip or RAR.

Whether the MIME type `m` is an archive type: ZIP, gzip or RAR.
sourceraw docstring

audio-or-video?clj/s

(audio-or-video? m)

Whether the MIME type m is an audio or video type, including application/ogg.

Whether the MIME type `m` is an audio or video type, including
application/ogg.
sourceraw docstring

charsetclj/s

(charset m)

The charset parameter of the MIME type m as it's written, or nil.

The charset parameter of the MIME type `m` as it's written, or nil.
sourceraw docstring

decodeclj/s

(decode b label)

The text of the bytes b in the encoding that label names, e.g. latin1, or in UTF-8 when it names none. A byte order mark overrides the label and isn't part of the text.

This is a convenience, and not part of the MIME Sniffing Standard. The library decodes windows-1252, which browsers use for Latin-1 and ASCII, the same way everywhere, even where TextDecoder knows only UTF-8, e.g. in React Native. Other encodings use the platform's decoders (java.nio.charset on the JVM and TextDecoder in JavaScript), which can differ from the Encoding Standard. An encoding that the platform lacks, e.g. ISO-8859-10 on the JVM, decodes as UTF-8.

(decode (.getBytes "Blåbær" "windows-1252") "latin1")
;; => "Blåbær"
The text of the bytes `b` in the encoding that `label` names, e.g.
latin1, or in UTF-8 when it names none. A byte order mark overrides the
label and isn't part of the text.

This is a convenience, and not part of the MIME Sniffing Standard. The
library decodes windows-1252, which browsers use for Latin-1 and ASCII,
the same way everywhere, even where TextDecoder knows only UTF-8, e.g.
in React Native. Other encodings use the platform's decoders
(java.nio.charset on the JVM and TextDecoder in JavaScript), which can
differ from the Encoding Standard. An encoding that the platform lacks,
e.g. ISO-8859-10 on the JVM, decodes as UTF-8.

    (decode (.getBytes "Blåbær" "windows-1252") "latin1")
    ;; => "Blåbær"
sourceraw docstring

encodingclj/s

(encoding m)

The name of the encoding that the charset of the MIME type m names, or nil.

The name of the encoding that the charset of the MIME type `m` names,
or nil.
sourceraw docstring

essenceclj/s

(essence m)

The type and subtype of the MIME type m, e.g. text/html.

The type and subtype of the MIME type `m`, e.g. text/html.
sourceraw docstring

extractclj/s

(extract values)

The MIME type of a response with the Content-Type values (a string or a seq of strings), or nil when none of them parses.

It's the last value that parses, other than /. When it has no charset, it takes the charset of an earlier value with the same essence.

(extract ["text/html;charset=gbk;a=b" "text/html;x=y"])
;; => {:type "text" :subtype "html"
;;     :parameters {"x" "y" "charset" "gbk"}}
The MIME type of a response with the Content-Type `values` (a string or
a seq of strings), or nil when none of them parses.

It's the last value that parses, other than */*. When it has no charset,
it takes the charset of an earlier value with the same essence.

    (extract ["text/html;charset=gbk;a=b" "text/html;x=y"])
    ;; => {:type "text" :subtype "html"
    ;;     :parameters {"x" "y" "charset" "gbk"}}
sourceraw docstring

font?clj/s

(font? m)

Whether the MIME type m is a font type, e.g. font/woff2 or application/vnd.ms-fontobject.

Whether the MIME type `m` is a font type, e.g. font/woff2 or
application/vnd.ms-fontobject.
sourceraw docstring

html?clj/s

(html? m)

Whether the MIME type m is text/html (application/xhtml+xml is XML).

Whether the MIME type `m` is text/html (application/xhtml+xml is XML).
sourceraw docstring

image?clj/s

(image? m)

Whether the MIME type m is an image type, e.g. image/png.

Whether the MIME type `m` is an image type, e.g. image/png.
sourceraw docstring

javascript?clj/s

(javascript? m)

Whether the MIME type m is a JavaScript type, e.g. text/javascript or application/x-javascript.

Whether the MIME type `m` is a JavaScript type, e.g. text/javascript or
application/x-javascript.
sourceraw docstring

json?clj/s

(json? m)

Whether the MIME type m is a JSON type, e.g. one with a +json suffix.

Whether the MIME type `m` is a JSON type, e.g. one with a +json suffix.
sourceraw docstring

label->encodingclj/s

(label->encoding label)

The name of the encoding that the text label names, e.g. windows-1252 for latin1, or nil.

The name of the encoding that the text `label` names, e.g. windows-1252
for latin1, or nil.
sourceraw docstring

nosniff?clj/s

(nosniff? values)

Whether the X-Content-Type-Options values (a string or a seq of strings) forbid sniffing: whether the first is nosniff, ignoring case.

Whether the X-Content-Type-Options `values` (a string or a seq of
strings) forbid sniffing: whether the first is nosniff, ignoring case.
sourceraw docstring

parseclj/s

(parse s)

The MIME type in the text s, or nil when it doesn't parse.

(parse "Text/HTML; Charset="utf-8"") ;; => {:type "text" :subtype "html" ;; :parameters {"charset" "utf-8"}}

The MIME type in the text `s`, or nil when it doesn't parse.

(parse "Text/HTML; Charset=\"utf-8\"")
;; => {:type "text" :subtype "html"
;;     :parameters {"charset" "utf-8"}}
sourceraw docstring

scriptable?clj/s

(scriptable? m)

Whether the MIME type m is one that can hold scripts: XML, HTML or PDF.

Whether the MIME type `m` is one that can hold scripts: XML, HTML or
PDF.
sourceraw docstring

serializeclj/s

(serialize m)

The MIME type m as text, with quotes around each parameter value that isn't a token.

(serialize "Text/HTML; Charset=\"utf-8\"; x=(")
;; => "text/html;charset=utf-8;x=\"(\""
The MIME type `m` as text, with quotes around each parameter value that
isn't a token.

    (serialize "Text/HTML; Charset=\"utf-8\"; x=(")
    ;; => "text/html;charset=utf-8;x=\"(\""
sourceraw docstring

sniffclj/s

(sniff b)
(sniff b content-type)
(sniff b content-type {:keys [no-sniff?] :as opts})

The MIME type that a browser treats a resource as, by its first bytes b and its content-type, with the opts:

  • :no-sniff?, true when X-Content-Type-Options says nosniff

The bytes are a byte array on the JVM and a Uint8Array in ClojureScript. Only the first 1445 count. The Content-Type is the header's values (as extract takes them), a parsed MIME type or nil.

The result is the Content-Type unless it's missing or unknown, or it's an image, audio or video type that the bytes contradict. HTML and XML always stay.

(sniff (.getBytes "<!DOCTYPE html><title>Hi</title>") nil)
;; => {:type "text" :subtype "html"}

For an image or audio element, use the tables in patterns.

The MIME type that a browser treats a resource as, by its first bytes
`b` and its `content-type`, with the `opts`:

- :no-sniff?, true when X-Content-Type-Options says nosniff

The bytes are a byte array on the JVM and a Uint8Array in ClojureScript.
Only the first 1445 count. The Content-Type is the header's values (as
extract takes them), a parsed MIME type or nil.

The result is the Content-Type unless it's missing or unknown, or it's an
image, audio or video type that the bytes contradict. HTML and XML always
stay.

    (sniff (.getBytes "<!DOCTYPE html><title>Hi</title>") nil)
    ;; => {:type "text" :subtype "html"}

For an image or audio element, use the tables in patterns.
sourceraw docstring

valid?clj/s

(valid? s)

Whether HTTP's grammar allows the text s as a MIME type, e.g. text/html but not text/html;.

Whether HTTP's grammar allows the text `s` as a MIME type, e.g.
text/html but not text/html;.
sourceraw docstring

xml?clj/s

(xml? m)

Whether the MIME type m is an XML type, e.g. one with a +xml suffix.

Whether the MIME type `m` is an XML type, e.g. one with a +xml suffix.
sourceraw docstring

zip-based?clj/s

(zip-based? m)

Whether the MIME type m is based on ZIP, e.g. one with a +zip suffix.

Whether the MIME type `m` is based on ZIP, e.g. one with a +zip suffix.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close