1 // Copyright 2014-2017 The html5ever Project Developers. See the
2 // COPYRIGHT file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 #include <stdio.h>
11 
12 #include "html5ever.h"
13 
put_str(const char * x)14 void put_str(const char *x) {
15     fputs(x, stdout);
16 }
17 
put_buf(struct h5e_buf text)18 void put_buf(struct h5e_buf text) {
19     fwrite(text.data, text.len, 1, stdout);
20 }
21 
do_chars(void * user,struct h5e_buf text)22 void do_chars(void *user, struct h5e_buf text) {
23     put_str("CHARS : ");
24     put_buf(text);
25     put_str("\n");
26 }
27 
do_start_tag(void * user,struct h5e_buf name,int self_closing,size_t num_attrs)28 void do_start_tag(void *user, struct h5e_buf name, int self_closing, size_t num_attrs) {
29     put_str("TAG   : <");
30     put_buf(name);
31     if (self_closing) {
32         putchar('/');
33     }
34     put_str(">\n");
35 }
36 
do_tag_attr(void * user,struct h5e_buf name,struct h5e_buf value)37 void do_tag_attr(void *user, struct h5e_buf name, struct h5e_buf value) {
38     put_str("  ATTR: ");
39     put_buf(name);
40     put_str("=\"");
41     put_buf(value);
42     put_str("\"\n");
43 }
44 
do_end_tag(void * user,struct h5e_buf name)45 void do_end_tag(void *user, struct h5e_buf name) {
46     put_str("TAG   : </");
47     put_buf(name);
48     put_str(">\n");
49 }
50 
51 struct h5e_token_ops ops = {
52     .do_chars = do_chars,
53     .do_start_tag = do_start_tag,
54     .do_tag_attr = do_tag_attr,
55     .do_end_tag = do_end_tag,
56 };
57 
58 struct h5e_token_sink sink = {
59     .ops = &ops,
60     .user = NULL,
61 };
62 
main(int argc,char * argv[])63 int main(int argc, char *argv[]) {
64     if (argc < 2) {
65         printf("Usage: %s 'HTML fragment'\n", argv[0]);
66         return 1;
67     }
68 
69     struct h5e_tokenizer *tok = h5e_tokenizer_new(&sink);
70     h5e_tokenizer_feed(tok, h5e_buf_from_cstr(argv[1]));
71     h5e_tokenizer_end(tok);
72     h5e_tokenizer_free(tok);
73     return 0;
74 }
75