aboutsummaryrefslogtreecommitdiff
path: root/src/HtmlParser.c
blob: 8972730edc6d34089dc9baddaf33db76f2d64638 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "../include/HtmlParser.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>

static HtmlStringView void_tags[] = {
    {"area", 4},
    {"base", 4},
    {"br", 2},
    {"col", 3},
    {"command", 7},
    {"embed", 5},
    {"hr", 2},
    {"img", 3},
    {"input", 5},
    {"keygen", 6},
    {"link", 4},
    {"meta", 4},
    {"param", 5},
    {"source", 6},
    {"track", 5},
    {"wbr", 3},
    {NULL, 0}
};

static HtmlStringView script_tag = {"script", 6};

static int string_view_equals(HtmlStringView *self, HtmlStringView *other) {
    return self->size == other->size && memcmp(self->data, other->data, self->size) == 0;
}

static int is_whitespace(int c) {
    switch(c) {
        case ' ':
        case '\n':
        case '\r':
        case '\t':
        case '\v':
            return 1;
        default:
            return 0;
    }
}

static int is_newline(int c) {
    return c == '\n' || c == '\r';
}

static void lstrip(const char *str, size_t size, const char **output_str, size_t *output_size, int(*strip_filter_func)(int)) {
    size_t i = 0;
    while(i < size && strip_filter_func(str[i])) {
        ++i;
    }
    *output_str = str + i;
    *output_size = size - i;
}

static void rstrip(const char *str, size_t size, size_t *output_size, int(*strip_filter_func)(int)) {
    ssize_t i = size - 1;
    while(i >= 0 && strip_filter_func(str[i])) {
        --i;
    }
    *output_size = i + 1;
}

static void strip(const char *str, size_t size, const char **output_str, size_t *output_size, int(*strip_filter_func)(int)) {
    lstrip(str, size, output_str, output_size, strip_filter_func);
    rstrip(*output_str, *output_size, output_size, strip_filter_func);
}

static int is_void_tag(HtmlStringView *tag_name) {
    HtmlStringView *tag_iter = &void_tags[0];
    /* !DOCTYPE, !--, etc.... */
    if(tag_name->size > 0 && tag_name->data[0] == '!')
        return 1;
    while(tag_iter->data) {
        if(string_view_equals(tag_name, tag_iter))
            return 1;
        ++tag_iter;
    }
    return 0;
}

static void html_parser_reset(HtmlParser *self) {
    self->offset = 0;
    self->tag_name.data = NULL;
    self->tag_name.size = 0;
    self->attribute_key.data = NULL;
    self->attribute_key.size = 0;
    self->attribute_value.data = NULL;
    self->attribute_value.size = 0;
    self->text.data = NULL;
    self->text.size = 0;
    self->text_stripped.data = NULL;
    self->text_stripped.size = 0;
    self->is_tag_void = 0;
    self->inside_script_tag = 0;
    self->unclosed_tags_offset = 0;
}

void html_parser_init(HtmlParser *self, const char *html_source, size_t len, HtmlParseCallback parse_callback, void *userdata) {
    self->source = html_source;
    self->source_len = len;
    self->parse_callback = parse_callback;
    self->callback_userdata = userdata;
}

void html_parser_deinit(HtmlParser *self) {
    (void)self;
}

static char html_parser_next_char(HtmlParser *self) {
    if(self->offset < self->source_len) {
        char c = self->source[self->offset];
        ++self->offset;
        return c;
    }
    return '\0';
}

static char html_parser_peek_char(HtmlParser *self) {
    if(self->offset < self->source_len) {
        char c = self->source[self->offset];
        return c;
    }
    return '\0';
}

static void html_parser_advance_char(HtmlParser *self) {
    if(self->offset < self->source_len)
        ++self->offset;
}

