2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
9 * A load_spec has the following properties:
11 * Name: Required? Type: Description:
12 * ----- --------- ----- ------------
13 * uri required string Specifies the URI of the target.
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.
19 * element optional nsIDOMNode The DOM node of a load_spec created by load_spec_from_element.
21 * flags optional number Specifies flags to pass to nsIWebNavigation.loadURI
23 * cache_key optional nsISHEntry Specifies a key for accessing the target from the cache.
25 * referrer optional nsIURI Specifies the referrer URI to use to access the target.
27 * post_data optional nsIInputStream Specifies POST data to use to access the target.
28 * The request headers should be included in this stream.
31 * optional string Specifies the MIME type for the request 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.
38 * mime_type optional string Specifies the MIME type of the target.
40 * title optional string Specifies a title/description text associated with the target.
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.
45 * filename optional string Specifies a default filename to use to save the target.
48 * optional string Specifies a default filename extension to use to save the target.
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;
71 function load_spec_from_element (elem) {
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;
84 if ((elem instanceof Ci.nsIDOMHTMLAnchorElement ||
85 elem instanceof Ci.nsIDOMHTMLAreaElement ||
86 elem instanceof Ci.nsIDOMHTMLLinkElement) &&
87 elem.hasAttribute("href"))
90 title = elem.title || elem.textContent;
92 else if (elem instanceof Ci.nsIDOMHTMLImageElement) {
94 title = elem.title || elem.alt;
96 else if (elem.hasAttribute("id") ||
97 (elem instanceof Ci.nsIDOMHTMLAnchorElement &&
98 elem.hasAttribute("name"))) {
99 return page_fragment_load_spec(elem);
103 while (node && !(node instanceof Ci.nsIDOMHTMLAnchorElement))
104 node = node.parentNode;
106 if (node.hasAttribute("href"))
115 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) {
116 url = node.getAttributeNS(XLINK_NS, "href");
119 node = node.parentNode;
122 url = makeURLAbsolute(node.baseURI, url);
123 title = node.title || node.textContent;
126 if (url && url.length > 0) {
127 if (title && title.length == 0)
130 spec.source_frame = elem.ownerDocument.defaultView;
138 function load_spec (x) {
140 if (typeof(x) == "string")
141 x = get_url_or_webjump(x);
142 if (typeof(x) == "string")
144 else if ((x instanceof Ci.nsIDOMNode) ||
145 (x instanceof Ci.nsIDOMWindow))
147 spec = load_spec_from_element(x);
148 } else if (typeof(x) == "object") {
151 if (! load_spec_uri_string(spec))
152 throw new Error("Invalid load-spec");
153 spec.__proto__ = load_spec.prototype;
156 load_spec.prototype = {
157 constructor: load_spec,
158 toString: function () "[object load_spec]"
161 function load_spec_document (x) {
165 function load_spec_element (x) {
169 function load_spec_title (x) {
173 return x.document.title;
177 function load_spec_mime_type (x) {
181 return x.document.contentType || "application/octet-stream";
182 return mime_type_from_uri(load_spec_uri(x));
185 function load_spec_filename (x) {
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) {
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);
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;
217 function load_spec_referrer (x) {
221 load_spec_set_properties_from_sh_entry(x);
224 if (x.source_frame) {
225 x.referrer = x.source_frame.document.documentURIObject;
231 function load_spec_post_data (x) {
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]]);
242 load_spec_set_properties_from_sh_entry(x);
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) {
260 load_spec_set_properties_from_sh_entry(x);
266 function load_spec_source_frame (x) {
268 return x.source_frame;
270 return x.document.defaultView;
274 function load_spec_uri_string (x) {
277 if (x.document && x.document.defaultView)
278 return x.document.defaultView.location.href;
280 return x.document.documentURI;
284 function load_spec_uri (x) {
285 if (x.document && x.document.defaultView)
286 return make_uri(x.document.defaultView.location.href);
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 "+
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) {
329 var atomservice = Cc['@mozilla.org/atom-service;1']
330 .getService(Ci.nsIAtomService);
331 target.web_navigation.documentCharsetInfo.forcedCharset =
332 atomservice.getAtom(forced_charset);
337 flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
339 if (target instanceof content_buffer) {
341 target.web_navigation.loadURI(uri, flags, referrer, post_data, null /* headers */);
342 target._display_uri = uri;
343 buffer_description_change_hook.run(target);
345 /* Ignore error for now */
349 target.loadURI(uri, flags, referrer, post_data, null /* headers */);
351 /* Ignore error for now */
356 provide("load-spec");