Liking cljdoc? Tell your friends :D

dk.simongray.whatwg-mime

Parse and sniff MIME types 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 of the text

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 and sniff MIME types 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 of the text

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 MIME type: ZIP, gzip or RAR.

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

audio-or-video?clj/s

(audio-or-video? m)

Whether the MIME type m is of audio or video, application/ogg included.

Whether the MIME type `m` is of audio or video, application/ogg
included.
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

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 that 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 that 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 MIME type.

Whether the MIME type `m` is a font MIME type.
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 MIME type.

Whether the MIME type `m` is an image MIME type.
sourceraw docstring

javascript?clj/s

(javascript? m)

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

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

json?clj/s

(json? m)

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

Whether the MIME type `m` is a JSON MIME 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, in any case.

Whether the X-Content-Type-Options `values` (a string or a seq of
strings) forbid sniffing: whether the first is nosniff, in any 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 a scriptable MIME type: XML, HTML or PDF.

Whether the MIME type `m` is a scriptable MIME type: XML, HTML or PDF.
sourceraw docstring

serializeclj/s

(serialize m)

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

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

    (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?, 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 its values (as extract takes them), a 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?, 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 its values (as extract
takes them), a 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 MIME type, e.g. one with a +xml suffix.

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

zip-based?clj/s

(zip-based? m)

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

Whether the MIME type `m` is a ZIP-based MIME type, 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