Fixed none working focus event observing (#112).
authorDaniel Carl <danielcarl@gmx.de>
Sun, 30 Nov 2014 21:22:08 +0000 (22:22 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 30 Nov 2014 21:28:31 +0000 (22:28 +0100)
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
src/dom.c
src/hints.c

index aa0ff33..4e2e5bd 100644 (file)
@@ -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.
index bd6f6a5..561e3b8 100644 (file)
--- 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);
index fbeb631..9dec4ad 100644 (file)
@@ -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)) {