From fc0d77c910fa3a2fef2a0a7cd4efc54dccdab692 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Fri, 1 Mar 2013 15:10:26 +0100 Subject: [PATCH] Fixed some coding issues in hint.js. --- src/hint.js | 83 ++++++++++++++++++++++++++++++----------------------- src/hints.c | 18 ++++++------ 2 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/hint.js b/src/hint.js index 1738f78..84aeb27 100644 --- a/src/hint.js +++ b/src/hint.js @@ -6,7 +6,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { hintClassFocus: "__hint_container", eBg: bg, eBgf: bgf, - eFg: fg, + eFg: fg }; var hCont; @@ -60,7 +60,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { /* due to the different XPath result type, we will need two counter variables */ var rect, elem, text, node, show_text; - for (var i = 0; i < res.snapshotLength; i++) { + for (i = 0; i < res.snapshotLength; i++) { if (hintCount >= config.maxHints) { break; } @@ -110,8 +110,9 @@ VimpHints = function Hints(bg, bgf, fg, style) { for (var i = 0, nframes = frames.length; i < nframes; ++i) { elem = frames[i]; rect = elem.getBoundingClientRect(); - if (!elem.contentWindow || !rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY) + if (!elem.contentWindow || !rect || rect.left > maxX || rect.right < minX || rect.top > maxY || rect.bottom < minY) { continue; + } _helper(elem.contentWindow, offsetX + rect.left, offsetY + rect.top); } } @@ -131,7 +132,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { { var index = _getHintIdByNumber(curFocusNum); - if (typeof(hints[index + 1]) != "undefined") { + if (typeof(hints[index + 1]) !== "undefined") { _focus(hints[index + 1].number); } else { _focus(hints[0].number); @@ -142,7 +143,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { this.focusPrev = function() { var index = _getHintIdByNumber(curFocusNum); - if (index != 0 && typeof(hints[index - 1].number) != "undefined") { + if (index !== 0 && typeof(hints[index - 1].number) !== "undefined") { _focus(hints[index - 1].number); } else { _focus(hints[hints.length - 1].number); @@ -152,38 +153,38 @@ VimpHints = function Hints(bg, bgf, fg, style) { /* filters hints matching given number */ this.update = function(n) { - if (n == 0) { + if (n === 0) { return this.create(); } /* remove none matching hints */ var remove = []; - for (var i = 0; i < hints.length; ++i) { + var i; + for (i = 0; i < hints.length; ++i) { var hint = hints[i]; - if (0 != hint.number.toString().indexOf(n.toString())) { + if (0 !== hint.number.toString().indexOf(n.toString())) { remove.push(hint.number); } } - for (var i = 0; i < remove.length; ++i) { + for (i = 0; i < remove.length; ++i) { _removeHint(remove[i]); } if (hints.length === 1) { return this.fire(hints[0].number); - } else { - return _focus(n); } + return _focus(n); }; /* remove all hints and set previous style to them */ this.clear = function() { - if (hints.length == 0) { + if (hints.length === 0) { return; } for (var i = 0; i < hints.length; ++i) { var hint = hints[i]; - if (typeof(hint.elem) != "undefined") { + if (typeof(hint.elem) !== "undefined") { hint.elem.style.background = hint.background; hint.elem.style.color = hint.foreground; hint.span.parentNode.removeChild(hint.span); @@ -197,35 +198,43 @@ VimpHints = function Hints(bg, bgf, fg, style) { /* fires the modeevent on hint with given number */ this.fire = function(n) { - var doc, result; - if (!n) { - var n = curFocusNum; - } + n = n ? n : curFocusNum; + var result; var hint = _getHintByNumber(n); if (typeof(hint.elem) == "undefined") { return "DONE:"; } - var el = hint.elem; + var el = hint.elem; var tag = el.nodeName.toLowerCase(); this.clear(); - if (tag == "iframe" || tag == "frame" || tag == "textarea" || tag == "input" && (el.type == "text" || el.type == "password" || el.type == "checkbox" || el.type == "radio") || tag == "select") { + if (tag === "iframe" || tag === "frame" || tag === "textarea" || tag === "input" && (el.type === "text" || el.type === "password" || el.type === "checkbox" || el.type === "radio") || tag === "select") { el.focus(); - if (tag == "input" || tag == "textarea") { - return "INSERT:" + if (tag === "input" || tag === "textarea") { + return "INSERT:"; } return "DONE:"; } result = "DONE:"; switch (mode) { - case "f": _open(el); break; - case "F": _openNewWindow(el); break; - case "i": _open(el); break; - case "I": _openNewWindow(el); break; - default: result = "DATA:" + _getElemtSource(el); + case "f": + _open(el); + break; + case "F": + _openNewWindow(el); + break; + case "i": + _open(el); + break; + case "I": + _openNewWindow(el); + break; + default: + result = "DATA:" + _getElemtSource(el); + break; } return result; @@ -245,7 +254,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { curFocusNum = n; /* mark new hint as focused */ - var hint = _getHintByNumber(curFocusNum); + hint = _getHintByNumber(curFocusNum); if (hint !== null) { hint.elem.className = hint.elem.className.replace(config.hintClass, config.hintClassFocus); hint.elem.style.background = config.eBgf; @@ -256,6 +265,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { /* retrieves text content fro given element */ function _getTextFromElement(el) { + var text; if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) { text = el.value; } else if (el instanceof HTMLSelectElement) { @@ -267,7 +277,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { } else { text = el.textContent; } - return text.toLowerCase();; + return text.toLowerCase(); } /* retrieves the hint for given hint number */ @@ -283,8 +293,9 @@ VimpHints = function Hints(bg, bgf, fg, style) { /* retrieves the id of hint with given number */ function _getHintIdByNumber(n) { + var hint; for (var i = 0; i < hints.length; ++i) { - var hint = hints[i]; + hint = hints[i]; if (hint.number === n) { return i; } @@ -334,8 +345,8 @@ VimpHints = function Hints(bg, bgf, fg, style) { function _mouseEvent(elem, name) { - doc = elem.ownerDocument; - view = elem.contentWindow; + var doc = elem.ownerDocument; + var view = elem.contentWindow; var evObj = doc.createEvent("MouseEvents"); evObj.initMouseEvent(name, true, true, view, 0, 0, 0, 0, 0, false, false, false, false, 0, null); @@ -353,13 +364,13 @@ VimpHints = function Hints(bg, bgf, fg, style) { function _getXpathXpression(text) { var expr; - if (typeof(text) == "undefined") { + if (typeof(text) === "undefined") { text = ""; } switch (mode) { case "f": case "F": - if (text == "") { + if (text === "") { 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"; } else { 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 + "')]"; @@ -367,14 +378,14 @@ VimpHints = function Hints(bg, bgf, fg, style) { break; case "i": case "I": - if (text == "") { + if (text === "") { expr = "//img[@src]"; } else { expr = "//img[@src and contains(., '" + text + "')]"; } break; default: - if (text == "") { + if (text === "") { expr = "//*[@role='link' or @href] | //a[href] | //area | //img[not(ancestor::a)]"; } else { expr = "//*[(@role='link' or @href) and contains(., '" + text + "')] | //a[@href and contains(., '" + text + "')] | //area[contains(., '" + text + "')] | //img[not(ancestor::a) and contains(., '" + text + "')]"; @@ -383,4 +394,4 @@ VimpHints = function Hints(bg, bgf, fg, style) { } return expr; } -} +}; diff --git a/src/hints.c b/src/hints.c index 2042694..bb64e10 100644 --- a/src/hints.c +++ b/src/hints.c @@ -24,10 +24,7 @@ #include "command.h" #include "hint.js.h" -/* TODO use this in hinting script */ -#define MAX_HINTS 200 -#define HINT_CONTAINER_ID "__hint_container" -#define HINT_CLASS "__hint" +#define HINT_VAR "VpHint" static void hints_run_script(char* js); static void hints_fire(void); @@ -48,7 +45,7 @@ void hints_clear(void) { hints_observe_input(FALSE); if (GET_CLEAN_MODE() == VP_MODE_HINTING) { - char* js = g_strdup("hints.clear();"); + char* js = g_strdup_printf("%s.clear();", HINT_VAR); char* value = NULL; char* error = NULL; @@ -70,7 +67,8 @@ void hints_create(const char* input, guint mode, const guint prefixLength) vp.hints.num = 0; js = g_strdup_printf( - "hints = new VimpHints('%s', '%s', '%s', '%s');", + "var %s = new VimpHints('%s', '%s', '%s', '%s');", + HINT_VAR, style->hint_bg, style->hint_bg_focus, style->hint_fg, @@ -91,21 +89,21 @@ void hints_create(const char* input, guint mode, const guint prefixLength) type = (HINTS_TARGET_BLANK & mode) ? 'F' : 'f'; } - js = g_strdup_printf("hints.create('%s', '%c');", input ? input : "", type); + js = g_strdup_printf("%s.create('%s', '%c');", HINT_VAR, input ? input : "", type); hints_run_script(js); g_free(js); } void hints_update(const gulong num) { - char* js = g_strdup_printf("hints.update(%lu);", num); + char* js = g_strdup_printf("%s.update(%lu);", HINT_VAR, num); hints_run_script(js); g_free(js); } void hints_focus_next(const gboolean back) { - char* js = g_strdup(back ? "hints.focusPrev()" : "hints.focusNext();"); + char* js = g_strdup_printf(back ? "%s.focusPrev()" : "%s.focusNext();", HINT_VAR); hints_run_script(js); g_free(js); } @@ -159,7 +157,7 @@ static void hints_run_script(char* js) static void hints_fire(void) { hints_observe_input(FALSE); - char* js = g_strdup("hints.fire();"); + char* js = g_strdup_printf("%s.fire();", HINT_VAR); hints_run_script(js); g_free(js); } -- 2.20.1