point: buffer-outer-container overflow:hidden, buffer-container manually sized
[conkeror.git] / modules / mime.js
blobaf57a4037a8a03a11c98dab778ec88fa7ff379f8
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nicholas A. Zigarovich
4  * (C) Copyright 2009 John J. Foerch
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
12 /**
13  * mime_type_parse splits a mime type or mime type pattern on
14  * a / character, and returns a two element array of the halves.
15  * If mime_type does not contain a /, then the argument is returned
16  * unchanged.
17  */
18 function mime_type_parse (mime_type) {
19     var slash_idx = mime_type.indexOf("/");
20     if (slash_idx != -1)
21         return [mime_type.substring(0, slash_idx),
22                 mime_type.substring(slash_idx + 1)];
23     return mime_type;
26 function mime_type_table () {}
27 mime_type_table.prototype = {
28     constructor: mime_type_table,
29     table: {},
30     get: function (mime_type) {
31         var p = mime_type_parse(mime_type);
32         if (this.table[p[0]])
33             return this.table[p[0]][p[1]] ||
34                 this.table[p[0]]["*"] ||
35                 this.table["*"] || null;
36         else
37             return this.table["*"] || null;
38     },
39     set: function (mime_type, value) {
40         var p = mime_type_parse(mime_type);
41         if (p == "*")
42             return (this.table["*"] = value);
43         if (typeof p == "string") {
44             if (! this.table[p])
45                 this.table[p] = {};
46             return (this.table[p]["*"] = value);
47         }
48         if (! this.table[p[0]])
49             this.table[p[0]] = {};
50         return (this.table[p[0]][p[1]] = value);
51     }
55 /**
56  * define_mime_type_table makes a user variable of the given name that
57  * encapsulates the table given as the default value, having the methods
58  * `get' and `set' for maintaining an association of mime-type patterns
59  * with arbitrary values.
60  */
61 function define_mime_type_table (name, default_value, doc) {
62     var handlers = { table: default_value };
63     handlers.__proto__ = mime_type_table.prototype;
64     define_special_variable(name,
65         function () handlers,
66         function (table) handlers.table = table,
67         doc);
71 define_mime_type_table("external_content_handlers",
72     {
73         "*": getenv("EDITOR"),
74         text: { "*": getenv("EDITOR") },
75         image: { "*": "feh" },
76         video: { "*": "mplayer" },
77         audio: { "*": "mplayer" },
78         application: {
79             pdf: "evince",
80             postscript: "evince",
81             "x-dvi": "evince"
82         }
83     },
84     "Structure associating MIME types and MIME type patterns with "+
85     "the names of programs for handling those them.  The key \"*\" "+
86     "is a pattern-matching symbol which matches anything.");
89 /**
90  *
91  */
92 function mime_type_from_uri (uri) {
93     var type = "application/octet-stream";
94     try {
95         uri = make_uri(uri);
96         type = mime_service.getTypeFromURI(uri);
97     } catch (e) {}
98     return type;
103  */
104 function mime_info_from_mime_type (type) {
105     if (type == null)
106         type = "application/octet-stream";
107     try {
108         return mime_service.getFromTypeAndExtension(type, null);
109     } catch (e) {
110         return null;
111     }
114 provide("mime");