2 (c) 2009 by Leon Winter
3 (c) 2009, 2010 by Hannes Schueller
4 (c) 2010 by Hans-Peter Deifel
5 (c) 2011 by Daniel Carl
11 hintCss: "z-index:100000;font-family:monospace;font-size:10px;"
12 + "font-weight:bold;color:white;background-color:red;"
13 + "padding:0px 1px;position:absolute;",
14 hintClass: "hinting_mode_hint",
15 hintClassFocus: "hinting_mode_hint_focus",
16 elemBackground: "#ff0",
17 elemBackgroundFocus: "#8f0",
22 var currentFocusNum = 1;
26 this.createHints = function(inputText, hintMode)
33 var top_height = topwin.innerHeight;
34 var top_width = topwin.innerWidth;
40 function helper (win, offsetX, offsetY) {
41 var doc = win.document;
43 var win_height = win.height;
44 var win_width = win.width;
47 var minX = offsetX < 0 ? -offsetX : 0;
48 var minY = offsetY < 0 ? -offsetY : 0;
49 var maxX = offsetX + win_width > top_width ? top_width - offsetX : top_width;
50 var maxY = offsetY + win_height > top_height ? top_height - offsetY : top_height;
52 var scrollX = win.scrollX;
53 var scrollY = win.scrollY;
55 hintContainer = doc.createElement("div");
56 hintContainer.id = "hint_container";
58 xpath_expr = _getXpathXpression(inputText);
60 var res = doc.evaluate(xpath_expr, doc,
62 return "http://www.w3.org/1999/xhtml";
63 }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
65 /* generate basic hint element which will be cloned and updated later */
66 var hintSpan = doc.createElement("span");
67 hintSpan.setAttribute("class", config.hintClass);
68 hintSpan.style.cssText = config.hintCss;
70 /* due to the different XPath result type, we will need two counter variables */
71 var rect, elem, text, node, show_text;
72 for (var i = 0; i < res.snapshotLength; i++) {
73 if (hintCount >= config.maxAllowedHints) {
77 elem = res.snapshotItem(i);
78 rect = elem.getBoundingClientRect();
79 if (!rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY) {
83 var style = topwin.getComputedStyle(elem, "");
84 if (style.display == "none" || style.visibility != "visible") {
88 var leftpos = Math.max((rect.left + scrollX), scrollX);
89 var toppos = Math.max((rect.top + scrollY), scrollY);
91 /* making this block DOM compliant */
92 var hint = hintSpan.cloneNode(false);
93 hint.setAttribute("id", "vimprobablehint" + hintCount);
94 hint.style.left = leftpos + "px";
95 hint.style.top = toppos + "px";
96 text = doc.createTextNode(hintCount + 1);
97 hint.appendChild(text);
99 hintContainer.appendChild(hint);
105 background: elem.style.background,
106 foreground: elem.style.color}
109 /* make the link black to ensure it's readable */
110 elem.style.color = config.elemColor;
111 elem.style.background = config.elemBackground;
114 doc.documentElement.appendChild(hintContainer);
116 /* recurse into any iframe or frame element */
117 var frameTags = ["frame","iframe"];
118 for (var f = 0; f < frameTags.length; ++f) {
119 var frames = doc.getElementsByTagName(frameTags[f]);
120 for (i = 0, nframes = frames.length; i < nframes; ++i) {
122 rect = elem.getBoundingClientRect();
123 if (!elem.contentWindow || !rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY) {
126 helper(elem.contentWindow, offsetX + rect.left, offsetY + rect.top);
131 helper(topwin, 0, 0);
135 if (hintCount == 1) {
136 /* just one hinted element - might as well follow it */
141 /* set focus on hint with given number */
142 this.focusHint = function(n)
144 /* reset previous focused hint */
145 var hint = _getHintByNumber(currentFocusNum);
147 hint.elem.className = hint.elem.className.replace(config.hintClassFocus, config.hintClass);
148 hint.elem.style.background = config.elemBackground;
153 /* mark new hint as focused */
154 hint = _getHintByNumber(currentFocusNum);
156 hint.elem.className = hint.elem.className.replace(config.hintClass, config.hintClassFocus);
157 hint.elem.style.background = config.elemBackgroundFocus;
161 /* set focus to next avaiable hint */
162 this.focusNextHint = function()
164 var index = _getHintIdByNumber(currentFocusNum);
166 if (typeof(hints[index + 1]) != "undefined") {
167 this.focusHint(hints[index + 1].number);
169 this.focusHint(hints[0].number);
173 /* set focus to previous avaiable hint */
174 this.focusPreviousHint = function()
176 var index = _getHintIdByNumber(currentFocusNum);
177 if (index !== 0 && typeof(hints[index - 1].number) != "undefined") {
178 this.focusHint(hints[index - 1].number);
180 this.focusHint(hints[hints.length - 1].number);
184 /* filters hints matching given number */
185 this.updateHints = function(n)
188 return this.createHints();
190 /* remove none matching hints */
192 for (i = 0; i < hints.length; ++i) {
194 if (0 !== hint.number.toString().indexOf(n.toString())) {
195 remove.push(hint.number);
199 for (i = 0; i < remove.length; ++i) {
200 _removeHint(remove[i]);
203 if (hints.length === 1) {
204 return this.fire(hints[0].number);
206 return this.focusHint(n);
210 this.clearFocus = function()
212 if (document.activeElement && document.activeElement.blur) {
213 document.activeElement.blur();
217 /* remove all hints and set previous style to them */
218 this.clearHints = function()
220 if (hints.length === 0) {
223 for (var i = 0; i < hints.length; ++i) {
225 if (typeof(hint.elem) != "undefined") {
226 hint.elem.style.background = hint.background;
227 hint.elem.style.color = hint.foreground;
228 hint.span.parentNode.removeChild(hint.span);
232 hintContainer.parentNode.removeChild(hintContainer);
233 window.onkeyup = null;
236 /* fires the modeevent on hint with given number */
237 this.fire = function(n)
240 n = n ? n : currentFocusNum;
241 var hint = _getHintByNumber(n);
242 if (typeof(hint.elem) == "undefined") {
247 var tag = el.nodeName.toLowerCase();
251 if (tag == "input" || tag == "textarea" || tag == "select") {
252 if (el.type == "radio" || el.type == "checkbox") {
257 if (el.type == "submit" || el.type == "reset" || el.type == "button" || el.type === "image") {
264 if (tag == "iframe" || tag == "frame") {
271 case "f": result = _open(el); break;
272 case "F": result = _openNewWindow(el); break;
273 case "i": result = "open;" + _getElemtSource(el); break;
274 case "I": result = "tabopen;" + _getElemtSource(el); break;
275 case "l": result = "show_link;" + _getElemtSource(el); break;
276 case "s": result = "save;" + _getElemtSource(el); break;
277 case "y": result = "yank;" + _getElemtSource(el); break;
278 case "O": result = "colon;" + _getElemtSource(el); break;
279 default: result = _getElemtSource(el); break;
285 this.focusInput = function()
287 if (document.getElementsByTagName("body")[0] === null || typeof(document.getElementsByTagName("body")[0]) != "object") {
291 /* prefixing html: will result in namespace error */
292 var hinttags = "//input[@type='text'] | //input[@type='password'] | //textarea";
293 var r = document.evaluate(hinttags, document,
295 return "http://www.w3.org/1999/xhtml";
296 }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
302 for (i = 0; i < r.snapshotLength; i++) {
303 var elem = r.snapshotItem(i);
305 if (elem.style.display != "none" && elem.style.visibility != "hidden") {
311 if (j === 1 && elem.style.display != "none" && elem.style.visibility != "hidden") {
315 if (elem == document.activeElement) {
320 /* no appropriate field found focused - focus the first one */
321 if (j === 0 && first !== null) {
327 /* retrieves text content fro given element */
328 function _getTextFromElement(el)
330 if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
332 } else if (el instanceof HTMLSelectElement) {
333 if (el.selectedIndex >= 0) {
334 text = el.item(el.selectedIndex).text;
339 text = el.textContent;
341 return text.toLowerCase();
344 /* retrieves the hint for given hint number */
345 function _getHintByNumber(n)
347 var index = _getHintIdByNumber(n);
348 if (index !== null) {
354 /* retrieves the id of hint with given number */
355 function _getHintIdByNumber(n)
357 for (var i = 0; i < hints.length; ++i) {
359 if (hint.number === n) {
366 /* removes hint with given number from hints array */
367 function _removeHint(n)
369 var index = _getHintIdByNumber(n);
370 if (index === null) {
373 var hint = hints[index];
374 if (hint.number === n) {
375 hint.elem.style.background = hint.background;
376 hint.elem.style.color = hint.foreground;
377 hint.span.parentNode.removeChild(hint.span);
379 /* remove hints from all hints */
380 hints.splice(index, 1);
384 /* opens given element */
387 if (elem.target == "_blank") {
388 elem.removeAttribute("target");
394 /* opens given element into new window */
395 function _openNewWindow(elem)
397 var oldTarget = elem.target;
399 /* set target to open in new window */
400 elem.target = "_blank";
402 elem.target = oldTarget;
407 /* fire moudedown and click event on given element */
408 function _clickElement(elem)
410 doc = elem.ownerDocument;
411 view = elem.contentWindow;
413 var evObj = doc.createEvent("MouseEvents");
414 evObj.initMouseEvent("mousedown", true, true, view, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, null);
415 elem.dispatchEvent(evObj);
417 evObj = doc.createEvent("MouseEvents");
418 evObj.initMouseEvent("click", true, true, view, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, null);
419 elem.dispatchEvent(evObj);
422 /* retrieves the url of given element */
423 function _getElemtSource(elem)
425 var url = elem.href || elem.src;
429 /* retrieves the xpath expression according to mode */
430 function _getXpathXpression(text)
433 if (typeof(text) == "undefined") {
440 expr = "//*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @role='link' or @href] | //input[not(@type='hidden')] | //a[href] | //area | //textarea | //button | //select";
442 expr = "//*[(@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @role='link' or @href) and contains(., '" + text + "')] | //input[not(@type='hidden') and contains(., '" + text + "')] | //a[@href and contains(., '" + text + "')] | //area[contains(., '" + text + "')] | //textarea[contains(., '" + text + "')] | //button[contains(@value, '" + text + "')] | //select[contains(., '" + text + "')]";
448 expr = "//img[@src]";
450 expr = "//img[@src and contains(., '" + text + "')]";
455 expr = "//*[@role='link' or @href] | //a[href] | //area | //img[not(ancestor::a)]";
457 expr = "//*[(@role='link' or @href) and contains(., '" + text + "')] | //a[@href and contains(., '" + text + "')] | //area[contains(., '" + text + "')] | //img[not(ancestor::a) and contains(., '" + text + "')]";