utils.c

filter: Statementstmt 0.0% 0 / 362 Branchbran 0.0% 0 / 208 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 570 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« third_party/modest/source/myurl/url.c ↑ Summary blib/lib/HTML5/DOM/CSS.pm »
1 #include "utils.h"
2  
3 /*
4 async parsing
5 */
6 0 void *html5_dom_mythread_function(void *arg) {
7 0 mythread_context_t *ctx = (mythread_context_t *) arg;
8 0 mythread_t *mythread = ctx->mythread;
9
10 0 mythread_mutex_wait(mythread, ctx->mutex);
11 0 ctx->func(ctx->id, ctx);
12 0 mythread_nanosleep_destroy(ctx->timespec);
13 0 ctx->opt = MyTHREAD_OPT_QUIT;
14 0 mythread_mutex_close(mythread, ctx->mutex);
15
16 0 return NULL;
17 }
18  
19 0 static int html5_dom_fd_write(int fd, const char *data, int size) {
20 0 ▶ if (fd > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd > -1) { 0 0 0
21 #ifdef MyCORE_OS_WINDOWS_NT
22 return _write(fd, data, size);
23 #else
24 0 return write(fd, data, size);
25 #endif
26 }
27 0 return 0;
28 }
29  
30 0 int html5_dom_async_parse(html5_dom_async_result *result) {
31 mystatus_t status;
32
33 // create parser
34 0 html5_dom_parser_t *self = html5_dom_parser_new(&result->opts);
35
36 // init myhtml
37 0 self->myhtml = myhtml_create();
38
39 0 ▶ if (self->opts.threads <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->opts.threads <= 1) { 0 0 0
40 0 status = myhtml_init(self->myhtml, MyHTML_OPTIONS_PARSE_MODE_SINGLE, 1, 0);
41 } else {
42 0 status = myhtml_init(self->myhtml, MyHTML_OPTIONS_DEFAULT, self->opts.threads, 0);
43 }
44
45 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
46 0 html5_dom_parser_free(self);
47 0 result->status = status;
48 0 result->done = true;
49 0 return html5_dom_fd_write(result->fd, "0", 1);
50 }
51
52 // init myhtml tree
53 0 myhtml_tree_t *tree = myhtml_tree_create();
54 0 status = myhtml_tree_init(tree, self->myhtml);
55 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
56 0 myhtml_tree_destroy(tree);
57 0 html5_dom_parser_free(self);
58 0 result->status = status;
59 0 result->done = true;
60 0 return html5_dom_fd_write(result->fd, "0", 1);
61 }
62
63 // detect encoding
64 0 myencoding_t encoding = html5_dom_auto_encoding(&result->opts, (const char **) &result->html, &result->length);
65
66 // apply options to tree
67 0 html5_dom_apply_tree_options(tree, &result->opts);
68
69 // try parse
70 0 status = myhtml_parse(tree, encoding, result->html, result->length);
71
72 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
73 0 myhtml_tree_destroy(tree);
74 0 html5_dom_parser_free(self);
75 0 result->status = status;
76 0 result->done = true;
77 0 return html5_dom_fd_write(result->fd, "0", 1);
78 }
79
80 0 result->done = true;
81 0 result->tree = tree;
82 0 result->parser = self;
83
84 // trigger event
85 0 return html5_dom_fd_write(result->fd, "1", 1);
86 }
87  
88 0 void html5_dom_async_parse_worker(mythread_id_t thread_id, void *arg) {
89 0 mythread_context_t *ctx = (mythread_context_t *) arg;
90 0 html5_dom_async_result *result = (html5_dom_async_result *) ctx->mythread->context;
91 0 html5_dom_async_parse(result);
92 0 }
93  
94 /*
95 parser
96 */
97 0 html5_dom_parser_t *html5_dom_parser_new(html5_dom_options_t *options) {
98 0 html5_dom_parser_t *self = (html5_dom_parser_t *) malloc(sizeof(html5_dom_parser_t));
99 0 memset(self, 0, sizeof(html5_dom_parser_t));
100 0 memcpy(&self->opts, options, sizeof(html5_dom_options_t));
101 0 return self;
102 }
103  
104 0 void *html5_dom_parser_free(html5_dom_parser_t *self) {
105 0 ▶ if (self->myhtml) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->myhtml) { 0 0 0
106 0 myhtml_destroy(self->myhtml);
107 0 self->myhtml = NULL;
108 }
109
110 0 ▶ if (self->mycss_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->mycss_entry) { 0 0 0
111 0 mycss_entry_destroy(self->mycss_entry, 1);
112 0 self->mycss_entry = NULL;
113 }
114
115 0 ▶ if (self->mycss) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->mycss) { 0 0 0
116 0 mycss_destroy(self->mycss, 1);
117 0 self->mycss = NULL;
118 }
119
120 0 ▶ if (self->finder) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->finder) { 0 0 0
121 0 modest_finder_destroy(self->finder, 1);
122 0 self->finder = NULL;
123 }
124
125 0 free(self);
126 0 }
127  
128 0 mystatus_t html5_dom_init_css(html5_dom_parser_t *parser) {
129 0 mystatus_t status = MyCSS_STATUS_OK;
130
131 0 ▶ if (!parser->mycss) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parser->mycss) { 0 0 0
132 0 parser->mycss = mycss_create();
133 0 status = mycss_init(parser->mycss);
134 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
135 0 mycss_destroy(parser->mycss, 1);
136 0 parser->mycss = NULL;
137 0 return status;
138 }
139 }
140
141 0 ▶ if (!parser->mycss_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parser->mycss_entry) { 0 0 0
142 0 parser->mycss_entry = mycss_entry_create();
143 0 status = mycss_entry_init(parser->mycss, parser->mycss_entry);
144 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
145 0 mycss_entry_destroy(parser->mycss_entry, 1);
146 0 mycss_destroy(parser->mycss, 1);
147 0 parser->mycss = NULL;
148 0 parser->mycss_entry = NULL;
149 0 return status;
150 }
151 }
152
153 0 return status;
154 }
155  
156 0 myhtml_tree_node_t *html5_dom_parse_fragment(html5_dom_options_t *opts, myhtml_tree_t *tree, myhtml_tag_id_t tag_id, myhtml_namespace_t ns,
157 const char *text, size_t length, html5_fragment_parts_t *parts, mystatus_t *status_out)
158 {
159 mystatus_t status;
160
161 0 myhtml_t *parser = myhtml_tree_get_myhtml(tree);
162
163 // cteate temorary tree
164 0 myhtml_tree_t *fragment_tree = myhtml_tree_create();
165 0 status = myhtml_tree_init(fragment_tree, parser);
166 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
167 0 *status_out = status;
168 0 myhtml_tree_destroy(tree);
169 0 return NULL;
170 }
171
172 0 html5_dom_apply_tree_options(fragment_tree, opts);
173
174 0 myencoding_t encoding = html5_dom_auto_encoding(opts, &text, &length);
175
176 // parse fragment from text
177 0 status = myhtml_parse_fragment(fragment_tree, encoding, text, length, tag_id, ns);
178 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
179 0 *status_out = status;
180 0 myhtml_tree_destroy(tree);
181 0 return NULL;
182 }
183
184 // clone fragment from temporary tree to persistent tree
185 0 myhtml_tree_node_t *node = html5_dom_recursive_clone_node(tree, myhtml_tree_get_node_html(fragment_tree), parts);
186
187 0 ▶ if (node) {
Branch: 1 branch
BranchTrueFalseTotal
if (node) { 0 0 0
188 0 html5_dom_tree_t *context = (html5_dom_tree_t *) node->tree->context;
189 0 ▶ if (!context->fragment_tag_id)
Branch: 1 branch
BranchTrueFalseTotal
if (!context->fragment_tag_id) 0 0 0
190 0 context->fragment_tag_id = html5_dom_tag_id_by_name(tree, "-fragment", 9, true);
191 0 node->tag_id = context->fragment_tag_id;
192 }
193
194 0 myhtml_tree_destroy(fragment_tree);
195
196 0 *status_out = status;
197
198 0 return node;
199 }
200  
201 0 void html5_dom_apply_tree_options(myhtml_tree_t *tree, html5_dom_options_t *opts) {
202 0 ▶ if (opts->scripts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->scripts) { 0 0 0
203 0 tree->flags |= MyHTML_TREE_FLAGS_SCRIPT;
204 } else {
205 0 tree->flags &= ~MyHTML_TREE_FLAGS_SCRIPT;
206 }
207
208 0 ▶ if (opts->ignore_doctype)
Branch: 1 branch
BranchTrueFalseTotal
if (opts->ignore_doctype) 0 0 0
209 0 tree->parse_flags |= MyHTML_TREE_PARSE_FLAGS_WITHOUT_DOCTYPE_IN_TREE;
210
211 0 ▶ if (opts->ignore_whitespace)
Branch: 1 branch
BranchTrueFalseTotal
if (opts->ignore_whitespace) 0 0 0
212 0 tree->parse_flags |= MyHTML_TREE_PARSE_FLAGS_SKIP_WHITESPACE_TOKEN;
213 0 }
214  
215 /*
216 misc
217 */
218 0 const char *modest_strerror(mystatus_t status) {
219 0 switch (status) {
220 #include "gen/modest_errors.c"
221 }
222 0 ▶ return status ? "UNKNOWN" : "";
Branch: 1 branch
BranchTrueFalseTotal
return status ? "UNKNOWN" : ""; 0 0 0
223 }
224  
225 0 int html5_dom_get_ua_display_prop(myhtml_tree_node_t *node) {
226 0 switch (node->tag_id) {
227 #include "gen/tags_ua_style.c"
228 }
229 0 return TAG_UA_STYLE_INLINE;
230 }
231  
232 0 void html5_dom_rtrim_mystring(mycore_string_t *str, char c) {
233 0 size_t i = str->length;
234 0 ▶ while (i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (i > 0) { 0 0 0
235 0 --i;
236
237 0 ▶ if (str->data[i] != c)
Branch: 1 branch
BranchTrueFalseTotal
if (str->data[i] != c) 0 0 0
238 0 break;
239
240 0 str->data[i] = '\0';
241 0 --str->length;
242 }
243 0 }
244  
245 /*
246 finders & css
247 */
248 0 void _modest_finder_callback_found_with_one_node(modest_finder_t *finder, myhtml_tree_node_t *node,
249 mycss_selectors_list_t *selector_list, mycss_selectors_entry_t *selector, mycss_selectors_specificity_t *spec, void *ctx)
250 {
251 0 myhtml_tree_node_t **result_node = (myhtml_tree_node_t **) ctx;
252 0 ▶ if (!*result_node)
Branch: 1 branch
BranchTrueFalseTotal
if (!*result_node) 0 0 0
253 0 *result_node = node;
254 0 }
255  
256 0 void *html5_node_finder(html5_dom_parser_t *parser, modest_finder_selector_combinator_f func,
257 myhtml_tree_node_t *scope, mycss_selectors_entries_list_t *list, size_t list_size, mystatus_t *status_out, bool one)
258 {
259 0 *status_out = MODEST_STATUS_OK;
260
261 0 ▶ if (!scope)
Branch: 1 branch
BranchTrueFalseTotal
if (!scope) 0 0 0
262 0 return NULL;
263
264 // Init finder
265 mystatus_t status;
266 0 ▶ if (parser->finder) {
Branch: 1 branch
BranchTrueFalseTotal
if (parser->finder) { 0 0 0
267 0 parser->finder = modest_finder_create();
268 0 status = modest_finder_init(parser->finder);
269 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
270 0 *status_out = status;
271 0 modest_finder_destroy(parser->finder, 1);
272 0 return NULL;
273 }
274 }
275
276 0 ▶ if (one) {
Branch: 1 branch
BranchTrueFalseTotal
if (one) { 0 0 0
277 // Process selector entries
278 0 myhtml_tree_node_t *node = NULL;
279 0 ▶ for (size_t i = 0; i < list_size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < list_size; ++i) { 0 0 0
280 0 func(parser->finder, scope, NULL, list[i].entry, &list[i].specificity,
281 _modest_finder_callback_found_with_one_node, &node);
282
283 0 ▶ if (node)
Branch: 1 branch
BranchTrueFalseTotal
if (node) 0 0 0
284 0 break;
285 }
286
287 0 return (void *) node;
288 } else {
289 // Init collection for results
290 0 myhtml_collection_t *collection = myhtml_collection_create(4096, &status);
291 0 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 0 0
292 0 *status_out = MODEST_STATUS_ERROR_MEMORY_ALLOCATION;
293 0 return NULL;
294 }
295
296 // Process selector entries
297 0 ▶ for (size_t i = 0; i < list_size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < list_size; ++i) { 0 0 0
298 0 func(parser->finder, scope, NULL, list[i].entry, &list[i].specificity,
299 modest_finder_callback_found_with_collection, collection);
300 }
301
302 0 return (void *) collection;
303 }
304 }
305  
306 0 modest_finder_selector_combinator_f html5_find_selector_func(const char *c, int combo_len) {
307 0 ▶ if (combo_len == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (combo_len == 2) { 0 0 0
308 0 ▶ if (c[0] == '|' && c[1] == '|')
Branch: 2 branches
BranchTrueFalseTotal
if (c[0] == '|' && c[1] == '|') 0 0 0
if (c[0] == '|' && c[1] == '|') 0 0 0
309 0 return modest_finder_node_combinator_column;
310 0 ▶ if ((c[0] == '>' && c[1] == '>'))
Branch: 2 branches
BranchTrueFalseTotal
if ((c[0] == '>' && c[1] == '>')) 0 0 0
if ((c[0] == '>' && c[1] == '>')) 0 0 0
311 0 return modest_finder_node_combinator_descendant;
312 0 ▶ } else if (combo_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (combo_len == 1) { 0 0 0
313 0 ▶ if (c[0] == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (c[0] == '>') 0 0 0
314 0 return modest_finder_node_combinator_child;
315 0 ▶ if (c[0] == '+')
Branch: 1 branch
BranchTrueFalseTotal
if (c[0] == '+') 0 0 0
316 0 return modest_finder_node_combinator_next_sibling;
317 0 ▶ if (c[0] == '~')
Branch: 1 branch
BranchTrueFalseTotal
if (c[0] == '~') 0 0 0
318 0 return modest_finder_node_combinator_following_sibling;
319 0 ▶ if (c[0] == '^')
Branch: 1 branch
BranchTrueFalseTotal
if (c[0] == '^') 0 0 0
320 0 return modest_finder_node_combinator_begin;
321 }
322 0 return modest_finder_node_combinator_descendant;
323 }
324  
325 0 mycss_selectors_list_t *html5_parse_selector(mycss_entry_t *entry, const char *query, size_t query_len, mystatus_t *status_out) {
326 mystatus_t status;
327
328 0 *status_out = MyCSS_STATUS_OK;
329
330 0 mycss_selectors_list_t *list = mycss_selectors_parse(mycss_entry_selectors(entry), MyENCODING_UTF_8, query, query_len, &status);
331 0 ▶ if (status || list == NULL || (list->flags & MyCSS_SELECTORS_FLAGS_SELECTOR_BAD)) {
Branch: 3 branches
BranchTrueFalseTotal
if (status || list == NULL || (list->flags & MyCSS_SELECTORS_FLAGS_SELECTOR_BAD)) { 0 0 0
if (status || list == NULL || (list->flags & MyCSS_SELECTORS_FLAGS_SELECTOR_BAD)) { 0 0 0
if (status || list == NULL || (list->flags & MyCSS_SELECTORS_FLAGS_SELECTOR_BAD)) { 0 0 0
332 0 ▶ if (list)
Branch: 1 branch
BranchTrueFalseTotal
if (list) 0 0 0
333 0 mycss_selectors_list_destroy(mycss_entry_selectors(entry), list, true);
334 0 *status_out = status;
335 0 return NULL;
336 }
337
338 0 return list;
339 }
340  
341 /*
342 nodes
343 */
344 0 myhtml_tag_id_t html5_dom_tag_id_by_name(myhtml_tree_t *tree, const char *tag_str, size_t tag_len, bool allow_create) {
345 0 const myhtml_tag_context_t *tag_ctx = myhtml_tag_get_by_name(tree->tags, tag_str, tag_len);
346 0 ▶ if (tag_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_ctx) { 0 0 0
347 0 return tag_ctx->id;
348 0 ▶ } else if (allow_create) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (allow_create) { 0 0 0
349 // add custom tag
350 0 return myhtml_tag_add(tree->tags, tag_str, tag_len, MyHTML_TOKENIZER_STATE_DATA, true);
351 }
352 0 return MyHTML_TAG__UNDEF;
353 }
354  
355 // Safe copy node from native or foreign tree
356 0 myhtml_tree_node_t *html5_dom_copy_foreign_node(myhtml_tree_t *tree, myhtml_tree_node_t *node) {
357 // Create new node
358 0 myhtml_tree_node_t *new_node = myhtml_tree_node_create(tree);
359 0 new_node->tag_id = node->tag_id;
360 0 new_node->ns = node->ns;
361
362 // Copy custom tag
363 0 ▶ if (tree != node->tree && node->tag_id >= MyHTML_TAG_LAST_ENTRY) {
Branch: 2 branches
BranchTrueFalseTotal
if (tree != node->tree && node->tag_id >= MyHTML_TAG_LAST_ENTRY) { 0 0 0
if (tree != node->tree && node->tag_id >= MyHTML_TAG_LAST_ENTRY) { 0 0 0
364 0 new_node->tag_id = MyHTML_TAG__UNDEF;
365
366 // Get tag name in foreign tree
367 0 const myhtml_tag_context_t *tag_ctx = myhtml_tag_get_by_id(node->tree->tags, node->tag_id);
368 0 ▶ if (tag_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_ctx) { 0 0 0
369 // Get same tag in native tree
370 0 new_node->tag_id = html5_dom_tag_id_by_name(tree, tag_ctx->name, tag_ctx->name_length, true);
371 }
372 }
373
374 0 ▶ if (node->token) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->token) { 0 0 0
375 // Wait, if node not yet done
376 0 myhtml_token_node_wait_for_done(node->tree->token, node->token);
377
378 // Copy node token
379 0 new_node->token = myhtml_token_node_create(tree->token, tree->mcasync_rules_token_id);
380 0 ▶ if (!new_node->token) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_node->token) { 0 0 0
381 0 myhtml_tree_node_delete(new_node);
382 0 return NULL;
383 }
384
385 0 new_node->token->tag_id = node->token->tag_id;
386 0 new_node->token->type = node->token->type;
387 0 new_node->token->attr_first = NULL;
388 0 new_node->token->attr_last = NULL;
389 0 ▶ new_node->token->raw_begin = tree != node->tree ? 0 : node->token->raw_begin;
Branch: 1 branch
BranchTrueFalseTotal
new_node->token->raw_begin = tree != node->tree ? 0 : node->token->raw_begin; 0 0 0
390 0 ▶ new_node->token->raw_length = tree != node->tree ? 0 : node->token->raw_length;
Branch: 1 branch
BranchTrueFalseTotal
new_node->token->raw_length = tree != node->tree ? 0 : node->token->raw_length; 0 0 0
391 0 ▶ new_node->token->element_begin = tree != node->tree ? 0 : node->token->element_begin;
Branch: 1 branch
BranchTrueFalseTotal
new_node->token->element_begin = tree != node->tree ? 0 : node->token->element_begin; 0 0 0
392 0 ▶ new_node->token->element_length = tree != node->tree ? 0 : node->token->element_length;
Branch: 1 branch
BranchTrueFalseTotal
new_node->token->element_length = tree != node->tree ? 0 : node->token->element_length; 0 0 0
393 0 new_node->token->type = new_node->token->type | MyHTML_TOKEN_TYPE_DONE;
394
395 // Copy text data
396 0 ▶ if (node->token->str.length) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->token->str.length) { 0 0 0
397 0 mycore_string_init(tree->mchar, tree->mchar_node_id, &new_node->token->str, node->token->str.length + 1);
398 0 mycore_string_append(&new_node->token->str, node->token->str.data, node->token->str.length);
399 } else {
400 0 mycore_string_clean_all(&new_node->token->str);
401 }
402
403 // Copy node attributes
404 0 myhtml_token_attr_t *attr = node->token->attr_first;
405 0 ▶ while (attr) {
Branch: 1 branch
BranchTrueFalseTotal
while (attr) { 0 0 0
406 0 myhtml_token_attr_copy(tree->token, attr, new_node->token, tree->mcasync_rules_attr_id);
407 0 attr = attr->next;
408 }
409 }
410
411 0 return new_node;
412 }
413  
414 0 myhtml_tree_node_t *html5_dom_recursive_clone_node(myhtml_tree_t *tree, myhtml_tree_node_t *node, html5_fragment_parts_t *parts) {
415 0 myhtml_tree_node_t *new_node = html5_dom_copy_foreign_node(tree, node);
416 0 myhtml_tree_node_t *child = myhtml_node_child(node);
417
418 0 ▶ if (parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (parts) { 0 0 0
419 0 ▶ if (node == node->tree->node_html)
Branch: 1 branch
BranchTrueFalseTotal
if (node == node->tree->node_html) 0 0 0
420 0 parts->node_html = new_node;
421 0 ▶ else if (node == node->tree->node_head)
Branch: 1 branch
BranchTrueFalseTotal
else if (node == node->tree->node_head) 0 0 0
422 0 parts->node_head = new_node;
423 0 ▶ else if (node == node->tree->node_body)
Branch: 1 branch
BranchTrueFalseTotal
else if (node == node->tree->node_body) 0 0 0
424 0 parts->node_body = new_node;
425 0 ▶ else if (node == node->tree->document)
Branch: 1 branch
BranchTrueFalseTotal
else if (node == node->tree->document) 0 0 0
426 0 parts->document = new_node;
427 }
428
429 0 ▶ while (child) {
Branch: 1 branch
BranchTrueFalseTotal
while (child) { 0 0 0
430 0 myhtml_tree_node_add_child(new_node, html5_dom_recursive_clone_node(tree, child, parts));
431 0 child = myhtml_node_next(child);
432 }
433
434 0 return new_node;
435 }
436  
437 // Try to implements https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute
438 // Using default user-agent box model types for tags instead of real css.
439 0 void html5_dom_recursive_node_inner_text(myhtml_tree_node_t *node, html5_dom_inner_text_state_t *state) {
440 0 ▶ if (node->tag_id == MyHTML_TAG__TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->tag_id == MyHTML_TAG__TEXT) { 0 0 0
441 0 size_t text_len = 0;
442 0 const char *text = myhtml_node_text(node, &text_len);
443
444 0 bool is_empty = true;
445 0 ▶ for (size_t i = 0; i < text_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < text_len; ++i) { 0 0 0
446 // skip CR
447 0 ▶ if (text[i] == '\r')
Branch: 1 branch
BranchTrueFalseTotal
if (text[i] == '\r') 0 0 0
448 0 continue;
449
450 // collapse spaces
451 0 ▶ if (isspace(text[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (isspace(text[i])) { 0 0 0
452 0 ▶ bool skip_spaces = (state->value.length > 0 && state->value.data[state->value.length - 1] == ' ') || state->new_line;
Branch: 3 branches
BranchTrueFalseTotal
bool skip_spaces = (state->value.length > 0 && state->value.data[state->value.length - 1] == ' ') || state->new_line; 0 0 0
bool skip_spaces = (state->value.length > 0 && state->value.data[state->value.length - 1] == ' ') || state->new_line; 0 0 0
bool skip_spaces = (state->value.length > 0 && state->value.data[state->value.length - 1] == ' ') || state->new_line; 0 0 0
453 0 ▶ if (skip_spaces)
Branch: 1 branch
BranchTrueFalseTotal
if (skip_spaces) 0 0 0
454 0 continue;
455 0 mycore_string_append_one(&state->value, ' ');
456 }
457 // save other chars
458 else {
459 0 mycore_string_append_one(&state->value, text[i]);
460 0 is_empty = false;
461 0 state->new_line = false;
462 }
463 }
464
465 0 ▶ if (!is_empty)
Branch: 1 branch
BranchTrueFalseTotal
if (!is_empty) 0 0 0
466 0 state->last_br = false;
467 0 ▶ } else if (node_is_element(node)) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (node_is_element(node)) { 0 0 0
} else if (node_is_element(node)) { 0 0 0
} else if (node_is_element(node)) { 0 0 0
} else if (node_is_element(node)) { 0 0 0
468 // get default box model type for tag
469 0 int display = html5_dom_get_ua_display_prop(node);
470
471 // skip hidden nodes
472 0 ▶ if (display == TAG_UA_STYLE_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (display == TAG_UA_STYLE_NONE) 0 0 0
473 0 return;
474
475 // skip some special nodes
476 0 ▶ switch (node->tag_id) {
Branch: 1 branch
BranchTrueFalseTotal
switch (node->tag_id) { 0 0 0
477 0 case MyHTML_TAG_TEXTAREA:
478 case MyHTML_TAG_INPUT:
479 case MyHTML_TAG_AUDIO:
480 case MyHTML_TAG_VIDEO:
481 0 return;
482 }
483
484 // <br> always inserts \n
485 0 ▶ if (node->tag_id == MyHTML_TAG_BR) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->tag_id == MyHTML_TAG_BR) { 0 0 0
486 0 mycore_string_append_one(&state->value, '\n');
487 0 state->new_line = true;
488 0 state->last_br = true;
489 } else {
490 0 ▶ switch (display) {
Branch: 1 branch
BranchTrueFalseTotal
switch (display) { 0 0 0
491 0 case TAG_UA_STYLE_BLOCK:
492 case TAG_UA_STYLE_TABLE:
493 case TAG_UA_STYLE_TABLE_CAPTION:
494 // if last token - line break, then collapse
495 // if last token - text, then insert new line break
496 0 ▶ if (!state->last_br) {
Branch: 1 branch
BranchTrueFalseTotal
if (!state->last_br) { 0 0 0
497 0 html5_dom_rtrim_mystring(&state->value, ' ');
498 0 mycore_string_append_one(&state->value, '\n');
499 0 state->new_line = true;
500 0 state->last_br = true;
501 }
502 0 break;
503 }
504
505 0 myhtml_tree_node_t *child = myhtml_node_child(node);
506 0 ▶ while (child) {
Branch: 1 branch
BranchTrueFalseTotal
while (child) { 0 0 0
507 0 html5_dom_recursive_node_inner_text(child, state);
508 0 child = myhtml_node_next(child);
509 }
510
511 0 switch (display) {
512 0 case TAG_UA_STYLE_BLOCK:
513 case TAG_UA_STYLE_TABLE:
514 case TAG_UA_STYLE_TABLE_CAPTION:
515 // if last token - line break, then collapse
516 // if last token - text, then insert new line break
517 0 ▶ if (!state->last_br) {
Branch: 1 branch
BranchTrueFalseTotal
if (!state->last_br) { 0 0 0
518 0 html5_dom_rtrim_mystring(&state->value, ' ');
519 0 ▶ if (node->tag_id == MyHTML_TAG_P) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->tag_id == MyHTML_TAG_P) { 0 0 0
520 // chrome inserts two \n after <p>
521 0 mycore_string_append_one(&state->value, '\n');
522 0 mycore_string_append_one(&state->value, '\n');
523 } else {
524 0 mycore_string_append_one(&state->value, '\n');
525 }
526 0 state->new_line = true;
527 0 state->last_br = true;
528 }
529 0 break;
530
531 0 case TAG_UA_STYLE_TABLE_CELL:
532 {
533 0 bool is_last_cell = false;
534 0 myhtml_tree_node_t *cell = myhtml_node_last_child(myhtml_node_parent(node));
535 0 ▶ while (cell) {
Branch: 1 branch
BranchTrueFalseTotal
while (cell) { 0 0 0
536 0 ▶ if (html5_dom_get_ua_display_prop(cell) == TAG_UA_STYLE_TABLE_CELL) {
Branch: 1 branch
BranchTrueFalseTotal
if (html5_dom_get_ua_display_prop(cell) == TAG_UA_STYLE_TABLE_CELL) { 0 0 0
537 0 is_last_cell = cell == node;
538 0 break;
539 }
540 0 cell = myhtml_node_prev(cell);
541 }
542
543 0 ▶ if (!is_last_cell) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_last_cell) { 0 0 0
544 0 html5_dom_rtrim_mystring(&state->value, ' ');
545 0 mycore_string_append_one(&state->value, '\t');
546 }
547
548 0 state->new_line = true;
549 }
550 0 break;
551
552 0 case TAG_UA_STYLE_TABLE_ROW:
553 {
554 0 bool is_last_row = false;
555 0 myhtml_tree_node_t *row = myhtml_node_last_child(myhtml_node_parent(node));
556 0 ▶ while (row) {
Branch: 1 branch
BranchTrueFalseTotal
while (row) { 0 0 0
557 0 ▶ if (html5_dom_get_ua_display_prop(row) == TAG_UA_STYLE_TABLE_ROW) {
Branch: 1 branch
BranchTrueFalseTotal
if (html5_dom_get_ua_display_prop(row) == TAG_UA_STYLE_TABLE_ROW) { 0 0 0
558 0 is_last_row = (row == node);
559 0 break;
560 }
561 0 row = myhtml_node_prev(row);
562 }
563
564 0 ▶ if (!is_last_row) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_last_row) { 0 0 0
565 0 html5_dom_rtrim_mystring(&state->value, ' ');
566 0 mycore_string_append_one(&state->value, '\n');
567 0 state->last_br = true;
568 }
569
570 0 state->new_line = true;
571 }
572 0 break;
573 }
574 }
575 }
576 }
577  
578 // Safe delete nodes only if it has not perl object representation
579 0 void html5_tree_node_delete_recursive(myhtml_tree_node_t *node) {
580 0 ▶ if (!myhtml_node_get_data(node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!myhtml_node_get_data(node)) { 0 0 0
581 0 myhtml_tree_node_t *child = myhtml_node_child(node);
582 0 ▶ if (child) {
Branch: 1 branch
BranchTrueFalseTotal
if (child) { 0 0 0
583 0 ▶ while (child) {
Branch: 1 branch
BranchTrueFalseTotal
while (child) { 0 0 0
584 0 myhtml_tree_node_t *next = myhtml_node_next(child);
585 0 myhtml_tree_node_remove(child);
586 0 html5_tree_node_delete_recursive(child);
587 0 child = next;
588 }
589 }
590 0 myhtml_tree_node_delete(node);
591 }
592 0 }
593  
594 /*
595 attrs
596 */
597 0 void html5_dom_replace_attr_value(myhtml_tree_node_t *node, const char *key, size_t key_len, const char *val, size_t val_len, myencoding_t encoding) {
598 0 myhtml_tree_attr_t *attr = myhtml_attribute_by_key(node, key, key_len);
599 0 ▶ if (attr) { // edit
Branch: 1 branch
BranchTrueFalseTotal
if (attr) { // edit 0 0 0
600 // destroy original value
601 0 mycore_string_destroy(&attr->value, 0);
602
603 // set new value
604 0 mycore_string_init(node->tree->mchar, node->tree->mchar_node_id, &attr->value, (val_len + 1));
605
606 // apply encoding
607 0 ▶ if (encoding == MyENCODING_UTF_8) {
Branch: 1 branch
BranchTrueFalseTotal
if (encoding == MyENCODING_UTF_8) { 0 0 0
608 0 mycore_string_append(&attr->value, val, val_len);
609 } else {
610 0 myencoding_string_append(&attr->value, val, val_len, encoding);
611 }
612 } else { // add new
613 0 myhtml_attribute_add(node, key, key_len, val, val_len, encoding);
614 }
615 0 }
616  
617 /*
618 encoding
619 */
620 0 myencoding_t html5_dom_auto_encoding(html5_dom_options_t *opts, const char **html_str, size_t *html_length) {
621 // Try to determine encoding
622 myencoding_t encoding;
623 0 ▶ if (opts->encoding == MyENCODING_AUTO) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->encoding == MyENCODING_AUTO) { 0 0 0
624 0 encoding = MyENCODING_NOT_DETERMINED;
625 0 ▶ if (*html_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (*html_length) { 0 0 0
626 // Search encoding in meta-tags
627 0 ▶ if (opts->encoding_use_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->encoding_use_meta) { 0 0 0
628 0 size_t size = opts->encoding_prescan_limit < *html_length ? opts->encoding_prescan_limit : *html_length;
629 0 encoding = myencoding_prescan_stream_to_determine_encoding(*html_str, size);
630 }
631
632 0 ▶ if (encoding == MyENCODING_NOT_DETERMINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (encoding == MyENCODING_NOT_DETERMINED) { 0 0 0
633 // Check BOM
634 0 ▶ if (!opts->encoding_use_bom || !myencoding_detect_and_cut_bom(*html_str, *html_length, &encoding, html_str, html_length)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!opts->encoding_use_bom || !myencoding_detect_and_cut_bom(*html_str, *html_length, &encoding, html_str, html_length)) { 0 0 0
if (!opts->encoding_use_bom || !myencoding_detect_and_cut_bom(*html_str, *html_length, &encoding, html_str, html_length)) { 0 0 0
635 // Check heuristic
636 0 ▶ if (!myencoding_detect(*html_str, *html_length, &encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!myencoding_detect(*html_str, *html_length, &encoding)) { 0 0 0
637 // Can't determine encoding, use default
638 0 encoding = opts->default_encoding;
639 }
640 }
641 }
642 } else {
643 0 encoding = opts->default_encoding;
644 }
645 } else {
646 0 encoding = opts->encoding;
647 }
648 0 return encoding;
649 }
« third_party/modest/source/myurl/url.c ↑ Summary blib/lib/HTML5/DOM/CSS.pm »