aboutsummaryrefslogtreecommitdiff
path: root/src/HtmlTree.c
blob: faef08a0d26b921b3010b5f288cdadd6653cf759 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "../include/HtmlTree.h"
#include <stdlib.h>
#include <assert.h>

static void html_node_deinit(HtmlNode *self);

static void html_node_init(HtmlNode *self) {
    self->name_or_value.data = NULL;
    self->name_or_value.size = 0;
    self->parent = NULL;
    self->first_child = NULL;
    self->last_child = NULL;
    self->first_attr = NULL;
    self->node_type = HTML_NODE_NODE;
}

/* |parent| can't be NULL */
static HtmlNode* html_node_create(HtmlStringView name_or_value, HtmlNode *parent, HtmlNodeType node_type) {
    HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild));
    assert(parent);
    if(!node_child)
        return NULL;

    node_child->node.name_or_value = name_or_value;
    node_child->node.parent = parent;
    node_child->node.first_child = NULL;
    node_child->node.last_child = NULL;
    node_child->node.first_attr = NULL;
    node_child->node.node_type = node_type;

    node_child->next = NULL;

    if(!parent->first_child) {
        parent->first_child = node_child;
        parent->last_child = node_child;
    } else {
        parent->last_child->next = node_child;
        parent->last_child = node_child;
    }

    return &node_child->node;
}

static void html_node_child_deinit(HtmlNodeChild *self) {
    html_node_deinit(&self->node);
    if(self->next) {
        html_node_child_deinit(self->next);
        free(self->next);
        self->next = NULL;
    }
}

static void html_attribute_deinit(HtmlAttribute *self) {
    if(self->next) {
        html_attribute_deinit(self->next);
        free(self->next);
        self->next = NULL;
    }
}

void html_node_deinit(HtmlNode *self) {
    self->name_or_value.data = NULL;
    self->name_or_value.size = 0;
    self->parent = NULL;

    if(self->first_child) {
        html_node_child_deinit(self->first_child);
        free(self->first_child);
        self->first_child = NULL;
        self->last_child = NULL;
    }

    if(self->first_attr) {
        html_attribute_deinit(self->first_attr);
        free(self->first_attr);
        self->first_attr = NULL;
    }
}

typedef struct {
    HtmlNode *root_node;
    HtmlNode *current_node;
    HtmlAttribute *current_node_last_attribute;
} ParseUserdata;

static int parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, void *userdata) {
    ParseUserdata *parse_userdata = userdata;

    switch(parse_type) {
        case HTML_PARSE_TAG_START: {
            HtmlNode *new_node = html_node_create(html_parser->tag_name, parse_userdata->current_node, HTML_NODE_NODE);
            if(!new_node)
                return 1;

            parse_userdata->current_node = new_node;
            parse_userdata->current_node_last_attribute = NULL;
            break;
        }
        case HTML_PARSE_TAG_END: {
            HtmlNode *parent_node = parse_userdata->current_node->parent;
            if(parent_node && parent_node != parse_userdata->root_node) {
                parse_userdata->current_node = parent_node;
                parse_userdata->current_node_last_attribute = NULL;
            }
            break;
        }
        case HTML_PARSE_ATTRIBUTE: {
            HtmlAttribute *new_attr = malloc(sizeof(HtmlAttribute));
            if(!new_attr)
                return 1;

            new_attr->key = html_parser->attribute_key;
            new_attr->value = html_parser->attribute_value;
            new_attr->next = NULL;

            if(parse_userdata->current_node_last_attribute)
                parse_userdata->current_node_last_attribute->next = new_attr;
            else
                parse_userdata->current_node->first_attr = new_attr;

            parse_userdata->current_node_last_attribute = new_attr;
            break;
        }
        case HTML_PARSE_TEXT:
        case HTML_PARSE_JAVASCRIPT_CODE: {
            HtmlNode *new_node = html_node_create(html_parser->text_stripped, parse_userdata->current_node, parse_type == HTML_PARSE_TEXT ? HTML_NODE_TEXT : HTML_NODE_JS);
            if(!new_node)
                return 1;

            parse_userdata->current_node_last_attribute = NULL;
            break;
        }
    }

    return 0;
}

int html_parse_to_tree(HtmlTree *self, const char *html_source, size_t len) {
    int result;
    ParseUserdata parse_userdata;
    html_node_init(&self->root_node);

    parse_userdata.root_node = &self->root_node;
    parse_userdata.current_node = &self->root_node;
    parse_userdata.current_node_last_attribute = NULL;
    result = html_parser_parse(html_source, len, parse_callback, &parse_userdata);

    if(result != 0)
        html_tree_deinit(self);
    return result;
}

void html_tree_deinit(HtmlTree *self) {
    html_node_deinit(&self->root_node);
}

HtmlAttribute* html_node_get_attribute_by_name(HtmlNode *self, HtmlStringView name) {
    HtmlAttribute *attr = self->first_attr;
    for(; attr; attr = attr->next) {
        if(html_string_view_equals_case_insensitive(&attr->key, &name))
            return attr;
    }
    return NULL;
}