1 /*
2  * Copyright (C) 2018-2019 Alexander Borisov
3  *
4  * Author: Alexander Borisov <borisov@lexbor.com>
5  */
6 
7 #include "lexbor/html/tree/insertion_mode.h"
8 #include "lexbor/html/tree/open_elements.h"
9 
10 
11 /*
12  * "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th"
13  */
14 lxb_inline bool
lxb_html_tree_insertion_mode_in_select_in_table_ct(lxb_html_tree_t * tree,lxb_html_token_t * token)15 lxb_html_tree_insertion_mode_in_select_in_table_ct(lxb_html_tree_t *tree,
16                                                    lxb_html_token_t *token)
17 {
18     lxb_html_tree_parse_error(tree, token, LXB_HTML_RULES_ERROR_UNTO);
19 
20     lxb_html_tree_open_elements_pop_until_tag_id(tree, LXB_TAG_SELECT,
21                                                  LXB_NS_HTML, true);
22 
23     lxb_html_tree_reset_insertion_mode_appropriately(tree);
24 
25     return false;
26 }
27 
28 /*
29  * "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th"
30  */
31 lxb_inline bool
lxb_html_tree_insertion_mode_in_select_in_table_ct_closed(lxb_html_tree_t * tree,lxb_html_token_t * token)32 lxb_html_tree_insertion_mode_in_select_in_table_ct_closed(lxb_html_tree_t *tree,
33                                                           lxb_html_token_t *token)
34 {
35     lxb_dom_node_t *node;
36 
37     lxb_html_tree_parse_error(tree, token, LXB_HTML_RULES_ERROR_UNCLTO);
38 
39     node = lxb_html_tree_element_in_scope(tree, token->tag_id, LXB_NS_HTML,
40                                           LXB_HTML_TAG_CATEGORY_SCOPE_TABLE);
41     if (node == NULL) {
42         return true;
43     }
44 
45     lxb_html_tree_open_elements_pop_until_tag_id(tree, LXB_TAG_SELECT,
46                                                  LXB_NS_HTML, true);
47 
48     lxb_html_tree_reset_insertion_mode_appropriately(tree);
49 
50     return false;
51 }
52 
53 lxb_inline bool
lxb_html_tree_insertion_mode_in_select_in_table_anything_else(lxb_html_tree_t * tree,lxb_html_token_t * token)54 lxb_html_tree_insertion_mode_in_select_in_table_anything_else(lxb_html_tree_t *tree,
55                                                               lxb_html_token_t *token)
56 {
57     return lxb_html_tree_insertion_mode_in_select(tree, token);
58 }
59 
60 lxb_inline bool
lxb_html_tree_insertion_mode_in_select_in_table_anything_else_closed(lxb_html_tree_t * tree,lxb_html_token_t * token)61 lxb_html_tree_insertion_mode_in_select_in_table_anything_else_closed(lxb_html_tree_t *tree,
62                                                                      lxb_html_token_t *token)
63 {
64     return lxb_html_tree_insertion_mode_in_select_in_table_anything_else(tree,
65                                                                          token);
66 }
67 
68 bool
lxb_html_tree_insertion_mode_in_select_in_table(lxb_html_tree_t * tree,lxb_html_token_t * token)69 lxb_html_tree_insertion_mode_in_select_in_table(lxb_html_tree_t *tree,
70                                                 lxb_html_token_t *token)
71 {
72     if (token->tag_id >= LXB_TAG__LAST_ENTRY) {
73         if (token->type & LXB_HTML_TOKEN_TYPE_CLOSE) {
74             return lxb_html_tree_insertion_mode_in_select_in_table_anything_else_closed(tree,
75                                                                                         token);
76         }
77 
78         return lxb_html_tree_insertion_mode_in_select_in_table_anything_else(tree,
79                                                                              token);
80     }
81 
82     if (token->type & LXB_HTML_TOKEN_TYPE_CLOSE) {
83         switch (token->tag_id) {
84             case LXB_TAG_CAPTION:
85             case LXB_TAG_TABLE:
86             case LXB_TAG_TBODY:
87             case LXB_TAG_TFOOT:
88             case LXB_TAG_THEAD:
89             case LXB_TAG_TR:
90             case LXB_TAG_TH:
91             case LXB_TAG_TD:
92                 return lxb_html_tree_insertion_mode_in_select_in_table_ct_closed(tree,
93                                                                                  token);
94             default:
95                 return lxb_html_tree_insertion_mode_in_select_in_table_anything_else_closed(tree,
96                                                                                             token);
97         }
98     }
99 
100     switch (token->tag_id) {
101         case LXB_TAG_CAPTION:
102         case LXB_TAG_TABLE:
103         case LXB_TAG_TBODY:
104         case LXB_TAG_TFOOT:
105         case LXB_TAG_THEAD:
106         case LXB_TAG_TR:
107         case LXB_TAG_TH:
108         case LXB_TAG_TD:
109             return lxb_html_tree_insertion_mode_in_select_in_table_ct(tree,
110                                                                       token);
111         default:
112             return lxb_html_tree_insertion_mode_in_select_in_table_anything_else(tree,
113                                                                                  token);
114     }
115 }
116