wpt/css/css-images/gradient/color-stops-parsing.html fails
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Base / MIMETypeUtilities.js
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.fileExtensionForURL = function(url)
27 {
28     let lastPathComponent = parseURL(url).lastPathComponent;
29     if (!lastPathComponent)
30         return null;
31
32     let index = lastPathComponent.lastIndexOf(".");
33     if (index === -1)
34         return null;
35
36     if (index === lastPathComponent.length - 1)
37         return null;
38
39     return lastPathComponent.substr(index + 1);
40 };
41
42 WI.mimeTypeForFileExtension = function(extension)
43 {
44     const extensionToMIMEType = {
45         // Document types.
46         "html": "text/html",
47         "xhtml": "application/xhtml+xml",
48         "xml": "text/xml",
49
50         // Script types.
51         "js": "text/javascript",
52         "mjs": "text/javascript",
53         "json": "application/json",
54         "clj": "text/x-clojure",
55         "coffee": "text/x-coffeescript",
56         "ls": "text/x-livescript",
57         "ts": "text/typescript",
58         "ps": "application/postscript",
59         "jsx": "text/jsx",
60
61         // Stylesheet types.
62         "css": "text/css",
63         "less": "text/x-less",
64         "sass": "text/x-sass",
65         "scss": "text/x-scss",
66
67         // Image types.
68         "bmp": "image/bmp",
69         "gif": "image/gif",
70         "jpeg": "image/jpeg",
71         "jpg": "image/jpeg",
72         "pdf": "application/pdf",
73         "png": "image/png",
74         "tif": "image/tiff",
75         "tiff": "image/tiff",
76         "xbm": "image/x-xbitmap",
77         "webp": "image/webp",
78         "ico": "image/x-icon",
79
80         "ogx": "application/ogg",
81         "ogg": "audio/ogg",
82         "oga": "audio/ogg",
83         "ogv": "video/ogg",
84
85         // Annodex
86         "anx": "application/annodex",
87         "axa": "audio/annodex",
88         "axv": "video/annodex",
89         "spx": "audio/speex",
90
91         // WebM
92         "webm": "video/webm",
93
94         // MPEG
95         "m1a": "audio/mpeg",
96         "m2a": "audio/mpeg",
97         "mpg": "video/mpeg",
98         "m15": "video/mpeg",
99         "m1s": "video/mpeg",
100         "m1v": "video/mpeg",
101         "m75": "video/mpeg",
102         "mpa": "video/mpeg",
103         "mpeg": "video/mpeg",
104         "mpm": "video/mpeg",
105         "mpv": "video/mpeg",
106
107         // MPEG playlist
108         "m3u8": "application/x-mpegurl",
109         "m3url": "audio/x-mpegurl",
110         "m3u": "audio/x-mpegurl",
111
112         // MPEG-4
113         "m4v": "video/x-m4v",
114         "m4a": "audio/x-m4a",
115         "m4b": "audio/x-m4b",
116         "m4p": "audio/x-m4p",
117
118         // MP3
119         "mp3": "audio/mp3",
120
121         // MPEG-2
122         "mp2": "video/x-mpeg2",
123         "vob": "video/mpeg2",
124         "mod": "video/mpeg2",
125         "m2ts": "video/m2ts",
126         "m2t": "video/x-m2ts",
127
128         // 3GP/3GP2
129         "3gpp": "audio/3gpp",
130         "3g2": "audio/3gpp2",
131         "amc": "application/x-mpeg",
132
133         // AAC
134         "aac": "audio/aac",
135         "adts": "audio/aac",
136         "m4r": "audio/x-aac",
137
138         // CoreAudio File
139         "caf": "audio/x-caf",
140         "gsm": "audio/x-gsm",
141
142         // ADPCM
143         "wav": "audio/x-wav",
144
145         // Text Track
146         "vtt": "text/vtt",
147
148         // Font
149         "woff": "font/woff",
150         "woff2": "font/woff2",
151         "otf": "font/otf",
152         "ttf": "font/ttf",
153         "sfnt": "font/sfnt",
154
155         // Miscellaneous types.
156         "svg": "image/svg+xml",
157         "txt": "text/plain",
158         "xsl": "text/xsl"
159     };
160
161     return extensionToMIMEType[extension] || null;
162 };
163
164 WI.fileExtensionForMIMEType = function(mimeType)
165 {
166     if (!mimeType)
167         return null;
168
169     const mimeTypeToExtension = {
170         // Document types.
171         "text/html": "html",
172         "application/xhtml+xml": "xhtml",
173         "application/xml": "xml",
174         "text/xml": "xml",
175
176         // Script types.
177         "application/ecmascript": "js",
178         "application/javascript": "js",
179         "application/x-ecmascript": "js",
180         "application/x-javascript": "js",
181         "text/ecmascript": "js",
182         "text/javascript": "js",
183         "text/javascript1.0": "js",
184         "text/javascript1.1": "js",
185         "text/javascript1.2": "js",
186         "text/javascript1.3": "js",
187         "text/javascript1.4": "js",
188         "text/javascript1.5": "js",
189         "text/jscript": "js",
190         "text/x-ecmascript": "js",
191         "text/x-javascript": "js",
192         "application/json": "json",
193         "text/x-clojure": "clj",
194         "text/x-coffeescript": "coffee",
195         "text/livescript": "ls",
196         "text/x-livescript": "ls",
197         "text/typescript": "ts",
198         "application/postscript": "ps",
199         "text/jsx": "jsx",
200
201         // Stylesheet types.
202         "text/css": "css",
203         "text/x-less": "less",
204         "text/x-sass": "sass",
205         "text/x-scss": "scss",
206
207         // Image types.
208         "image/bmp": "bmp",
209         "image/gif": "gif",
210         "image/jp2": "jp2",
211         "image/jpeg": "jpg",
212         "application/pdf": "pdf",
213         "text/pdf": "pdf",
214         "image/png": "png",
215         "image/tiff": "tiff",
216         "image/x-xbitmap": "xbm",
217         "image/webp": "webp",
218         "image/vnd.microsoft.icon": "ico",
219         "image/x-icon": "ico",
220
221         // Ogg
222         "application/ogg": "ogx",
223         "audio/ogg": "ogg",
224
225         // Annodex
226         "application/annodex": "anx",
227         "audio/annodex": "axa",
228         "video/annodex": "axv",
229         "audio/speex": "spx",
230
231         // WebM
232         "video/webm": "webm",
233         "audio/webm": "webm",
234
235         // MPEG
236         "video/mpeg": "mpeg",
237
238         // MPEG playlist
239         "application/vnd.apple.mpegurl": "m3u8",
240         "application/mpegurl": "m3u8",
241         "application/x-mpegurl": "m3u8",
242         "audio/mpegurl": "m3u",
243         "audio/x-mpegurl": "m3u",
244
245         // MPEG-4
246         "video/x-m4v": "m4v",
247         "audio/x-m4a": "m4a",
248         "audio/x-m4b": "m4b",
249         "audio/x-m4p": "m4p",
250         "audio/mp4": "m4a",
251
252         // MP3
253         "audio/mp3": "mp3",
254         "audio/x-mp3": "mp3",
255         "audio/x-mpeg": "mp3",
256
257         // MPEG-2
258         "video/x-mpeg2": "mp2",
259         "video/mpeg2": "vob",
260         "video/m2ts": "m2ts",
261         "video/x-m2ts": "m2t",
262
263         // 3GP/3GP2
264         "audio/3gpp": "3gpp",
265         "audio/3gpp2": "3g2",
266         "application/x-mpeg": "amc",
267
268         // AAC
269         "audio/aac": "aac",
270         "audio/x-aac": "m4r",
271
272         // CoreAudio File
273         "audio/x-caf": "caf",
274         "audio/x-gsm": "gsm",
275
276         // ADPCM
277         "audio/x-wav": "wav",
278         "audio/vnd.wave": "wav",
279
280         // Text Track
281         "text/vtt": "vtt",
282
283         // Font
284         "font/woff": "woff",
285         "font/woff2": "woff2",
286         "font/otf": "otf",
287         "font/ttf": "ttf",
288         "font/sfnt": "sfnt",
289
290         // Miscellaneous types.
291         "image/svg+xml": "svg",
292         "text/plain": "txt",
293         "text/xsl": "xsl",
294     };
295
296     let extension = mimeTypeToExtension[mimeType];
297     if (extension)
298         return extension;
299
300     if (mimeType.endsWith("+json"))
301         return "json";
302     if (mimeType.endsWith("+xml"))
303         return "xml";
304
305     return null;
306 };
307
308 WI.shouldTreatMIMETypeAsText = function(mimeType)
309 {
310     if (!mimeType)
311         return false;
312
313     if (mimeType.startsWith("text/"))
314         return true;
315
316     if (mimeType.endsWith("+json") || mimeType.endsWith("+xml"))
317         return true;
318
319     let extension = WI.fileExtensionForMIMEType(mimeType);
320     if (extension === "xml")
321         return true;
322
323     // Various script and JSON mime types.
324     if (extension === "js" || extension === "json")
325         return true;
326
327     // Various media text mime types.
328     if (extension === "m3u8" || extension === "m3u")
329         return true;
330
331     if (mimeType.startsWith("application/"))
332         return mimeType.endsWith("script") || mimeType.endsWith("json") || mimeType.endsWith("xml");
333
334     return false;
335 };