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