summaryrefslogtreecommitdiff
path: root/src/dom.c
blob: 9ded553f33639fc34c0d7abc4e14c58d0ac0ba87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2013 Daniel Carl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

#include "main.h"
#include "dom.h"

extern VbCore vb;

static gboolean auto_insert(Element *element);
static gboolean editable_focus_cb(Element *element, Event *event);
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);

    /* the focus was not set automatically - add event listener to track focus
     * events on the document */
    if (!auto_insert(active)) {
        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), true, NULL
        );
    }
}

/**
 * Remove focus from active and editable elements.
 */
void dom_clear_focus(WebKitWebView *view)
{
    Element *active = dom_get_active_element(view);
    if (active) {
        webkit_dom_element_blur(active);
    }
}

/**
 * Indicates if the given dom element is an editable element like text input,
 * password or textarea.
 */
gboolean dom_is_editable(Element *element)
{
    gboolean result = false;
    char *tagname, *type;

    if (!element) {
        return result;
    }

    tagname = webkit_dom_element_get_tag_name(element);
    type    = webkit_dom_element_get_attribute(element, "type");
    if (!g_ascii_strcasecmp(tagname, "textarea")) {
        result = true;
    } else if (!g_ascii_strcasecmp(tagname, "input")
        && g_ascii_strcasecmp(type, "submit")
        && g_ascii_strcasecmp(type, "reset")
        && g_ascii_strcasecmp(type, "image")
    ) {
        result = true;
    } else {
        result = false;
    }
    g_free(tagname);
    g_free(type);

    return result;
}

Element *dom_get_active_element(WebKitWebView *view)
{
    return get_active_element(webkit_web_view_get_dom_document(view));
}

static gboolean auto_insert(Element *element)
{
    if (dom_is_editable(element)) {
        vb_set_mode(VB_MODE_INSERT, false);
        return true;
    }
    return false;
}

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), true
    );
    if (CLEAN_MODE(vb.state.mode) != VB_MODE_INSERT) {
        EventTarget *target = webkit_dom_event_get_target(event);
        auto_insert((void*)target);
    }
    return false;
}

static Element *get_active_element(Document *doc)
{
    char *tagname;
    Document *d = NULL;
    Element *active, *result = NULL;

    active = webkit_dom_html_document_get_active_element((void*)doc);
    if (!active) {
        return result;
    }
    tagname = webkit_dom_element_get_tag_name(active);

    if (!g_strcmp0(tagname, "FRAME")) {
        d = webkit_dom_html_frame_element_get_content_document(WEBKIT_DOM_HTML_FRAME_ELEMENT(active));
        result = get_active_element(d);
    } else if (!g_strcmp0(tagname, "IFRAME")) {
        d = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(active));
        result = get_active_element(d);
    }
    g_free(tagname);

    if (result) {
        return result;
    }

    return active;
}