whitespace
[conkeror.git] / modules / load-spec.js
blobf786c6ffc15eb5fff7a7d07d527a058976d8b1dc
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  */
52 in_module(null);
54 require("webjump.js");
56 function page_fragment_load_spec (elem) {
57     var uri = makeURLAbsolute(elem.ownerDocument.documentURI,
58                               "#" + (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 = {
157     constructor: load_spec,
158     toString: function () "[object load_spec]"
161 function load_spec_document (x) {
162     return x.document;
165 function load_spec_element (x) {
166     return x.element;
169 function load_spec_title (x) {
170     if (x.title)
171         return x.title;
172     if (x.document)
173         return x.document.title;
174     return null;
177 function load_spec_mime_type (x) {
178     if (x.mime_type)
179         return x.mime_type;
180     if (x.document)
181         return x.document.contentType || "application/octet-stream";
182     return mime_type_from_uri(load_spec_uri(x));
185 function load_spec_filename (x) {
186     return x.filename;
189 function load_spec_filename_extension (x) {
190     return x.filename_extension;
193 function get_web_navigation_for_frame (frame) {
194     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
195     return ifr.getInterface(Ci.nsIWebNavigation);
198 function get_SHEntry_for_document (doc) {
199     try {
200         var frame = doc.defaultView;
201         var webNav = get_web_navigation_for_frame(frame);
202         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
203         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
204         return desc;
205     } catch (e) { return null; }
208 function load_spec_set_properties_from_sh_entry (x) {
209     var sh_entry = get_SHEntry_for_document(x.document);
210     if (sh_entry != null) {
211         x.cache_key = sh_entry;
212         x.referrer = sh_entry.referrerURI;
213         x.post_data = sh_entry.postData;
214     }
217 function load_spec_referrer (x) {
218     if (x.referrer)
219         return x.referrer;
220     if (x.document) {
221         load_spec_set_properties_from_sh_entry(x);
222         return x.referrer;
223     }
224     if (x.source_frame) {
225         x.referrer = x.source_frame.document.documentURIObject;
226         return x.referrer;
227     }
228     return null;
231 function load_spec_post_data (x) {
232     if (x.post_data)
233         return x.post_data;
234     if (x.raw_post_data) {
235         let y = x.raw_post_data;
236         if (typeof(y) == "string")
237             y = string_input_stream(y);
238         x.post_data = mime_input_stream(y, [["Content-Type", x.request_mime_type]]);
239         return x.post_data;
240     }
241     if (x.document) {
242         load_spec_set_properties_from_sh_entry(x);
243         return x.post_data;
244     }
245     return null;
248 function load_spec_raw_post_data (x) {
249     return x.raw_post_data;
252 function load_spec_request_mime_type (x) {
253     return x.request_mime_type;
256 function load_spec_cache_key (x) {
257     if (x.cache_key)
258         return x.cache_key;
259     if (x.document) {
260         load_spec_set_properties_from_sh_entry(x);
261         return x.cache_key;
262     }
263     return null;
266 function load_spec_source_frame (x) {
267     if (x.source_frame)
268         return x.source_frame;
269     if (x.document)
270         return x.document.defaultView;
271     return null;
274 function load_spec_uri_string (x) {
275     if (x.uri)
276         return x.uri;
277     if (x.document && x.document.defaultView)
278         return x.document.defaultView.location.href;
279     if (x.document)
280         return x.document.documentURI;
281     return null;
284 function load_spec_uri (x) {
285     if (x.document && x.document.defaultView)
286         return make_uri(x.document.defaultView.location.href);
287     if (x.document)
288         return x.document.documentURIObject;
289     return make_uri(load_spec_uri_string(x));
292 function load_spec_flags (x) {
293     return x.load_spec_flags;
296 function load_spec_mime_info (x) {
297     var type = load_spec_mime_type(x);
298     return mime_info_from_mime_type(type);
301 function load_spec_default_shell_command (x) {
302     var mime_type = load_spec_mime_type(x);
303     return external_content_handlers.get(mime_type);
306 function load_spec_forced_charset (x) {
307     return x.forced_charset;
310 define_variable('forced_charset_list', null,
311     "Alist mapping url-regexps to forced charsets.  The first match "+
312     "will be used.");
314 /* Target can be either a content_buffer or an nsIWebNavigation */
315 function apply_load_spec (target, spec) {
316     if (! (spec instanceof load_spec))
317         spec = load_spec(spec);
318     var uri = load_spec_uri_string(spec);
319     var flags = load_spec_flags(spec);
320     var referrer = load_spec_referrer(spec);
321     var post_data = load_spec_post_data(spec);
322     var forced_charset = load_spec_forced_charset(spec);
324     if (! forced_charset && forced_charset_list)
325         forced_charset = predicate_alist_match(forced_charset_list, uri);
327     if (forced_charset) {
328         try {
329             var atomservice = Cc['@mozilla.org/atom-service;1']
330                 .getService(Ci.nsIAtomService);
331             target.web_navigation.documentCharsetInfo.forcedCharset =
332                 atomservice.getAtom(forced_charset);
333         } catch (e) {}
334     }
336     if (flags == null)
337         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
339     if (target instanceof content_buffer) {
340         try {
341             target.web_navigation.loadURI(uri, flags, referrer, post_data, null /* headers */);
342             target._display_uri = uri;
343             buffer_description_change_hook.run(target);
344         } catch (e) {
345             /* Ignore error for now */
346         }
347     } else {
348         try {
349             target.loadURI(uri, flags, referrer, post_data, null /* headers */);
350         } catch (e) {
351             /* Ignore error for now */
352         }
353     }
356 provide("load-spec");