From a03a3465979a79510d667c4c8ae9ec0bef291ddc Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sun, 30 Nov 2014 22:22:08 +0100 Subject: [PATCH] Fixed none working focus event observing (#112). Now the strict-focus=on prevents vimb only from switching to input mode if focus is on editable element on page load (for example set by a body onload script). Vimb follow all further focus events. --- doc/vimb.1 | 8 +++++--- src/dom.c | 39 ++++++++++++++++++++------------------- src/hints.c | 6 +++++- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/doc/vimb.1 b/doc/vimb.1 index aa0ff33..4e2e5bd 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -1335,9 +1335,11 @@ Foreground of statusbar for untrusted https pages. Statusbar font for untrusted https pages. .TP .B strict-focus (bool) -Indicates if vimb follows the instruction of the page to focus a form field -without user interaction. If set the true, vimb will no switch to input mode -if the pages say so and vimb will remove the focus from the form field. +Vimb checks if an editable element is focused and switch to input mode. If +strict-focus is enabled, this isn't done for focused element on page load +(without user interaction), instead the focus is removed from the focused +element. Focus changed that appear after the page was completely loaded are +not affected by this setting. .TP .B strict-ssl (bool) If 'on', vimb will not load a untrusted https site. diff --git a/src/dom.c b/src/dom.c index bd6f6a5..561e3b8 100644 --- a/src/dom.c +++ b/src/dom.c @@ -32,24 +32,28 @@ static Element *get_active_element(Document *doc); void dom_check_auto_insert(WebKitWebView *view) { - Document *doc = webkit_web_view_get_dom_document(view); - Element *active = get_active_element(doc); + Element *active; + HtmlElement *element; + Document *doc = webkit_web_view_get_dom_document(view); + + if (vb.config.strict_focus) { + /* if there is focus on an element right after page load - remove it + * if strict-focus is enabled */ + dom_clear_focus(view); + } else { + /* if active element is editable - switch vimb to input mode */ + active = get_active_element(doc); + auto_insert(active); + } - if (vb.config.strict_focus || !auto_insert(active)) { - /* if the strict-focus is on also blur the possible active element */ - if (vb.config.strict_focus) { - dom_clear_focus(view); - } - /* the focus was not set automatically - add event listener to track - * focus events on the document */ - HtmlElement *element = webkit_dom_document_get_body(doc); - if (!element) { - element = WEBKIT_DOM_HTML_ELEMENT(webkit_dom_document_get_document_element(doc)); - } - webkit_dom_event_target_add_event_listener( - WEBKIT_DOM_EVENT_TARGET(element), "focus", G_CALLBACK(editable_focus_cb), false, NULL - ); + /* add event listener to track focus events on the document */ + element = webkit_dom_document_get_body(doc); + if (!element) { + element = WEBKIT_DOM_HTML_ELEMENT(webkit_dom_document_get_document_element(doc)); } + webkit_dom_event_target_add_event_listener( + WEBKIT_DOM_EVENT_TARGET(element), "focus", G_CALLBACK(editable_focus_cb), true, NULL + ); } /** @@ -209,9 +213,6 @@ static gboolean auto_insert(Element *element) static gboolean editable_focus_cb(Element *element, Event *event) { - webkit_dom_event_target_remove_event_listener( - WEBKIT_DOM_EVENT_TARGET(element), "focus", G_CALLBACK(editable_focus_cb), false - ); if (vb.mode->id != 'i') { EventTarget *target = webkit_dom_event_get_target(event); auto_insert((void*)target); diff --git a/src/hints.c b/src/hints.c index fbeb631..9dec4ad 100644 --- a/src/hints.c +++ b/src/hints.c @@ -302,7 +302,11 @@ static gboolean call_hints_function(const char *func, int count, JSValueRef para /* following return values mark fired hints */ if (!strncmp(value, "DONE:", 5)) { fire_timeout(false); - if (!hints.gmode) { + /* Change to normal mode only if we are crrently in command mode and + * we are not in g-mode hinting. This is required to not switch to + * normal mode when the hinting triggered a click that set focus on + * editable element that lead vimb to switch to input mode. */ + if (!hints.gmode && vb.mode->id == 'c') { mode_enter('n'); } } else if (!strncmp(value, "INSERT:", 7)) { -- 2.20.1