2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2009 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
13 require("load-spec.js");
15 define_variable("homepage", "chrome://conkeror-help/content/help.html",
16 "The url loaded by default for new content buffers.");
18 define_buffer_local_hook("content_buffer_finished_loading_hook");
19 define_buffer_local_hook("content_buffer_started_loading_hook");
20 define_buffer_local_hook("content_buffer_progress_change_hook");
21 define_buffer_local_hook("content_buffer_location_change_hook");
22 define_buffer_local_hook("content_buffer_status_change_hook");
23 define_buffer_local_hook("content_buffer_focus_change_hook");
24 define_buffer_local_hook("content_buffer_dom_link_added_hook");
25 define_buffer_local_hook("content_buffer_popup_blocked_hook");
27 define_current_buffer_hook("current_content_buffer_finished_loading_hook", "content_buffer_finished_loading_hook");
28 define_current_buffer_hook("current_content_buffer_progress_change_hook", "content_buffer_progress_change_hook");
29 define_current_buffer_hook("current_content_buffer_location_change_hook", "content_buffer_location_change_hook");
30 define_current_buffer_hook("current_content_buffer_status_change_hook", "content_buffer_status_change_hook");
31 define_current_buffer_hook("current_content_buffer_focus_change_hook", "content_buffer_focus_change_hook");
34 function content_buffer_modality (buffer) {
35 var elem = buffer.focused_element;
36 function push_keymaps (tag) {
37 buffer.content_modalities.map(
40 buffer.keymaps.push(m[tag]);
44 push_keymaps('normal');
46 let p = elem.parentNode;
47 while (p && !(p instanceof Ci.nsIDOMHTMLFormElement))
52 if (elem instanceof Ci.nsIDOMHTMLInputElement) {
53 var type = (elem.getAttribute("type") || "").toLowerCase();
54 if ({checkbox:1, radio:1, submit:1, reset:1}[type])
55 return push_keymaps(type);
57 return push_keymaps('text');
59 if (elem instanceof Ci.nsIDOMHTMLTextAreaElement)
60 return push_keymaps('textarea');
61 if (elem instanceof Ci.nsIDOMHTMLSelectElement)
62 return push_keymaps('select');
63 if (elem instanceof Ci.nsIDOMHTMLAnchorElement)
64 return push_keymaps('anchor');
65 if (elem instanceof Ci.nsIDOMHTMLButtonElement)
66 return push_keymaps('button');
67 if (elem instanceof Ci.nsIDOMHTMLEmbedElement ||
68 elem instanceof Ci.nsIDOMHTMLObjectElement)
70 return push_keymaps('embed');
72 var frame = buffer.focused_frame;
73 if (frame && frame.document.designMode &&
74 frame.document.designMode == "on")
76 return push_keymaps('richedit');
79 switch (elem.contentEditable) {
81 return push_keymaps('richedit');
85 elem = elem.parentNode;
92 define_keywords("$load");
93 function content_buffer (window) {
95 this.constructor_begin();
97 conkeror.buffer.call(this, window, forward_keywords(arguments));
99 this.browser.addProgressListener(this);
101 this.browser.addEventListener("DOMTitleChanged", function (event) {
102 buffer_title_change_hook.run(buffer);
103 }, true /* capture */);
105 this.browser.addEventListener("scroll", function (event) {
106 buffer_scroll_hook.run(buffer);
107 }, true /* capture */);
109 this.browser.addEventListener("focus", function (event) {
110 content_buffer_focus_change_hook.run(buffer, event);
111 }, true /* capture */);
113 this.browser.addEventListener("DOMLinkAdded", function (event) {
114 content_buffer_dom_link_added_hook.run(buffer, event);
115 }, true /* capture */);
117 this.browser.addEventListener("DOMPopupBlocked", function (event) {
118 dumpln("Blocked popup: " + event.popupWindowURI.spec);
119 content_buffer_popup_blocked_hook.run(buffer, event);
120 }, true /* capture */);
122 this.ignore_initial_blank = true;
124 var lspec = arguments.$load;
126 if (lspec.url == "about:blank")
127 this.ignore_initial_blank = false;
129 /* Ensure that an existing load of about:blank is stopped */
130 this.web_navigation.stop(Ci.nsIWebNavigation.STOP_ALL);
136 this.modalities.push(content_buffer_modality);
137 this.content_modalities = [{
138 normal: content_buffer_normal_keymap,
139 form: content_buffer_form_keymap,
140 checkbox: content_buffer_checkbox_keymap,
141 radio: content_buffer_checkbox_keymap,
142 submit: content_buffer_button_keymap,
143 reset: content_buffer_button_keymap,
144 text: content_buffer_text_keymap,
145 textarea: content_buffer_textarea_keymap,
146 select: content_buffer_select_keymap,
147 anchor: content_buffer_anchor_keymap,
148 button: content_buffer_button_keymap,
149 embed: content_buffer_embed_keymap,
150 richedit: content_buffer_richedit_keymap
153 this.constructor_end();
156 content_buffer.prototype = {
157 constructor: content_buffer,
159 destroy: function () {
160 this.browser.removeProgressListener(this);
161 buffer.prototype.destroy.call(this);
164 content_modalities: null,
166 get scrollX () { return this.top_frame.scrollX; },
167 get scrollY () { return this.top_frame.scrollY; },
168 get scrollMaxX () { return this.top_frame.scrollMaxX; },
169 get scrollMaxY () { return this.top_frame.scrollMaxY; },
171 /* Used to display the correct URI when the buffer opens initially
172 * even before loading has progressed far enough for currentURI to
173 * contain the correct URI. */
176 get display_uri_string () {
177 if (this._display_uri)
178 return this._display_uri;
179 if (this.current_uri)
180 return this.current_uri.spec;
184 get title () { return this.browser.contentTitle; },
185 get description () { return this.display_uri_string; },
187 load: function (load_spec) {
188 apply_load_spec(this, load_spec);
195 // nsIWebProgressListener interface
197 QueryInterface: generate_QI(Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference),
199 // This method is called to indicate state changes.
200 onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) {
203 if (aStateFlags & Ci.nsIWebProgressListener.STATE_START) {
204 this._request_count++;
205 } else if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
206 const NS_ERROR_UNKNOWN_HOST = 2152398878;
207 if (--this._request_count > 0 && aStatus == NS_ERROR_UNKNOWN_HOST) {
208 // to prevent bug 235825: wait for the request handled
209 // by the automatic keyword resolver
212 // since we (try to) only handle STATE_STOP of the last request,
213 // the count of open requests should now be 0
214 this._request_count = 0;
217 if (aStateFlags & Ci.nsIWebProgressListener.STATE_START &&
218 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
219 // It's okay to clear what the user typed when we start
220 // loading a document. If the user types, this counter gets
221 // set to zero, if the document load ends without an
222 // onLocationChange, this counter gets decremented
223 // (so we keep it while switching tabs after failed loads)
225 content_buffer_started_loading_hook.run(this);
226 } else if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
227 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
228 if (this.loading == true) {
229 this.loading = false;
230 content_buffer_finished_loading_hook.run(this);
234 if (aStateFlags & (Ci.nsIWebProgressListener.STATE_STOP |
235 Ci.nsIWebProgressListener.STATE_START)) {
237 this.set_default_message("Done");
241 // This method is called to indicate progress changes for the
242 // currently loading page.
243 onProgressChange: function (webProgress, request, curSelf, maxSelf,
246 content_buffer_progress_change_hook.run(this, request, curSelf, maxSelf, curTotal, maxTotal);
249 // This method is called to indicate a change to the current location.
250 // The url can be gotten as location.spec.
251 onLocationChange: function (webProgress, request, location) {
252 /* Attempt to ignore onLocationChange calls due to the initial
253 * loading of about:blank by all xul:browser elements. */
254 if (location.spec == "about:blank" && this.ignore_initial_blank)
257 this.ignore_initial_blank = false;
259 //dumpln("spec: " + location.spec +" ;;; " + this.display_uri_string);
260 /* Use the real location URI now */
261 this._display_uri = null;
262 this.set_input_mode();
263 content_buffer_location_change_hook.run(this, request, location);
264 buffer_description_change_hook.run(this);
267 // This method is called to indicate a status changes for the currently
268 // loading page. The message is already formatted for display.
269 // Status messages could be displayed in the minibuffer output area.
270 onStatusChange: function (webProgress, request, status, msg) {
271 this.set_default_message(msg);
272 content_buffer_status_change_hook.run(this, request, status, msg);
275 // This method is called when the security state of the browser changes.
276 onSecurityChange: function (webProgress, request, state) {
277 //FIXME: implement this.
279 const WPL = Components.interfaces.nsIWebProgressListener;
281 if (state & WPL.STATE_IS_INSECURE) {
282 // update visual indicator
284 var level = "unknown";
285 if (state & WPL.STATE_IS_SECURE) {
286 if (state & WPL.STATE_SECURE_HIGH)
288 else if (state & WPL.STATE_SECURE_MED)
290 else if (state & WPL.STATE_SECURE_LOW)
292 } else if (state & WPL_STATE_IS_BROKEN) {
295 // provide a visual indicator of the security state here.
300 /* Inherit from buffer */
301 __proto__: buffer.prototype
305 add_hook("current_content_buffer_finished_loading_hook",
307 buffer.window.minibuffer.show("Done");
310 add_hook("current_content_buffer_status_change_hook",
311 function (buffer, request, status, msg) {
312 buffer.set_default_message(msg);
317 define_variable("read_url_handler_list", [],
318 "A list of handler functions which transform a typed url into a valid " +
319 "url or webjump. If the typed input is not valid then each function " +
320 "on this list is tried in turn. The handler function is called with " +
321 "a single string argument and it should return either a string or " +
322 "null. The result of the first function on the list that returns a " +
323 "string is used in place of the input.");
326 * read_url_make_default_webjump_handler returns a function that
327 * transforms any input into the given webjump. It should be the last
328 * handler on read_url_handler_list (because any input is
331 function read_url_make_default_webjump_handler (default_webjump) {
332 return function (input) {
333 return default_webjump + " " + input;
338 * read_url_make_blank_url_handler returns a function that replaces a
339 * blank (empty) input with the given url (or webjump). The url may
340 * perform some function, eg. "javascript:location.reload()".
342 function read_url_make_blank_url_handler (url) {
343 return function (input) {
344 if (input.length == 0)
350 minibuffer.prototype.try_read_url_handlers = function (input) {
352 for (var i = 0; i < read_url_handler_list.length; ++i) {
353 if ((result = read_url_handler_list[i](input)))
359 define_variable("url_completion_use_webjumps", true,
360 "Specifies whether URL completion should complete webjumps.");
362 define_variable("url_completion_use_bookmarks", true,
363 "Specifies whether URL completion should complete bookmarks.");
365 define_variable("url_completion_use_history", false,
366 "Specifies whether URL completion should complete using browser "+
369 define_variable("minibuffer_read_url_select_initial", true,
370 "Specifies whether a URL presented in the minibuffer for editing "+
371 "should be selected. This affects find-alternate-url.");
374 minibuffer_auto_complete_preferences["url"] = true;
375 minibuffer.prototype.read_url = function () {
376 keywords(arguments, $prompt = "URL:", $history = "url", $initial_value = "",
377 $use_webjumps = url_completion_use_webjumps,
378 $use_history = url_completion_use_history,
379 $use_bookmarks = url_completion_use_bookmarks);
380 var completer = url_completer($use_webjumps = arguments.$use_webjumps,
381 $use_bookmarks = arguments.$use_bookmarks,
382 $use_history = arguments.$use_history);
383 var result = yield this.read(
384 $prompt = arguments.$prompt,
385 $history = arguments.$history,
386 $completer = completer,
387 $initial_value = arguments.$initial_value,
388 $auto_complete = "url",
389 $select = minibuffer_read_url_select_initial,
390 $match_required = false);
391 if (!possibly_valid_url(result) && !get_webjump(result))
392 result = this.try_read_url_handlers(result);
393 if (result == "") // well-formedness check. (could be better!)
394 throw ("invalid url or webjump (\""+ result +"\")");
395 yield co_return(load_spec(result));
402 function overlink_update_status (buffer, node) {
403 if (node && node.href.length > 0)
404 buffer.window.minibuffer.show("Link: " + node.href);
406 buffer.window.minibuffer.clear();
409 function overlink_predicate (node) {
410 while (node && !(node instanceof Ci.nsIDOMHTMLAnchorElement))
411 node = node.parentNode;
415 function overlink_initialize (buffer) {
416 buffer.current_overlink = null;
417 buffer.overlink_mouseover = function (event) {
418 if (buffer != buffer.window.buffers.current ||
419 event.target == buffer.browser)
423 var node = overlink_predicate(event.target);
425 buffer.current_overlink = event.target;
426 overlink_update_status(buffer, node);
429 buffer.overlink_mouseout = function (event) {
430 if (buffer != buffer.window.buffers.current)
432 if (buffer.current_overlink == event.target) {
433 buffer.current_overlink = null;
434 overlink_update_status(buffer, null);
437 buffer.browser.addEventListener("mouseover", buffer.overlink_mouseover, true);
438 buffer.browser.addEventListener("mouseout", buffer.overlink_mouseout, true);
441 define_global_mode("overlink_mode",
443 add_hook("create_buffer_hook", overlink_initialize);
444 for_each_buffer(overlink_initialize);
446 function disable () {
447 remove_hook("create_buffer_hook", overlink_initialize);
448 for_each_buffer(function (b) {
449 b.browser.removeEventListener("mouseover", b.overlink_mouseover, true);
450 b.browser.removeEventListener("mouseout", b.overlink_mouseout, true);
451 delete b.current_overlink;
452 delete b.overlink_mouseover;
453 delete b.overlink_mouseout;
461 * Navigation Commands
463 function go_back (b, prefix) {
465 go_forward(b, -prefix);
467 check_buffer(b, content_buffer);
469 if (b.web_navigation.canGoBack) {
470 var hist = b.web_navigation.sessionHistory;
471 var idx = hist.index - prefix;
474 b.web_navigation.gotoIndex(idx);
476 throw interactive_error("Can't go back");
480 "Go back in the session history for the current buffer.",
481 function (I) {go_back(I.buffer, I.p);});
484 function go_forward (b, prefix) {
488 check_buffer(b, content_buffer);
490 if (b.web_navigation.canGoForward) {
491 var hist = b.web_navigation.sessionHistory;
492 var idx = hist.index + prefix;
493 if (idx >= hist.count) idx = hist.count-1;
494 b.web_navigation.gotoIndex(idx);
496 throw interactive_error("Can't go forward");
499 interactive("forward",
500 "Go forward in the session history for the current buffer.",
501 function (I) {go_forward(I.buffer, I.p);});
504 function stop_loading (b) {
505 check_buffer(b, content_buffer);
506 b.web_navigation.stop(Ci.nsIWebNavigation.STOP_NETWORK);
509 interactive("stop-loading",
510 "Stop loading the current document.",
511 function (I) {stop_loading(I.buffer);});
514 function reload (b, bypass_cache, element, forced_charset) {
515 check_buffer(b, content_buffer);
517 if (element instanceof Ci.nsIDOMHTMLImageElement) {
519 var cache = Cc['@mozilla.org/image/cache;1']
520 .getService(Ci.imgICache);
521 cache.removeEntry(make_uri(element.src));
524 element.parentNode.replaceChild(element.cloneNode(true), element);
525 } else if (b.current_uri.spec != b.display_uri_string) {
526 apply_load_spec(b, load_spec({ uri: b.display_uri_string,
527 forced_charset: forced_charset }));
529 var flags = bypass_cache == null ?
530 Ci.nsIWebNavigation.LOAD_FLAGS_NONE :
531 Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
533 if (! forced_charset && forced_charset_list)
534 forced_charset = predicate_alist_match(forced_charset_list,
537 if (forced_charset) {
539 var atomservice = Cc['@mozilla.org/atom-service;1']
540 .getService(Ci.nsIAtomService);
541 b.web_navigation.documentCharsetInfo.forcedCharset =
542 atomservice.getAtom(forced_charset);
545 b.web_navigation.reload(flags);
549 interactive("reload",
550 "Reload the current document.\n" +
551 "If a prefix argument is specified, the cache is bypassed. If a "+
552 "DOM node is supplied via browser object, that node will be "+
555 check_buffer(I.buffer, content_buffer);
556 var element = yield read_browser_object(I);
557 reload(I.buffer, I.P, element, I.forced_charset);
559 $browser_object = null);
562 * browserDOMWindow: intercept window opening
564 function initialize_browser_dom_window (window) {
565 window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow =
566 new browser_dom_window(window);
569 define_variable("browser_default_open_target", OPEN_NEW_BUFFER,
570 "Specifies how new window requests by content pages (e.g. by "+
571 "window.open from JavaScript or by using the target attribute of "+
572 "anchor and form elements) will be handled. This will generally "+
573 "be `OPEN_NEW_BUFFER', `OPEN_NEW_BUFFER_BACKGROUND', or "+
574 "`OPEN_NEW_WINDOW'.");
577 function browser_dom_window (window) {
578 this.window = window;
579 this.next_target = null;
581 browser_dom_window.prototype = {
582 constructor: browser_dom_window,
583 QueryInterface: generate_QI(Ci.nsIBrowserDOMWindow),
585 openURI: function (aURI, aOpener, aWhere, aContext) {
586 // Reference: http://www.xulplanet.com/references/xpcomref/ifaces/nsIBrowserDOMWindow.html
587 var target = this.next_target;
588 if (target == null || target == FOLLOW_DEFAULT)
589 target = browser_default_open_target;
590 this.next_target = null;
592 /* Determine the opener buffer */
593 var opener = get_buffer_from_frame(this.window, aOpener);
595 switch (browser_default_open_target) {
596 case OPEN_CURRENT_BUFFER:
598 case FOLLOW_CURRENT_FRAME:
600 case OPEN_NEW_BUFFER:
601 var buffer = new content_buffer(this.window, $opener = opener);
602 this.window.buffers.current = buffer;
603 return buffer.top_frame;
604 case OPEN_NEW_BUFFER_BACKGROUND:
605 var buffer = new content_buffer(this.window, $opener = opener);
606 return buffer.top_frame;
607 case OPEN_NEW_WINDOW:
608 default: /* shouldn't be needed */
610 /* We don't call make_window here, because that will result
611 * in the URL being loaded as the top-level document,
612 * instead of within a browser buffer. Instead, we can
613 * rely on Mozilla using browser.chromeURL. */
614 window_set_extra_arguments(
615 {initial_buffer_creator: buffer_creator(content_buffer, $opener = opener)}
622 add_hook("window_initialize_early_hook", initialize_browser_dom_window);
624 define_keywords("$display_name", "$enable", "$disable", "$doc");
625 function define_page_mode (name) {
627 var display_name = arguments.$display_name;
628 var enable = arguments.$enable;
629 var disable = arguments.$disable;
630 var doc = arguments.$doc;
631 define_buffer_mode(name,
632 $display_name = display_name,
633 $class = "page_mode",
634 $enable = function (buffer) {
636 local: { __proto__: buffer.local }
640 buffer.set_input_mode();
642 $disable = function (buffer) {
646 buffer.default_browser_object_classes = {};
647 buffer.set_input_mode();
651 ignore_function_for_get_caller_source_code_reference("define_page_mode");
654 define_variable("auto_mode_list", [],
655 "A list of mappings from URI regular expressions to page modes.");
657 function page_mode_auto_update (buffer) {
658 var uri = buffer.current_uri.spec;
659 var mode = predicate_alist_match(auto_mode_list, uri);
662 else if (buffer.page_mode)
663 conkeror[buffer.page_mode](buffer, false);
666 add_hook("content_buffer_location_change_hook", page_mode_auto_update);
668 provide("content-buffer");