aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-html-editor-utils.c
blob: 2807ea94b06c79dcf8ca6a0f3ca2b11580576d3b (plain) (blame)
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
/*
 * e-html-editor-utils.c
 *
 * Copyright (C) 2012 Dan Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-html-editor-utils.h"
#include <string.h>

/**
 * e_html_editor_dom_node_find_parent_element:
 * @node: Start node
 * @tagname: Tag name of element to search
 *
 * Recursively searches for first occurance of element with given @tagname
 * that is parent of given @node.
 *
 * Returns: A #WebKitDOMElement with @tagname representing parent of @node or
 * @NULL when @node has no parent with given @tagname. When @node matches @tagname,
 * then the @node is returned.
 */
WebKitDOMElement *
e_html_editor_dom_node_find_parent_element (WebKitDOMNode *node,
                                            const gchar *tagname)
{
    gint taglen = strlen (tagname);

    while (node) {

        if (WEBKIT_DOM_IS_ELEMENT (node)) {
            gchar *node_tagname;

            node_tagname = webkit_dom_element_get_tag_name (
                        WEBKIT_DOM_ELEMENT (node));

            if (node_tagname &&
                (strlen (node_tagname) == taglen) &&
                (g_ascii_strncasecmp (node_tagname, tagname, taglen) == 0)) {
                g_free (node_tagname);
                return WEBKIT_DOM_ELEMENT (node);
            }

            g_free (node_tagname);
        }

        node = WEBKIT_DOM_NODE (webkit_dom_node_get_parent_element (node));
    }

    return NULL;
}

/**
 * e_html_editor_dom_node_find_child_element:
 * @node: Start node
 * @tagname: Tag name of element to search.
 *
 * Recursively searches for first occurence of element with given @tagname that
 * is a child of @node.
 *
 * Returns: A #WebKitDOMElement with @tagname representing a child of @node or
 * @NULL when @node has no child with given @tagname. When @node matches @tagname,
 * then the @node is returned.
 */
WebKitDOMElement *
e_html_editor_dom_node_find_child_element (WebKitDOMNode *node,
                                           const gchar *tagname)
{
    WebKitDOMNode *start_node = node;
    gint taglen = strlen (tagname);

    do {
        if (WEBKIT_DOM_IS_ELEMENT (node)) {
            gchar *node_tagname;

            node_tagname = webkit_dom_element_get_tag_name (
                    WEBKIT_DOM_ELEMENT (node));

            if (node_tagname &&
                (strlen (node_tagname) == taglen) &&
                (g_ascii_strncasecmp (node_tagname, tagname, taglen) == 0)) {
                g_free (node_tagname);
                return WEBKIT_DOM_ELEMENT (node);
            }

            g_free (node_tagname);
        }

        if (webkit_dom_node_has_child_nodes (node)) {
            node = webkit_dom_node_get_first_child (node);
        } else if (webkit_dom_node_get_next_sibling (node)) {
            node = webkit_dom_node_get_next_sibling (node);
        } else {
            node = webkit_dom_node_get_parent_node (node);
        }
    } while (!webkit_dom_node_is_same_node (node, start_node));

    return NULL;
}