whatwg-mimetype 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/lib/mime-type.js +49 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,6 +30,10 @@ This package's algorithms conform to those of the WHATWG [MIME Sniffing Standard
|
|
|
30
30
|
|
|
31
31
|
This package's main module's default export is a class, `MIMEType`. Its constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an `Error` will be thrown.
|
|
32
32
|
|
|
33
|
+
### The `parse()` static factory method
|
|
34
|
+
|
|
35
|
+
As an alternative to the constructor, you can use `MIMEType.parse(string)`. The only difference is that `parse()` will return `null` on failed parsing, whereas the constructor will throw. It thus makes the most sense to use the constructor in cases where unparseable MIME types would be exceptional, and use `parse()` when dealing with input from some unconstrained source.
|
|
36
|
+
|
|
33
37
|
### Properties
|
|
34
38
|
|
|
35
39
|
- `type`: the MIME type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `"text"`
|
|
@@ -48,8 +52,9 @@ This package's main module's default export is a class, `MIMEType`. Its construc
|
|
|
48
52
|
- `toString()` serializes the MIME type to a string
|
|
49
53
|
- `isHTML()`: returns true if this instance represents [a HTML MIME type](https://mimesniff.spec.whatwg.org/#html-mime-type)
|
|
50
54
|
- `isXML()`: returns true if this instance represents [an XML MIME type](https://mimesniff.spec.whatwg.org/#xml-mime-type)
|
|
55
|
+
- `isJavaScript({ allowParameters })`: returns true if this instance represents [a JavaScript MIME type](https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type); `allowParameters` can be set to true to allow arbitrary parameters, instead of their presence causing the method to return `false`
|
|
51
56
|
|
|
52
|
-
_Note: the `isHTML()` and `
|
|
57
|
+
_Note: the `isHTML()`, `isXML()`, and `isJavaScript()` methods are speculative, and may be removed or changed in future major versions. See [whatwg/mimesniff#48](https://github.com/whatwg/mimesniff/issues/48) for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom._
|
|
53
58
|
|
|
54
59
|
## `MIMETypeParameters` API
|
|
55
60
|
|
package/lib/mime-type.js
CHANGED
|
@@ -20,6 +20,14 @@ module.exports = class MIMEType {
|
|
|
20
20
|
this._parameters = new MIMETypeParameters(result.parameters);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
static parse(string) {
|
|
24
|
+
try {
|
|
25
|
+
return new this(string);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
23
31
|
get essence() {
|
|
24
32
|
return `${this.type}/${this.subtype}`;
|
|
25
33
|
}
|
|
@@ -68,6 +76,47 @@ module.exports = class MIMEType {
|
|
|
68
76
|
return serialize(this);
|
|
69
77
|
}
|
|
70
78
|
|
|
79
|
+
isJavaScript({ allowParameters = false } = {}) {
|
|
80
|
+
switch (this._type) {
|
|
81
|
+
case "text": {
|
|
82
|
+
switch (this._subtype) {
|
|
83
|
+
case "ecmascript":
|
|
84
|
+
case "javascript":
|
|
85
|
+
case "javascript1.0":
|
|
86
|
+
case "javascript1.1":
|
|
87
|
+
case "javascript1.2":
|
|
88
|
+
case "javascript1.3":
|
|
89
|
+
case "javascript1.4":
|
|
90
|
+
case "javascript1.5":
|
|
91
|
+
case "jscript":
|
|
92
|
+
case "livescript":
|
|
93
|
+
case "x-ecmascript":
|
|
94
|
+
case "x-javascript": {
|
|
95
|
+
return allowParameters || this._parameters.size === 0;
|
|
96
|
+
}
|
|
97
|
+
default: {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
case "application": {
|
|
103
|
+
switch (this._subtype) {
|
|
104
|
+
case "ecmascript":
|
|
105
|
+
case "javascript":
|
|
106
|
+
case "x-ecmascript":
|
|
107
|
+
case "x-javascript": {
|
|
108
|
+
return allowParameters || this._parameters.size === 0;
|
|
109
|
+
}
|
|
110
|
+
default: {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
71
120
|
isXML() {
|
|
72
121
|
return (this._subtype === "xml" && (this._type === "text" || this._type === "application")) ||
|
|
73
122
|
this._subtype.endsWith("+xml");
|