static int is_alpha(char c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

static int is_digit(char c) {
    return c >= '0' && c <= '9';
}

static int is_identifier_char(char c) {
    return is_alpha(c) || is_digit(c) || c == '-' || c == '_' || c == '!';
}

static void html_parser_try_append_unclosed_tag(HtmlParser *self, const char *data, size_t size) {
    if(self->unclosed_tags_offset == UNCLOSED_TAGS_SIZE) {
        fprintf(stderr, "Reached the maximum number of unclosed tags! the html source is too broken\n");
        return;
    }
    self->unclosed_tags[self->unclosed_tags_offset].data = data;
    self->unclosed_tags[self->unclosed_tags_offset].size = size;
    ++self->unclosed_tags_offset;
}

static void html_parser_pop_unclosed_tag(HtmlParser *self) {
    assert(self->unclosed_tags_offset > 0);
    --self->unclosed_tags_offset;
}

static void html_parser_try_pop_unclosed_tag(HtmlParser *self) {
    if(self->unclosed_tags_offset > 0)
        --self->unclosed_tags_offset;
}

static int html_parser_try_get_top_unclosed_tag(HtmlParser *self, HtmlStringView *result) {
    if(self->unclosed_tags_offset > 0) {
        *result = self->unclosed_tags[self->unclosed_tags_offset - 1];
        return 1;
    }
    return 0;
}

static void html_parser_skip_whitespace(HtmlParser *self) {
    for(;;) {
        char c = html_parser_peek_char(self);
        if(is_whitespace(c)) {
            html_parser_advance_char(self);
        } else {
            break;
        }
    }
}

static int is_attribute_value_char(char c) {
    switch(c) {
        case '"':
        case '\'':
        case '`':
        case '<':
        case '>':
        case '&':
            return 0;
        default:
            return 1;
    }
}

/* TODO: Unescape html characters in attribute value */
static void html_parser_parse_attribute_value_quoted(HtmlParser *self, char quote_symbol) {
    self->attribute_value.data = self->source + self->offset;
    for(;;) {
        char c = html_parser_peek_char(self);
        if(c == quote_symbol) {
            self->attribute_value.size = (self->source + self->offset) - self->attribute_value.data;
            html_parser_advance_char(self);
            break;
        } else if(c == '\0') {
            self->attribute_value.size = (self->source + self->offset) - self->attribute_value.data;
            break;
        } else {
            html_parser_advance_char(self);
        }
    }
    strip(self->attribute_value.data, self->attribute_value.size, &self->attribute_value.data, &self->attribute_value.size, is_newline);
}

static void html_parser_parse_attribute_value(HtmlParser *self) {
    self->attribute_value.data = self->source + self->offset;
    for(;;) {
        char c = html_parser_peek_char(self);
        if(!is_attribute_value_char(c) || c == '\0')
            break;
        else
            html_parser_advance_char(self);
    }
    self->attribute_value.size = (self->source + self->offset) - self->attribute_value.data;
}

static void html_parser_goto_end_of_js_string(HtmlParser *self, char quote_symbol) {
    int escape_quote = 0;
    for(;;) {
        char c = html_parser_next_char(self);
        if(!escape_quote && c == quote_symbol) {
            return;
        } else if(c == '\\') {
            escape_quote = !escape_quote;
        } else if(c == '\0') {
            return;
        } else {
            escape_quote = 0;
        }
    }
}

static void html_parser_goto_script_end_tag(HtmlParser *self) {
    self->text.data = self->source + self->offset;
    self->text.size = 0;
    for(;;) {
        char c = html_parser_peek_char(self);
        if(c == '"' || c == '\'') {
            html_parser_advance_char(self);
            html_parser_goto_end_of_js_string(self, c);
        } else if(c == '<' && self->offset + 7 < self->source_len && memcmp(self->source + self->offset + 1, "/script", 7) == 0) {
            self->text.size = (self->source + self->offset) - self->text.data;
            strip(self->text.data, self->text.size, &self->text.data, &self->text.size, is_whitespace);
            self->offset += 7;
            for(;;) {
                c = html_parser_peek_char(self);
                if(c == '>') {
                    html_parser_advance_char(self);
                    break;
                } else if(c == '\0') {
                    break;
                } else {
                    html_parser_advance_char(self);
                }
            }
            break;
        } else if(c == '\0') {
            self->text.size = (self->source + self->offset) - self->text.data;
            strip(self->text.data, self->text.size, &self->text.data, &self->text.size, is_whitespace);
            break;
        } else {
            html_parser_advance_char(self);
        }
    }
    self->parse_callback(self, HTML_PARSE_JAVASCRIPT_CODE, self->callback_userdata);
}

static void html_parser_goto_comment_end(HtmlParser *self) {
    for(;;) {
        if(self->source_len - self->offset >= 3 && memcmp(self->source + self->offset, "-->", 3) == 0) {
            self->offset += 3;
            break;
        }
        html_parser_advance_char(self);
    }
}

static void html_parser_parse_tag_start(HtmlParser *self) {
    int tag_name_found = 0;
    for(;;) {
        char c = html_parser_next_char(self);
        if(c == '>') {
            if(tag_name_found && self->is_tag_void)
                self->parse_callback(self, HTML_PARSE_TAG_END, self->callback_userdata);
            self->is_tag_void = 0;
            
            if(self->inside_script_tag) {
                self->inside_script_tag = 0;
                /* <script> tags require special handling since they can have </script> inside a javascript string */
                html_parser_goto_script_end_tag(self);
            }
            return;
        } else if(c == '/') {
            if(html_parser_peek_char(self) == '>') {
                html_parser_advance_char(self);
                if(tag_name_found) {
                    self->parse_callback(self, HTML_PARSE_TAG_END, self->callback_userdata);
                    if(!self->is_tag_void)
                        html_parser_try_pop_unclosed_tag(self);
                }
                self->is_tag_void = 0;
                self->inside_script_tag = 0;
                return;
            }
        } else if(is_identifier_char(c)) {
            HtmlStringView identifier;
            identifier.data = self->source + self->offset - 1;
            for(;;) {
                c = html_parser_peek_char(self);
                if(is_identifier_char(c)) {
                    html_parser_advance_char(self);
                } else {
                    break;
                }
            }
            identifier.size = (self->source + self->offset) - identifier.data;
            if(tag_name_found) {
                /* attribute name */
                self->attribute_key = identifier;
                self->attribute_value.data = NULL;
                self->attribute_value.size = 0;

                html_parser_skip_whitespace(self);
                c = html_parser_peek_char(self);
                if(c == '=') {
                    html_parser_advance_char(self);
                    html_parser_skip_whitespace(self);
                    c = html_parser_peek_char(self);
                    if(c == '"' || c == '\'' || c == '`') {
                        html_parser_advance_char(self);
                        html_parser_parse_attribute_value_quoted(self, c);
                    } else if(is_attribute_value_char(c)) {
                        html_parser_advance_char(self);
                        html_parser_parse_attribute_value(self);
                    }
                }
                self->parse_callback(self, HTML_PARSE_ATTRIBUTE, self->callback_userdata);
            } else {
                /* tag name */
                self->tag_name = identifier;
                tag_name_found = 1;
                if(self->tag_name.size == 3 && memcmp(self->tag_name.data, "!--", 3) == 0) {
                    html_parser_goto_comment_end(self);
                    return;
                }
                self->is_tag_void = is_void_tag(&self->tag_name);
                if(!self->is_tag_void) {
                    html_parser_try_append_unclosed_tag(self, self->tag_name.data, self->tag_name.size);
                    self->inside_script_tag = string_view_equals(&self->tag_name, &script_tag);
                }
                self->parse_callback(self, HTML_PARSE_TAG_START, self->callback_userdata);
            }
        } else if(c == '\0') {
            return;
        }
    }
}

static void html_parser_parse_tag_end(HtmlParser *self) {
    int tag_name_found = 0;
    for(;;) {
        char c = html_parser_peek_char(self);
        if(c == '>') {
            html_parser_advance_char(self);
            return;
        } else if(!tag_name_found && is_identifier_char(c)) {
            HtmlStringView tag_end_name;
            tag_end_name.data = self->source + self->offset;
            html_parser_advance_char(self);
            for(;;) {
                c = html_parser_peek_char(self);
                if(is_identifier_char(c)) {
                    html_parser_advance_char(self);
                } else {
                    break;
                }
            }
            tag_end_name.size = (self->source + self->offset) - tag_end_name.data;
            tag_name_found = 1;

            /* void tags close themselves, this is probably invalid html but we choose to ignore it silently */
            if(is_void_tag(&tag_end_name)) {
                fprintf(stderr, "Warning: got end tag for void tag '%.*s'\n", (int)tag_end_name.size, tag_end_name.data);
                continue;
            }

            ssize_t found_start_tag_index = -1;
            for(ssize_t i = self->unclosed_tags_offset - 1; i >= 0; --i) {
                if(string_view_equals(&self->unclosed_tags[i], &tag_end_name)) {
                    found_start_tag_index = i;
                    break;
                }
            }

            if(found_start_tag_index != -1) {
                for(; self->unclosed_tags_offset > (size_t)found_start_tag_index; --self->unclosed_tags_offset) {
                    self->tag_name = self->unclosed_tags[self->unclosed_tags_offset - 1];
                    self->parse_callback(self, HTML_PARSE_TAG_END, self->callback_userdata);
                }
            } else {
                fprintf(stderr, "Warning: start tag not found for end tag '%.*s'\n", (int)tag_end_name.size, tag_end_name.data);
            }
        } else if(c == '\0') {
            return;
        } else {
            html_parser_advance_char(self);
        }
    }
}

void html_parser_parse(HtmlParser *self) {
    html_parser_reset(self);
    for(;;) {
        char c = html_parser_next_char(self);
        if(c == '<') {
            if(html_parser_peek_char(self) == '/') {
                html_parser_advance_char(self);
                html_parser_parse_tag_end(self);
            } else {
                html_parser_parse_tag_start(self);
            }
        } else if(c == '\0') {
            break;
        } else {
            self->text.data = (self->source + self->offset) - 1;
            for(;;) {
                c = html_parser_peek_char(self);
                if(c == '<' || c == '\0')
                    break;
                else
                    html_parser_advance_char(self);
            }
            self->text.size = (self->source + self->offset) - self->text.data;
            strip(self->text.data, self->text.size, &self->text_stripped.data, &self->text_stripped.size, is_whitespace);
            self->parse_callback(self, HTML_PARSE_TEXT, self->callback_userdata);
        }
    }

    HtmlStringView top_unclosed_tag;
    while(html_parser_try_get_top_unclosed_tag(self, &top_unclosed_tag)) {
        self->tag_name = top_unclosed_tag;
        self->parse_callback(self, HTML_PARSE_TAG_END, self->callback_userdata);
        html_parser_pop_unclosed_tag(self);
    }
}