view-as-mime-type: fix bug in interactive declaration
[conkeror.git] / modules / load-spec.js
blobc20300d4bd640076e21c86b4479d9fa0d00ab79a
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 /**
9  * A load_spec has the following properties:
10  *
11  * Name:        Required?   Type:           Description:
12  * -----        ---------   -----           ------------
13  * uri          required    string          Specifies the URI of the target.
14  *
15  * document     optional    nsIDOMDocument  Specifies a document corresponding to the target.
16  *                                          Can also provide a default value for the mime_type property,
17  *                                          the title property, and the source_frame property.
18  *
19  * element      optional    nsIDOMNode      The DOM node of a load_spec created by load_spec_from_element.
20  *
21  * flags        optional    number          Specifies flags to pass to nsIWebNavigation.loadURI
22  *
23  * cache_key    optional    nsISHEntry      Specifies a key for accessing the target from the cache.
24  *
25  * referrer     optional    nsIURI          Specifies the referrer URI to use to access the target.
26  *
27  * post_data    optional    nsIInputStream  Specifies POST data to use to access the target.
28  *                                          The request headers should be included in this stream.
29  *
30  * request_mime_type
31  *              optional    string          Specifies the MIME type for the request post data.
32  *
33  * raw_post_data
34  *              optional    nsIInputStream  Specifies the POST data to use to access the target.
35  *                                          The request_mime_type property must also be set.
36  *                                          This provides a value for post_data.
37  *
38  * mime_type    optional    string          Specifies the MIME type of the target.
39  *
40  * title        optional    string          Specifies a title/description text associated with the target.
41  *
42  * source_frame optional    nsIDOMWindow    Specifies the frame from which the link to the target was "obtained".
43  *                                          Can provide a default value for referrer if document is not specified.
44  *
45  * filename     optional    string          Specifies a default filename to use to save the target.
46  *
47  * filename_extension
48  *              optional    string          Specifies a default filename extension to use to save the target.
49  *
50  * suggest_filename_from_uri
51  *              optional    boolean         Specifies whether to attempt to generate a filename from the URI.
52  *                                          Defaults to true.
53  */
55 require("webjump.js");
57 function load_spec_from_element (elem) {
58     var spec = {};
59     if (elem instanceof Ci.nsIDOMWindow)
60         spec.document = elem.document;
62     else if (elem instanceof Ci.nsIDOMHTMLFrameElement ||
63              elem instanceof Ci.nsIDOMHTMLIFrameElement)
64         spec.document = elem.contentDocument;
66     else {
67         var url = null;
68         var title = null;
70         if (elem instanceof Ci.nsIDOMHTMLAnchorElement ||
71             elem instanceof Ci.nsIDOMHTMLAreaElement ||
72             elem instanceof Ci.nsIDOMHTMLLinkElement) {
73             if (elem.hasAttribute("href"))
74                 url = elem.href;
75             title = elem.title || elem.textContent;
76         }
77         else if (elem instanceof Ci.nsIDOMHTMLImageElement) {
78             url = elem.src;
79             title = elem.title || elem.alt;
80         }
81         else {
82             var node = elem;
83             while (node && !(node instanceof Ci.nsIDOMHTMLAnchorElement))
84                 node = node.parentNode;
85             if (node) {
86                 if (node.hasAttribute("href"))
87                     url = node.href;
88                 else
89                     node = null;
90             }
91             if (!node) {
92                 // Try simple XLink
93                 node = elem;
94                 while (node) {
95                     if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) {
96                         url = node.getAttributeNS(XLINK_NS, "href");
97                         break;
98                     }
99                     node = node.parentNode;
100                 }
101                 if (url)
102                     url = makeURLAbsolute(node.baseURI, url);
103                 title = node.title || node.textContent;
104             }
105         }
106         if (url && url.length > 0) {
107             if (title && title.length == 0)
108                 title = null;
109             spec.uri = url;
110             spec.source_frame = elem.ownerDocument.defaultView;
111             spec.title = title;
112         }
113         spec.element = elem;
114     }
115     return spec;
118 function load_spec (x) {
119     var spec;
120     if (typeof(x) == "string")
121         x = get_url_or_webjump(x);
122     if (typeof(x) == "string")
123         spec = { uri: x };
124     else if ((x instanceof Ci.nsIDOMNode) ||
125              (x instanceof Ci.nsIDOMWindow))
126     {
127         spec = load_spec_from_element(x);
128     } else if (typeof(x) == "object") {
129         spec = x;
130     }
131     if (! load_spec_uri_string(spec))
132         throw new Error("Invalid load-spec");
133     spec.__proto__ = load_spec.prototype;
134     return spec;
136 load_spec.prototype = {};
138 function load_spec_document (x) {
139     return x.document;
142 function load_spec_element (x) {
143     return x.element;
146 function load_spec_title (x) {
147     if (x.title)
148         return x.title;
149     if (x.document)
150         return x.document.title;
151     return null;
154 function load_spec_mime_type (x) {
155     if (x.mime_type)
156         return x.mime_type;
157     if (x.document)
158         return x.document.contentType || "application/octet-stream";
159     return mime_type_from_uri(load_spec_uri(x));
162 function load_spec_filename (x) {
163     return x.filename;
166 function load_spec_filename_extension (x) {
167     return x.filename_extension;
170 function get_web_navigation_for_frame (frame) {
171     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
172     return ifr.getInterface(Ci.nsIWebNavigation);
175 function get_SHEntry_for_document (doc) {
176     try {
177         var frame = doc.defaultView;
178         var webNav = get_web_navigation_for_frame(frame);
179         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
180         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
181         return desc;
182     } catch (e) { return null; }
185 function load_spec_set_properties_from_sh_entry (x) {
186     var sh_entry = get_SHEntry_for_document(x.document);
187     if (sh_entry != null) {
188         x.cache_key = sh_entry;
189         x.referrer = sh_entry.referrerURI;
190         x.post_data = sh_entry.postData;
191     }
194 function load_spec_referrer (x) {
195     if (x.referrer)
196         return x.referrer;
197     if (x.document) {
198         load_spec_set_properties_from_sh_entry(x);
199         return x.referrer;
200     }
201     if (x.source_frame) {
202         x.referrer = x.source_frame.document.documentURIObject;
203         return x.referrer;
204     }
205     return null;
208 function load_spec_post_data (x) {
209     if (x.post_data)
210         return x.post_data;
211     if (x.raw_post_data) {
212         let y = x.raw_post_data;
213         if (typeof(y) == "string")
214             y = string_input_stream(y);
215         x.post_data = mime_input_stream(y, [["Content-Type", x.request_mime_type]]);
216         return x.post_data;
217     }
218     if (x.document) {
219         load_spec_set_properties_from_sh_entry(x);
220         return x.post_data;
221     }
222     return null;
225 function load_spec_raw_post_data (x) {
226     return x.raw_post_data;
229 function load_spec_request_mime_type (x) {
230     return x.request_mime_type;
233 function load_spec_cache_key (x) {
234     if (x.cache_key)
235         return x.cache_key;
236     if (x.document) {
237         load_spec_set_properties_from_sh_entry(x);
238         return x.cache_key;
239     }
240     return null;
243 function load_spec_source_frame (x) {
244     if (x.source_frame)
245         return x.source_frame;
246     if (x.document)
247         return x.document.defaultView;
248     return null;
251 function load_spec_uri_string (x) {
252     if (x.uri)
253         return x.uri;
254     if (x.document && x.document.defaultView)
255         return x.document.defaultView.location.href;
256     if (x.document)
257         return x.document.documentURI;
258     return null;
261 function load_spec_uri (x) {
262     if (x.document && x.document.defaultView)
263         return make_uri(x.document.defaultView.location.href);
264     if (x.document)
265         return x.document.documentURIObject;
266     return make_uri(load_spec_uri_string(x));
269 function load_spec_flags (x) {
270     return x.load_spec_flags;
273 function load_spec_mime_info (x) {
274     var type = load_spec_mime_type(x);
275     return mime_info_from_mime_type(type);
278 function load_spec_default_shell_command (x) {
279     var mime_type = load_spec_mime_type(x);
280     return get_mime_type_external_handler(mime_type);
283 function load_spec_forced_charset (x) {
284     return x.forced_charset;
287 define_variable('forced_charset_list', null,
288     "Alist mapping url-regexps to forced charsets.  The first match "+
289     "will be used.");
291 /* Target can be either a content_buffer or an nsIWebNavigation */
292 function apply_load_spec (target, spec) {
293     if (! (spec instanceof load_spec))
294         spec = load_spec(spec);
295     var uri = load_spec_uri_string(spec);
296     var flags = load_spec_flags(spec);
297     var referrer = load_spec_referrer(spec);
298     var post_data = load_spec_post_data(spec);
299     var forced_charset = load_spec_forced_charset(spec);
301     if (! forced_charset && forced_charset_list)
302         forced_charset = predicate_alist_match(forced_charset_list, uri);
304     if (forced_charset) {
305         try {
306             var atomservice = Cc['@mozilla.org/atom-service;1']
307                 .getService(Ci.nsIAtomService);
308             target.web_navigation.documentCharsetInfo.forcedCharset =
309                 atomservice.getAtom(forced_charset);
310         } catch (e) {}
311     }
313     if (flags == null)
314         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
316     if (target instanceof content_buffer) {
317         try {
318             target.web_navigation.loadURI(uri, flags, referrer, post_data, null /* headers */);
319             target._display_URI = uri;
320             buffer_description_change_hook.run(target);
321         } catch (e) {
322             /* Ignore error for now */
323         }
324     } else {
325         try {
326             target.loadURI(uri, flags, referrer, post_data, null /* headers */);
327         } catch (e) {
328             /* Ignore error for now */
329         }
330     }