-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_parser_document_node.c
More file actions
275 lines (211 loc) · 4.25 KB
/
Copy pathhtml_parser_document_node.c
File metadata and controls
275 lines (211 loc) · 4.25 KB
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
#include <stdlib.h>
#include "html_parser_types.h"
#include "html_parser_assert.h"
#include "html_parser_mem.h"
#include "html_parser_fmt.h"
#include "html_parser_text.h"
#include "html_parser_attribute_list.h"
#include "html_parser_document_node.h"
#define T Node_T
/* type definition */
struct T {
Chunk_E cf;
struct T *parent;
struct T *sibling;
struct T *back;
union {
struct tag {
Tag_E tf; /* { T_OPTNL, T_STRCT, T_VOID } */
const char *name; /* atom */
Attr_list_T *attrs;
struct T *child;
} *tag;
struct content {
const char *text;
} *content;
} type;
};
/* static function prototypes */
static char *print_tag(T *n);
static char *print_content(T *n);
T Node_new_tag(Tag_E tf, const char *name, Attr_list_T *attrs)
{
T node;
struct tag *t;
assert(name);
NEW(node);
node->cf = C_TAGND;
node->parent = NULL;
node->sibling = NULL;
node->back = NULL;
NEW(t);
t->tf = tf;
t->name = name;
t->attrs = attrs;
t->child = NULL;
node->type.tag = t;
return node;
}
T Node_new_content(const char *text)
{
T node;
struct content *c;
assert(text && *text);
NEW(node);
node->cf = C_CNTND;
node->parent = NULL;
node->sibling = NULL;
node->back = NULL;
NEW(c);
c->text = text;
node->type.content = c;
return node;
}
T Node_clone(T n)
{
T cl;
if (C_TAGND == Node_type(n)) {
Attr_list_T *lp = Node_attr_list(n);
Attr_list_T cl_list = Attr_list_clone(*lp);
cl = Node_new_tag(Node_tag_type(n), Node_name(n),
&cl_list);
} else {
const char *tmp = Fmt_string("%s", n->type.content->text);
cl = Node_new_content(tmp);
}
return NULL;
}
Chunk_E Node_type(T n)
{
assert(n);
return n->cf;
}
Tag_E Node_tag_type(T n)
{
assert(n);
assert(n->cf == C_TAGND);
return n->type.tag->tf;
}
int Node_has_child(T *n)
{
int ret = 0;
assert(n && *n);
if (((*n)->cf == C_TAGND) &&
((*n)->type.tag->child != NULL))
ret = 1;
return ret;
}
int Node_has_sibling(T *n)
{
assert(n && *n);
return ((*n)->sibling != NULL);
}
int Node_has_parent(T *n)
{
assert(n && *n);
return((*n)->parent != NULL);
}
int Node_has_back(T *n)
{
assert(n && *n);
return((*n)->back != NULL);
}
T *Node_parent(T *n)
{
assert(n && *n);
return (&(*n)->parent);
}
T *Node_back(T *n)
{
assert(n && *n);
return (&(*n)->back);
}
T *Node_child(T *n) /* user error if node has no child */
{
assert(n && *n);
return (&(*n)->type.tag->child);
}
T *Node_sibling(T *n) /* user error if node has no sibling */
{
assert(n && *n);
return (&((*n)->sibling));
}
const char *Node_name(T n)
{
assert(n);
/*assert((*n)->cf == C_TAGND);*/
return n->type.tag->name;
}
Attr_list_T *Node_attr_list(T n)
{
assert(n);
assert(n->cf == C_TAGND);
return n->type.tag->attrs;
}
T *Node_add_child(T *parent, T *child)
{
assert(parent && child);
(*parent)->type.tag->child = *child; /* child link */
(*child)->back = *parent; /* back link */
Node_add_parent(parent, child); /* parent link */
return child;
}
T *Node_add_sibling(T *first, T *second)
{
assert(first && *first);
assert(second && *second);
(*first)->sibling = *second; /* sibling link */
(*second)->back = *first; /* back link */
return second;
}
T *Node_add_parent(T *parent, T *child)
{
assert(parent && *parent);
assert(child && *child);
(*child)->parent = *parent;
return parent;
}
const char *Node_print(T *n)
{
const char *out;
assert(*n);
if (C_TAGND == Node_type(*n)) {
out = print_tag(n);
} else {
out = print_content(n);
}
return out;
}
void Node_free(T *n)
{
assert(n && *n);
if (C_TAGND == Node_type(*n)) {
/* name is an atom i.e. don't free it */
struct tag *tmp = (*n)->type.tag;
if (tmp->attrs)
Attr_list_free(tmp->attrs);
FREE(tmp);
} else {
struct content *tmp = (*n)->type.content;
FREE(tmp);
}
FREE(*n);
}
static char *print_tag(T *n)
{
Text_T buf = Text_put("<");
buf = Text_cat(buf, Text_put((*n)->type.tag->name));
if ((*n)->type.tag->attrs) {
buf = Text_cat(buf, Text_box(" ", 1));
buf = Text_cat(buf, Text_put(Attr_list_print(*(*n)->type.tag->attrs)));
}
buf = Text_cat(buf, Text_put(">"));
return Text_get(NULL, -1, buf);
}
static char *print_content(T *n)
{
Text_T buf = Text_put("\"");
buf = Text_cat(buf, Text_put((*n)->type.content->text));
buf = Text_cat(buf, Text_put("\""));
return Text_get(NULL, -1, buf);
}