minibuffer-completion.js: Avoid some minor errors
[conkeror.git] / modules / load-spec.js
blobb2519bba97e45861eec15b03e0f6df21bbd7f778
2 /**
3  * A load_spec has the following properties:
4  *
5  * Name:        Required?   Type:           Description:
6  * -----        ---------   -----           ------------
7  * uri          required    string          Specifies the URI of the target.
8  *
9  * document     optional    nsIDOMDocument  Specifies a document corresponding to the target.
10  *                                          Can also provide a default value for the mime_type property,
11  *                                          the title property, and the source_frame property.
12  *
13  * flags        optional    number          Specifies flags to pass to nsIWebNavigation.loadURI
14  *
15  * cache_key    optional    nsISHEntry      Specifies a key for accessing the target from the cache.
16  *
17  * referrer     optional    nsIURI          Specifies the referrer URI to use to access the target.
18  *
19  * post_data    optional    nsIInputStream  Specifies POST data to use to access the target.
20  *                                          The request headers should be included in this stream.
21  *
22  * request_mime_type
23  *              optional    string          Specifies the MIME type for the request post data.
24  *
25  * raw_post_data
26  *              optional    nsIInputStream  Specifies the POST data to use to access the target.
27  *                                          The request_mime_type property must also be set.
28  *                                          This provides a value for post_data.
29  *
30  * mime_type    optional    string          Specifies the MIME type of the target.
31  *
32  * title        optional    string          Specifies a title/description text associated with the target.
33  *
34  * source_frame optional    nsIDOMWindow    Specifies the frame from which the link to the target was "obtained".
35  *                                          Can provide a default value for referrer if document is not specified.
36  *
37  * filename     optional    string          Specifies a default filename to use to save the target.
38  *
39  * filename_extension
40  *              optional    string          Specifies a default filename extension to use to save the target.
41  *
42  * suggest_filename_from_uri
43  *              optional    boolean         Specifies whether to attempt to generate a filename from the URI.
44  *                                          Defaults to true.
45  */
46 function load_spec(x) { x.__proto__ = load_spec.prototype; return x; }
48 function is_load_spec(x) {
49     return (typeof(x) == "string") || (x instanceof load_spec);
52 function load_spec_document(x) {
53     return x.document;
56 function load_spec_title(x) {
57     if (x.title)
58         return x.title;
59     if (x.document)
60         return x.document.title;
61     return null;
64 function load_spec_mime_type(x) {
65     if (typeof(x) == "object") {
66         if (x.mime_type)
67             return x.mime_type;
68         if (x.document)
69             return x.document.contentType || "application/octet-stream";
70     }
71     return mime_type_from_uri(load_spec_uri(x));
74 function load_spec_filename(x) {
75     return x.filename;
78 function load_spec_filename_extension(x) {
79     return x.filename_extension;
82 function get_web_navigation_for_frame(frame) {
83     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
84     return ifr.getInterface(Ci.nsIWebNavigation);
87 function get_SHEntry_for_document(doc)
89     try
90     {
91         var frame = doc.defaultView;
92         var webNav = get_web_navigation_for_frame(frame);
93         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
94         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
95         return desc;
96     } catch (e) { return null; }
99 function load_spec_set_properties_from_sh_entry(x) {
100     var sh_entry = get_SHEntry_for_document(x.document);
101     if (sh_entry != null) {
102         x.cache_key = sh_entry;
103         x.referrer = sh_entry.referrerURI;
104         x.post_data = sh_entry.postData;
105     }
108 function load_spec_referrer(x) {
109     if (x.referrer)
110         return x.referrer;
111     if (x.document) {
112         load_spec_set_properties_from_sh_entry(x);
113         return x.referrer;
114     }
115     if (x.source_frame) {
116         x.referrer = x.source_frame.document.documentURIObject;
117         return x.referrer;
118     }
119     return null;
122 function load_spec_post_data(x) {
123     if (x.post_data)
124         return x.post_data;
125     if (x.raw_post_data) {
126         let y = x.raw_post_data;
127         if (typeof(y) == "string")
128             y = string_input_stream(y);
129         x.post_data = mime_input_stream(y, [["Content-Type", x.request_mime_type]]);
130         return x.post_data;
131     }
132     if (x.document) {
133         load_spec_set_properties_from_sh_entry(x);
134         return x.post_data;
135     }
136     return null;
139 function load_spec_raw_post_data(x) {
140     return x.raw_post_data;
143 function load_spec_request_mime_type(x) {
144     return x.request_mime_type;
147 function load_spec_cache_key(x) {
148     if (x.cache_key)
149         return x.cache_key;
150     if (x.document) {
151         load_spec_set_properties_from_sh_entry(x);
152         return x.cache_key;
153     }
154     return null;
157 function load_spec_source_frame(x) {
158     if (x.source_frame)
159         return x.source_frame;
160     if (x.document)
161         return x.document.defaultView;
162     return null;
165 function load_spec_uri_string(x) {
166     if (typeof(x) == "string")
167         return x;
168     if (x.uri)
169         return x.uri;
170     if (x.document && x.document.defaultView)
171         return x.document.defaultView.location.href;
172     if (x.document)
173         return x.document.documentURI;
174     return null;
177 function load_spec_uri(x) {
178     if (x.document && x.document.defaultView)
179         return x.document.defaultView.location.href;
180     if (x.document)
181         return x.document.documentURIObject;
182     return make_uri(load_spec_uri_string(x));
185 function load_spec_flags(x) {
186     return x.load_spec_flags;
189 function load_spec_mime_info(x) {
190     var type = load_spec_mime_type(x);
191     return mime_info_from_mime_type(type);
194 function load_spec_default_shell_command(x) {
195     var mime_type = load_spec_mime_type(x);
196     return get_external_handler_for_mime_type(mime_type);
199 /* Target can be either a content_buffer or an nsIWebNavigation */
200 function apply_load_spec(target, spec) {
201     var uri = load_spec_uri_string(spec);
202     var flags = load_spec_flags(spec);
203     var referrer = load_spec_referrer(spec);
204     var post_data = load_spec_post_data(spec);
206     if (flags == null)
207         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
209     if (target instanceof content_buffer) {
210         target._display_URI = uri;
211         target = target.web_navigation;
212         //buffer_description_change_hook.run(target);
213     }
214     target.loadURI(uri, flags, referrer, post_data, null /* headers */);