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