From: Daniel Carl Date: Sun, 3 Mar 2013 10:08:55 +0000 (+0100) Subject: Removed hint processings out of the javascript. X-Git-Url: https://git.owens.tech/assets/static/git-logo.png/assets/static/git-logo.png/git?a=commitdiff_plain;h=7d38785554ea973a6b33dda025d237858f7b94bd;p=vimb.git Removed hint processings out of the javascript. Cause of the processings that where know in the c-layer and the javascript layer too, we got really complicated code. Now the javascript-layer did no know anything of how the hinted sources should be used by the browser. This only concern that is kept in the javascript are the different types of elements that are hintable. --- diff --git a/src/command.c b/src/command.c index 7679185..dc62778 100644 --- a/src/command.c +++ b/src/command.c @@ -68,13 +68,13 @@ static CommandInfo cmd_list[] = { {"complete", command_complete, {0}}, {"complete-back", command_complete, {1}}, {"inspect", command_inspect, {0}}, - {"hint-link", command_hints, {HINTS_TYPE_LINK, "."}}, - {"hint-link-new", command_hints, {HINTS_TYPE_LINK | HINTS_TARGET_BLANK, ","}}, - {"hint-input-open", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS | HINTS_PROCESS_INPUT, ";o"}}, - {"hint-input-tabopen", command_hints, {HINTS_TYPE_LINK | HINTS_TARGET_BLANK | HINTS_PROCESS | HINTS_PROCESS_INPUT, ";t"}}, - {"hint-yank", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS | HINTS_PROCESS_YANK, ";y"}}, - {"hint-image-open", command_hints, {HINTS_TYPE_IMAGE, ";i"}}, - {"hint-image-tabopen", command_hints, {HINTS_TYPE_IMAGE | HINTS_TARGET_BLANK, ";I"}}, + {"hint-link", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS_OPEN, "."}}, + {"hint-link-new", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS_OPEN | HINTS_OPEN_NEW, ","}}, + {"hint-input-open", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS_INPUT, ";o"}}, + {"hint-input-tabopen", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS_INPUT | HINTS_OPEN_NEW, ";t"}}, + {"hint-yank", command_hints, {HINTS_TYPE_LINK | HINTS_PROCESS_YANK, ";y"}}, + {"hint-image-open", command_hints, {HINTS_TYPE_IMAGE | HINTS_PROCESS_OPEN, ";i"}}, + {"hint-image-tabopen", command_hints, {HINTS_TYPE_IMAGE | HINTS_PROCESS_OPEN | HINTS_OPEN_NEW, ";I"}}, {"hint-focus-next", command_hints_focus, {0}}, {"hint-focus-prev", command_hints_focus, {1}}, {"yank-uri", command_yank, {COMMAND_YANK_PRIMARY | COMMAND_YANK_SECONDARY | COMMAND_YANK_URI}}, diff --git a/src/hints.c b/src/hints.c index 5748f06..6433eed 100644 --- a/src/hints.c +++ b/src/hints.c @@ -86,12 +86,10 @@ void hints_create(const char* input, guint mode, const guint prefixLength) } /* convert the mode into the type chare used in the hint script */ - if (mode & HINTS_PROCESS) { - type = 'd'; - } else if (mode & HINTS_TYPE_IMAGE) { - type = (HINTS_TARGET_BLANK & mode) ? 'I' : 'i'; - } else { - type = (HINTS_TARGET_BLANK & mode) ? 'F' : 'f'; + if (mode & HINTS_TYPE_LINK) { + type = 'l'; + } else if (HINTS_TYPE_IMAGE) { + type = 'i'; } js = g_strdup_printf("%s.create('%s', '%c');", HINT_VAR, input ? input : "", type); @@ -147,21 +145,19 @@ static void hints_run_script(char* js) } else if (!strncmp(value, "DATA:", 5)) { hints_observe_input(FALSE); Arg a = {0}; - if (mode & HINTS_TYPE_IMAGE) { - a.s = (value + 5); - a.i = (mode & HINTS_TARGET_BLANK) ? VP_TARGET_NEW : VP_TARGET_CURRENT; + char* v = (value + 5); + if (mode & HINTS_PROCESS_OPEN) { + a.s = v; + a.i = (mode & HINTS_OPEN_NEW) ? VP_TARGET_NEW : VP_TARGET_CURRENT; command_open(&a); + } else if (mode & HINTS_PROCESS_INPUT) { + a.s = g_strconcat((mode & HINTS_OPEN_NEW) ? ":tabopen " : ":open ", v, NULL); + command_input(&a); + g_free(a.s); } else { - HintsProcess type = HINTS_GET_PROCESSING(mode); - if (type == HINTS_PROCESS_INPUT) { - a.s = g_strconcat((mode & HINTS_TARGET_BLANK) ? ":tabopen " : ":open ", (value + 5), NULL); - command_input(&a); - g_free(a.s); - } else if (type == HINTS_PROCESS_YANK) { - a.i = COMMAND_YANK_PRIMARY | COMMAND_YANK_SECONDARY; - a.s = (value + 5); - command_yank(&a); - } + a.i = COMMAND_YANK_PRIMARY | COMMAND_YANK_SECONDARY; + a.s = v; + command_yank(&a); } } g_free(value); @@ -192,6 +188,10 @@ static void hints_observe_input(gboolean observe) g_signal_handler_disconnect(G_OBJECT(vp.gui.inputbox), keypressHandler); changeHandler = 0; keypressHandler = 0; + + /* clear the input box - TODO move this to a better place */ + gtk_widget_grab_focus(GTK_WIDGET(vp.gui.webview)); + gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), ""); } } diff --git a/src/hints.h b/src/hints.h index aa5f65f..bc0b2d8 100644 --- a/src/hints.h +++ b/src/hints.h @@ -22,24 +22,17 @@ #include "main.h" -#define HINTS_GET_PROCESSING(type) ((type) & ~(HINTS_TYPE_LINK | HINTS_TYPE_IMAGE | HINTS_PROCESS | HINTS_TARGET_BLANK)) - -typedef enum { - HINTS_TYPE_LINK = (1 << 1), - HINTS_TYPE_IMAGE = (1 << 2), - HINTS_TYPE_LAST = HINTS_TYPE_IMAGE, -} HintsType; +#define HINTS_GET_TYPE(n) ((n) & (HINTS_TYPE_LINK | HINTS_TYPE_IMAGE)) enum { - HINTS_PROCESS = (1 << 3), - HINTS_TARGET_BLANK = (1 << 4) + HINTS_TYPE_LINK = 1, + HINTS_TYPE_IMAGE = 2, + HINTS_PROCESS_INPUT = (1 << 2), + HINTS_PROCESS_YANK = (1 << 3), + HINTS_PROCESS_OPEN = (1 << 4), + HINTS_OPEN_NEW = (1 << 5), }; -typedef enum { - HINTS_PROCESS_INPUT = (1 << 5), - HINTS_PROCESS_YANK = (1 << 6), -} HintsProcess; - void hints_init(WebKitWebFrame* frame); void hints_create(const char* input, guint mode, const guint prefixLength); void hints_update(const gulong num); diff --git a/src/hints.js b/src/hints.js index 6852d93..325e6db 100644 --- a/src/hints.js +++ b/src/hints.js @@ -29,7 +29,8 @@ VimpHints = function Hints(bg, bgf, fg, style) { var hintCount = 0; this.clear(); - function _helper (win, offsetX, offsetY) { + function _helper (win, offsetX, offsetY) + { var doc = win.document; var win_height = win.height; @@ -47,12 +48,13 @@ VimpHints = function Hints(bg, bgf, fg, style) { hCont = doc.createElement("div"); hCont.id = "hint_container"; - xpath_expr = _getXpathXpression(inputText); + xpath_expr = _getXpath(inputText); - var res = doc.evaluate(xpath_expr, doc, - function (p) { - return "http://www.w3.org/1999/xhtml"; - }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + var res = doc.evaluate( + xpath_expr, doc, + function (p) {return "http://www.w3.org/1999/xhtml";}, + XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null + ); /* generate basic hint element which will be cloned and updated later */ var hintSpan = doc.createElement("span"); @@ -73,7 +75,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { } var style = topwin.getComputedStyle(elem, ""); - if (style.display == "none" || style.visibility != "visible") { + if (style.display === "none" || style.visibility !== "visible") { continue; } @@ -134,9 +136,8 @@ VimpHints = function Hints(bg, bgf, fg, style) { if (typeof(hints[index + 1]) !== "undefined") { return _focus(hints[index + 1].number); - } else { - return _focus(hints[0].number); } + return _focus(hints[0].number); }; /* set focus to previous avaiable hint */ @@ -145,9 +146,8 @@ VimpHints = function Hints(bg, bgf, fg, style) { var index = _getHintIdByNumber(curFocusNum); if (index !== 0 && typeof(hints[index - 1].number) !== "undefined") { return _focus(hints[index - 1].number); - } else { - return _focus(hints[hints.length - 1].number); } + return _focus(hints[hints.length - 1].number); }; /* filters hints matching given number */ @@ -199,10 +199,9 @@ VimpHints = function Hints(bg, bgf, fg, style) { this.fire = function(n) { n = n ? n : curFocusNum; - var result = "DONE:"; var hint = _getHintByNumber(n); if (!hint || typeof(hint.elem) == "undefined") { - return result; + return "DONE:"; } var el = hint.elem; @@ -211,7 +210,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { this.clear(); if (tag === "iframe" || tag === "frame" || tag === "textarea" || tag === "select" || tag === "input" - && (el.type != "image" && el.type != "submit") + && (el.type !== "image" && el.type !== "submit") ) { el.focus(); if (tag === "input" || tag === "textarea") { @@ -220,19 +219,7 @@ VimpHints = function Hints(bg, bgf, fg, style) { return "DONE:"; } - switch (mode) { - case "f": - _open(el); - break; - case "F": - _openNewWindow(el); - break; - default: - result = "DATA:" + _getElemtSource(el); - break; - } - - return result; + return "DATA:" + _getSrc(el);; }; /* set focus on hint with given number */ @@ -254,27 +241,10 @@ VimpHints = function Hints(bg, bgf, fg, style) { hint.elem.className = hint.elem.className.replace(config.hintClass, config.hintClassFocus); hint.elem.style.background = config.eBgf; _mouseEvent(hint.elem, "mouseover"); - var source = _getElemtSource(hint.elem); - return "OVER:" + (source ? source : ""); - } - } + var source = _getSrc(hint.elem); - /* 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) { - if (el.selectedIndex >= 0) { - text = el.item(el.selectedIndex).text; - } else{ - text = ""; - } - } else { - text = el.textContent; + return "OVER:" + (source ? source : ""); } - return text.toLowerCase(); } /* retrieves the hint for given hint number */ @@ -318,28 +288,6 @@ VimpHints = function Hints(bg, bgf, fg, style) { } } - /* opens given element */ - function _open(elem) - { - if (elem.target == "_blank") { - elem.removeAttribute("target"); - } - _mouseEvent(elem, "moudedown"); - _mouseEvent(elem, "click"); - } - - /* opens given element into new window */ - function _openNewWindow(elem) - { - var oldTarget = elem.target; - - /* set target to open in new window */ - elem.target = "_blank"; - _mouseEvent(elem, "moudedown"); - _mouseEvent(elem, "click"); - elem.target = oldTarget; - } - function _mouseEvent(elem, name) { var doc = elem.ownerDocument; @@ -351,41 +299,38 @@ VimpHints = function Hints(bg, bgf, fg, style) { } /* retrieves the url of given element */ - function _getElemtSource(elem) + function _getSrc(elem) { - var url = elem.href || elem.src; - return url; + return elem.href || elem.src; } /* retrieves the xpath expression according to mode */ - function _getXpathXpression(text) + function _getXpath(s) { var expr; - if (typeof(text) === "undefined") { - text = ""; + if (typeof(s) === "undefined") { + s = ""; } switch (mode) { - case "f": - case "F": - if (text === "") { + case "l": + if (s === "") { 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 + "')]"; + expr = "//*[(@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @role='link' or @href) and contains(., '" + s + "')] | //input[not(@type='hidden') and contains(., '" + s + "')] | //a[@href and contains(., '" + s + "')] | //area[contains(., '" + s + "')] | //textarea[contains(., '" + s + "')] | //button[contains(@value, '" + s + "')] | //select[contains(., '" + s + "')]"; } break; case "i": - case "I": - if (text === "") { + if (s === "") { expr = "//img[@src]"; } else { - expr = "//img[@src and contains(., '" + text + "')]"; + expr = "//img[@src and contains(., '" + s + "')]"; } break; default: - if (text === "") { + if (s === "") { 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 + "')]"; + expr = "//*[(@role='link' or @href) and contains(., '" + s + "')] | //a[@href and contains(., '" + s + "')] | //area[contains(., '" + s + "')] | //img[not(ancestor::a) and contains(., '" + s + "')]"; } break; }