summaryrefslogtreecommitdiff
path: root/src/dom.c
blob: c37242323ddc57b5dba7f310a8edfe61f71ace33 (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
/**
 * 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 dom_auto_insert(Element *element);
static gboolean dom_editable_focus_cb(Element *element, Event *event);
static Element *dom_get_active_element(Document *doc);


void dom_check_auto_insert(void)
{
    Document *doc   = webkit_web_view_get_dom_document(vb.gui.webview);
    Element *active = dom_get_active_element(doc);

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

/**
 * Remove focus from active and editable elements.
 */
void dom_clear_focus(void)
{
    Document *doc   = webkit_web_view_get_dom_document(vb.gui.webview);
    Element *active = dom_get_active_element(doc);

    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;
    if (!element) {
        return result;
    }

    char *tagname = webkit_dom_element_get_tag_name(element);
    char *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;
}

static gboolean dom_auto_insert(Element *element)
{
    if (dom_is_editable(element)) {
        vb_set_mode(VB_MODE_INSERT, FALSE);
        return TRUE;
    }
    return FALSE;
}

static gboolean dom_editable_focus_cb(Element *element, Event *event)
{
    webkit_dom_event_target_remove_event_listener(
        WEBKIT_DOM_EVENT_TARGET(element), "focus", G_CALLBACK(dom_editable_focus_cb), true
    );
    if (CLEAN_MODE(vb.state.mode) != VB_MODE_INSERT) {
        EventTarget *target = webkit_dom_event_get_target(event);
        dom_auto_insert((void*)target);
    }
    return FALSE;
}

static Element *dom_get_active_element(Document *doc)
{
    Document *d     = NULL;
    Element *active = webkit_dom_html_document_get_active_element((void*)doc);
    char *tagname   = webkit_dom_element_get_tag_name(active);
    Element *result = NULL;

    if (!g_strcmp0(tagname, "FRAME")) {
        d = webkit_dom_html_frame_element_get_content_document(WEBKIT_DOM_HTML_FRAME_ELEMENT(active));
        result = dom_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 = dom_get_active_element(d);
    }
    g_free(tagname);

    if (result) {
        g_free(active);

        return result;
    }

    return active;
}