define load_paths user variable in conkeror.js
[conkeror/arlinius.git] / modules / load-spec.js
blobab8b7a10a88fba854f2583fa75f3a2afe3d557b9
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 page_fragment_load_spec (elem) {
58     var uri = makeURLAbsolute(elem.baseURI, "#" + (elem.id || elem.name));
59     var title = elem.ownerDocument.title;
60     if (elem.textContent) {
61         if (title) title += ' - ';
62         title += elem.textContent;
63     }
64     return {
65         uri: uri,
66         element: elem,
67         title: title
68     };
71 function load_spec_from_element (elem) {
72     var spec = {};
73     if (elem instanceof Ci.nsIDOMWindow)
74         spec.document = elem.document;
76     else if (elem instanceof Ci.nsIDOMHTMLFrameElement ||
77              elem instanceof Ci.nsIDOMHTMLIFrameElement)
78         spec.document = elem.contentDocument;
80     else {
81         var url = null;
82         var title = null;
84         if ((elem instanceof Ci.nsIDOMHTMLAnchorElement ||
85              elem instanceof Ci.nsIDOMHTMLAreaElement ||
86              elem instanceof Ci.nsIDOMHTMLLinkElement) &&
87             elem.hasAttribute("href"))
88         {
89             url = elem.href;
90             title = elem.title || elem.textContent;
91         }
92         else if (elem instanceof Ci.nsIDOMHTMLImageElement) {
93             url = elem.src;
94             title = elem.title || elem.alt;
95         }
96         else if (elem.hasAttribute("id") ||
97                  (elem instanceof Ci.nsIDOMHTMLAnchorElement &&
98                   elem.hasAttribute("name"))) {
99             return page_fragment_load_spec(elem);
100         }
101         else {
102             var node = elem;
103             while (node && !(node instanceof Ci.nsIDOMHTMLAnchorElement))
104                 node = node.parentNode;
105             if (node) {
106                 if (node.hasAttribute("href"))
107                     url = node.href;
108                 else
109                     node = null;
110             }
111             if (!node) {
112                 // Try simple XLink
113                 node = elem;
114                 while (node) {
115                     if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) {
116                         url = node.getAttributeNS(XLINK_NS, "href");
117                         break;
118                     }
119                     node = node.parentNode;
120                 }
121                 if (url)
122                     url = makeURLAbsolute(node.baseURI, url);
123                 title = node.title || node.textContent;
124             }
125         }
126         if (url && url.length > 0) {
127             if (title && title.length == 0)
128                 title = null;
129             spec.uri = url;
130             spec.source_frame = elem.ownerDocument.defaultView;
131             spec.title = title;
132         }
133         spec.element = elem;
134     }
135     return spec;
138 function load_spec (x) {
139     var spec;
140     if (typeof(x) == "string")
141         x = get_url_or_webjump(x);
142     if (typeof(x) == "string")
143         spec = { uri: x };
144     else if ((x instanceof Ci.nsIDOMNode) ||
145              (x instanceof Ci.nsIDOMWindow))
146     {
147         spec = load_spec_from_element(x);
148     } else if (typeof(x) == "object") {
149         spec = x;
150     }
151     if (! load_spec_uri_string(spec))
152         throw new Error("Invalid load-spec");
153     spec.__proto__ = load_spec.prototype;
154     return spec;
156 load_spec.prototype = {};
158 function load_spec_document (x) {
159     return x.document;
162 function load_spec_element (x) {
163     return x.element;
166 function load_spec_title (x) {
167     if (x.title)
168         return x.title;
169     if (x.document)
170         return x.document.title;
171     return null;
174 function load_spec_mime_type (x) {
175     if (x.mime_type)
176         return x.mime_type;
177     if (x.document)
178         return x.document.contentType || "application/octet-stream";
179     return mime_type_from_uri(load_spec_uri(x));
182 function load_spec_filename (x) {
183     return x.filename;
186 function load_spec_filename_extension (x) {
187     return x.filename_extension;
190 function get_web_navigation_for_frame (frame) {
191     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
192     return ifr.getInterface(Ci.nsIWebNavigation);
195 function get_SHEntry_for_document (doc) {
196     try {
197         var frame = doc.defaultView;
198         var webNav = get_web_navigation_for_frame(frame);
199         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
200         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
201         return desc;
202     } catch (e) { return null; }
205 function load_spec_set_properties_from_sh_entry (x) {
206     var sh_entry = get_SHEntry_for_document(x.document);
207     if (sh_entry != null) {
208         x.cache_key = sh_entry;
209         x.referrer = sh_entry.referrerURI;
210         x.post_data = sh_entry.postData;
211     }
214 function load_spec_referrer (x) {
215     if (x.referrer)
216         return x.referrer;
217     if (x.document) {
218         load_spec_set_properties_from_sh_entry(x);
219         return x.referrer;
220     }
221     if (x.source_frame) {
222         x.referrer = x.source_frame.document.documentURIObject;
223         return x.referrer;
224     }
225     return null;
228 function load_spec_post_data (x) {
229     if (x.post_data)
230         return x.post_data;
231     if (x.raw_post_data) {
232         let y = x.raw_post_data;
233         if (typeof(y) == "string")
234             y = string_input_stream(y);
235         x.post_data = mime_input_stream(y, [["Content-Type", x.request_mime_type]]);
236         return x.post_data;
237     }
238     if (x.document) {
239         load_spec_set_properties_from_sh_entry(x);
240         return x.post_data;
241     }
242     return null;
245 function load_spec_raw_post_data (x) {
246     return x.raw_post_data;
249 function load_spec_request_mime_type (x) {
250     return x.request_mime_type;
253 function load_spec_cache_key (x) {
254     if (x.cache_key)
255         return x.cache_key;
256     if (x.document) {
257         load_spec_set_properties_from_sh_entry(x);
258         return x.cache_key;
259     }
260     return null;
263 function load_spec_source_frame (x) {
264     if (x.source_frame)
265         return x.source_frame;
266     if (x.document)
267         return x.document.defaultView;
268     return null;
271 function load_spec_uri_string (x) {
272     if (x.uri)
273         return x.uri;
274     if (x.document && x.document.defaultView)
275         return x.document.defaultView.location.href;
276     if (x.document)
277         return x.document.documentURI;
278     return null;
281 function load_spec_uri (x) {
282     if (x.document && x.document.defaultView)
283         return make_uri(x.document.defaultView.location.href);
284     if (x.document)
285         return x.document.documentURIObject;
286     return make_uri(load_spec_uri_string(x));
289 function load_spec_flags (x) {
290     return x.load_spec_flags;
293 function load_spec_mime_info (x) {
294     var type = load_spec_mime_type(x);
295     return mime_info_from_mime_type(type);
298 function load_spec_default_shell_command (x) {
299     var mime_type = load_spec_mime_type(x);
300     return external_content_handlers.get(mime_type);
303 function load_spec_forced_charset (x) {
304     return x.forced_charset;
307 define_variable('forced_charset_list', null,
308     "Alist mapping url-regexps to forced charsets.  The first match "+
309     "will be used.");
311 /* Target can be either a content_buffer or an nsIWebNavigation */
312 function apply_load_spec (target, spec) {
313     if (! (spec instanceof load_spec))
314         spec = load_spec(spec);
315     var uri = load_spec_uri_string(spec);
316     var flags = load_spec_flags(spec);
317     var referrer = load_spec_referrer(spec);
318     var post_data = load_spec_post_data(spec);
319     var forced_charset = load_spec_forced_charset(spec);
321     if (! forced_charset && forced_charset_list)
322         forced_charset = predicate_alist_match(forced_charset_list, uri);
324     if (forced_charset) {
325         try {
326             var atomservice = Cc['@mozilla.org/atom-service;1']
327                 .getService(Ci.nsIAtomService);
328             target.web_navigation.documentCharsetInfo.forcedCharset =
329                 atomservice.getAtom(forced_charset);
330         } catch (e) {}
331     }
333     if (flags == null)
334         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
336     if (target instanceof content_buffer) {
337         try {
338             target.web_navigation.loadURI(uri, flags, referrer, post_data, null /* headers */);
339             target._display_uri = uri;
340             buffer_description_change_hook.run(target);
341         } catch (e) {
342             /* Ignore error for now */
343         }
344     } else {
345         try {
346             target.loadURI(uri, flags, referrer, post_data, null /* headers */);
347         } catch (e) {
348             /* Ignore error for now */
349         }
350     }