1 /***** BEGIN LICENSE BLOCK *****
2 Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 The contents of this file are subject to the Mozilla Public License Version
5 1.1 (the "License"); you may not use this file except in compliance with
6 the License. You may obtain a copy of the License at
7 http://www.mozilla.org/MPL/
9 Software distributed under the License is distributed on an "AS IS" basis,
10 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 for the specific language governing rights and limitations under the
14 The Initial Developer of the Original Code is Shawn Betts.
15 Portions created by the Initial Developer are Copyright (C) 2004,2005
16 by the Initial Developer. All Rights Reserved.
18 Alternatively, the contents of this file may be used under the terms of
19 either the GNU General Public License Version 2 or later (the "GPL"), or
20 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21 in which case the provisions of the GPL or the LGPL are applicable instead
22 of those above. If you wish to allow use of your version of this file only
23 under the terms of either the GPL or the LGPL, and not to allow others to
24 use your version of this file under the terms of the MPL, indicate your
25 decision by deleting the provisions above and replace them with the notice
26 and other provisions required by the GPL or the LGPL. If you do not delete
27 the provisions above, a recipient may use your version of this file under
28 the terms of any one of the MPL, the GPL or the LGPL.
29 ***** END LICENSE BLOCK *****/
32 * Utility functions for application scope.
37 function string_hashset() {
40 string_hashset.prototype = {
41 constructor : string_hashset,
47 contains : function(s) {
48 return (("-" + s) in this);
51 remove : function (s) {
55 for_each : function (f) {
63 function string_hashmap() {
66 string_hashmap.prototype = {
67 constructor : string_hashmap,
69 put : function(s,value) {
70 this["-" + s] = value;
73 contains : function(s) {
74 return (("-" + s) in this);
77 get : function(s, default_value) {
83 get_put_default : function(s, default_value) {
86 return (this["-" + s] = default_value);
89 remove : function (s) {
93 for_each : function (f) {
96 f(i.slice(1), this[i]);
100 for_each_value : function (f) {
101 for (var i in this) {
108 /// Window title formatting
111 * Default tile formatter. The page url is ignored. If there is a
112 * page_title, returns: "Page title - Conkeror". Otherwise, it
113 * returns just: "Conkeror".
115 function default_title_formatter (frame)
117 var page_title = frame.buffers.current.title;
119 if (page_title && page_title.length > 0)
120 return page_title + " - Conkeror";
125 var title_format_fn = null;
127 function set_window_title (frame)
129 frame.document.title = title_format_fn(frame);
132 function init_window_title ()
134 title_format_fn = default_title_formatter;
136 add_hook("frame_initialize_late_hook", set_window_title);
137 add_hook("current_browser_buffer_location_change_hook",
139 set_window_title(buffer.frame);
141 add_hook("select_buffer_hook", function (buffer) { set_window_title(buffer.frame); }, true);
142 add_hook("current_buffer_title_change_hook",
144 set_window_title(buffer.frame);
150 // Put the string on the clipboard
151 function writeToClipboard(str)
153 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
154 .getService(Components.interfaces.nsIClipboardHelper);
155 gClipboardHelper.copyString(str);
159 function makeURLAbsolute (base, url)
162 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
163 .getService(Components.interfaces.nsIIOService);
164 var baseURI = ioService.newURI(base, null, null);
166 return ioService.newURI (baseURI.resolve (url), null, null).spec;
170 function get_link_location (element)
172 if (element && element.getAttribute("href")) {
173 var loc = element.getAttribute("href");
174 return makeURLAbsolute(element.baseURI, loc);
180 function makeURL(aURL)
182 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
183 .getService(Components.interfaces.nsIIOService);
184 return ioService.newURI(aURL, null, null);
187 function makeFileURL(aFile)
189 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
190 .getService(Components.interfaces.nsIIOService);
191 return ioService.newFileURI(aFile);
195 function get_document_content_disposition (document_o)
197 var content_disposition = null;
199 content_disposition =
200 document_o.defaultView
201 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
202 .getInterface(Components.interfaces.nsIDOMWindowUtils)
203 .getDocumentMetadata("content-disposition");
205 return content_disposition;
209 function open_url_in_prompt(prefix, str)
215 } else if (prefix <= 4) {
216 return str + " in new buffer:";
218 return str + " in new frame:";
223 function set_focus_no_scroll(frame, element)
225 frame.document.commandDispatcher.suppressFocusScroll = true;
227 frame.document.commandDispatcher.suppressFocusScroll = false;
231 function do_N_times(func, n)
233 var args = Array.prototype.slice.call(arguments, 2);
235 func.apply(null, args);
238 // remove whitespace from the beginning and end
239 function trim_whitespace (str)
241 var tmp = new String (str);
242 return tmp.replace (/^\s+/, "").replace (/\s+$/, "");
245 function abs_point (node)
250 pt.x = node.offsetLeft;
251 pt.y = node.offsetTop;
252 // find imagemap's coordinates
253 if (node.tagName == "AREA") {
254 var coords = node.getAttribute("coords").split(",");
255 pt.x += Number(coords[0]);
256 pt.y += Number(coords[1]);
259 node = node.offsetParent;
260 // Sometimes this fails, so just return what we got.
262 while (node.tagName != "BODY") {
263 pt.x += node.offsetLeft;
264 pt.y += node.offsetTop;
265 node = node.offsetParent;
269 // while (node.tagName != "BODY") {
270 // alert("okay: " + node + " " + node.tagName + " " + pt.x + " " + pt.y);
271 // node = node.offsetParent;
279 // possible return values: 'Darwin', 'Linux', 'WINNT', ...
280 var appinfo = Cc['@mozilla.org/xre/app-info;1'].createInstance (Ci.nsIXULRuntime);
284 var default_directory = null;
286 function set_default_directory(directory_s) {
287 function getenv (variable) {
288 var env = Cc['@mozilla.org/process/environment;1'].createInstance(Ci.nsIEnvironment);
289 if (env.exists (variable))
290 return env.get(variable);
296 if ( this.get_os() == "WINNT")
298 directory_s = getenv ('USERPROFILE') ||
299 getenv ('HOMEDRIVE') + getenv ('HOMEPATH');
302 directory_s = getenv ('HOME');
306 default_directory = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
307 default_directory.initWithPath (directory_s);
310 set_default_directory();
312 const XHTML_NS = "http://www.w3.org/1999/xhtml";
313 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
314 const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
316 function create_XUL(frame, tag_name)
318 return frame.document.createElementNS(XUL_NS, tag_name);
322 /* Used in calls to XPath evaluate */
323 function xpath_lookup_namespace(prefix) {
324 if (prefix == "xhtml")
331 function method_caller(obj, func) {
333 func.apply(obj, arguments);