2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2009 John J. Foerch
5 * Portions of this file are derived from Vimperator,
6 * (C) Copyright 2006-2007 Martin Stubenschrott.
8 * Use, modification, and distribution are subject to the terms specified in the
14 define_variable("active_img_hint_background_color", "#88FF00",
15 "Color for the active image hint background.");
17 define_variable("img_hint_background_color", "yellow",
18 "Color for inactive image hint backgrounds.");
20 define_variable("active_hint_background_color", "#88FF00",
21 "Color for the active hint background.");
23 define_variable("hint_background_color", "yellow",
24 "Color for the inactive hint.");
28 * Register hints style sheet
30 const hints_stylesheet = "chrome://conkeror-gui/content/hints.css";
31 register_user_stylesheet(hints_stylesheet);
34 function hints_simple_text_match (text, pattern) {
35 var pos = text.indexOf(pattern);
38 return [pos, pos + pattern.length];
41 define_variable('hints_text_match', hints_simple_text_match,
42 "A function which takes a string and a pattern (another string) "+
43 "and returns an array of [start, end] indices if the pattern was "+
44 "found in the string, or false if it was not.");
48 * In the hints interaction, a node can be selected either by typing
49 * the number of its associated hint, or by typing substrings of the
50 * text content of the node. In the case of selecting by text
51 * content, multiple substrings can be given by separating them with
54 function hint_manager (window, xpath_expr, focused_frame, focused_element) {
57 this.valid_hints = [];
58 this.xpath_expr = xpath_expr;
59 this.focused_frame = focused_frame;
60 this.focused_element = focused_element;
61 this.last_selected_hint = null;
64 this.generate_hints();
67 hint_manager.prototype = {
68 current_hint_string: "",
69 current_hint_number: -1,
72 * Create an initially hidden hint span element absolutely
73 * positioned over each element that matches
74 * hint_xpath_expression. This is done recursively for all frames
75 * and iframes. Information about the resulting hints are also
76 * stored in the hints array.
78 generate_hints: function () {
79 var topwin = this.window;
80 var top_height = topwin.innerHeight;
81 var top_width = topwin.innerWidth;
82 var hints = this.hints;
83 var xpath_expr = this.xpath_expr;
84 var focused_frame_hint = null, focused_element_hint = null;
85 var focused_frame = this.focused_frame;
86 var focused_element = this.focused_element;
88 function helper (window, offsetX, offsetY) {
89 var win_height = window.height;
90 var win_width = window.width;
93 var minX = offsetX < 0 ? -offsetX : 0;
94 var minY = offsetY < 0 ? -offsetY : 0;
95 var maxX = offsetX + win_width > top_width ? top_width - offsetX : top_width;
96 var maxY = offsetY + win_height > top_height ? top_height - offsetY : top_height;
98 var scrollX = window.scrollX;
99 var scrollY = window.scrollY;
101 var doc = window.document;
102 var res = doc.evaluate(xpath_expr, doc, xpath_lookup_namespace,
103 Ci.nsIDOMXPathResult.UNORDERED_NODE_ITERATOR_TYPE,
104 null /* existing results */);
106 var base_node = doc.createElementNS(XHTML_NS, "span");
107 base_node.className = "__conkeror_hint";
109 var fragment = doc.createDocumentFragment();
110 var rect, elem, text, node, show_text;
113 elem = res.iterateNext();
117 break; // Iterator may have been invalidated by page load activity
119 rect = elem.getBoundingClientRect();
120 if (elem instanceof Ci.nsIDOMHTMLAreaElement) {
121 rect = { top: rect.top,
125 var coords = elem.getAttribute("coords");
127 coords = coords.match(/^(-?\d+)\D+(-?\d+)/);
128 if (coords.length == 3) {
129 rect.left += parseInt(coords[1]);
130 rect.top += parseInt(coords[2]);
134 if (!rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY)
136 let style = topwin.getComputedStyle(elem, "");
137 if (style.display == "none" || style.visibility == "hidden")
139 if (! (elem instanceof Ci.nsIDOMHTMLAreaElement))
140 rect = elem.getClientRects()[0];
144 if (elem instanceof Ci.nsIDOMHTMLInputElement || elem instanceof Ci.nsIDOMHTMLTextAreaElement)
146 else if (elem instanceof Ci.nsIDOMHTMLSelectElement) {
147 if (elem.selectedIndex >= 0)
148 text = elem.item(elem.selectedIndex).text;
151 } else if (elem instanceof Ci.nsIDOMHTMLFrameElement) {
152 text = elem.name ? elem.name : "";
153 } else if (/^\s*$/.test(elem.textContent) &&
154 elem.childNodes.length == 1 &&
155 elem.childNodes.item(0) instanceof Ci.nsIDOMHTMLImageElement) {
156 text = elem.childNodes.item(0).alt;
159 text = elem.textContent;
160 text = text.toLowerCase();
162 node = base_node.cloneNode(true);
163 node.style.left = (rect.left + scrollX) + "px";
164 node.style.top = (rect.top + scrollY) + "px";
165 fragment.appendChild(node);
167 let hint = { text: text,
172 show_text: show_text };
174 hint.saved_color = elem.style.color;
175 hint.saved_bgcolor = elem.style.backgroundColor;
179 if (elem == focused_element)
180 focused_element_hint = hint;
181 else if ((elem instanceof Ci.nsIDOMHTMLFrameElement ||
182 elem instanceof Ci.nsIDOMHTMLIFrameElement) &&
183 elem.contentWindow == focused_frame)
184 focused_frame_hint = hint;
186 doc.documentElement.appendChild(fragment);
188 /* Recurse into any IFRAME or FRAME elements */
189 var frametag = "frame";
191 var frames = doc.getElementsByTagName(frametag);
192 for (var i = 0, nframes = frames.length; i < nframes; ++i) {
194 rect = elem.getBoundingClientRect();
195 if (!rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY)
197 helper(elem.contentWindow, offsetX + rect.left, offsetY + rect.top);
199 if (frametag == "frame") frametag = "iframe"; else break;
202 helper(topwin, 0, 0);
203 this.last_selected_hint = focused_element_hint || focused_frame_hint;
206 /* Updates valid_hints and also re-numbers and re-displays all hints. */
207 update_valid_hints: function () {
208 this.valid_hints = [];
209 var active_number = this.current_hint_number;
211 var tokens = this.current_hint_string.split(" ");
212 var rect, h, text, img_hint, doc, scrollX, scrollY;
213 var hints = this.hints;
216 for (var i = 0, nhints = hints.length; i < nhints; ++i) {
219 for (var j = 0, ntokens = tokens.length; j < ntokens; ++j) {
220 if (! hints_text_match(text, tokens[j])) {
223 h.hint.style.display = "none";
225 h.img_hint.style.display = "none";
226 if (h.saved_color != null) {
227 h.elem.style.backgroundColor = h.saved_bgcolor;
228 h.elem.style.color = h.saved_color;
235 var cur_number = this.valid_hints.length + 1;
238 if (h == this.last_selected_hint && active_number == -1)
239 this.current_hint_number = active_number = cur_number;
243 if (text.length == 0 && h.elem.firstChild &&
244 h.elem.firstChild instanceof Ci.nsIDOMHTMLImageElement)
245 img_elem = h.elem.firstChild;
246 else if (h.elem instanceof Ci.nsIDOMHTMLImageElement)
251 rect = img_elem.getBoundingClientRect();
253 doc = h.elem.ownerDocument;
254 scrollX = doc.defaultView.scrollX;
255 scrollY = doc.defaultView.scrollY;
256 img_hint = doc.createElementNS(XHTML_NS, "span");
257 img_hint.className = "__conkeror_img_hint";
258 img_hint.style.left = (rect.left + scrollX) + "px";
259 img_hint.style.top = (rect.top + scrollY) + "px";
260 img_hint.style.width = (rect.right - rect.left) + "px";
261 img_hint.style.height = (rect.bottom - rect.top) + "px";
262 h.img_hint = img_hint;
263 doc.documentElement.appendChild(img_hint);
268 var bgcolor = (active_number == cur_number) ?
269 active_img_hint_background_color : img_hint_background_color;
270 h.img_hint.style.backgroundColor = bgcolor;
271 h.img_hint.style.display = "inline";
275 if (!h.img_hint && h.elem.style)
276 h.elem.style.backgroundColor = (active_number == cur_number) ?
277 active_hint_background_color : hint_background_color;
280 h.elem.style.color = "black";
282 var label = "" + cur_number;
283 if (h.elem instanceof Ci.nsIDOMHTMLFrameElement) {
285 } else if (h.show_text && !/^\s*$/.test(text)) {
286 let substrs = [[0,4]];
287 for (j = 0; j < ntokens; ++j) {
288 let m = hints_text_match(text, tokens[j]);
289 if (m == false) continue;
290 splice_range(substrs, m[0], m[1] + 2);
292 label += " " + substrs.map(function (x) {
293 return text.substring(x[0],Math.min(x[1], text.length));
294 }).join("..") + "..";
296 h.hint.textContent = label;
297 h.hint.style.display = "inline";
298 this.valid_hints.push(h);
301 if (active_number == -1)
305 select_hint: function (index) {
306 var old_index = this.current_hint_number;
307 if (index == old_index)
309 var vh = this.valid_hints;
310 if (old_index >= 1 && old_index <= vh.length) {
311 var h = vh[old_index - 1];
313 h.img_hint.style.backgroundColor = img_hint_background_color;
315 h.elem.style.backgroundColor = hint_background_color;
317 this.current_hint_number = index;
318 if (index >= 1 && index <= vh.length) {
319 var h = vh[index - 1];
321 h.img_hint.style.backgroundColor = active_img_hint_background_color;
323 h.elem.style.backgroundColor = active_hint_background_color;
324 this.last_selected_hint = h;
328 hide_hints: function () {
329 for (var i = 0, nhints = this.hints.length; i < nhints; ++i) {
330 var h = this.hints[i];
333 if (h.saved_color != null) {
334 h.elem.style.color = h.saved_color;
335 h.elem.style.backgroundColor = h.saved_bgcolor;
338 h.img_hint.style.display = "none";
339 h.hint.style.display = "none";
344 remove: function () {
345 for (var i = 0, nhints = this.hints.length; i < nhints; ++i) {
346 var h = this.hints[i];
347 if (h.visible && h.saved_color != null) {
348 h.elem.style.color = h.saved_color;
349 h.elem.style.backgroundColor = h.saved_bgcolor;
352 h.img_hint.parentNode.removeChild(h.img_hint);
353 h.hint.parentNode.removeChild(h.hint);
356 this.valid_hints = [];
360 /* Show panel with currently selected URL. */
361 function hints_url_panel (hints, window) {
362 var g = new dom_generator(window.document, XUL_NS);
364 var p = g.element("hbox", "class", "panel url", "flex", "0");
365 g.element("label", p, "value", "URL:", "class", "url-panel-label");
366 var url_value = g.element("label", p, "class", "url-panel-value",
367 "crop", "end", "flex", "1");
368 window.minibuffer.insert_before(p);
370 p.update = function () {
371 url_value.value = "";
372 if (hints.manager && hints.manager.last_selected_hint) {
375 spec = load_spec(hints.manager.last_selected_hint.elem);
378 var uri = load_spec_uri_string(spec);
379 if (uri) url_value.value = uri;
384 p.destroy = function () {
385 this.parentNode.removeChild(this);
391 define_variable("hints_display_url_panel", false,
392 "When selecting a hint, the URL can be displayed in a panel above "+
393 "the minibuffer. This is useful for confirming that the correct "+
394 "link is selected and that the URL is not evil. This option is "+
395 "most useful when hints_auto_exit_delay is long or disabled.");
404 define_keywords("$keymap", "$auto", "$hint_xpath_expression", "$multiple");
405 function hints_minibuffer_state (window, continuation, buffer) {
406 keywords(arguments, $keymap = hint_keymap, $auto);
407 basic_minibuffer_state.call(this, window, $prompt = arguments.$prompt);
408 if (hints_display_url_panel)
409 this.url_panel = hints_url_panel(this, buffer.window);
410 this.original_prompt = arguments.$prompt;
411 this.continuation = continuation;
412 this.keymap = arguments.$keymap;
413 this.auto_exit = arguments.$auto ? true : false;
414 this.xpath_expr = arguments.$hint_xpath_expression;
415 this.auto_exit_timer_ID = null;
416 this.multiple = arguments.$multiple;
417 this.focused_element = buffer.focused_element;
418 this.focused_frame = buffer.focused_frame;
420 hints_minibuffer_state.prototype = {
421 __proto__: basic_minibuffer_state.prototype,
425 load: function (window) {
426 basic_minibuffer_state.prototype.load.call(this, window);
428 var buf = window.buffers.current;
429 this.manager = new hint_manager(buf.top_frame, this.xpath_expr,
430 this.focused_frame, this.focused_element);
432 this.manager.update_valid_hints();
433 this.window = window;
435 this.url_panel.update();
437 clear_auto_exit_timer: function () {
438 if (this.auto_exit_timer_ID != null) {
439 this.window.clearTimeout(this.auto_exit_timer_ID);
440 this.auto_exit_timer_ID = null;
443 unload: function (window) {
444 this.clear_auto_exit_timer();
445 this.manager.hide_hints();
447 basic_minibuffer_state.prototype.unload.call(this, window);
449 destroy: function (window) {
450 this.clear_auto_exit_timer();
451 this.manager.remove();
453 this.url_panel.destroy();
454 basic_minibuffer_state.prototype.destroy.call(this, window);
456 update_minibuffer: function (m) {
457 if (this.typed_number.length > 0)
458 m.prompt = this.original_prompt + " #" + this.typed_number;
460 m.prompt = this.original_prompt;
462 this.url_panel.update();
465 handle_auto_exit: function (m, ambiguous) {
466 let window = m.window;
467 var num = this.manager.current_hint_number;
471 let delay = ambiguous ? hints_ambiguous_auto_exit_delay : hints_auto_exit_delay;
473 this.auto_exit_timer_ID = window.setTimeout(function () { hints_exit(window, s); },
477 ran_minibuffer_command: function (m) {
478 this.handle_input(m);
481 handle_input: function (m) {
483 this.clear_auto_exit_timer();
484 this.typed_number = "";
485 this.typed_string = m._input_text;
486 this.manager.current_hint_string = this.typed_string;
487 this.manager.current_hint_number = -1;
488 this.manager.update_valid_hints();
489 if (this.manager.valid_hints.length == 1)
490 this.handle_auto_exit(m, false /* unambiguous */);
491 else if (this.manager.valid_hints.length > 1)
492 this.handle_auto_exit(m, true /* ambiguous */);
493 this.update_minibuffer(m);
497 define_variable("hints_auto_exit_delay", 0,
498 "Delay (in milliseconds) after the most recent key stroke before a "+
499 "sole matching element is automatically selected. When zero, "+
500 "automatic selection is disabled. A value of 500 is a good "+
501 "starting point for an average-speed typist.");
503 define_variable("hints_ambiguous_auto_exit_delay", 0,
504 "Delay (in milliseconds) after the most recent key stroke before the "+
505 "first of an ambiguous match is automatically selected. If this is "+
506 "set to 0, automatic selection in ambiguous matches is disabled.");
508 interactive("hints-handle-number", null,
510 let s = I.minibuffer.check_state(hints_minibuffer_state);
511 s.clear_auto_exit_timer();
512 var ch = String.fromCharCode(I.event.charCode);
513 var auto_exit_ambiguous = null; // null -> no auto exit; false -> not ambiguous; true -> ambiguous
514 /* TODO: implement number escaping */
516 s.typed_number += ch;
518 s.manager.select_hint(parseInt(s.typed_number));
519 var num = s.manager.current_hint_number;
520 if (num > 0 && num <= s.manager.valid_hints.length)
521 auto_exit_ambiguous = num * 10 > s.manager.valid_hints.length ? false : true;
524 hints_exit(I.window, s);
527 auto_exit_ambiguous = false;
529 if (auto_exit_ambiguous !== null)
530 s.handle_auto_exit(I.minibuffer, auto_exit_ambiguous);
531 s.update_minibuffer(I.minibuffer);
534 function hints_backspace (window, s) {
535 let m = window.minibuffer;
536 s.clear_auto_exit_timer();
537 if (s.typed_number.length > 0) {
538 s.typed_number = s.typed_number.substring(0, s.typed_number.length - 1);
539 var num = s.typed_number.length > 0 ? parseInt(s.typed_number) : 1;
540 s.manager.select_hint(num);
541 } else if (s.typed_string.length > 0) {
542 s.typed_string = s.typed_string.substring(0, s.typed_string.length - 1);
543 m._input_text = s.typed_string;
545 s.manager.current_hint_string = s.typed_string;
546 s.manager.current_hint_number = -1;
547 s.manager.update_valid_hints();
549 s.update_minibuffer(m);
551 interactive("hints-backspace", null,
553 hints_backspace(I.window, I.minibuffer.check_state(hints_minibuffer_state));
556 function hints_next (window, s, count) {
557 s.clear_auto_exit_timer();
559 var cur = s.manager.current_hint_number - 1;
560 var vh = s.manager.valid_hints;
562 cur = (cur + count) % vh.length;
565 s.manager.select_hint(cur + 1);
567 s.update_minibuffer(window);
569 interactive("hints-next", null,
571 hints_next(I.window, I.minibuffer.check_state(hints_minibuffer_state), I.p);
574 interactive("hints-previous", null,
576 hints_next(I.window, I.minibuffer.check_state(hints_minibuffer_state), -I.p);
579 function hints_exit (window, s) {
580 var cur = s.manager.current_hint_number;
582 if (cur > 0 && cur <= s.manager.valid_hints.length) {
583 elem = s.manager.valid_hints[cur - 1].elem;
584 } else if (cur == 0) {
585 elem = window.buffers.current.top_frame;
588 var c = s.continuation;
589 delete s.continuation;
590 window.minibuffer.pop_state();
596 interactive("hints-exit", null,
598 hints_exit(I.window, I.minibuffer.check_state(hints_minibuffer_state));
602 define_keywords("$buffer");
603 minibuffer.prototype.read_hinted_element = function () {
605 var buf = arguments.$buffer;
606 var s = new hints_minibuffer_state(this.window, (yield CONTINUATION), buf, forward_keywords(arguments));
608 var result = yield SUSPEND;
609 yield co_return(result);