nokogiri 1.10.9 → 1.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +38 -0
  3. data/LICENSE-DEPENDENCIES.md +1632 -1022
  4. data/LICENSE.md +1 -1
  5. data/README.md +190 -95
  6. data/bin/nokogiri +63 -50
  7. data/dependencies.yml +34 -66
  8. data/ext/nokogiri/depend +38 -358
  9. data/ext/nokogiri/extconf.rb +909 -422
  10. data/ext/nokogiri/gumbo.c +610 -0
  11. data/ext/nokogiri/html4_document.c +171 -0
  12. data/ext/nokogiri/html4_element_description.c +299 -0
  13. data/ext/nokogiri/html4_entity_lookup.c +37 -0
  14. data/ext/nokogiri/html4_sax_parser.c +40 -0
  15. data/ext/nokogiri/html4_sax_parser_context.c +98 -0
  16. data/ext/nokogiri/html4_sax_push_parser.c +96 -0
  17. data/ext/nokogiri/libxml2_polyfill.c +114 -0
  18. data/ext/nokogiri/nokogiri.c +258 -105
  19. data/ext/nokogiri/nokogiri.h +207 -90
  20. data/ext/nokogiri/test_global_handlers.c +40 -0
  21. data/ext/nokogiri/xml_attr.c +18 -18
  22. data/ext/nokogiri/xml_attribute_decl.c +22 -22
  23. data/ext/nokogiri/xml_cdata.c +33 -33
  24. data/ext/nokogiri/xml_comment.c +19 -31
  25. data/ext/nokogiri/xml_document.c +499 -323
  26. data/ext/nokogiri/xml_document_fragment.c +17 -36
  27. data/ext/nokogiri/xml_dtd.c +65 -59
  28. data/ext/nokogiri/xml_element_content.c +63 -55
  29. data/ext/nokogiri/xml_element_decl.c +31 -31
  30. data/ext/nokogiri/xml_encoding_handler.c +54 -21
  31. data/ext/nokogiri/xml_entity_decl.c +37 -35
  32. data/ext/nokogiri/xml_entity_reference.c +17 -19
  33. data/ext/nokogiri/xml_namespace.c +131 -61
  34. data/ext/nokogiri/xml_node.c +1429 -723
  35. data/ext/nokogiri/xml_node_set.c +257 -225
  36. data/ext/nokogiri/xml_processing_instruction.c +18 -20
  37. data/ext/nokogiri/xml_reader.c +340 -231
  38. data/ext/nokogiri/xml_relax_ng.c +87 -99
  39. data/ext/nokogiri/xml_sax_parser.c +269 -176
  40. data/ext/nokogiri/xml_sax_parser_context.c +286 -152
  41. data/ext/nokogiri/xml_sax_push_parser.c +111 -64
  42. data/ext/nokogiri/xml_schema.c +132 -140
  43. data/ext/nokogiri/xml_syntax_error.c +52 -23
  44. data/ext/nokogiri/xml_text.c +37 -30
  45. data/ext/nokogiri/xml_xpath_context.c +373 -185
  46. data/ext/nokogiri/xslt_stylesheet.c +342 -191
  47. data/gumbo-parser/CHANGES.md +63 -0
  48. data/gumbo-parser/Makefile +129 -0
  49. data/gumbo-parser/THANKS +27 -0
  50. data/gumbo-parser/src/Makefile +34 -0
  51. data/gumbo-parser/src/README.md +41 -0
  52. data/gumbo-parser/src/ascii.c +75 -0
  53. data/gumbo-parser/src/ascii.h +115 -0
  54. data/gumbo-parser/src/attribute.c +42 -0
  55. data/gumbo-parser/src/attribute.h +17 -0
  56. data/gumbo-parser/src/char_ref.c +22225 -0
  57. data/gumbo-parser/src/char_ref.h +29 -0
  58. data/gumbo-parser/src/char_ref.rl +2154 -0
  59. data/gumbo-parser/src/error.c +658 -0
  60. data/gumbo-parser/src/error.h +152 -0
  61. data/gumbo-parser/src/foreign_attrs.c +103 -0
  62. data/gumbo-parser/src/foreign_attrs.gperf +27 -0
  63. data/gumbo-parser/src/insertion_mode.h +33 -0
  64. data/gumbo-parser/src/macros.h +91 -0
  65. data/gumbo-parser/src/nokogiri_gumbo.h +953 -0
  66. data/gumbo-parser/src/parser.c +4932 -0
  67. data/gumbo-parser/src/parser.h +41 -0
  68. data/gumbo-parser/src/replacement.h +33 -0
  69. data/gumbo-parser/src/string_buffer.c +103 -0
  70. data/gumbo-parser/src/string_buffer.h +68 -0
  71. data/gumbo-parser/src/string_piece.c +48 -0
  72. data/gumbo-parser/src/svg_attrs.c +174 -0
  73. data/gumbo-parser/src/svg_attrs.gperf +77 -0
  74. data/gumbo-parser/src/svg_tags.c +137 -0
  75. data/gumbo-parser/src/svg_tags.gperf +55 -0
  76. data/gumbo-parser/src/tag.c +223 -0
  77. data/gumbo-parser/src/tag_lookup.c +382 -0
  78. data/gumbo-parser/src/tag_lookup.gperf +170 -0
  79. data/gumbo-parser/src/tag_lookup.h +13 -0
  80. data/gumbo-parser/src/token_buffer.c +79 -0
  81. data/gumbo-parser/src/token_buffer.h +71 -0
  82. data/gumbo-parser/src/token_type.h +17 -0
  83. data/gumbo-parser/src/tokenizer.c +3464 -0
  84. data/gumbo-parser/src/tokenizer.h +112 -0
  85. data/gumbo-parser/src/tokenizer_states.h +339 -0
  86. data/gumbo-parser/src/utf8.c +245 -0
  87. data/gumbo-parser/src/utf8.h +164 -0
  88. data/gumbo-parser/src/util.c +66 -0
  89. data/gumbo-parser/src/util.h +34 -0
  90. data/gumbo-parser/src/vector.c +111 -0
  91. data/gumbo-parser/src/vector.h +45 -0
  92. data/lib/nokogiri/class_resolver.rb +67 -0
  93. data/lib/nokogiri/css/node.rb +14 -8
  94. data/lib/nokogiri/css/parser.rb +399 -377
  95. data/lib/nokogiri/css/parser.y +250 -245
  96. data/lib/nokogiri/css/parser_extras.rb +16 -71
  97. data/lib/nokogiri/css/selector_cache.rb +38 -0
  98. data/lib/nokogiri/css/syntax_error.rb +3 -1
  99. data/lib/nokogiri/css/tokenizer.rb +7 -5
  100. data/lib/nokogiri/css/tokenizer.rex +11 -9
  101. data/lib/nokogiri/css/xpath_visitor.rb +242 -96
  102. data/lib/nokogiri/css.rb +122 -17
  103. data/lib/nokogiri/decorators/slop.rb +11 -11
  104. data/lib/nokogiri/encoding_handler.rb +57 -0
  105. data/lib/nokogiri/extension.rb +32 -0
  106. data/lib/nokogiri/gumbo.rb +15 -0
  107. data/lib/nokogiri/html.rb +38 -27
  108. data/lib/nokogiri/{html → html4}/builder.rb +4 -2
  109. data/lib/nokogiri/html4/document.rb +235 -0
  110. data/lib/nokogiri/html4/document_fragment.rb +166 -0
  111. data/lib/nokogiri/{html → html4}/element_description.rb +3 -1
  112. data/lib/nokogiri/html4/element_description_defaults.rb +2040 -0
  113. data/lib/nokogiri/html4/encoding_reader.rb +121 -0
  114. data/lib/nokogiri/{html → html4}/entity_lookup.rb +4 -2
  115. data/lib/nokogiri/html4/sax/parser.rb +48 -0
  116. data/lib/nokogiri/html4/sax/parser_context.rb +15 -0
  117. data/lib/nokogiri/{html → html4}/sax/push_parser.rb +12 -11
  118. data/lib/nokogiri/html4.rb +42 -0
  119. data/lib/nokogiri/html5/builder.rb +40 -0
  120. data/lib/nokogiri/html5/document.rb +199 -0
  121. data/lib/nokogiri/html5/document_fragment.rb +200 -0
  122. data/lib/nokogiri/html5/node.rb +103 -0
  123. data/lib/nokogiri/html5.rb +368 -0
  124. data/lib/nokogiri/jruby/dependencies.rb +3 -0
  125. data/lib/nokogiri/jruby/nokogiri_jars.rb +43 -0
  126. data/lib/nokogiri/syntax_error.rb +2 -0
  127. data/lib/nokogiri/version/constant.rb +6 -0
  128. data/lib/nokogiri/version/info.rb +224 -0
  129. data/lib/nokogiri/version.rb +3 -108
  130. data/lib/nokogiri/xml/attr.rb +55 -3
  131. data/lib/nokogiri/xml/attribute_decl.rb +6 -2
  132. data/lib/nokogiri/xml/builder.rb +83 -35
  133. data/lib/nokogiri/xml/cdata.rb +3 -1
  134. data/lib/nokogiri/xml/character_data.rb +2 -0
  135. data/lib/nokogiri/xml/document.rb +359 -130
  136. data/lib/nokogiri/xml/document_fragment.rb +170 -54
  137. data/lib/nokogiri/xml/dtd.rb +4 -2
  138. data/lib/nokogiri/xml/element_content.rb +12 -2
  139. data/lib/nokogiri/xml/element_decl.rb +6 -2
  140. data/lib/nokogiri/xml/entity_decl.rb +7 -3
  141. data/lib/nokogiri/xml/entity_reference.rb +2 -0
  142. data/lib/nokogiri/xml/namespace.rb +44 -0
  143. data/lib/nokogiri/xml/node/save_options.rb +23 -8
  144. data/lib/nokogiri/xml/node.rb +1168 -420
  145. data/lib/nokogiri/xml/node_set.rb +145 -67
  146. data/lib/nokogiri/xml/notation.rb +13 -0
  147. data/lib/nokogiri/xml/parse_options.rb +145 -52
  148. data/lib/nokogiri/xml/pp/character_data.rb +9 -6
  149. data/lib/nokogiri/xml/pp/node.rb +47 -30
  150. data/lib/nokogiri/xml/pp.rb +4 -2
  151. data/lib/nokogiri/xml/processing_instruction.rb +4 -1
  152. data/lib/nokogiri/xml/reader.rb +68 -41
  153. data/lib/nokogiri/xml/relax_ng.rb +60 -17
  154. data/lib/nokogiri/xml/sax/document.rb +198 -111
  155. data/lib/nokogiri/xml/sax/parser.rb +144 -67
  156. data/lib/nokogiri/xml/sax/parser_context.rb +119 -6
  157. data/lib/nokogiri/xml/sax/push_parser.rb +9 -5
  158. data/lib/nokogiri/xml/sax.rb +54 -4
  159. data/lib/nokogiri/xml/schema.rb +116 -39
  160. data/lib/nokogiri/xml/searchable.rb +139 -95
  161. data/lib/nokogiri/xml/syntax_error.rb +29 -5
  162. data/lib/nokogiri/xml/text.rb +2 -0
  163. data/lib/nokogiri/xml/xpath/syntax_error.rb +4 -2
  164. data/lib/nokogiri/xml/xpath.rb +15 -4
  165. data/lib/nokogiri/xml/xpath_context.rb +15 -4
  166. data/lib/nokogiri/xml.rb +45 -55
  167. data/lib/nokogiri/xslt/stylesheet.rb +32 -8
  168. data/lib/nokogiri/xslt.rb +103 -30
  169. data/lib/nokogiri.rb +59 -75
  170. data/lib/xsd/xmlparser/nokogiri.rb +32 -29
  171. data/patches/libxml2/0009-allow-wildcard-namespaces.patch +77 -0
  172. data/patches/libxml2/0010-update-config.guess-and-config.sub-for-libxml2.patch +224 -0
  173. data/patches/libxml2/0011-rip-out-libxml2-s-libc_single_threaded-support.patch +30 -0
  174. data/patches/libxml2/0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch +244 -0
  175. data/patches/libxslt/0001-update-config.guess-and-config.sub-for-libxslt.patch +224 -0
  176. data/ports/archives/libxml2-2.13.6.tar.xz +0 -0
  177. data/ports/archives/libxslt-1.1.42.tar.xz +0 -0
  178. metadata +123 -295
  179. data/ext/nokogiri/html_document.c +0 -170
  180. data/ext/nokogiri/html_document.h +0 -10
  181. data/ext/nokogiri/html_element_description.c +0 -279
  182. data/ext/nokogiri/html_element_description.h +0 -10
  183. data/ext/nokogiri/html_entity_lookup.c +0 -32
  184. data/ext/nokogiri/html_entity_lookup.h +0 -8
  185. data/ext/nokogiri/html_sax_parser_context.c +0 -116
  186. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  187. data/ext/nokogiri/html_sax_push_parser.c +0 -87
  188. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  189. data/ext/nokogiri/xml_attr.h +0 -9
  190. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  191. data/ext/nokogiri/xml_cdata.h +0 -9
  192. data/ext/nokogiri/xml_comment.h +0 -9
  193. data/ext/nokogiri/xml_document.h +0 -23
  194. data/ext/nokogiri/xml_document_fragment.h +0 -10
  195. data/ext/nokogiri/xml_dtd.h +0 -10
  196. data/ext/nokogiri/xml_element_content.h +0 -10
  197. data/ext/nokogiri/xml_element_decl.h +0 -9
  198. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  199. data/ext/nokogiri/xml_entity_decl.h +0 -10
  200. data/ext/nokogiri/xml_entity_reference.h +0 -9
  201. data/ext/nokogiri/xml_io.c +0 -61
  202. data/ext/nokogiri/xml_io.h +0 -11
  203. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  204. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  205. data/ext/nokogiri/xml_namespace.h +0 -14
  206. data/ext/nokogiri/xml_node.h +0 -13
  207. data/ext/nokogiri/xml_node_set.h +0 -12
  208. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  209. data/ext/nokogiri/xml_reader.h +0 -10
  210. data/ext/nokogiri/xml_relax_ng.h +0 -9
  211. data/ext/nokogiri/xml_sax_parser.h +0 -39
  212. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  213. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  214. data/ext/nokogiri/xml_schema.h +0 -9
  215. data/ext/nokogiri/xml_syntax_error.h +0 -13
  216. data/ext/nokogiri/xml_text.h +0 -9
  217. data/ext/nokogiri/xml_xpath_context.h +0 -10
  218. data/ext/nokogiri/xslt_stylesheet.h +0 -14
  219. data/lib/nokogiri/html/document.rb +0 -335
  220. data/lib/nokogiri/html/document_fragment.rb +0 -49
  221. data/lib/nokogiri/html/element_description_defaults.rb +0 -671
  222. data/lib/nokogiri/html/sax/parser.rb +0 -62
  223. data/lib/nokogiri/html/sax/parser_context.rb +0 -16
  224. data/patches/libxml2/0001-Revert-Do-not-URI-escape-in-server-side-includes.patch +0 -78
  225. data/patches/libxml2/0004-libxml2.la-is-in-top_builddir.patch +0 -25
  226. data/patches/libxml2/0005-Fix-infinite-loop-in-xmlStringLenDecodeEntities.patch +0 -32
  227. data/ports/archives/libxml2-2.9.10.tar.gz +0 -0
  228. data/ports/archives/libxslt-1.1.34.tar.gz +0 -0
  229. /data/patches/libxml2/{0002-Remove-script-macro-support.patch → 0001-Remove-script-macro-support.patch} +0 -0
  230. /data/patches/libxml2/{0003-Update-entities-to-remove-handling-of-ssi.patch → 0002-Update-entities-to-remove-handling-of-ssi.patch} +0 -0
@@ -0,0 +1,4932 @@
1
+ /*
2
+ Copyright 2017-2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include <assert.h>
19
+ #include <stdarg.h>
20
+ #include <stdint.h>
21
+ #include <stdlib.h>
22
+ #include <string.h>
23
+
24
+ #include "ascii.h"
25
+ #include "attribute.h"
26
+ #include "error.h"
27
+ #include "nokogiri_gumbo.h"
28
+ #include "insertion_mode.h"
29
+ #include "macros.h"
30
+ #include "parser.h"
31
+ #include "replacement.h"
32
+ #include "tokenizer.h"
33
+ #include "tokenizer_states.h"
34
+ #include "token_buffer.h"
35
+ #include "utf8.h"
36
+ #include "util.h"
37
+ #include "vector.h"
38
+
39
+ typedef uint8_t TagSet[GUMBO_TAG_LAST + 1];
40
+ #define TAG(tag) [GUMBO_TAG_##tag] = (1 << GUMBO_NAMESPACE_HTML)
41
+ #define TAG_SVG(tag) [GUMBO_TAG_##tag] = (1 << GUMBO_NAMESPACE_SVG)
42
+ #define TAG_MATHML(tag) [GUMBO_TAG_##tag] = (1 << GUMBO_NAMESPACE_MATHML)
43
+
44
+ #define GUMBO_EMPTY_SOURCE_POSITION_INIT { .line = 0, .column = 0, .offset = 0 }
45
+ #define kGumboEmptySourcePosition (const GumboSourcePosition) \
46
+ GUMBO_EMPTY_SOURCE_POSITION_INIT
47
+
48
+ const GumboOptions kGumboDefaultOptions = {
49
+ .tab_stop = 8,
50
+ .stop_on_first_error = false,
51
+ .max_attributes = 400,
52
+ .max_tree_depth = 400,
53
+ .max_errors = -1,
54
+ .fragment_context = NULL,
55
+ .fragment_namespace = GUMBO_NAMESPACE_HTML,
56
+ .fragment_encoding = NULL,
57
+ .quirks_mode = GUMBO_DOCTYPE_NO_QUIRKS,
58
+ .fragment_context_has_form_ancestor = false,
59
+ .parse_noscript_content_as_text = false,
60
+ };
61
+
62
+ #define STRING(s) {.data = s, .length = sizeof(s) - 1}
63
+ #define TERMINATOR {.data = NULL, .length = 0}
64
+
65
+ // The doctype arrays have an explicit terminator because we want to pass them
66
+ // to a helper function, and passing them as a pointer discards sizeof
67
+ // information. The SVG arrays are used only by one-off functions, and so loops
68
+ // over them use sizeof directly instead of a terminator.
69
+
70
+ static const GumboStringPiece kQuirksModePublicIdPrefixes[] = {
71
+ STRING("+//Silmaril//dtd html Pro v0r11 19970101//"),
72
+ STRING("-//AdvaSoft Ltd//DTD HTML 3.0 asWedit + extensions//"),
73
+ STRING("-//AS//DTD HTML 3.0 asWedit + extensions//"),
74
+ STRING("-//IETF//DTD HTML 2.0 Level 1//"),
75
+ STRING("-//IETF//DTD HTML 2.0 Level 2//"),
76
+ STRING("-//IETF//DTD HTML 2.0 Strict Level 1//"),
77
+ STRING("-//IETF//DTD HTML 2.0 Strict Level 2//"),
78
+ STRING("-//IETF//DTD HTML 2.0 Strict//"),
79
+ STRING("-//IETF//DTD HTML 2.0//"),
80
+ STRING("-//IETF//DTD HTML 2.1E//"),
81
+ STRING("-//IETF//DTD HTML 3.0//"),
82
+ STRING("-//IETF//DTD HTML 3.2 Final//"),
83
+ STRING("-//IETF//DTD HTML 3.2//"),
84
+ STRING("-//IETF//DTD HTML 3//"),
85
+ STRING("-//IETF//DTD HTML Level 0//"),
86
+ STRING("-//IETF//DTD HTML Level 1//"),
87
+ STRING("-//IETF//DTD HTML Level 2//"),
88
+ STRING("-//IETF//DTD HTML Level 3//"),
89
+ STRING("-//IETF//DTD HTML Strict Level 0//"),
90
+ STRING("-//IETF//DTD HTML Strict Level 1//"),
91
+ STRING("-//IETF//DTD HTML Strict Level 2//"),
92
+ STRING("-//IETF//DTD HTML Strict Level 3//"),
93
+ STRING("-//IETF//DTD HTML Strict//"),
94
+ STRING("-//IETF//DTD HTML//"),
95
+ STRING("-//Metrius//DTD Metrius Presentational//"),
96
+ STRING("-//Microsoft//DTD Internet Explorer 2.0 HTML Strict//"),
97
+ STRING("-//Microsoft//DTD Internet Explorer 2.0 HTML//"),
98
+ STRING("-//Microsoft//DTD Internet Explorer 2.0 Tables//"),
99
+ STRING("-//Microsoft//DTD Internet Explorer 3.0 HTML Strict//"),
100
+ STRING("-//Microsoft//DTD Internet Explorer 3.0 HTML//"),
101
+ STRING("-//Microsoft//DTD Internet Explorer 3.0 Tables//"),
102
+ STRING("-//Netscape Comm. Corp.//DTD HTML//"),
103
+ STRING("-//Netscape Comm. Corp.//DTD Strict HTML//"),
104
+ STRING("-//O'Reilly and Associates//DTD HTML 2.0//"),
105
+ STRING("-//O'Reilly and Associates//DTD HTML Extended 1.0//"),
106
+ STRING("-//O'Reilly and Associates//DTD HTML Extended Relaxed 1.0//"),
107
+ STRING(
108
+ "-//SoftQuad Software//DTD HoTMetaL PRO 6.0::19990601::)"
109
+ "extensions to HTML 4.0//"),
110
+ STRING(
111
+ "-//SoftQuad//DTD HoTMetaL PRO 4.0::19971010::"
112
+ "extensions to HTML 4.0//"),
113
+ STRING("-//Spyglass//DTD HTML 2.0 Extended//"),
114
+ STRING("-//SQ//DTD HTML 2.0 HoTMetaL + extensions//"),
115
+ STRING("-//Sun Microsystems Corp.//DTD HotJava HTML//"),
116
+ STRING("-//Sun Microsystems Corp.//DTD HotJava Strict HTML//"),
117
+ STRING("-//W3C//DTD HTML 3 1995-03-24//"),
118
+ STRING("-//W3C//DTD HTML 3.2 Draft//"),
119
+ STRING("-//W3C//DTD HTML 3.2 Final//"),
120
+ STRING("-//W3C//DTD HTML 3.2//"),
121
+ STRING("-//W3C//DTD HTML 3.2S Draft//"),
122
+ STRING("-//W3C//DTD HTML 4.0 Frameset//"),
123
+ STRING("-//W3C//DTD HTML 4.0 Transitional//"),
124
+ STRING("-//W3C//DTD HTML Experimental 19960712//"),
125
+ STRING("-//W3C//DTD HTML Experimental 970421//"),
126
+ STRING("-//W3C//DTD W3 HTML//"),
127
+ STRING("-//W3O//DTD W3 HTML 3.0//"),
128
+ STRING("-//WebTechs//DTD Mozilla HTML 2.0//"),
129
+ STRING("-//WebTechs//DTD Mozilla HTML//"),
130
+ TERMINATOR
131
+ };
132
+
133
+ static const GumboStringPiece kQuirksModePublicIdExactMatches[] = {
134
+ STRING("-//W3O//DTD W3 HTML Strict 3.0//EN//"),
135
+ STRING("-/W3C/DTD HTML 4.0 Transitional/EN"),
136
+ STRING("HTML"),
137
+ TERMINATOR
138
+ };
139
+
140
+ static const GumboStringPiece kQuirksModeSystemIdExactMatches[] = {
141
+ STRING("http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"),
142
+ TERMINATOR
143
+ };
144
+
145
+ static const GumboStringPiece kLimitedQuirksPublicIdPrefixes[] = {
146
+ STRING("-//W3C//DTD XHTML 1.0 Frameset//"),
147
+ STRING("-//W3C//DTD XHTML 1.0 Transitional//"),
148
+ TERMINATOR
149
+ };
150
+
151
+ static const GumboStringPiece kSystemIdDependentPublicIdPrefixes[] = {
152
+ STRING("-//W3C//DTD HTML 4.01 Frameset//"),
153
+ STRING("-//W3C//DTD HTML 4.01 Transitional//"),
154
+ TERMINATOR
155
+ };
156
+
157
+ // Indexed by GumboNamespaceEnum; keep in sync with that.
158
+ static const char* kLegalXmlns[] = {
159
+ "http://www.w3.org/1999/xhtml",
160
+ "http://www.w3.org/2000/svg",
161
+ "http://www.w3.org/1998/Math/MathML"
162
+ };
163
+
164
+ // The "scope marker" for the list of active formatting elements. We use a
165
+ // pointer to this as a generic marker element, since the particular element
166
+ // scope doesn't matter.
167
+ static const GumboNode kActiveFormattingScopeMarker;
168
+
169
+ // The tag_is and tag_in function use true & false to denote start & end tags,
170
+ // but for readability, we define constants for them here.
171
+ static const bool kStartTag = true;
172
+ static const bool kEndTag = false;
173
+
174
+ // Because GumboStringPieces are immutable, we can't insert a character directly
175
+ // into a text node. Instead, we accumulate all pending characters here and
176
+ // flush them out to a text node whenever a new element is inserted.
177
+ //
178
+ // https://html.spec.whatwg.org/multipage/parsing.html#insert-a-character
179
+ typedef struct _TextNodeBufferState {
180
+ // The accumulated text to be inserted into the current text node.
181
+ GumboStringBuffer _buffer;
182
+
183
+ // A pointer to the original text represented by this text node. Note that
184
+ // because of foster parenting and other strange DOM manipulations, this may
185
+ // include other non-text HTML tags in it; it is defined as the span of
186
+ // original text from the first character in this text node to the last
187
+ // character in this text node.
188
+ const char* _start_original_text;
189
+
190
+ // The source position of the start of this text node.
191
+ GumboSourcePosition _start_position;
192
+
193
+ // The type of node that will be inserted (TEXT, CDATA, or WHITESPACE).
194
+ GumboNodeType _type;
195
+ } TextNodeBufferState;
196
+
197
+ typedef struct GumboInternalParserState {
198
+ // https://html.spec.whatwg.org/multipage/parsing.html#insertion-mode
199
+ GumboInsertionMode _insertion_mode;
200
+
201
+ // Used for run_generic_parsing_algorithm, which needs to switch back to the
202
+ // original insertion mode at its conclusion.
203
+ GumboInsertionMode _original_insertion_mode;
204
+
205
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-stack-of-open-elements
206
+ GumboVector /*GumboNode*/ _open_elements;
207
+
208
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-list-of-active-formatting-elements
209
+ GumboVector /*GumboNode*/ _active_formatting_elements;
210
+
211
+ // The stack of template insertion modes.
212
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-insertion-mode
213
+ GumboVector /*InsertionMode*/ _template_insertion_modes;
214
+
215
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-element-pointers
216
+ GumboNode* _head_element;
217
+ GumboNode* _form_element;
218
+
219
+ // The element used as fragment context when parsing in fragment mode
220
+ GumboNode* _fragment_ctx;
221
+
222
+ // The flag for when the spec says "Reprocess the current token in..."
223
+ bool _reprocess_current_token;
224
+
225
+ // The flag for "acknowledge the token's self-closing flag".
226
+ bool _self_closing_flag_acknowledged;
227
+
228
+ // The "frameset-ok" flag from the spec.
229
+ bool _frameset_ok;
230
+
231
+ // The flag for "If the next token is a LINE FEED, ignore that token...".
232
+ bool _ignore_next_linefeed;
233
+
234
+ // The flag for "whenever a node would be inserted into the current node, it
235
+ // must instead be foster parented". This is used for misnested table
236
+ // content, which needs to be handled according to "in body" rules yet foster
237
+ // parented outside of the table.
238
+ // It would perhaps be more explicit to have this as a parameter to
239
+ // handle_in_body and insert_element, but given how special-purpose this is
240
+ // and the number of call-sites that would need to take the extra parameter,
241
+ // it's easier just to have a state flag.
242
+ bool _foster_parent_insertions;
243
+
244
+ // The accumulated text node buffer state.
245
+ TextNodeBufferState _text_node;
246
+
247
+ // The accumulated character tokens in tables for error purposes.
248
+ GumboCharacterTokenBuffer _table_character_tokens;
249
+
250
+ // The current token.
251
+ GumboToken* _current_token;
252
+
253
+ // The way that the spec is written, the </body> and </html> tags are *always*
254
+ // implicit, because encountering one of those tokens merely switches the
255
+ // insertion mode out of "in body". So we have individual state flags for
256
+ // those end tags that are then inspected by pop_current_node when the <body>
257
+ // and <html> nodes are popped to set the GUMBO_INSERTION_IMPLICIT_END_TAG
258
+ // flag appropriately.
259
+ bool _closed_body_tag;
260
+ bool _closed_html_tag;
261
+ } GumboParserState;
262
+
263
+ static bool token_has_attribute(const GumboToken* token, const char* name) {
264
+ assert(token->type == GUMBO_TOKEN_START_TAG);
265
+ return gumbo_get_attribute(&token->v.start_tag.attributes, name) != NULL;
266
+ }
267
+
268
+ // Checks if the value of the specified attribute is a case-insensitive match
269
+ // for the specified string.
270
+ static bool attribute_matches (
271
+ const GumboVector* attributes,
272
+ const char* name,
273
+ const char* value
274
+ ) {
275
+ const GumboAttribute* attr = gumbo_get_attribute(attributes, name);
276
+ return attr ? gumbo_ascii_strcasecmp(value, attr->value) == 0 : false;
277
+ }
278
+
279
+ // Checks if the value of the specified attribute is a case-sensitive match
280
+ // for the specified string.
281
+ static bool attribute_matches_case_sensitive (
282
+ const GumboVector* attributes,
283
+ const char* name,
284
+ const char* value
285
+ ) {
286
+ const GumboAttribute* attr = gumbo_get_attribute(attributes, name);
287
+ return attr ? strcmp(value, attr->value) == 0 : false;
288
+ }
289
+
290
+ // Checks if the specified attribute vectors are identical.
291
+ static bool all_attributes_match (
292
+ const GumboVector* attr1,
293
+ const GumboVector* attr2
294
+ ) {
295
+ unsigned int num_unmatched_attr2_elements = attr2->length;
296
+ for (unsigned int i = 0; i < attr1->length; ++i) {
297
+ const GumboAttribute* attr = attr1->data[i];
298
+ if (attribute_matches_case_sensitive(attr2, attr->name, attr->value)) {
299
+ --num_unmatched_attr2_elements;
300
+ } else {
301
+ return false;
302
+ }
303
+ }
304
+ return num_unmatched_attr2_elements == 0;
305
+ }
306
+
307
+ static void set_frameset_not_ok(GumboParser* parser) {
308
+ gumbo_debug("Setting frameset_ok to false.\n");
309
+ parser->_parser_state->_frameset_ok = false;
310
+ }
311
+
312
+ static GumboNode* create_node(GumboNodeType type) {
313
+ GumboNode* node = gumbo_alloc(sizeof(GumboNode));
314
+ node->parent = NULL;
315
+ node->index_within_parent = -1;
316
+ node->type = type;
317
+ node->parse_flags = GUMBO_INSERTION_NORMAL;
318
+ return node;
319
+ }
320
+
321
+ static GumboNode* new_document_node(void) {
322
+ GumboNode* document_node = create_node(GUMBO_NODE_DOCUMENT);
323
+ document_node->parse_flags = GUMBO_INSERTION_BY_PARSER;
324
+ gumbo_vector_init(1, &document_node->v.document.children);
325
+
326
+ // Must be initialized explicitly, as there's no guarantee that we'll see a
327
+ // doc type token.
328
+ GumboDocument* document = &document_node->v.document;
329
+ document->has_doctype = false;
330
+ document->name = NULL;
331
+ document->public_identifier = NULL;
332
+ document->system_identifier = NULL;
333
+ document->doc_type_quirks_mode = GUMBO_DOCTYPE_NO_QUIRKS;
334
+ return document_node;
335
+ }
336
+
337
+ static void output_init(GumboParser* parser) {
338
+ GumboOutput* output = gumbo_alloc(sizeof(GumboOutput));
339
+ output->root = NULL;
340
+ output->document = new_document_node();
341
+ output->document_error = false;
342
+ output->status = GUMBO_STATUS_OK;
343
+ parser->_output = output;
344
+ gumbo_init_errors(parser);
345
+ }
346
+
347
+ static void parser_state_init(GumboParser* parser) {
348
+ GumboParserState* parser_state = gumbo_alloc(sizeof(GumboParserState));
349
+ parser_state->_insertion_mode = GUMBO_INSERTION_MODE_INITIAL;
350
+ parser_state->_reprocess_current_token = false;
351
+ parser_state->_frameset_ok = true;
352
+ parser_state->_ignore_next_linefeed = false;
353
+ parser_state->_foster_parent_insertions = false;
354
+ parser_state->_text_node._type = GUMBO_NODE_WHITESPACE;
355
+ gumbo_string_buffer_init(&parser_state->_text_node._buffer);
356
+ gumbo_character_token_buffer_init(&parser_state->_table_character_tokens);
357
+ gumbo_vector_init(10, &parser_state->_open_elements);
358
+ gumbo_vector_init(5, &parser_state->_active_formatting_elements);
359
+ gumbo_vector_init(5, &parser_state->_template_insertion_modes);
360
+ parser_state->_head_element = NULL;
361
+ parser_state->_form_element = NULL;
362
+ parser_state->_fragment_ctx = NULL;
363
+ parser_state->_current_token = NULL;
364
+ parser_state->_closed_body_tag = false;
365
+ parser_state->_closed_html_tag = false;
366
+ parser->_parser_state = parser_state;
367
+ }
368
+
369
+ typedef void (*TreeTraversalCallback)(GumboNode* node);
370
+
371
+ static void tree_traverse(GumboNode* node, TreeTraversalCallback callback) {
372
+ GumboNode* current_node = node;
373
+ unsigned int offset = 0;
374
+
375
+ tailcall:
376
+ switch (current_node->type) {
377
+ case GUMBO_NODE_DOCUMENT:
378
+ case GUMBO_NODE_TEMPLATE:
379
+ case GUMBO_NODE_ELEMENT: {
380
+ GumboVector* children = (current_node->type == GUMBO_NODE_DOCUMENT)
381
+ ? &current_node->v.document.children
382
+ : &current_node->v.element.children
383
+ ;
384
+ if (offset >= children->length) {
385
+ assert(offset == children->length);
386
+ break;
387
+ } else {
388
+ current_node = children->data[offset];
389
+ offset = 0;
390
+ goto tailcall;
391
+ }
392
+ }
393
+ case GUMBO_NODE_TEXT:
394
+ case GUMBO_NODE_CDATA:
395
+ case GUMBO_NODE_COMMENT:
396
+ case GUMBO_NODE_WHITESPACE:
397
+ assert(offset == 0);
398
+ break;
399
+ }
400
+
401
+ offset = current_node->index_within_parent + 1;
402
+ GumboNode* next_node = current_node->parent;
403
+ callback(current_node);
404
+ if (current_node == node) {
405
+ return;
406
+ }
407
+ current_node = next_node;
408
+ goto tailcall;
409
+ }
410
+
411
+ static void destroy_node_callback(GumboNode* node) {
412
+ switch (node->type) {
413
+ case GUMBO_NODE_DOCUMENT: {
414
+ GumboDocument* doc = &node->v.document;
415
+ gumbo_free((void*) doc->children.data);
416
+ gumbo_free((void*) doc->name);
417
+ gumbo_free((void*) doc->public_identifier);
418
+ gumbo_free((void*) doc->system_identifier);
419
+ } break;
420
+ case GUMBO_NODE_TEMPLATE:
421
+ case GUMBO_NODE_ELEMENT:
422
+ for (unsigned int i = 0; i < node->v.element.attributes.length; ++i) {
423
+ gumbo_destroy_attribute(node->v.element.attributes.data[i]);
424
+ }
425
+ gumbo_free(node->v.element.attributes.data);
426
+ gumbo_free(node->v.element.children.data);
427
+ if (node->v.element.tag == GUMBO_TAG_UNKNOWN)
428
+ gumbo_free((void *)node->v.element.name);
429
+ break;
430
+ case GUMBO_NODE_TEXT:
431
+ case GUMBO_NODE_CDATA:
432
+ case GUMBO_NODE_COMMENT:
433
+ case GUMBO_NODE_WHITESPACE:
434
+ gumbo_free((void*) node->v.text.text);
435
+ break;
436
+ }
437
+ gumbo_free(node);
438
+ }
439
+
440
+ static void destroy_node(GumboNode* node) {
441
+ tree_traverse(node, &destroy_node_callback);
442
+ }
443
+
444
+ static void destroy_fragment_ctx_element(GumboNode* ctx);
445
+
446
+ static void parser_state_destroy(GumboParser* parser) {
447
+ GumboParserState* state = parser->_parser_state;
448
+ if (state->_fragment_ctx) {
449
+ destroy_fragment_ctx_element(state->_fragment_ctx);
450
+ }
451
+ gumbo_vector_destroy(&state->_active_formatting_elements);
452
+ gumbo_vector_destroy(&state->_open_elements);
453
+ gumbo_vector_destroy(&state->_template_insertion_modes);
454
+ gumbo_string_buffer_destroy(&state->_text_node._buffer);
455
+ gumbo_character_token_buffer_destroy(&state->_table_character_tokens);
456
+ gumbo_free(state);
457
+ }
458
+
459
+ static GumboNode* get_document_node(const GumboParser* parser) {
460
+ return parser->_output->document;
461
+ }
462
+
463
+ static bool is_fragment_parser(const GumboParser* parser) {
464
+ return !!parser->_parser_state->_fragment_ctx;
465
+ }
466
+
467
+ // Returns the node at the bottom of the stack of open elements, or NULL if no
468
+ // elements have been added yet.
469
+ static GumboNode* get_current_node(const GumboParser* parser) {
470
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
471
+ if (open_elements->length == 0) {
472
+ assert(!parser->_output->root);
473
+ return NULL;
474
+ }
475
+ assert(open_elements->length > 0);
476
+ assert(open_elements->data != NULL);
477
+ return open_elements->data[open_elements->length - 1];
478
+ }
479
+
480
+ static GumboNode* get_adjusted_current_node(const GumboParser* parser) {
481
+ const GumboParserState* state = parser->_parser_state;
482
+ if (state->_open_elements.length == 1 && state->_fragment_ctx) {
483
+ return state->_fragment_ctx;
484
+ }
485
+ return get_current_node(parser);
486
+ }
487
+
488
+ // Returns true if the given needle is in the given array of literal
489
+ // GumboStringPieces. If exact_match is true, this requires that they match
490
+ // exactly; otherwise, this performs a prefix match to check if any of the
491
+ // elements in haystack start with needle. This always performs a
492
+ // case-insensitive match.
493
+ static bool is_in_static_list (
494
+ const GumboStringPiece* needle,
495
+ const GumboStringPiece* haystack,
496
+ bool exact_match
497
+ ) {
498
+ if (needle->length == 0)
499
+ return false;
500
+ if (exact_match) {
501
+ for (size_t i = 0; haystack[i].data; ++i) {
502
+ if (gumbo_string_equals_ignore_case(needle, &haystack[i]))
503
+ return true;
504
+ }
505
+ } else {
506
+ for (size_t i = 0; haystack[i].data; ++i) {
507
+ if (gumbo_string_prefix_ignore_case(&haystack[i], needle))
508
+ return true;
509
+ }
510
+ }
511
+ return false;
512
+ }
513
+
514
+ static void set_insertion_mode(GumboParser* parser, GumboInsertionMode mode) {
515
+ parser->_parser_state->_insertion_mode = mode;
516
+ }
517
+
518
+ static void push_template_insertion_mode (
519
+ GumboParser* parser,
520
+ GumboInsertionMode mode
521
+ ) {
522
+ gumbo_vector_add (
523
+ (void*) mode,
524
+ &parser->_parser_state->_template_insertion_modes
525
+ );
526
+ }
527
+
528
+ static void pop_template_insertion_mode(GumboParser* parser) {
529
+ gumbo_vector_pop(&parser->_parser_state->_template_insertion_modes);
530
+ }
531
+
532
+ // Returns the current template insertion mode. If the stack of template
533
+ // insertion modes is empty, this returns GUMBO_INSERTION_MODE_INITIAL.
534
+ static GumboInsertionMode get_current_template_insertion_mode (
535
+ const GumboParser* parser
536
+ ) {
537
+ GumboVector* modes = &parser->_parser_state->_template_insertion_modes;
538
+ if (modes->length == 0) {
539
+ return GUMBO_INSERTION_MODE_INITIAL;
540
+ }
541
+ return (GumboInsertionMode)(intptr_t) modes->data[(modes->length - 1)];
542
+ }
543
+
544
+ // Returns true if the specified token is either a start or end tag
545
+ // (specified by is_start) with one of the tag types in the TagSet.
546
+ static bool tag_in (
547
+ const GumboToken* token,
548
+ bool is_start,
549
+ const TagSet* tags
550
+ ) {
551
+ GumboTag token_tag;
552
+ if (is_start && token->type == GUMBO_TOKEN_START_TAG) {
553
+ token_tag = token->v.start_tag.tag;
554
+ } else if (!is_start && token->type == GUMBO_TOKEN_END_TAG) {
555
+ token_tag = token->v.end_tag.tag;
556
+ } else {
557
+ return false;
558
+ }
559
+ return (*tags)[(unsigned) token_tag] != 0u;
560
+ }
561
+
562
+ // Like tag_in, but for the single-tag case.
563
+ static bool tag_is(const GumboToken* token, bool is_start, GumboTag tag) {
564
+ if (is_start && token->type == GUMBO_TOKEN_START_TAG) {
565
+ return token->v.start_tag.tag == tag;
566
+ }
567
+ if (!is_start && token->type == GUMBO_TOKEN_END_TAG) {
568
+ return token->v.end_tag.tag == tag;
569
+ }
570
+ return false;
571
+ }
572
+
573
+ static inline bool tagset_includes (
574
+ const TagSet* tagset,
575
+ GumboNamespaceEnum ns,
576
+ GumboTag tag
577
+ ) {
578
+ return ((*tagset)[(unsigned) tag] & (1u << (unsigned) ns)) != 0u;
579
+ }
580
+
581
+ // Like tag_in, but checks for the tag of a node, rather than a token.
582
+ static bool node_tag_in_set(const GumboNode* node, const TagSet* tags) {
583
+ assert(node != NULL);
584
+ if (node->type != GUMBO_NODE_ELEMENT && node->type != GUMBO_NODE_TEMPLATE) {
585
+ return false;
586
+ }
587
+ return tagset_includes (
588
+ tags,
589
+ node->v.element.tag_namespace,
590
+ node->v.element.tag
591
+ );
592
+ }
593
+
594
+ static bool node_qualified_tagname_is (
595
+ const GumboNode* node,
596
+ GumboNamespaceEnum ns,
597
+ GumboTag tag,
598
+ const char *name
599
+ ) {
600
+ assert(node);
601
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
602
+ assert(node->v.element.name);
603
+ assert(tag != GUMBO_TAG_UNKNOWN || name);
604
+ GumboTag element_tag = node->v.element.tag;
605
+ const char *element_name = node->v.element.name;
606
+ assert(element_tag != GUMBO_TAG_UNKNOWN || element_name);
607
+ if (node->v.element.tag_namespace != ns || element_tag != tag)
608
+ return false;
609
+ if (tag != GUMBO_TAG_UNKNOWN)
610
+ return true;
611
+ return !gumbo_ascii_strcasecmp(element_name, name);
612
+ }
613
+
614
+ static bool node_html_tagname_is (
615
+ const GumboNode* node,
616
+ GumboTag tag,
617
+ const char *name
618
+ ) {
619
+ return node_qualified_tagname_is(node, GUMBO_NAMESPACE_HTML, tag, name);
620
+ }
621
+
622
+ static bool node_tagname_is (
623
+ const GumboNode* node,
624
+ GumboTag tag,
625
+ const char *name
626
+ ) {
627
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
628
+ return node_qualified_tagname_is(node, node->v.element.tag_namespace, tag, name);
629
+ }
630
+
631
+ // Like node_tag_in, but for the single-tag case.
632
+ static bool node_qualified_tag_is (
633
+ const GumboNode* node,
634
+ GumboNamespaceEnum ns,
635
+ GumboTag tag
636
+ ) {
637
+ assert(node);
638
+ assert(tag != GUMBO_TAG_UNKNOWN);
639
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
640
+ return
641
+ node->v.element.tag == tag
642
+ && node->v.element.tag_namespace == ns;
643
+ }
644
+
645
+ // Like node_tag_in, but for the single-tag case in the HTML namespace
646
+ static bool node_html_tag_is(const GumboNode* node, GumboTag tag) {
647
+ return node_qualified_tag_is(node, GUMBO_NAMESPACE_HTML, tag);
648
+ }
649
+
650
+ // https://html.spec.whatwg.org/multipage/parsing.html#reset-the-insertion-mode-appropriately
651
+ // This is a helper function that returns the appropriate insertion mode instead
652
+ // of setting it. Returns GUMBO_INSERTION_MODE_INITIAL as a sentinel value to
653
+ // indicate that there is no appropriate insertion mode, and the loop should
654
+ // continue.
655
+ static GumboInsertionMode get_appropriate_insertion_mode (
656
+ const GumboParser* parser,
657
+ int index
658
+ ) {
659
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
660
+ const GumboNode* node = open_elements->data[index];
661
+ const bool is_last = index == 0;
662
+
663
+ if (is_last && is_fragment_parser(parser)) {
664
+ node = parser->_parser_state->_fragment_ctx;
665
+ }
666
+
667
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
668
+ if (node->v.element.tag_namespace != GUMBO_NAMESPACE_HTML) {
669
+ return is_last ? GUMBO_INSERTION_MODE_IN_BODY : GUMBO_INSERTION_MODE_INITIAL;
670
+ }
671
+
672
+ switch (node->v.element.tag) {
673
+ case GUMBO_TAG_SELECT: {
674
+ if (is_last) {
675
+ return GUMBO_INSERTION_MODE_IN_SELECT;
676
+ }
677
+ for (int i = index; i > 0; --i) {
678
+ const GumboNode* ancestor = open_elements->data[i];
679
+ if (node_html_tag_is(ancestor, GUMBO_TAG_TEMPLATE)) {
680
+ return GUMBO_INSERTION_MODE_IN_SELECT;
681
+ }
682
+ if (node_html_tag_is(ancestor, GUMBO_TAG_TABLE)) {
683
+ return GUMBO_INSERTION_MODE_IN_SELECT_IN_TABLE;
684
+ }
685
+ }
686
+ return GUMBO_INSERTION_MODE_IN_SELECT;
687
+ }
688
+ case GUMBO_TAG_TD:
689
+ case GUMBO_TAG_TH:
690
+ if (!is_last) return GUMBO_INSERTION_MODE_IN_CELL;
691
+ break;
692
+ case GUMBO_TAG_TR:
693
+ return GUMBO_INSERTION_MODE_IN_ROW;
694
+ case GUMBO_TAG_TBODY:
695
+ case GUMBO_TAG_THEAD:
696
+ case GUMBO_TAG_TFOOT:
697
+ return GUMBO_INSERTION_MODE_IN_TABLE_BODY;
698
+ case GUMBO_TAG_CAPTION:
699
+ return GUMBO_INSERTION_MODE_IN_CAPTION;
700
+ case GUMBO_TAG_COLGROUP:
701
+ return GUMBO_INSERTION_MODE_IN_COLUMN_GROUP;
702
+ case GUMBO_TAG_TABLE:
703
+ return GUMBO_INSERTION_MODE_IN_TABLE;
704
+ case GUMBO_TAG_TEMPLATE:
705
+ return get_current_template_insertion_mode(parser);
706
+ case GUMBO_TAG_HEAD:
707
+ if (!is_last) return GUMBO_INSERTION_MODE_IN_HEAD;
708
+ break;
709
+ case GUMBO_TAG_BODY:
710
+ return GUMBO_INSERTION_MODE_IN_BODY;
711
+ case GUMBO_TAG_FRAMESET:
712
+ return GUMBO_INSERTION_MODE_IN_FRAMESET;
713
+ case GUMBO_TAG_HTML:
714
+ return parser->_parser_state->_head_element
715
+ ? GUMBO_INSERTION_MODE_AFTER_HEAD
716
+ : GUMBO_INSERTION_MODE_BEFORE_HEAD;
717
+ default:
718
+ break;
719
+ }
720
+ return is_last ? GUMBO_INSERTION_MODE_IN_BODY : GUMBO_INSERTION_MODE_INITIAL;
721
+ }
722
+
723
+ // This performs the actual "reset the insertion mode" loop.
724
+ static void reset_insertion_mode_appropriately(GumboParser* parser) {
725
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
726
+ for (int i = open_elements->length; --i >= 0;) {
727
+ GumboInsertionMode mode = get_appropriate_insertion_mode(parser, i);
728
+ if (mode != GUMBO_INSERTION_MODE_INITIAL) {
729
+ set_insertion_mode(parser, mode);
730
+ return;
731
+ }
732
+ }
733
+ // Should never get here, because is_last will be set on the last iteration
734
+ // and will force GUMBO_INSERTION_MODE_IN_BODY.
735
+ assert(0);
736
+ }
737
+
738
+ static void parser_add_parse_error (
739
+ GumboParser* parser,
740
+ const GumboToken* token
741
+ ) {
742
+ gumbo_debug("Adding parse error.\n");
743
+ GumboError* error = gumbo_add_error(parser);
744
+ if (!error) {
745
+ return;
746
+ }
747
+ error->type = GUMBO_ERR_PARSER;
748
+ error->position = token->position;
749
+ error->original_text = token->original_text;
750
+ GumboParserError* extra_data = &error->v.parser;
751
+ extra_data->input_type = token->type;
752
+ extra_data->input_tag = GUMBO_TAG_UNKNOWN;
753
+ extra_data->input_name = NULL;
754
+ if (token->type == GUMBO_TOKEN_START_TAG)
755
+ {
756
+ extra_data->input_tag = token->v.start_tag.tag;
757
+ if (extra_data->input_tag == GUMBO_TAG_UNKNOWN && token->v.start_tag.name) {
758
+ extra_data->input_name = gumbo_strdup(token->v.start_tag.name);
759
+ }
760
+ }
761
+ else if (token->type == GUMBO_TOKEN_END_TAG)
762
+ {
763
+ extra_data->input_tag = token->v.end_tag.tag;
764
+ if (extra_data->input_tag == GUMBO_TAG_UNKNOWN && token->v.end_tag.name) {
765
+ extra_data->input_name = gumbo_strdup(token->v.end_tag.name);
766
+ }
767
+ }
768
+ const GumboParserState* state = parser->_parser_state;
769
+ extra_data->parser_state = state->_insertion_mode;
770
+ gumbo_vector_init(state->_open_elements.length, &extra_data->tag_stack);
771
+ for (unsigned int i = 0; i < state->_open_elements.length; ++i) {
772
+ const GumboNode* node = state->_open_elements.data[i];
773
+ assert (
774
+ node->type == GUMBO_NODE_ELEMENT
775
+ || node->type == GUMBO_NODE_TEMPLATE
776
+ );
777
+ void *tag;
778
+ if (node->v.element.tag == GUMBO_TAG_UNKNOWN && node->v.element.name) {
779
+ tag = gumbo_strdup(node->v.element.name);
780
+ } else {
781
+ tag = (void *)(uintptr_t)node->v.element.tag;
782
+ }
783
+ gumbo_vector_add(tag, &extra_data->tag_stack);
784
+ }
785
+ }
786
+
787
+ // https://html.spec.whatwg.org/multipage/parsing.html#mathml-text-integration-point
788
+ static bool is_mathml_integration_point(const GumboNode* node) {
789
+ static const TagSet mathml_integration_point_tags = {
790
+ TAG_MATHML(MI), TAG_MATHML(MO), TAG_MATHML(MN),
791
+ TAG_MATHML(MS), TAG_MATHML(MTEXT)
792
+ };
793
+ return node_tag_in_set(node, &mathml_integration_point_tags);
794
+ }
795
+
796
+ // https://html.spec.whatwg.org/multipage/parsing.html#html-integration-point
797
+ static bool is_html_integration_point(const GumboNode* node) {
798
+ static const TagSet html_integration_point_svg_tags = {
799
+ TAG_SVG(FOREIGNOBJECT), TAG_SVG(DESC), TAG_SVG(TITLE)
800
+ };
801
+ if (node_tag_in_set(node, &html_integration_point_svg_tags)) {
802
+ return true;
803
+ }
804
+
805
+ const bool is_mathml_annotation_xml_element = node_qualified_tag_is (
806
+ node,
807
+ GUMBO_NAMESPACE_MATHML,
808
+ GUMBO_TAG_ANNOTATION_XML
809
+ );
810
+ const GumboVector* attributes = &node->v.element.attributes;
811
+ if (
812
+ is_mathml_annotation_xml_element
813
+ && (
814
+ attribute_matches(attributes, "encoding", "text/html")
815
+ || attribute_matches(attributes, "encoding", "application/xhtml+xml")
816
+ )
817
+ ) {
818
+ return true;
819
+ }
820
+
821
+ return false;
822
+ }
823
+
824
+ // This represents a place to insert a node, consisting of a target parent and a
825
+ // child index within that parent. If the node should be inserted at the end of
826
+ // the parent's child, index will be -1.
827
+ typedef struct {
828
+ GumboNode* target;
829
+ int index;
830
+ } InsertionLocation;
831
+
832
+ static InsertionLocation get_appropriate_insertion_location (
833
+ const GumboParser* parser,
834
+ GumboNode* override_target
835
+ ) {
836
+ InsertionLocation retval = {override_target, -1};
837
+ if (retval.target == NULL) {
838
+ // No override target; default to the current node, but special-case the
839
+ // root node since get_current_node() assumes the stack of open elements is
840
+ // non-empty.
841
+ retval.target = (parser->_output->root != NULL)
842
+ ? get_current_node(parser)
843
+ : get_document_node(parser)
844
+ ;
845
+ }
846
+ if (
847
+ !parser->_parser_state->_foster_parent_insertions
848
+ || !node_tag_in_set(retval.target, &(const TagSet) {
849
+ TAG(TABLE), TAG(TBODY), TAG(TFOOT), TAG(THEAD), TAG(TR)
850
+ })
851
+ ) {
852
+ return retval;
853
+ }
854
+
855
+ // Foster-parenting case.
856
+ int last_template_index = -1;
857
+ int last_table_index = -1;
858
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
859
+ for (unsigned int i = 0; i < open_elements->length; ++i) {
860
+ if (node_html_tag_is(open_elements->data[i], GUMBO_TAG_TEMPLATE)) {
861
+ last_template_index = i;
862
+ }
863
+ if (node_html_tag_is(open_elements->data[i], GUMBO_TAG_TABLE)) {
864
+ last_table_index = i;
865
+ }
866
+ }
867
+ if (
868
+ last_template_index != -1
869
+ && (last_table_index == -1 || last_template_index > last_table_index)
870
+ ) {
871
+ retval.target = open_elements->data[last_template_index];
872
+ return retval;
873
+ }
874
+ if (last_table_index == -1) {
875
+ retval.target = open_elements->data[0];
876
+ return retval;
877
+ }
878
+ const GumboNode* last_table = open_elements->data[last_table_index];
879
+ if (last_table->parent != NULL) {
880
+ retval.target = last_table->parent;
881
+ retval.index = last_table->index_within_parent;
882
+ return retval;
883
+ }
884
+
885
+ retval.target = open_elements->data[last_table_index - 1];
886
+ return retval;
887
+ }
888
+
889
+ // Appends a node to the end of its parent, setting the "parent" and
890
+ // "index_within_parent" fields appropriately.
891
+ static void append_node(GumboNode* parent, GumboNode* node) {
892
+ assert(node->parent == NULL);
893
+ assert(node->index_within_parent == (unsigned int) -1);
894
+ GumboVector* children;
895
+ if (
896
+ parent->type == GUMBO_NODE_ELEMENT
897
+ || parent->type == GUMBO_NODE_TEMPLATE
898
+ ) {
899
+ children = &parent->v.element.children;
900
+ } else {
901
+ assert(parent->type == GUMBO_NODE_DOCUMENT);
902
+ children = &parent->v.document.children;
903
+ }
904
+ node->parent = parent;
905
+ node->index_within_parent = children->length;
906
+ gumbo_vector_add((void*) node, children);
907
+ assert(node->index_within_parent < children->length);
908
+ }
909
+
910
+ // Inserts a node at the specified InsertionLocation, updating the
911
+ // "parent" and "index_within_parent" fields of it and all its siblings.
912
+ // If the index of the location is -1, this calls append_node.
913
+ static void insert_node(GumboNode* node, InsertionLocation location) {
914
+ assert(node->parent == NULL);
915
+ assert(node->index_within_parent == (unsigned int) -1);
916
+ GumboNode* parent = location.target;
917
+ int index = location.index;
918
+ if (index != -1) {
919
+ GumboVector* children = NULL;
920
+ if (
921
+ parent->type == GUMBO_NODE_ELEMENT
922
+ || parent->type == GUMBO_NODE_TEMPLATE
923
+ ) {
924
+ children = &parent->v.element.children;
925
+ } else if (parent->type == GUMBO_NODE_DOCUMENT) {
926
+ children = &parent->v.document.children;
927
+ assert(children->length == 0);
928
+ } else {
929
+ assert(0);
930
+ }
931
+
932
+ assert(index >= 0);
933
+ assert((unsigned int) index < children->length);
934
+ node->parent = parent;
935
+ node->index_within_parent = index;
936
+ gumbo_vector_insert_at((void*) node, index, children);
937
+ assert(node->index_within_parent < children->length);
938
+ for (unsigned int i = index + 1; i < children->length; ++i) {
939
+ GumboNode* sibling = children->data[i];
940
+ sibling->index_within_parent = i;
941
+ assert(sibling->index_within_parent < children->length);
942
+ }
943
+ } else {
944
+ append_node(parent, node);
945
+ }
946
+ }
947
+
948
+ static void maybe_flush_text_node_buffer(GumboParser* parser) {
949
+ GumboParserState* state = parser->_parser_state;
950
+ TextNodeBufferState* buffer_state = &state->_text_node;
951
+ if (buffer_state->_buffer.length == 0) {
952
+ return;
953
+ }
954
+
955
+ assert (
956
+ buffer_state->_type == GUMBO_NODE_WHITESPACE
957
+ || buffer_state->_type == GUMBO_NODE_TEXT
958
+ || buffer_state->_type == GUMBO_NODE_CDATA
959
+ );
960
+ GumboNode* text_node = create_node(buffer_state->_type);
961
+ GumboText* text_node_data = &text_node->v.text;
962
+ text_node_data->text = gumbo_string_buffer_to_string(&buffer_state->_buffer);
963
+ text_node_data->original_text.data = buffer_state->_start_original_text;
964
+ text_node_data->original_text.length =
965
+ state->_current_token->original_text.data -
966
+ buffer_state->_start_original_text;
967
+ text_node_data->start_pos = buffer_state->_start_position;
968
+
969
+ gumbo_debug (
970
+ "Flushing text node buffer of %.*s.\n",
971
+ (int) buffer_state->_buffer.length,
972
+ buffer_state->_buffer.data
973
+ );
974
+
975
+ InsertionLocation location = get_appropriate_insertion_location(parser, NULL);
976
+ if (location.target->type == GUMBO_NODE_DOCUMENT) {
977
+ // The DOM does not allow Document nodes to have Text children, so per the
978
+ // spec, they are dropped on the floor.
979
+ destroy_node(text_node);
980
+ } else {
981
+ insert_node(text_node, location);
982
+ }
983
+
984
+ gumbo_string_buffer_clear(&buffer_state->_buffer);
985
+ buffer_state->_type = GUMBO_NODE_WHITESPACE;
986
+ assert(buffer_state->_buffer.length == 0);
987
+ }
988
+
989
+ static void record_end_of_element (
990
+ const GumboToken* current_token,
991
+ GumboElement* element
992
+ ) {
993
+ element->end_pos = current_token->position;
994
+ element->original_end_tag =
995
+ (current_token->type == GUMBO_TOKEN_END_TAG)
996
+ ? current_token->original_text
997
+ : kGumboEmptyString;
998
+ }
999
+
1000
+ static GumboNode* pop_current_node(GumboParser* parser) {
1001
+ GumboParserState* state = parser->_parser_state;
1002
+ maybe_flush_text_node_buffer(parser);
1003
+ if (state->_open_elements.length > 0) {
1004
+ assert(node_html_tag_is(state->_open_elements.data[0], GUMBO_TAG_HTML));
1005
+ gumbo_debug (
1006
+ "Popping %s node.\n",
1007
+ gumbo_normalized_tagname(get_current_node(parser)->v.element.tag)
1008
+ );
1009
+ }
1010
+ GumboNode* current_node = gumbo_vector_pop(&state->_open_elements);
1011
+ if (!current_node) {
1012
+ assert(state->_open_elements.length == 0);
1013
+ return NULL;
1014
+ }
1015
+ assert (
1016
+ current_node->type == GUMBO_NODE_ELEMENT
1017
+ || current_node->type == GUMBO_NODE_TEMPLATE
1018
+ );
1019
+ bool is_closed_body_or_html_tag =
1020
+ (
1021
+ node_html_tag_is(current_node, GUMBO_TAG_BODY)
1022
+ && state->_closed_body_tag
1023
+ ) || (
1024
+ node_html_tag_is(current_node, GUMBO_TAG_HTML)
1025
+ && state->_closed_html_tag
1026
+ )
1027
+ ;
1028
+ if (
1029
+ (
1030
+ state->_current_token->type != GUMBO_TOKEN_END_TAG
1031
+ || !node_qualified_tagname_is (
1032
+ current_node,
1033
+ GUMBO_NAMESPACE_HTML,
1034
+ state->_current_token->v.end_tag.tag,
1035
+ state->_current_token->v.end_tag.name
1036
+ )
1037
+ )
1038
+ && !is_closed_body_or_html_tag
1039
+ ) {
1040
+ current_node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
1041
+ }
1042
+ if (!is_closed_body_or_html_tag) {
1043
+ record_end_of_element(state->_current_token, &current_node->v.element);
1044
+ }
1045
+ return current_node;
1046
+ }
1047
+
1048
+ static void append_comment_node (
1049
+ GumboParser* parser,
1050
+ GumboNode* node,
1051
+ const GumboToken* token
1052
+ ) {
1053
+ maybe_flush_text_node_buffer(parser);
1054
+ GumboNode* comment = create_node(GUMBO_NODE_COMMENT);
1055
+ comment->type = GUMBO_NODE_COMMENT;
1056
+ comment->parse_flags = GUMBO_INSERTION_NORMAL;
1057
+ comment->v.text.text = token->v.text;
1058
+ comment->v.text.original_text = token->original_text;
1059
+ comment->v.text.start_pos = token->position;
1060
+ append_node(node, comment);
1061
+ }
1062
+
1063
+ // https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-row-context
1064
+ static void clear_stack_to_table_row_context(GumboParser* parser) {
1065
+ static const TagSet tags = {TAG(HTML), TAG(TR), TAG(TEMPLATE)};
1066
+ while (!node_tag_in_set(get_current_node(parser), &tags)) {
1067
+ pop_current_node(parser);
1068
+ }
1069
+ }
1070
+
1071
+ // https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-context
1072
+ static void clear_stack_to_table_context(GumboParser* parser) {
1073
+ static const TagSet tags = {TAG(HTML), TAG(TABLE), TAG(TEMPLATE)};
1074
+ while (!node_tag_in_set(get_current_node(parser), &tags)) {
1075
+ pop_current_node(parser);
1076
+ }
1077
+ }
1078
+
1079
+ // https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-body-context
1080
+ static void clear_stack_to_table_body_context(GumboParser* parser) {
1081
+ static const TagSet tags = {
1082
+ TAG(HTML), TAG(TBODY), TAG(TFOOT), TAG(THEAD), TAG(TEMPLATE)
1083
+ };
1084
+ while (!node_tag_in_set(get_current_node(parser), &tags)) {
1085
+ pop_current_node(parser);
1086
+ }
1087
+ }
1088
+
1089
+ // Creates a parser-inserted element in the HTML namespace and returns it.
1090
+ static GumboNode* create_element(GumboParser* parser, GumboTag tag) {
1091
+ // XXX: This will fail for creating fragments with an element with tag
1092
+ // GUMBO_TAG_UNKNOWN
1093
+ assert(tag != GUMBO_TAG_UNKNOWN);
1094
+ GumboNode* node = create_node(GUMBO_NODE_ELEMENT);
1095
+ GumboElement* element = &node->v.element;
1096
+ gumbo_vector_init(1, &element->children);
1097
+ gumbo_vector_init(0, &element->attributes);
1098
+ element->tag = tag;
1099
+ element->name = gumbo_normalized_tagname(tag);
1100
+ element->tag_namespace = GUMBO_NAMESPACE_HTML;
1101
+ element->original_tag = kGumboEmptyString;
1102
+ element->original_end_tag = kGumboEmptyString;
1103
+ element->start_pos = (parser->_parser_state->_current_token)
1104
+ ? parser->_parser_state->_current_token->position
1105
+ : kGumboEmptySourcePosition
1106
+ ;
1107
+ element->end_pos = kGumboEmptySourcePosition;
1108
+ return node;
1109
+ }
1110
+
1111
+ // Constructs an element from the given start tag token.
1112
+ static GumboNode* create_element_from_token (
1113
+ GumboToken* token,
1114
+ GumboNamespaceEnum tag_namespace
1115
+ ) {
1116
+ assert(token->type == GUMBO_TOKEN_START_TAG);
1117
+ GumboTokenStartTag* start_tag = &token->v.start_tag;
1118
+
1119
+ GumboNodeType type =
1120
+ (
1121
+ tag_namespace == GUMBO_NAMESPACE_HTML
1122
+ && start_tag->tag == GUMBO_TAG_TEMPLATE
1123
+ )
1124
+ ? GUMBO_NODE_TEMPLATE
1125
+ : GUMBO_NODE_ELEMENT
1126
+ ;
1127
+
1128
+ GumboNode* node = create_node(type);
1129
+ GumboElement* element = &node->v.element;
1130
+ gumbo_vector_init(1, &element->children);
1131
+ element->attributes = start_tag->attributes;
1132
+ element->tag = start_tag->tag;
1133
+ element->name = start_tag->name ? start_tag->name : gumbo_normalized_tagname(start_tag->tag);
1134
+ element->tag_namespace = tag_namespace;
1135
+
1136
+ assert(token->original_text.length >= 2);
1137
+ assert(token->original_text.data[0] == '<');
1138
+ assert(token->original_text.data[token->original_text.length - 1] == '>');
1139
+ element->original_tag = token->original_text;
1140
+ element->start_pos = token->position;
1141
+ element->original_end_tag = kGumboEmptyString;
1142
+ element->end_pos = kGumboEmptySourcePosition;
1143
+
1144
+ // The element takes ownership of the attributes and name from the token, so
1145
+ // any allocated-memory fields should be nulled out.
1146
+ start_tag->attributes = kGumboEmptyVector;
1147
+ start_tag->name = NULL;
1148
+ return node;
1149
+ }
1150
+
1151
+ // https://html.spec.whatwg.org/multipage/parsing.html#insert-an-html-element
1152
+ static void insert_element (
1153
+ GumboParser* parser,
1154
+ GumboNode* node,
1155
+ bool is_reconstructing_formatting_elements
1156
+ ) {
1157
+ GumboParserState* state = parser->_parser_state;
1158
+ // NOTE(jdtang): The text node buffer must always be flushed before inserting
1159
+ // a node, otherwise we're handling nodes in a different order than the spec
1160
+ // mandated. However, one clause of the spec (character tokens in the body)
1161
+ // requires that we reconstruct the active formatting elements *before* adding
1162
+ // the character, and reconstructing the active formatting elements may itself
1163
+ // result in the insertion of new elements (which should be pushed onto the
1164
+ // stack of open elements before the buffer is flushed). We solve this (for
1165
+ // the time being, the spec has been rewritten for <template> and the new
1166
+ // version may be simpler here) with a boolean flag to this method.
1167
+ if (!is_reconstructing_formatting_elements) {
1168
+ maybe_flush_text_node_buffer(parser);
1169
+ }
1170
+ InsertionLocation location = get_appropriate_insertion_location(parser, NULL);
1171
+ insert_node(node, location);
1172
+ gumbo_vector_add((void*) node, &state->_open_elements);
1173
+ }
1174
+
1175
+ // Convenience method that combines create_element_from_token and
1176
+ // insert_element, inserting the generated element directly into the current
1177
+ // node. Returns the node inserted.
1178
+ static GumboNode* insert_element_from_token (
1179
+ GumboParser* parser,
1180
+ GumboToken* token
1181
+ ) {
1182
+ GumboNode* element = create_element_from_token(token, GUMBO_NAMESPACE_HTML);
1183
+ insert_element(parser, element, false);
1184
+ gumbo_debug (
1185
+ "Inserting <%s> element (@%p) from token.\n",
1186
+ gumbo_normalized_tagname(element->v.element.tag),
1187
+ (void*)element
1188
+ );
1189
+ return element;
1190
+ }
1191
+
1192
+ // Convenience method that combines create_element and insert_element, inserting
1193
+ // a parser-generated element of a specific tag type. Returns the node
1194
+ // inserted.
1195
+ static GumboNode* insert_element_of_tag_type (
1196
+ GumboParser* parser,
1197
+ GumboTag tag,
1198
+ GumboParseFlags reason
1199
+ ) {
1200
+ GumboNode* element = create_element(parser, tag);
1201
+ element->parse_flags |= GUMBO_INSERTION_BY_PARSER | reason;
1202
+ insert_element(parser, element, false);
1203
+ gumbo_debug (
1204
+ "Inserting <%s> element (@%p) from tag type.\n",
1205
+ gumbo_normalized_tagname(tag),
1206
+ (void*)element
1207
+ );
1208
+ return element;
1209
+ }
1210
+
1211
+ // Convenience method for creating foreign namespaced element. Returns the node
1212
+ // inserted.
1213
+ static GumboNode* insert_foreign_element (
1214
+ GumboParser* parser,
1215
+ GumboToken* token,
1216
+ GumboNamespaceEnum tag_namespace
1217
+ ) {
1218
+ assert(token->type == GUMBO_TOKEN_START_TAG);
1219
+ GumboNode* element = create_element_from_token(token, tag_namespace);
1220
+ insert_element(parser, element, false);
1221
+ gumbo_debug (
1222
+ "Inserting <%s> foreign element (@%p).\n",
1223
+ gumbo_normalized_tagname(element->v.element.tag),
1224
+ (void*)element
1225
+ );
1226
+ if (
1227
+ token_has_attribute(token, "xmlns")
1228
+ && !attribute_matches_case_sensitive (
1229
+ &token->v.start_tag.attributes,
1230
+ "xmlns",
1231
+ kLegalXmlns[tag_namespace]
1232
+ )
1233
+ ) {
1234
+ // TODO(jdtang): Since there're multiple possible error codes here, we
1235
+ // eventually need reason codes to differentiate them.
1236
+ parser_add_parse_error(parser, token);
1237
+ }
1238
+ if (
1239
+ token_has_attribute(token, "xmlns:xlink")
1240
+ && !attribute_matches_case_sensitive (
1241
+ &token->v.start_tag.attributes,
1242
+ "xmlns:xlink",
1243
+ "http://www.w3.org/1999/xlink"
1244
+ )
1245
+ ) {
1246
+ parser_add_parse_error(parser, token);
1247
+ }
1248
+ return element;
1249
+ }
1250
+
1251
+ static void insert_text_token(GumboParser* parser, GumboToken* token) {
1252
+ assert (
1253
+ token->type == GUMBO_TOKEN_WHITESPACE
1254
+ || token->type == GUMBO_TOKEN_CHARACTER
1255
+ || token->type == GUMBO_TOKEN_NULL
1256
+ || token->type == GUMBO_TOKEN_CDATA
1257
+ );
1258
+ TextNodeBufferState* buffer_state = &parser->_parser_state->_text_node;
1259
+ if (buffer_state->_buffer.length == 0) {
1260
+ // Initialize position fields.
1261
+ buffer_state->_start_original_text = token->original_text.data;
1262
+ buffer_state->_start_position = token->position;
1263
+ }
1264
+ gumbo_string_buffer_append_codepoint (
1265
+ token->v.character,
1266
+ &buffer_state->_buffer
1267
+ );
1268
+ if (token->type == GUMBO_TOKEN_CHARACTER) {
1269
+ buffer_state->_type = GUMBO_NODE_TEXT;
1270
+ } else if (token->type == GUMBO_TOKEN_CDATA) {
1271
+ buffer_state->_type = GUMBO_NODE_CDATA;
1272
+ }
1273
+ gumbo_debug("Inserting text token '%c'.\n", token->v.character);
1274
+ }
1275
+
1276
+ // https://html.spec.whatwg.org/multipage/parsing.html#generic-rcdata-element-parsing-algorithm
1277
+ static void run_generic_parsing_algorithm (
1278
+ GumboParser* parser,
1279
+ GumboToken* token,
1280
+ GumboTokenizerEnum lexer_state
1281
+ ) {
1282
+ insert_element_from_token(parser, token);
1283
+ gumbo_tokenizer_set_state(parser, lexer_state);
1284
+ GumboParserState* parser_state = parser->_parser_state;
1285
+ parser_state->_original_insertion_mode = parser_state->_insertion_mode;
1286
+ parser_state->_insertion_mode = GUMBO_INSERTION_MODE_TEXT;
1287
+ }
1288
+
1289
+ static void acknowledge_self_closing_tag(GumboParser* parser) {
1290
+ parser->_parser_state->_self_closing_flag_acknowledged = true;
1291
+ }
1292
+
1293
+ // Returns true if there's an anchor tag in the list of active formatting
1294
+ // elements, and fills in its index if so.
1295
+ static bool find_last_anchor_index(GumboParser* parser, int* anchor_index) {
1296
+ GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
1297
+ for (int i = elements->length; --i >= 0;) {
1298
+ GumboNode* node = elements->data[i];
1299
+ if (node == &kActiveFormattingScopeMarker) {
1300
+ return false;
1301
+ }
1302
+ if (node_html_tag_is(node, GUMBO_TAG_A)) {
1303
+ *anchor_index = i;
1304
+ return true;
1305
+ }
1306
+ }
1307
+ return false;
1308
+ }
1309
+
1310
+ // Counts the number of open formatting elements in the list of active
1311
+ // formatting elements (after the last active scope marker) that have a specific
1312
+ // tag. If this is > 0, then earliest_matching_index will be filled in with the
1313
+ // index of the first such element.
1314
+ static int count_formatting_elements_of_tag (
1315
+ GumboParser* parser,
1316
+ const GumboNode* desired_node,
1317
+ int* earliest_matching_index
1318
+ ) {
1319
+ const GumboElement* desired_element = &desired_node->v.element;
1320
+ GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
1321
+ int num_identical_elements = 0;
1322
+ for (int i = elements->length; --i >= 0;) {
1323
+ GumboNode* node = elements->data[i];
1324
+ if (node == &kActiveFormattingScopeMarker) {
1325
+ break;
1326
+ }
1327
+ assert(node->type == GUMBO_NODE_ELEMENT);
1328
+ if (
1329
+ node_qualified_tagname_is (
1330
+ node,
1331
+ desired_element->tag_namespace,
1332
+ desired_element->tag,
1333
+ desired_element->name
1334
+ )
1335
+ && all_attributes_match(&node->v.element.attributes, &desired_element->attributes)
1336
+ ) {
1337
+ num_identical_elements++;
1338
+ *earliest_matching_index = i;
1339
+ }
1340
+ }
1341
+ return num_identical_elements;
1342
+ }
1343
+
1344
+ // https://html.spec.whatwg.org/multipage/parsing.html#reconstruct-the-active-formatting-elements
1345
+ static void add_formatting_element(GumboParser* parser, const GumboNode* node) {
1346
+ assert (
1347
+ node == &kActiveFormattingScopeMarker
1348
+ || node->type == GUMBO_NODE_ELEMENT
1349
+ );
1350
+ GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
1351
+ if (node == &kActiveFormattingScopeMarker) {
1352
+ gumbo_debug("Adding a scope marker.\n");
1353
+ } else {
1354
+ gumbo_debug("Adding a formatting element.\n");
1355
+ }
1356
+
1357
+ // Hunt for identical elements.
1358
+ int earliest_identical_element = elements->length;
1359
+ int num_identical_elements = count_formatting_elements_of_tag (
1360
+ parser,
1361
+ node,
1362
+ &earliest_identical_element
1363
+ );
1364
+
1365
+ // Noah's Ark clause: if there're at least 3, remove the earliest.
1366
+ if (num_identical_elements >= 3) {
1367
+ gumbo_debug (
1368
+ "Noah's ark clause: removing element at %d.\n",
1369
+ earliest_identical_element
1370
+ );
1371
+ gumbo_vector_remove_at(earliest_identical_element, elements);
1372
+ }
1373
+
1374
+ gumbo_vector_add((void*) node, elements);
1375
+ }
1376
+
1377
+ static bool is_open_element(const GumboParser* parser, const GumboNode* node) {
1378
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
1379
+ for (unsigned int i = 0; i < open_elements->length; ++i) {
1380
+ if (open_elements->data[i] == node) {
1381
+ return true;
1382
+ }
1383
+ }
1384
+ return false;
1385
+ }
1386
+
1387
+ // Clones attributes, tags, etc. of a node, but does not copy the content. The
1388
+ // clone shares no structure with the original node: all owned strings and
1389
+ // values are fresh copies.
1390
+ static GumboNode* clone_node (
1391
+ GumboNode* node,
1392
+ GumboParseFlags reason
1393
+ ) {
1394
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
1395
+ GumboNode* new_node = gumbo_alloc(sizeof(GumboNode));
1396
+ *new_node = *node;
1397
+ new_node->parent = NULL;
1398
+ new_node->index_within_parent = -1;
1399
+ // Clear the GUMBO_INSERTION_IMPLICIT_END_TAG flag, as the cloned node may
1400
+ // have a separate end tag.
1401
+ new_node->parse_flags &= ~GUMBO_INSERTION_IMPLICIT_END_TAG;
1402
+ new_node->parse_flags |= reason | GUMBO_INSERTION_BY_PARSER;
1403
+ GumboElement* element = &new_node->v.element;
1404
+ gumbo_vector_init(1, &element->children);
1405
+
1406
+ const GumboVector* old_attributes = &node->v.element.attributes;
1407
+ gumbo_vector_init(old_attributes->length, &element->attributes);
1408
+ for (unsigned int i = 0; i < old_attributes->length; ++i) {
1409
+ const GumboAttribute* old_attr = old_attributes->data[i];
1410
+ GumboAttribute* attr = gumbo_alloc(sizeof(GumboAttribute));
1411
+ *attr = *old_attr;
1412
+ attr->name = gumbo_strdup(old_attr->name);
1413
+ attr->value = gumbo_strdup(old_attr->value);
1414
+ gumbo_vector_add(attr, &element->attributes);
1415
+ }
1416
+ return new_node;
1417
+ }
1418
+
1419
+ // "Reconstruct active formatting elements" part of the spec.
1420
+ // This implementation is based on the html5lib translation from the
1421
+ // mess of GOTOs in the spec to reasonably structured programming.
1422
+ // https://github.com/html5lib/html5lib-python/blob/master/html5lib/treebuilders/base.py
1423
+ static void reconstruct_active_formatting_elements(GumboParser* parser) {
1424
+ GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
1425
+ // Step 1
1426
+ if (elements->length == 0) {
1427
+ return;
1428
+ }
1429
+
1430
+ // Step 2 & 3
1431
+ unsigned int i = elements->length - 1;
1432
+ GumboNode* element = elements->data[i];
1433
+ if (
1434
+ element == &kActiveFormattingScopeMarker
1435
+ || is_open_element(parser, element)
1436
+ ) {
1437
+ return;
1438
+ }
1439
+
1440
+ // Step 6
1441
+ do {
1442
+ if (i == 0) {
1443
+ // Step 4
1444
+ i = -1; // Incremented to 0 below.
1445
+ break;
1446
+ }
1447
+ // Step 5
1448
+ element = elements->data[--i];
1449
+ } while (
1450
+ element != &kActiveFormattingScopeMarker
1451
+ && !is_open_element(parser, element)
1452
+ );
1453
+
1454
+ ++i;
1455
+ gumbo_debug (
1456
+ "Reconstructing elements from %u on %s parent.\n",
1457
+ i,
1458
+ gumbo_normalized_tagname(get_current_node(parser)->v.element.tag)
1459
+ );
1460
+ for (; i < elements->length; ++i) {
1461
+ // Step 7 & 8.
1462
+ assert(elements->length > 0);
1463
+ assert(i < elements->length);
1464
+ element = elements->data[i];
1465
+ assert(element != &kActiveFormattingScopeMarker);
1466
+ GumboNode* clone = clone_node (
1467
+ element,
1468
+ GUMBO_INSERTION_RECONSTRUCTED_FORMATTING_ELEMENT
1469
+ );
1470
+ // Step 9.
1471
+ InsertionLocation location =
1472
+ get_appropriate_insertion_location(parser, NULL);
1473
+ insert_node(clone, location);
1474
+ gumbo_vector_add (
1475
+ (void*) clone,
1476
+ &parser->_parser_state->_open_elements
1477
+ );
1478
+
1479
+ // Step 10.
1480
+ elements->data[i] = clone;
1481
+ gumbo_debug (
1482
+ "Reconstructed %s element at %u.\n",
1483
+ gumbo_normalized_tagname(clone->v.element.tag),
1484
+ i
1485
+ );
1486
+ }
1487
+ }
1488
+
1489
+ static void clear_active_formatting_elements(GumboParser* parser) {
1490
+ GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
1491
+ int num_elements_cleared = 0;
1492
+ const GumboNode* node;
1493
+ do {
1494
+ node = gumbo_vector_pop(elements);
1495
+ ++num_elements_cleared;
1496
+ } while (node && node != &kActiveFormattingScopeMarker);
1497
+ gumbo_debug (
1498
+ "Cleared %d elements from active formatting list.\n",
1499
+ num_elements_cleared
1500
+ );
1501
+ }
1502
+
1503
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode
1504
+ GumboQuirksModeEnum gumbo_compute_quirks_mode (
1505
+ const char *name,
1506
+ const char *pubid_str,
1507
+ const char *sysid_str
1508
+ ) {
1509
+
1510
+ GumboStringPiece pubid = {
1511
+ .data = pubid_str,
1512
+ .length = pubid_str? strlen(pubid_str) : 0,
1513
+ };
1514
+ GumboStringPiece sysid = {
1515
+ .data = sysid_str,
1516
+ .length = sysid_str? strlen(sysid_str) : 0,
1517
+ };
1518
+ bool has_system_identifier = !!sysid_str;
1519
+ if (
1520
+ name == NULL
1521
+ || strcmp(name, "html")
1522
+ || is_in_static_list(&pubid, kQuirksModePublicIdPrefixes, false)
1523
+ || is_in_static_list(&pubid, kQuirksModePublicIdExactMatches, true)
1524
+ || is_in_static_list(&sysid, kQuirksModeSystemIdExactMatches, true)
1525
+ || (
1526
+ !has_system_identifier
1527
+ && is_in_static_list(&pubid, kSystemIdDependentPublicIdPrefixes, false)
1528
+ )
1529
+ ) {
1530
+ return GUMBO_DOCTYPE_QUIRKS;
1531
+ }
1532
+
1533
+ if (
1534
+ is_in_static_list(&pubid, kLimitedQuirksPublicIdPrefixes, false)
1535
+ || (
1536
+ has_system_identifier
1537
+ && is_in_static_list(&pubid, kSystemIdDependentPublicIdPrefixes, false)
1538
+ )
1539
+ ) {
1540
+ return GUMBO_DOCTYPE_LIMITED_QUIRKS;
1541
+ }
1542
+
1543
+ return GUMBO_DOCTYPE_NO_QUIRKS;
1544
+ }
1545
+
1546
+ static GumboQuirksModeEnum compute_quirks_mode(const GumboTokenDocType* doctype) {
1547
+ if (doctype->force_quirks)
1548
+ return GUMBO_DOCTYPE_QUIRKS;
1549
+ return gumbo_compute_quirks_mode (
1550
+ doctype->name,
1551
+ doctype->has_public_identifier? doctype->public_identifier : NULL,
1552
+ doctype->has_system_identifier? doctype->system_identifier : NULL
1553
+ );
1554
+ }
1555
+
1556
+ // The following functions are all defined by the "has an element in __ scope"
1557
+ // sections of the HTML5 spec:
1558
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-the-specific-scope
1559
+ // The basic idea behind them is that they check for an element of the given
1560
+ // qualified name, contained within a scope formed by a set of other qualified
1561
+ // names. For example, "has an element in list scope" looks for an element of
1562
+ // the given qualified name within the nearest enclosing <ol> or <ul>, along
1563
+ // with a bunch of generic element types that serve to "firewall" their content
1564
+ // from the rest of the document. Note that because of the way the spec is
1565
+ // written,
1566
+ // all elements are expected to be in the HTML namespace
1567
+ static bool has_an_element_in_specific_scope (
1568
+ const GumboParser* parser,
1569
+ int expected_size,
1570
+ const GumboTag* expected,
1571
+ bool negate,
1572
+ const TagSet* tags
1573
+ ) {
1574
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
1575
+ for (int i = open_elements->length; --i >= 0;) {
1576
+ const GumboNode* node = open_elements->data[i];
1577
+ if (node->type != GUMBO_NODE_ELEMENT && node->type != GUMBO_NODE_TEMPLATE) {
1578
+ continue;
1579
+ }
1580
+
1581
+ GumboTag node_tag = node->v.element.tag;
1582
+ GumboNamespaceEnum node_ns = node->v.element.tag_namespace;
1583
+ for (int j = 0; j < expected_size; ++j) {
1584
+ if (node_tag == expected[j] && node_ns == GUMBO_NAMESPACE_HTML) {
1585
+ return true;
1586
+ }
1587
+ }
1588
+
1589
+ bool found = tagset_includes(tags, node_ns, node_tag);
1590
+ if (negate != found) {
1591
+ return false;
1592
+ }
1593
+ }
1594
+ return false;
1595
+ }
1596
+
1597
+ // Checks for the presence of an open element of the specified tag type.
1598
+ static bool has_open_element(const GumboParser* parser, GumboTag tag) {
1599
+ static const TagSet tags = {TAG(HTML)};
1600
+ return has_an_element_in_specific_scope(parser, 1, &tag, false, &tags);
1601
+ }
1602
+
1603
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
1604
+ #define DEFAULT_SCOPE_TAGS \
1605
+ TAG(APPLET), \
1606
+ TAG(CAPTION), \
1607
+ TAG(HTML), \
1608
+ TAG(TABLE), \
1609
+ TAG(TD), \
1610
+ TAG(TH), \
1611
+ TAG(MARQUEE), \
1612
+ TAG(OBJECT), \
1613
+ TAG(TEMPLATE), \
1614
+ TAG_MATHML(MI), \
1615
+ TAG_MATHML(MO), \
1616
+ TAG_MATHML(MN), \
1617
+ TAG_MATHML(MS), \
1618
+ TAG_MATHML(MTEXT), \
1619
+ TAG_MATHML(ANNOTATION_XML), \
1620
+ TAG_SVG(FOREIGNOBJECT), \
1621
+ TAG_SVG(DESC), \
1622
+ TAG_SVG(TITLE)
1623
+
1624
+ static const TagSet heading_tags = {
1625
+ TAG(H1), TAG(H2), TAG(H3), TAG(H4), TAG(H5), TAG(H6)
1626
+ };
1627
+
1628
+ static const TagSet td_th_tags = {
1629
+ TAG(TD), TAG(TH)
1630
+ };
1631
+
1632
+ static const TagSet dd_dt_tags = {
1633
+ TAG(DD), TAG(DT)
1634
+ };
1635
+
1636
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
1637
+ static bool has_an_element_in_scope(const GumboParser* parser, GumboTag tag) {
1638
+ static const TagSet tags = {DEFAULT_SCOPE_TAGS};
1639
+ return has_an_element_in_specific_scope(parser, 1, &tag, false, &tags);
1640
+ }
1641
+
1642
+ // Like "has an element in scope", but for the specific case of looking for a
1643
+ // unique target node, not for any node with a given tag name. This duplicates
1644
+ // much of the algorithm from has_an_element_in_specific_scope because the
1645
+ // predicate is different when checking for an exact node, and it's easier &
1646
+ // faster just to duplicate the code for this one case than to try and
1647
+ // parameterize it.
1648
+ static bool has_node_in_scope(const GumboParser* parser, const GumboNode* node) {
1649
+ static const TagSet tags = {DEFAULT_SCOPE_TAGS};
1650
+ const GumboVector* open_elements = &parser->_parser_state->_open_elements;
1651
+ for (int i = open_elements->length; --i >= 0;) {
1652
+ const GumboNode* current = open_elements->data[i];
1653
+ const GumboNodeType type = current->type;
1654
+ if (current == node) {
1655
+ return true;
1656
+ }
1657
+ if (type != GUMBO_NODE_ELEMENT && type != GUMBO_NODE_TEMPLATE) {
1658
+ continue;
1659
+ }
1660
+ if (node_tag_in_set(current, &tags)) {
1661
+ return false;
1662
+ }
1663
+ }
1664
+ assert(false);
1665
+ return false;
1666
+ }
1667
+
1668
+ // Like has_an_element_in_scope, but restricts the expected qualified name to a
1669
+ // range of possible qualified names instead of just a single one.
1670
+ static bool has_an_element_in_scope_with_tagname (
1671
+ const GumboParser* parser,
1672
+ int len,
1673
+ const GumboTag expected[]
1674
+ ) {
1675
+ static const TagSet tags = {DEFAULT_SCOPE_TAGS};
1676
+ return has_an_element_in_specific_scope(parser, len, expected, false, &tags);
1677
+ }
1678
+
1679
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-list-item-scope
1680
+ static bool has_an_element_in_list_scope(const GumboParser* parser, GumboTag tag) {
1681
+ static const TagSet tags = {DEFAULT_SCOPE_TAGS, TAG(OL), TAG(UL)};
1682
+ return has_an_element_in_specific_scope(parser, 1, &tag, false, &tags);
1683
+ }
1684
+
1685
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-button-scope
1686
+ static bool has_an_element_in_button_scope(const GumboParser* parser, GumboTag tag) {
1687
+ static const TagSet tags = {DEFAULT_SCOPE_TAGS, TAG(BUTTON)};
1688
+ return has_an_element_in_specific_scope(parser, 1, &tag, false, &tags);
1689
+ }
1690
+
1691
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-table-scope
1692
+ static bool has_an_element_in_table_scope(const GumboParser* parser, GumboTag tag) {
1693
+ static const TagSet tags = {TAG(HTML), TAG(TABLE), TAG(TEMPLATE)};
1694
+ return has_an_element_in_specific_scope(parser, 1, &tag, false, &tags);
1695
+ }
1696
+
1697
+ // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-select-scope
1698
+ static bool has_an_element_in_select_scope(const GumboParser* parser, GumboTag tag) {
1699
+ static const TagSet tags = {TAG(OPTGROUP), TAG(OPTION)};
1700
+ return has_an_element_in_specific_scope(parser, 1, &tag, true, &tags);
1701
+ }
1702
+
1703
+ // https://html.spec.whatwg.org/multipage/parsing.html#generate-implied-end-tags
1704
+ // "exception" is the "element to exclude from the process" listed in the spec.
1705
+ // Pass GUMBO_TAG_LAST to not exclude any of them.
1706
+ static void generate_implied_end_tags (
1707
+ GumboParser* parser,
1708
+ GumboTag exception,
1709
+ const char* exception_name
1710
+ ) {
1711
+ static const TagSet tags = {
1712
+ TAG(DD), TAG(DT), TAG(LI), TAG(OPTGROUP), TAG(OPTION),
1713
+ TAG(P), TAG(RB), TAG(RP), TAG(RT), TAG(RTC)
1714
+ };
1715
+ while (
1716
+ node_tag_in_set(get_current_node(parser), &tags)
1717
+ && !node_html_tagname_is(get_current_node(parser), exception, exception_name)
1718
+ ) {
1719
+ pop_current_node(parser);
1720
+ }
1721
+ }
1722
+
1723
+ // This is the "generate all implied end tags thoroughly" clause of the spec.
1724
+ // https://html.spec.whatwg.org/multipage/parsing.html#closing-elements-that-have-implied-end-tags
1725
+ static void generate_all_implied_end_tags_thoroughly(GumboParser* parser) {
1726
+ static const TagSet tags = {
1727
+ TAG(CAPTION), TAG(COLGROUP), TAG(DD), TAG(DT), TAG(LI), TAG(OPTGROUP),
1728
+ TAG(OPTION), TAG(P), TAG(RB), TAG(RP), TAG(RT), TAG(RTC), TAG(TBODY),
1729
+ TAG(TD), TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
1730
+ };
1731
+ while (node_tag_in_set(get_current_node(parser), &tags)) {
1732
+ pop_current_node(parser);
1733
+ }
1734
+ }
1735
+
1736
+ // This factors out the clauses in the "in body" insertion mode checking "if
1737
+ // there is a node in the stack of open elements that is not" one of a list of
1738
+ // elements in which case it's a parse error.
1739
+ // This is used in "an end-of-file token", "an end tag whose tag name is
1740
+ // 'body'", and "an end tag whose tag name is 'html'".
1741
+ static bool stack_contains_nonclosable_element (
1742
+ GumboParser* parser
1743
+ ) {
1744
+ static const TagSet tags = {
1745
+ TAG(DD), TAG(DT), TAG(LI), TAG(OPTGROUP), TAG(OPTION), TAG(P), TAG(RB),
1746
+ TAG(RP), TAG(RT), TAG(RTC), TAG(TBODY), TAG(TD), TAG(TFOOT), TAG(TH),
1747
+ TAG(THEAD), TAG(TR), TAG(BODY), TAG(HTML),
1748
+ };
1749
+ GumboVector* open_elements = &parser->_parser_state->_open_elements;
1750
+ for (size_t i = 0; i < open_elements->length; ++i) {
1751
+ if (!node_tag_in_set(open_elements->data[i], &tags))
1752
+ return true;
1753
+ }
1754
+ return false;
1755
+ }
1756
+
1757
+ // This factors out the clauses relating to "act as if an end tag token with tag
1758
+ // name "table" had been seen. Returns true if there's a table element in table
1759
+ // scope which was successfully closed, false if not and the token should be
1760
+ // ignored. Does not add parse errors; callers should handle that.
1761
+ static bool close_table(GumboParser* parser) {
1762
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TABLE)) {
1763
+ return false;
1764
+ }
1765
+
1766
+ GumboNode* node = pop_current_node(parser);
1767
+ while (!node_html_tag_is(node, GUMBO_TAG_TABLE)) {
1768
+ node = pop_current_node(parser);
1769
+ }
1770
+ reset_insertion_mode_appropriately(parser);
1771
+ return true;
1772
+ }
1773
+
1774
+ // This factors out the clauses relating to "act as if an end tag token with tag
1775
+ // name `cell_tag` had been seen".
1776
+ static void close_table_cell (
1777
+ GumboParser* parser,
1778
+ const GumboToken* token,
1779
+ GumboTag cell_tag
1780
+ ) {
1781
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
1782
+ const GumboNode* node = get_current_node(parser);
1783
+ if (!node_html_tag_is(node, cell_tag))
1784
+ parser_add_parse_error(parser, token);
1785
+ do {
1786
+ node = pop_current_node(parser);
1787
+ } while (!node_html_tag_is(node, cell_tag));
1788
+
1789
+ clear_active_formatting_elements(parser);
1790
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
1791
+ }
1792
+
1793
+ // https://html.spec.whatwg.org/multipage/parsing.html#close-the-cell
1794
+ // This holds the logic to determine whether we should close a <td> or a <th>.
1795
+ static void close_current_cell(GumboParser* parser, const GumboToken* token) {
1796
+ GumboTag cell_tag;
1797
+ if (has_an_element_in_table_scope(parser, GUMBO_TAG_TD)) {
1798
+ assert(!has_an_element_in_table_scope(parser, GUMBO_TAG_TH));
1799
+ cell_tag = GUMBO_TAG_TD;
1800
+ } else {
1801
+ assert(has_an_element_in_table_scope(parser, GUMBO_TAG_TH));
1802
+ cell_tag = GUMBO_TAG_TH;
1803
+ }
1804
+ close_table_cell(parser, token, cell_tag);
1805
+ }
1806
+
1807
+ // This factors out the "act as if an end tag of tag name 'select' had been
1808
+ // seen" clause of the spec, since it's referenced in several places. It pops
1809
+ // all nodes from the stack until the current <select> has been closed, then
1810
+ // resets the insertion mode appropriately.
1811
+ static void close_current_select(GumboParser* parser) {
1812
+ GumboNode* node = pop_current_node(parser);
1813
+ while (!node_html_tag_is(node, GUMBO_TAG_SELECT)) {
1814
+ node = pop_current_node(parser);
1815
+ }
1816
+ reset_insertion_mode_appropriately(parser);
1817
+ }
1818
+
1819
+ // The list of nodes in the "special" category:
1820
+ // https://html.spec.whatwg.org/multipage/parsing.html#special
1821
+ static bool is_special_node(const GumboNode* node) {
1822
+ assert(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE);
1823
+ return node_tag_in_set(node, &(const TagSet) {
1824
+ TAG(ADDRESS), TAG(APPLET), TAG(AREA), TAG(ARTICLE),
1825
+ TAG(ASIDE), TAG(BASE), TAG(BASEFONT), TAG(BGSOUND), TAG(BLOCKQUOTE),
1826
+ TAG(BODY), TAG(BR), TAG(BUTTON), TAG(CAPTION), TAG(CENTER), TAG(COL),
1827
+ TAG(COLGROUP), TAG(DD), TAG(DETAILS), TAG(DIR),
1828
+ TAG(DIV), TAG(DL), TAG(DT), TAG(EMBED), TAG(FIELDSET),
1829
+ TAG(FIGCAPTION), TAG(FIGURE), TAG(FOOTER), TAG(FORM), TAG(FRAME),
1830
+ TAG(FRAMESET), TAG(H1), TAG(H2), TAG(H3), TAG(H4), TAG(H5), TAG(H6),
1831
+ TAG(HEAD), TAG(HEADER), TAG(HGROUP), TAG(HR), TAG(HTML), TAG(IFRAME),
1832
+ TAG(IMG), TAG(INPUT), TAG(LI), TAG(LINK), TAG(LISTING),
1833
+ TAG(MARQUEE), TAG(MENU), TAG(META), TAG(NAV), TAG(NOEMBED),
1834
+ TAG(NOFRAMES), TAG(NOSCRIPT), TAG(OBJECT), TAG(OL), TAG(P),
1835
+ TAG(PARAM), TAG(PLAINTEXT), TAG(PRE), TAG(SCRIPT), TAG(SECTION),
1836
+ TAG(SELECT), TAG(STYLE), TAG(SUMMARY), TAG(TABLE), TAG(TBODY),
1837
+ TAG(TD), TAG(TEMPLATE), TAG(TEXTAREA), TAG(TFOOT), TAG(TH),
1838
+ TAG(THEAD), TAG(TR), TAG(UL), TAG(WBR), TAG(XMP),
1839
+
1840
+ TAG_MATHML(MI), TAG_MATHML(MO), TAG_MATHML(MN), TAG_MATHML(MS),
1841
+ TAG_MATHML(MTEXT), TAG_MATHML(ANNOTATION_XML),
1842
+
1843
+ TAG_SVG(FOREIGNOBJECT), TAG_SVG(DESC),
1844
+
1845
+ // This TagSet needs to include the "title" element in both the
1846
+ // HTML and SVG namespaces. Using both TAG(TITLE) and TAG_SVG(TITLE)
1847
+ // won't work, due to the simplistic way in which the TAG macros are
1848
+ // implemented, so we do it like this instead:
1849
+ [GUMBO_TAG_TITLE] =
1850
+ (1 << GUMBO_NAMESPACE_HTML) |
1851
+ (1 << GUMBO_NAMESPACE_SVG)
1852
+ }
1853
+ );
1854
+ }
1855
+
1856
+ // Implicitly closes currently open elements until it reaches an element with
1857
+ // the
1858
+ // specified qualified name. If the elements closed are in the set handled by
1859
+ // generate_implied_end_tags, this is normal operation and this function returns
1860
+ // true. Otherwise, a parse error is recorded and this function returns false.
1861
+ static void implicitly_close_tags (
1862
+ GumboParser* parser,
1863
+ GumboToken* token,
1864
+ GumboNamespaceEnum target_ns,
1865
+ GumboTag target
1866
+ ) {
1867
+ assert(target != GUMBO_TAG_UNKNOWN);
1868
+ generate_implied_end_tags(parser, target, NULL);
1869
+ if (!node_qualified_tag_is(get_current_node(parser), target_ns, target)) {
1870
+ parser_add_parse_error(parser, token);
1871
+ while (
1872
+ !node_qualified_tag_is(get_current_node(parser), target_ns, target)
1873
+ ) {
1874
+ pop_current_node(parser);
1875
+ }
1876
+ }
1877
+ assert(node_qualified_tag_is(get_current_node(parser), target_ns, target));
1878
+ pop_current_node(parser);
1879
+ }
1880
+
1881
+ // If the stack of open elements has a <p> tag in button scope, this acts as if
1882
+ // a </p> tag was encountered, implicitly closing tags. Returns false if a
1883
+ // parse error occurs. This is a convenience function because this particular
1884
+ // clause appears several times in the spec.
1885
+ static void maybe_implicitly_close_p_tag (
1886
+ GumboParser* parser,
1887
+ GumboToken* token
1888
+ ) {
1889
+ if (has_an_element_in_button_scope(parser, GUMBO_TAG_P)) {
1890
+ implicitly_close_tags (
1891
+ parser,
1892
+ token,
1893
+ GUMBO_NAMESPACE_HTML,
1894
+ GUMBO_TAG_P
1895
+ );
1896
+ }
1897
+ }
1898
+
1899
+ // Convenience function to encapsulate the logic for closing <li> or <dd>/<dt>
1900
+ // tags. Pass true to is_li for handling <li> tags, false for <dd> and <dt>.
1901
+ static void maybe_implicitly_close_list_tag (
1902
+ GumboParser* parser,
1903
+ GumboToken* token,
1904
+ bool is_li
1905
+ ) {
1906
+ GumboParserState* state = parser->_parser_state;
1907
+ set_frameset_not_ok(parser);
1908
+ for (int i = state->_open_elements.length; --i >= 0;) {
1909
+ const GumboNode* node = state->_open_elements.data[i];
1910
+ bool is_list_tag = is_li
1911
+ ? node_html_tag_is(node, GUMBO_TAG_LI)
1912
+ : node_tag_in_set(node, &dd_dt_tags)
1913
+ ;
1914
+ if (is_list_tag) {
1915
+ implicitly_close_tags (
1916
+ parser,
1917
+ token,
1918
+ node->v.element.tag_namespace,
1919
+ node->v.element.tag
1920
+ );
1921
+ return;
1922
+ }
1923
+
1924
+ if (
1925
+ is_special_node(node)
1926
+ && !node_tag_in_set(node, &(const TagSet){TAG(ADDRESS), TAG(DIV), TAG(P)})
1927
+ ) {
1928
+ return;
1929
+ }
1930
+ }
1931
+ }
1932
+
1933
+ static void merge_attributes (
1934
+ GumboToken* token,
1935
+ GumboNode* node
1936
+ ) {
1937
+ assert(token->type == GUMBO_TOKEN_START_TAG);
1938
+ assert(node->type == GUMBO_NODE_ELEMENT);
1939
+ const GumboVector* token_attr = &token->v.start_tag.attributes;
1940
+ GumboVector* node_attr = &node->v.element.attributes;
1941
+
1942
+ for (unsigned int i = 0; i < token_attr->length; ++i) {
1943
+ GumboAttribute* attr = token_attr->data[i];
1944
+ if (!gumbo_get_attribute(node_attr, attr->name)) {
1945
+ // Ownership of the attribute is transferred by this gumbo_vector_add,
1946
+ // so it has to be nulled out of the original token so it doesn't get
1947
+ // double-deleted.
1948
+ gumbo_vector_add(attr, node_attr);
1949
+ token_attr->data[i] = NULL;
1950
+ }
1951
+ }
1952
+ // When attributes are merged, it means the token has been ignored and merged
1953
+ // with another token, so we need to free its memory. The attributes that are
1954
+ // transferred need to be nulled-out in the vector above so that they aren't
1955
+ // double-deleted.
1956
+ gumbo_token_destroy(token);
1957
+
1958
+ #ifndef NDEBUG
1959
+ // Mark this sentinel so the assertion in the main loop knows it's been
1960
+ // destroyed.
1961
+ token->v.start_tag.attributes = kGumboEmptyVector;
1962
+ #endif
1963
+ }
1964
+
1965
+ const char* gumbo_normalize_svg_tagname(const GumboStringPiece* tag) {
1966
+ const StringReplacement *replacement = gumbo_get_svg_tag_replacement (
1967
+ tag->data,
1968
+ tag->length
1969
+ );
1970
+ return replacement ? replacement->to : NULL;
1971
+ }
1972
+
1973
+ // https://html.spec.whatwg.org/multipage/parsing.html#adjust-foreign-attributes
1974
+ // This destructively modifies any matching attributes on the token and sets the
1975
+ // namespace appropriately.
1976
+ static void adjust_foreign_attributes(GumboToken* token) {
1977
+ assert(token->type == GUMBO_TOKEN_START_TAG);
1978
+ const GumboVector* attributes = &token->v.start_tag.attributes;
1979
+ for (unsigned int i = 0, n = attributes->length; i < n; ++i) {
1980
+ GumboAttribute* attr = attributes->data[i];
1981
+ const ForeignAttrReplacement* entry = gumbo_get_foreign_attr_replacement (
1982
+ attr->name,
1983
+ strlen(attr->name)
1984
+ );
1985
+ if (!entry) {
1986
+ continue;
1987
+ }
1988
+ gumbo_free((void*) attr->name);
1989
+ attr->attr_namespace = entry->attr_namespace;
1990
+ attr->name = gumbo_strdup(entry->local_name);
1991
+ }
1992
+ }
1993
+
1994
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign
1995
+ // This adjusts svg tags.
1996
+ static void adjust_svg_tag(GumboToken* token) {
1997
+ assert(token->type == GUMBO_TOKEN_START_TAG);
1998
+ if (token->v.start_tag.tag == GUMBO_TAG_FOREIGNOBJECT) {
1999
+ assert(token->v.start_tag.name == NULL);
2000
+ token->v.start_tag.name = (char *)"foreignObject";
2001
+ } else if (token->v.start_tag.tag == GUMBO_TAG_UNKNOWN) {
2002
+ assert(token->v.start_tag.name);
2003
+ const StringReplacement *replacement = gumbo_get_svg_tag_replacement(
2004
+ token->v.start_tag.name,
2005
+ strlen(token->v.start_tag.name)
2006
+ );
2007
+ if (replacement) {
2008
+ // This cast is safe because we allocated this memory and we'll free it.
2009
+ strcpy((char *)token->v.start_tag.name, replacement->to);
2010
+ }
2011
+ }
2012
+ }
2013
+
2014
+ // https://html.spec.whatwg.org/multipage/parsing.html#adjust-svg-attributes
2015
+ // This destructively modifies any matching attributes on the token.
2016
+ static void adjust_svg_attributes(GumboToken* token) {
2017
+ assert(token->type == GUMBO_TOKEN_START_TAG);
2018
+ const GumboVector* attributes = &token->v.start_tag.attributes;
2019
+ for (unsigned int i = 0, n = attributes->length; i < n; i++) {
2020
+ GumboAttribute* attr = (GumboAttribute*) attributes->data[i];
2021
+ const StringReplacement* replacement = gumbo_get_svg_attr_replacement (
2022
+ attr->name,
2023
+ attr->original_name.length
2024
+ );
2025
+ if (!replacement) {
2026
+ continue;
2027
+ }
2028
+ gumbo_free((void*) attr->name);
2029
+ attr->name = gumbo_strdup(replacement->to);
2030
+ }
2031
+ }
2032
+
2033
+ // https://html.spec.whatwg.org/multipage/parsing.html#adjust-mathml-attributes
2034
+ // Note that this may destructively modify the token with the new attribute
2035
+ // value.
2036
+ static void adjust_mathml_attributes(GumboToken* token) {
2037
+ assert(token->type == GUMBO_TOKEN_START_TAG);
2038
+ GumboAttribute* attr = gumbo_get_attribute (
2039
+ &token->v.start_tag.attributes,
2040
+ "definitionurl"
2041
+ );
2042
+ if (!attr) {
2043
+ return;
2044
+ }
2045
+ gumbo_free((void*) attr->name);
2046
+ attr->name = gumbo_strdup("definitionURL");
2047
+ }
2048
+
2049
+ static void maybe_add_doctype_error (
2050
+ GumboParser* parser,
2051
+ const GumboToken* token
2052
+ ) {
2053
+ const GumboTokenDocType* doctype = &token->v.doc_type;
2054
+ if (
2055
+ strcmp(doctype->name, "html")
2056
+ || doctype->has_public_identifier
2057
+ || (doctype->has_system_identifier
2058
+ && strcmp(doctype->system_identifier, "about:legacy-compat"))
2059
+ ) {
2060
+ parser_add_parse_error(parser, token);
2061
+ }
2062
+ }
2063
+
2064
+ static void remove_from_parent(GumboNode* node) {
2065
+ if (!node->parent) {
2066
+ // The node may not have a parent if, for example, it is a newly-cloned copy
2067
+ // of an active formatting element. DOM manipulations continue with the
2068
+ // orphaned fragment of the DOM tree until it's appended/foster-parented to
2069
+ // the common ancestor at the end of the adoption agency algorithm.
2070
+ return;
2071
+ }
2072
+ assert(node->parent->type == GUMBO_NODE_ELEMENT);
2073
+ GumboVector* children = &node->parent->v.element.children;
2074
+ int index = gumbo_vector_index_of(children, node);
2075
+ assert(index != -1);
2076
+
2077
+ gumbo_vector_remove_at(index, children);
2078
+ node->parent = NULL;
2079
+ node->index_within_parent = -1;
2080
+ for (unsigned int i = index; i < children->length; ++i) {
2081
+ GumboNode* child = children->data[i];
2082
+ child->index_within_parent = i;
2083
+ }
2084
+ }
2085
+
2086
+ // This is here to clean up memory when the spec says "Ignore current token."
2087
+ static void ignore_token(GumboParser* parser) {
2088
+ gumbo_debug("Ignoring token.\n");
2089
+ GumboToken* token = parser->_parser_state->_current_token;
2090
+ // Ownership of the token's internal buffers are normally transferred to the
2091
+ // element, but if no element is emitted (as happens in non-verbatim-mode
2092
+ // when a token is ignored), we need to free it here to prevent a memory
2093
+ // leak.
2094
+ gumbo_token_destroy(token);
2095
+ #ifndef NDEBUG
2096
+ if (token->type == GUMBO_TOKEN_START_TAG) {
2097
+ // Mark this sentinel so the assertion in the main loop knows it's been
2098
+ // destroyed.
2099
+ token->v.start_tag.attributes = kGumboEmptyVector;
2100
+ token->v.start_tag.name = NULL;
2101
+ }
2102
+ #endif
2103
+ }
2104
+
2105
+ // The token is usually an end tag; however, the adoption agency algorithm may
2106
+ // invoke this for an 'a' or 'nobr' start tag.
2107
+ // Returns false if there was an error.
2108
+ static void in_body_any_other_end_tag(GumboParser* parser, GumboToken* token)
2109
+ {
2110
+ GumboParserState* state = parser->_parser_state;
2111
+ GumboTag tag;
2112
+ const char* tagname;
2113
+
2114
+ if (token->type == GUMBO_TOKEN_END_TAG) {
2115
+ tag = token->v.end_tag.tag;
2116
+ tagname = token->v.end_tag.name;
2117
+ } else {
2118
+ assert(token->type == GUMBO_TOKEN_START_TAG);
2119
+ tag = token->v.start_tag.tag;
2120
+ tagname = token->v.start_tag.name;
2121
+ }
2122
+
2123
+ assert(state->_open_elements.length > 0);
2124
+ assert(node_html_tag_is(state->_open_elements.data[0], GUMBO_TAG_HTML));
2125
+ // Walk up the stack of open elements until we find one that either:
2126
+ // a) Matches the tag name we saw
2127
+ // b) Is in the "special" category.
2128
+ // If we see a), implicitly close everything up to and including it. If we
2129
+ // see b), then record a parse error, don't close anything (except the
2130
+ // implied end tags) and ignore the end tag token.
2131
+ for (int i = state->_open_elements.length; --i >= 0;) {
2132
+ const GumboNode* node = state->_open_elements.data[i];
2133
+ if (node_qualified_tagname_is(node, GUMBO_NAMESPACE_HTML, tag, tagname)) {
2134
+ generate_implied_end_tags(parser, tag, tagname);
2135
+ // <!DOCTYPE><body><sarcasm><foo></sarcasm> is an example of an error.
2136
+ // foo is the "current node" but sarcasm is node.
2137
+ // XXX: Write a test for this.
2138
+ if (node != get_current_node(parser)) {
2139
+ parser_add_parse_error(parser, token);
2140
+ }
2141
+ while (node != pop_current_node(parser))
2142
+ ; // Pop everything.
2143
+ return;
2144
+ } else if (is_special_node(node)) {
2145
+ parser_add_parse_error(parser, token);
2146
+ ignore_token(parser);
2147
+ return;
2148
+ }
2149
+ }
2150
+ // <html> is in the special category, so we should never get here.
2151
+ assert(0 && "unreachable");
2152
+ }
2153
+
2154
+ // https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser
2155
+ // Also described in the "in body" handling for end formatting tags.
2156
+ // Returns false if there was an error.
2157
+ static void adoption_agency_algorithm(GumboParser* parser, GumboToken* token)
2158
+ {
2159
+ GumboParserState* state = parser->_parser_state;
2160
+ gumbo_debug("Entering adoption agency algorithm.\n");
2161
+ // Step 1.
2162
+ GumboTag subject;
2163
+ if (token->type == GUMBO_TOKEN_START_TAG) {
2164
+ subject = token->v.start_tag.tag;
2165
+ } else {
2166
+ assert(token->type == GUMBO_TOKEN_END_TAG);
2167
+ subject = token->v.end_tag.tag;
2168
+ }
2169
+ assert(subject != GUMBO_TAG_UNKNOWN);
2170
+
2171
+ // Step 2.
2172
+ GumboNode* current_node = get_current_node(parser);
2173
+ if (
2174
+ node_html_tag_is(current_node, subject)
2175
+ && -1 == gumbo_vector_index_of (
2176
+ &state->_active_formatting_elements,
2177
+ current_node
2178
+ )
2179
+ ) {
2180
+ pop_current_node(parser);
2181
+ return;
2182
+ }
2183
+
2184
+ // Steps 3-5 & 21:
2185
+ for (unsigned int i = 0; i < 8; ++i) {
2186
+ // Step 6.
2187
+ GumboNode* formatting_node = NULL;
2188
+ int formatting_node_in_open_elements = -1;
2189
+ for (int j = state->_active_formatting_elements.length; --j >= 0;) {
2190
+ GumboNode* current_node = state->_active_formatting_elements.data[j];
2191
+ if (current_node == &kActiveFormattingScopeMarker) {
2192
+ gumbo_debug("Broke on scope marker; aborting.\n");
2193
+ // Last scope marker; abort the algorithm and handle according to "any
2194
+ // other end tag" (below).
2195
+ break;
2196
+ }
2197
+ if (node_html_tag_is(current_node, subject)) {
2198
+ // Found it.
2199
+ formatting_node = current_node;
2200
+ formatting_node_in_open_elements = gumbo_vector_index_of (
2201
+ &state->_open_elements,
2202
+ formatting_node
2203
+ );
2204
+ gumbo_debug (
2205
+ "Formatting element of tag %s at %d.\n",
2206
+ gumbo_normalized_tagname(subject),
2207
+ formatting_node_in_open_elements
2208
+ );
2209
+ break;
2210
+ }
2211
+ }
2212
+ if (!formatting_node) {
2213
+ // No matching tag; not a parse error outright, but fall through to the
2214
+ // "any other end tag" clause (which may potentially add a parse error,
2215
+ // but not always).
2216
+ gumbo_debug("No active formatting elements; aborting.\n");
2217
+ in_body_any_other_end_tag(parser, token);
2218
+ return;
2219
+ }
2220
+
2221
+ // Step 7
2222
+ if (formatting_node_in_open_elements == -1) {
2223
+ gumbo_debug("Formatting node not on stack of open elements.\n");
2224
+ parser_add_parse_error(parser, token);
2225
+ gumbo_vector_remove (
2226
+ formatting_node,
2227
+ &state->_active_formatting_elements
2228
+ );
2229
+ return;
2230
+ }
2231
+
2232
+ // Step 8
2233
+ if (!has_an_element_in_scope(parser, formatting_node->v.element.tag)) {
2234
+ parser_add_parse_error(parser, token);
2235
+ gumbo_debug("Element not in scope.\n");
2236
+ return;
2237
+ }
2238
+
2239
+ // Step 9
2240
+ if (formatting_node != get_current_node(parser))
2241
+ parser_add_parse_error(parser, token); // But continue onwards.
2242
+ assert(formatting_node);
2243
+ assert(!node_html_tag_is(formatting_node, GUMBO_TAG_HTML));
2244
+ assert(!node_html_tag_is(formatting_node, GUMBO_TAG_BODY));
2245
+
2246
+ // Step 10
2247
+ GumboNode* furthest_block = NULL;
2248
+ for (
2249
+ unsigned int j = formatting_node_in_open_elements;
2250
+ j < state->_open_elements.length;
2251
+ ++j
2252
+ ) {
2253
+ assert(j > 0);
2254
+ GumboNode* current = state->_open_elements.data[j];
2255
+ if (is_special_node(current)) {
2256
+ furthest_block = current;
2257
+ break;
2258
+ }
2259
+ }
2260
+ // Step 11.
2261
+ if (!furthest_block) {
2262
+ while (pop_current_node(parser) != formatting_node)
2263
+ ;
2264
+ gumbo_vector_remove (
2265
+ formatting_node,
2266
+ &state->_active_formatting_elements
2267
+ );
2268
+ return;
2269
+ }
2270
+ assert(!node_html_tag_is(furthest_block, GUMBO_TAG_HTML));
2271
+
2272
+ // Step 12.
2273
+ // Elements may be moved and reparented by this algorithm, so
2274
+ // common_ancestor is not necessarily the same as formatting_node->parent.
2275
+ GumboNode* common_ancestor = state->_open_elements.data [
2276
+ formatting_node_in_open_elements - 1
2277
+ ];
2278
+ gumbo_debug (
2279
+ "Common ancestor tag = %s, furthest block tag = %s.\n",
2280
+ gumbo_normalized_tagname(common_ancestor->v.element.tag),
2281
+ gumbo_normalized_tagname(furthest_block->v.element.tag)
2282
+ );
2283
+
2284
+ // Step 13.
2285
+ int bookmark = 1 + gumbo_vector_index_of (
2286
+ &state->_active_formatting_elements,
2287
+ formatting_node
2288
+ );
2289
+ gumbo_debug("Bookmark at %d.\n", bookmark);
2290
+ // Step 14.
2291
+ GumboNode* node = furthest_block;
2292
+ GumboNode* last_node = furthest_block;
2293
+ // Must be stored explicitly, in case node is removed from the stack of open
2294
+ // elements, to handle step 14.3.
2295
+ int saved_node_index = gumbo_vector_index_of(&state->_open_elements, node);
2296
+ assert(saved_node_index > 0);
2297
+ // Step 14.1.
2298
+ for (int j = 0;;) {
2299
+ // Step 14.2.
2300
+ ++j;
2301
+ // Step 14.3.
2302
+ int node_index = gumbo_vector_index_of(&state->_open_elements, node);
2303
+ gumbo_debug (
2304
+ "Current index: %d, last index: %d.\n",
2305
+ node_index,
2306
+ saved_node_index
2307
+ );
2308
+ if (node_index == -1) {
2309
+ node_index = saved_node_index;
2310
+ }
2311
+ saved_node_index = --node_index;
2312
+ assert(node_index > 0);
2313
+ assert((unsigned int) node_index < state->_open_elements.capacity);
2314
+ node = state->_open_elements.data[node_index];
2315
+ assert(node->parent);
2316
+ // Step 14.4.
2317
+ if (node == formatting_node) {
2318
+ break;
2319
+ }
2320
+ int formatting_index = gumbo_vector_index_of (
2321
+ &state->_active_formatting_elements,
2322
+ node
2323
+ );
2324
+ // Step 14.5.
2325
+ if (j > 3 && formatting_index != -1) {
2326
+ gumbo_debug("Removing formatting element at %d.\n", formatting_index);
2327
+ gumbo_vector_remove_at (
2328
+ formatting_index,
2329
+ &state->_active_formatting_elements
2330
+ );
2331
+ // Removing the element shifts all indices over by one, so we may need
2332
+ // to move the bookmark.
2333
+ if (formatting_index < bookmark) {
2334
+ --bookmark;
2335
+ gumbo_debug("Moving bookmark to %d.\n", bookmark);
2336
+ }
2337
+ continue;
2338
+ }
2339
+ if (formatting_index == -1) {
2340
+ // Step 14.6.
2341
+ gumbo_vector_remove_at(node_index, &state->_open_elements);
2342
+ continue;
2343
+ }
2344
+ // Step 14.7.
2345
+ // "common ancestor as the intended parent" doesn't actually mean insert
2346
+ // it into the common ancestor; that happens below.
2347
+ node = clone_node(node, GUMBO_INSERTION_ADOPTION_AGENCY_CLONED);
2348
+ assert(formatting_index >= 0);
2349
+ state->_active_formatting_elements.data[formatting_index] = node;
2350
+ assert(node_index >= 0);
2351
+ state->_open_elements.data[node_index] = node;
2352
+ // Step 14.8.
2353
+ if (last_node == furthest_block) {
2354
+ bookmark = formatting_index + 1;
2355
+ gumbo_debug("Bookmark moved to %d.\n", bookmark);
2356
+ assert((unsigned int) bookmark <= state->_active_formatting_elements.length);
2357
+ }
2358
+ // Step 14.9.
2359
+ last_node->parse_flags |= GUMBO_INSERTION_ADOPTION_AGENCY_MOVED;
2360
+ remove_from_parent(last_node);
2361
+ append_node(node, last_node);
2362
+ // Step 14.10.
2363
+ last_node = node;
2364
+ } // Step 14.11.
2365
+
2366
+ // Step 15.
2367
+ gumbo_debug (
2368
+ "Removing %s node from parent ",
2369
+ gumbo_normalized_tagname(last_node->v.element.tag)
2370
+ );
2371
+ remove_from_parent(last_node);
2372
+ last_node->parse_flags |= GUMBO_INSERTION_ADOPTION_AGENCY_MOVED;
2373
+ InsertionLocation location = get_appropriate_insertion_location (
2374
+ parser,
2375
+ common_ancestor
2376
+ );
2377
+ gumbo_debug (
2378
+ "and inserting it into %s.\n",
2379
+ gumbo_normalized_tagname(location.target->v.element.tag)
2380
+ );
2381
+ insert_node(last_node, location);
2382
+
2383
+ // Step 16.
2384
+ GumboNode* new_formatting_node = clone_node (
2385
+ formatting_node,
2386
+ GUMBO_INSERTION_ADOPTION_AGENCY_CLONED
2387
+ );
2388
+ formatting_node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
2389
+
2390
+ // Step 17. Instead of appending nodes one-by-one, we swap the children
2391
+ // vector of furthest_block with the empty children of new_formatting_node,
2392
+ // reducing memory traffic and allocations. We still have to reset their
2393
+ // parent pointers, though.
2394
+ GumboVector temp = new_formatting_node->v.element.children;
2395
+ new_formatting_node->v.element.children = furthest_block->v.element.children;
2396
+ furthest_block->v.element.children = temp;
2397
+
2398
+ temp = new_formatting_node->v.element.children;
2399
+ for (unsigned int i = 0; i < temp.length; ++i) {
2400
+ GumboNode* child = temp.data[i];
2401
+ child->parent = new_formatting_node;
2402
+ }
2403
+
2404
+ // Step 18.
2405
+ append_node(furthest_block, new_formatting_node);
2406
+
2407
+ // Step 19.
2408
+ // If the formatting node was before the bookmark, it may shift over all
2409
+ // indices after it, so we need to explicitly find the index and possibly
2410
+ // adjust the bookmark.
2411
+ int formatting_node_index = gumbo_vector_index_of (
2412
+ &state->_active_formatting_elements,
2413
+ formatting_node
2414
+ );
2415
+ assert(formatting_node_index != -1);
2416
+ if (formatting_node_index < bookmark) {
2417
+ gumbo_debug (
2418
+ "Formatting node at %d is before bookmark at %d; decrementing.\n",
2419
+ formatting_node_index, bookmark
2420
+ );
2421
+ --bookmark;
2422
+ }
2423
+ gumbo_vector_remove_at (
2424
+ formatting_node_index,
2425
+ &state->_active_formatting_elements
2426
+ );
2427
+ assert(bookmark >= 0);
2428
+ assert((unsigned int) bookmark <= state->_active_formatting_elements.length);
2429
+ gumbo_vector_insert_at (
2430
+ new_formatting_node,
2431
+ bookmark,
2432
+ &state->_active_formatting_elements
2433
+ );
2434
+
2435
+ // Step 20.
2436
+ gumbo_vector_remove(formatting_node, &state->_open_elements);
2437
+ int insert_at = 1 + gumbo_vector_index_of (
2438
+ &state->_open_elements,
2439
+ furthest_block
2440
+ );
2441
+ assert(insert_at >= 0);
2442
+ assert((unsigned int) insert_at <= state->_open_elements.length);
2443
+ gumbo_vector_insert_at (
2444
+ new_formatting_node,
2445
+ insert_at,
2446
+ &state->_open_elements
2447
+ );
2448
+ } // Step 21.
2449
+ }
2450
+
2451
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-end
2452
+ static void finish_parsing(GumboParser* parser) {
2453
+ gumbo_debug("Finishing parsing\n");
2454
+ maybe_flush_text_node_buffer(parser);
2455
+ GumboParserState* state = parser->_parser_state;
2456
+ for (
2457
+ GumboNode* node = pop_current_node(parser);
2458
+ node;
2459
+ node = pop_current_node(parser)
2460
+ ) {
2461
+ if (
2462
+ (node_html_tag_is(node, GUMBO_TAG_BODY) && state->_closed_body_tag)
2463
+ || (node_html_tag_is(node, GUMBO_TAG_HTML) && state->_closed_html_tag)
2464
+ ) {
2465
+ continue;
2466
+ }
2467
+ node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
2468
+ }
2469
+ while (pop_current_node(parser))
2470
+ ; // Pop them all.
2471
+ }
2472
+
2473
+ static void handle_initial(GumboParser* parser, GumboToken* token) {
2474
+ GumboDocument* document = &get_document_node(parser)->v.document;
2475
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2476
+ ignore_token(parser);
2477
+ return;
2478
+ }
2479
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2480
+ append_comment_node(parser, get_document_node(parser), token);
2481
+ return;
2482
+ }
2483
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2484
+ document->has_doctype = true;
2485
+ document->name = token->v.doc_type.name;
2486
+ document->public_identifier = token->v.doc_type.public_identifier;
2487
+ document->system_identifier = token->v.doc_type.system_identifier;
2488
+ document->doc_type_quirks_mode = compute_quirks_mode(&token->v.doc_type);
2489
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HTML);
2490
+ maybe_add_doctype_error(parser, token);
2491
+ return;
2492
+ }
2493
+ parser_add_parse_error(parser, token);
2494
+ document->doc_type_quirks_mode = GUMBO_DOCTYPE_QUIRKS;
2495
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HTML);
2496
+ parser->_parser_state->_reprocess_current_token = true;
2497
+ }
2498
+
2499
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-before-html-insertion-mode
2500
+ static void handle_before_html(GumboParser* parser, GumboToken* token) {
2501
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2502
+ parser_add_parse_error(parser, token);
2503
+ ignore_token(parser);
2504
+ return;
2505
+ }
2506
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2507
+ append_comment_node(parser, get_document_node(parser), token);
2508
+ return;
2509
+ }
2510
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2511
+ ignore_token(parser);
2512
+ return;
2513
+ }
2514
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2515
+ GumboNode* html_node = insert_element_from_token(parser, token);
2516
+ parser->_output->root = html_node;
2517
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HEAD);
2518
+ return;
2519
+ }
2520
+ if (
2521
+ token->type == GUMBO_TOKEN_END_TAG
2522
+ && !tag_in(token, false, &(const TagSet){TAG(HEAD), TAG(BODY), TAG(HTML), TAG(BR)})
2523
+ ) {
2524
+ parser_add_parse_error(parser, token);
2525
+ ignore_token(parser);
2526
+ return;
2527
+ }
2528
+ GumboNode* html_node = insert_element_of_tag_type (
2529
+ parser,
2530
+ GUMBO_TAG_HTML,
2531
+ GUMBO_INSERTION_IMPLIED
2532
+ );
2533
+ assert(html_node);
2534
+ parser->_output->root = html_node;
2535
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HEAD);
2536
+ parser->_parser_state->_reprocess_current_token = true;
2537
+ }
2538
+
2539
+ // Forward declarations because of mutual dependencies.
2540
+ static void handle_token(GumboParser* parser, GumboToken* token);
2541
+ static void handle_in_body(GumboParser* parser, GumboToken* token);
2542
+ static void handle_in_template(GumboParser* parser, GumboToken* token);
2543
+
2544
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode
2545
+ static void handle_before_head(GumboParser* parser, GumboToken* token) {
2546
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2547
+ ignore_token(parser);
2548
+ return;
2549
+ }
2550
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2551
+ append_comment_node(parser, get_current_node(parser), token);
2552
+ return;
2553
+ }
2554
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2555
+ parser_add_parse_error(parser, token);
2556
+ ignore_token(parser);
2557
+ return;
2558
+ }
2559
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2560
+ handle_in_body(parser, token);
2561
+ return;
2562
+ }
2563
+ if (tag_is(token, kStartTag, GUMBO_TAG_HEAD)) {
2564
+ GumboNode* node = insert_element_from_token(parser, token);
2565
+ parser->_parser_state->_head_element = node;
2566
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
2567
+ return;
2568
+ }
2569
+ if (
2570
+ token->type == GUMBO_TOKEN_END_TAG
2571
+ && !tag_in(token, kEndTag, &(const TagSet){TAG(HEAD), TAG(BODY), TAG(HTML), TAG(BR)})
2572
+ ) {
2573
+ parser_add_parse_error(parser, token);
2574
+ ignore_token(parser);
2575
+ return;
2576
+ }
2577
+ GumboNode* node = insert_element_of_tag_type (
2578
+ parser,
2579
+ GUMBO_TAG_HEAD,
2580
+ GUMBO_INSERTION_IMPLIED
2581
+ );
2582
+ parser->_parser_state->_head_element = node;
2583
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
2584
+ parser->_parser_state->_reprocess_current_token = true;
2585
+ }
2586
+
2587
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead
2588
+ static void handle_in_head(GumboParser* parser, GumboToken* token) {
2589
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2590
+ insert_text_token(parser, token);
2591
+ return;
2592
+ }
2593
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2594
+ append_comment_node(parser, get_current_node(parser), token);
2595
+ return;
2596
+ }
2597
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2598
+ parser_add_parse_error(parser, token);
2599
+ ignore_token(parser);
2600
+ return;
2601
+ }
2602
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2603
+ return handle_in_body(parser, token);
2604
+ }
2605
+ if (
2606
+ tag_in(token, kStartTag, &(const TagSet) {
2607
+ TAG(BASE), TAG(BASEFONT), TAG(BGSOUND), TAG(LINK)
2608
+ })
2609
+ ) {
2610
+ insert_element_from_token(parser, token);
2611
+ pop_current_node(parser);
2612
+ acknowledge_self_closing_tag(parser);
2613
+ return;
2614
+ }
2615
+ if (tag_is(token, kStartTag, GUMBO_TAG_META)) {
2616
+ insert_element_from_token(parser, token);
2617
+ pop_current_node(parser);
2618
+ acknowledge_self_closing_tag(parser);
2619
+ // NOTE(jdtang): Gumbo handles only UTF-8, so the encoding clause of the
2620
+ // spec doesn't apply. If clients want to handle meta-tag re-encoding, they
2621
+ // should specifically look for that string in the document and re-encode it
2622
+ // before passing to Gumbo.
2623
+ return;
2624
+ }
2625
+ if (tag_is(token, kStartTag, GUMBO_TAG_TITLE)) {
2626
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RCDATA);
2627
+ return;
2628
+ }
2629
+ if (
2630
+ tag_in(token, kStartTag, &(const TagSet){TAG(NOFRAMES), TAG(STYLE)})
2631
+ || (tag_is(token, kStartTag, GUMBO_TAG_NOSCRIPT) && parser->_options->parse_noscript_content_as_text)
2632
+ ) {
2633
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
2634
+ return;
2635
+ }
2636
+ if (tag_is(token, kStartTag, GUMBO_TAG_NOSCRIPT)) {
2637
+ insert_element_from_token(parser, token);
2638
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD_NOSCRIPT);
2639
+ return;
2640
+ }
2641
+ if (tag_is(token, kStartTag, GUMBO_TAG_SCRIPT)) {
2642
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_SCRIPT_DATA);
2643
+ return;
2644
+ }
2645
+ if (tag_is(token, kEndTag, GUMBO_TAG_HEAD)) {
2646
+ GumboNode* head = pop_current_node(parser);
2647
+ UNUSED_IF_NDEBUG(head);
2648
+ assert(node_html_tag_is(head, GUMBO_TAG_HEAD));
2649
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_HEAD);
2650
+ return;
2651
+ }
2652
+ if (
2653
+ tag_in(token, kEndTag, &(const TagSet){TAG(BODY), TAG(HTML), TAG(BR)})
2654
+ ) {
2655
+ pop_current_node(parser);
2656
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_HEAD);
2657
+ parser->_parser_state->_reprocess_current_token = true;
2658
+ return;
2659
+ }
2660
+ if (tag_is(token, kStartTag, GUMBO_TAG_TEMPLATE)) {
2661
+ insert_element_from_token(parser, token);
2662
+ add_formatting_element(parser, &kActiveFormattingScopeMarker);
2663
+ set_frameset_not_ok(parser);
2664
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TEMPLATE);
2665
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TEMPLATE);
2666
+ return;
2667
+ }
2668
+ if (tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)) {
2669
+ if (!has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
2670
+ parser_add_parse_error(parser, token);
2671
+ ignore_token(parser);
2672
+ return;
2673
+ }
2674
+ generate_all_implied_end_tags_thoroughly(parser);
2675
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_TEMPLATE))
2676
+ parser_add_parse_error(parser, token);
2677
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_TEMPLATE))
2678
+ ;
2679
+ clear_active_formatting_elements(parser);
2680
+ pop_template_insertion_mode(parser);
2681
+ reset_insertion_mode_appropriately(parser);
2682
+ return;
2683
+ }
2684
+ if (
2685
+ tag_is(token, kStartTag, GUMBO_TAG_HEAD)
2686
+ || (token->type == GUMBO_TOKEN_END_TAG)
2687
+ ) {
2688
+ parser_add_parse_error(parser, token);
2689
+ ignore_token(parser);
2690
+ return;
2691
+ }
2692
+ pop_current_node(parser);
2693
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_HEAD);
2694
+ parser->_parser_state->_reprocess_current_token = true;
2695
+ return;
2696
+ }
2697
+
2698
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inheadnoscript
2699
+ static void handle_in_head_noscript(GumboParser* parser, GumboToken* token) {
2700
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2701
+ parser_add_parse_error(parser, token);
2702
+ ignore_token(parser);
2703
+ return;
2704
+ }
2705
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2706
+ handle_in_body(parser, token);
2707
+ return;
2708
+ }
2709
+ if (tag_is(token, kEndTag, GUMBO_TAG_NOSCRIPT)) {
2710
+ const GumboNode* node = pop_current_node(parser);
2711
+ assert(node_html_tag_is(node, GUMBO_TAG_NOSCRIPT));
2712
+ UNUSED_IF_NDEBUG(node);
2713
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
2714
+ return;
2715
+ }
2716
+ if (
2717
+ token->type == GUMBO_TOKEN_WHITESPACE
2718
+ || token->type == GUMBO_TOKEN_COMMENT
2719
+ || tag_in (token, kStartTag, &(const TagSet) {
2720
+ TAG(BASEFONT), TAG(BGSOUND), TAG(LINK),
2721
+ TAG(META), TAG(NOFRAMES), TAG(STYLE)
2722
+ })
2723
+ ) {
2724
+ handle_in_head(parser, token);
2725
+ return;
2726
+ }
2727
+ if (
2728
+ tag_in(token, kStartTag, &(const TagSet){TAG(HEAD), TAG(NOSCRIPT)})
2729
+ || (
2730
+ token->type == GUMBO_TOKEN_END_TAG
2731
+ && !tag_is(token, kEndTag, GUMBO_TAG_BR)
2732
+ )
2733
+ ) {
2734
+ parser_add_parse_error(parser, token);
2735
+ ignore_token(parser);
2736
+ return;
2737
+ }
2738
+ parser_add_parse_error(parser, token);
2739
+ const GumboNode* node = pop_current_node(parser);
2740
+ assert(node_html_tag_is(node, GUMBO_TAG_NOSCRIPT));
2741
+ UNUSED_IF_NDEBUG(node);
2742
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
2743
+ parser->_parser_state->_reprocess_current_token = true;
2744
+ }
2745
+
2746
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-after-head-insertion-mode
2747
+ static void handle_after_head(GumboParser* parser, GumboToken* token) {
2748
+ GumboParserState* state = parser->_parser_state;
2749
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2750
+ insert_text_token(parser, token);
2751
+ return;
2752
+ }
2753
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2754
+ append_comment_node(parser, get_current_node(parser), token);
2755
+ return;
2756
+ }
2757
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2758
+ parser_add_parse_error(parser, token);
2759
+ ignore_token(parser);
2760
+ return;
2761
+ }
2762
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2763
+ handle_in_body(parser, token);
2764
+ return;
2765
+ }
2766
+ if (tag_is(token, kStartTag, GUMBO_TAG_BODY)) {
2767
+ insert_element_from_token(parser, token);
2768
+ set_frameset_not_ok(parser);
2769
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
2770
+ return;
2771
+ }
2772
+ if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
2773
+ insert_element_from_token(parser, token);
2774
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_FRAMESET);
2775
+ return;
2776
+ }
2777
+ if (
2778
+ tag_in(token, kStartTag, &(const TagSet) {
2779
+ TAG(BASE), TAG(BASEFONT), TAG(BGSOUND), TAG(LINK), TAG(META),
2780
+ TAG(NOFRAMES), TAG(SCRIPT), TAG(STYLE), TAG(TEMPLATE), TAG(TITLE)
2781
+ })
2782
+ ) {
2783
+ parser_add_parse_error(parser, token);
2784
+ assert(state->_head_element != NULL);
2785
+ // This must be flushed before we push the head element on, as there may be
2786
+ // pending character tokens that should be attached to the root.
2787
+ maybe_flush_text_node_buffer(parser);
2788
+ gumbo_vector_add(state->_head_element, &state->_open_elements);
2789
+ handle_in_head(parser, token);
2790
+ gumbo_vector_remove(state->_head_element, &state->_open_elements);
2791
+ return;
2792
+ }
2793
+ if (tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)) {
2794
+ handle_in_head(parser, token);
2795
+ return;
2796
+ }
2797
+ if (
2798
+ tag_is(token, kStartTag, GUMBO_TAG_HEAD)
2799
+ || (
2800
+ token->type == GUMBO_TOKEN_END_TAG
2801
+ && !tag_in(token, kEndTag, &(const TagSet){TAG(BODY), TAG(HTML), TAG(BR)})
2802
+ )
2803
+ ) {
2804
+ parser_add_parse_error(parser, token);
2805
+ ignore_token(parser);
2806
+ return;
2807
+ }
2808
+ insert_element_of_tag_type(parser, GUMBO_TAG_BODY, GUMBO_INSERTION_IMPLIED);
2809
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
2810
+ state->_reprocess_current_token = true;
2811
+ }
2812
+
2813
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
2814
+ static void handle_in_body(GumboParser* parser, GumboToken* token) {
2815
+ GumboParserState* state = parser->_parser_state;
2816
+ assert(state->_open_elements.length > 0);
2817
+ if (token->type == GUMBO_TOKEN_NULL) {
2818
+ parser_add_parse_error(parser, token);
2819
+ ignore_token(parser);
2820
+ return;
2821
+ }
2822
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
2823
+ reconstruct_active_formatting_elements(parser);
2824
+ insert_text_token(parser, token);
2825
+ return;
2826
+ }
2827
+ if (
2828
+ token->type == GUMBO_TOKEN_CHARACTER
2829
+ || token->type == GUMBO_TOKEN_CDATA
2830
+ ) {
2831
+ reconstruct_active_formatting_elements(parser);
2832
+ insert_text_token(parser, token);
2833
+ set_frameset_not_ok(parser);
2834
+ return;
2835
+ }
2836
+ if (token->type == GUMBO_TOKEN_COMMENT) {
2837
+ append_comment_node(parser, get_current_node(parser), token);
2838
+ return;
2839
+ }
2840
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
2841
+ parser_add_parse_error(parser, token);
2842
+ ignore_token(parser);
2843
+ return;
2844
+ }
2845
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
2846
+ parser_add_parse_error(parser, token);
2847
+ if (has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
2848
+ ignore_token(parser);
2849
+ return;
2850
+ }
2851
+ assert(parser->_output->root != NULL);
2852
+ assert(parser->_output->root->type == GUMBO_NODE_ELEMENT);
2853
+ merge_attributes(token, parser->_output->root);
2854
+ return;
2855
+ }
2856
+ if (
2857
+ tag_in(token, kStartTag, &(const TagSet) {
2858
+ TAG(BASE), TAG(BASEFONT), TAG(BGSOUND), TAG(LINK),
2859
+ TAG(META), TAG(NOFRAMES), TAG(SCRIPT), TAG(STYLE), TAG(TEMPLATE),
2860
+ TAG(TITLE)
2861
+ })
2862
+ || tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)
2863
+ ) {
2864
+ handle_in_head(parser, token);
2865
+ return;
2866
+ }
2867
+ if (tag_is(token, kStartTag, GUMBO_TAG_BODY)) {
2868
+ parser_add_parse_error(parser, token);
2869
+ if (
2870
+ state->_open_elements.length < 2
2871
+ || !node_html_tag_is(state->_open_elements.data[1], GUMBO_TAG_BODY)
2872
+ || has_open_element(parser, GUMBO_TAG_TEMPLATE)
2873
+ ) {
2874
+ ignore_token(parser);
2875
+ } else {
2876
+ set_frameset_not_ok(parser);
2877
+ merge_attributes(token, state->_open_elements.data[1]);
2878
+ }
2879
+ return;
2880
+ }
2881
+ if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
2882
+ parser_add_parse_error(parser, token);
2883
+ if (
2884
+ state->_open_elements.length < 2
2885
+ || !node_html_tag_is(state->_open_elements.data[1], GUMBO_TAG_BODY)
2886
+ || !state->_frameset_ok
2887
+ ) {
2888
+ ignore_token(parser);
2889
+ return;
2890
+ }
2891
+ // Save the body node for later removal.
2892
+ GumboNode* body_node = state->_open_elements.data[1];
2893
+
2894
+ // Pop all nodes except root HTML element.
2895
+ GumboNode* node;
2896
+ do {
2897
+ node = pop_current_node(parser);
2898
+ } while (node != state->_open_elements.data[1]);
2899
+
2900
+ // Removing & destroying the body node is going to kill any nodes that have
2901
+ // been added to the list of active formatting elements, and so we should
2902
+ // clear it to prevent a use-after-free if the list of active formatting
2903
+ // elements is reconstructed afterwards. This may happen if whitespace
2904
+ // follows the </frameset>.
2905
+ clear_active_formatting_elements(parser);
2906
+
2907
+ // Remove the body node. We may want to factor this out into a generic
2908
+ // helper, but right now this is the only code that needs to do this.
2909
+ GumboVector* children = &parser->_output->root->v.element.children;
2910
+ for (unsigned int i = 0; i < children->length; ++i) {
2911
+ if (children->data[i] == body_node) {
2912
+ gumbo_vector_remove_at(i, children);
2913
+ break;
2914
+ }
2915
+ }
2916
+ destroy_node(body_node);
2917
+
2918
+ // Insert the <frameset>, and switch the insertion mode.
2919
+ insert_element_from_token(parser, token);
2920
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_FRAMESET);
2921
+ return;
2922
+ }
2923
+ if (token->type == GUMBO_TOKEN_EOF) {
2924
+ if (get_current_template_insertion_mode(parser) !=
2925
+ GUMBO_INSERTION_MODE_INITIAL) {
2926
+ handle_in_template(parser, token);
2927
+ return;
2928
+ }
2929
+ if (stack_contains_nonclosable_element(parser))
2930
+ parser_add_parse_error(parser, token);
2931
+ return;
2932
+ }
2933
+ if (tag_is(token, kEndTag, GUMBO_TAG_BODY)) {
2934
+ if (!has_an_element_in_scope(parser, GUMBO_TAG_BODY)) {
2935
+ parser_add_parse_error(parser, token);
2936
+ ignore_token(parser);
2937
+ return;
2938
+ }
2939
+ if (stack_contains_nonclosable_element(parser))
2940
+ parser_add_parse_error(parser, token);
2941
+ GumboNode* body = state->_open_elements.data[1];
2942
+ assert(node_html_tag_is(body, GUMBO_TAG_BODY));
2943
+ record_end_of_element(state->_current_token, &body->v.element);
2944
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_BODY);
2945
+ return;
2946
+ }
2947
+ if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
2948
+ if (!has_an_element_in_scope(parser, GUMBO_TAG_BODY)) {
2949
+ parser_add_parse_error(parser, token);
2950
+ ignore_token(parser);
2951
+ return;
2952
+ }
2953
+ if (stack_contains_nonclosable_element(parser))
2954
+ parser_add_parse_error(parser, token);
2955
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_BODY);
2956
+ parser->_parser_state->_reprocess_current_token = true;
2957
+ return;
2958
+ }
2959
+ if (
2960
+ tag_in(token, kStartTag, &(const TagSet) {
2961
+ TAG(ADDRESS), TAG(ARTICLE), TAG(ASIDE), TAG(BLOCKQUOTE), TAG(CENTER),
2962
+ TAG(DETAILS), TAG(DIALOG), TAG(DIR), TAG(DIV), TAG(DL), TAG(FIELDSET),
2963
+ TAG(FIGCAPTION), TAG(FIGURE), TAG(FOOTER), TAG(HEADER), TAG(HGROUP),
2964
+ TAG(MAIN), TAG(MENU), TAG(NAV), TAG(OL), TAG(P), TAG(SECTION),
2965
+ TAG(SUMMARY), TAG(UL), TAG(SEARCH)
2966
+ })
2967
+ ) {
2968
+ maybe_implicitly_close_p_tag(parser, token);
2969
+ insert_element_from_token(parser, token);
2970
+ return;
2971
+ }
2972
+ if (tag_in(token, kStartTag, &heading_tags)) {
2973
+ maybe_implicitly_close_p_tag(parser, token);
2974
+ if (node_tag_in_set(get_current_node(parser), &heading_tags)) {
2975
+ parser_add_parse_error(parser, token);
2976
+ pop_current_node(parser);
2977
+ }
2978
+ insert_element_from_token(parser, token);
2979
+ return;
2980
+ }
2981
+ if (tag_in(token, kStartTag, &(const TagSet){TAG(PRE), TAG(LISTING)})) {
2982
+ maybe_implicitly_close_p_tag(parser, token);
2983
+ insert_element_from_token(parser, token);
2984
+ state->_ignore_next_linefeed = true;
2985
+ set_frameset_not_ok(parser);
2986
+ return;
2987
+ }
2988
+ if (tag_is(token, kStartTag, GUMBO_TAG_FORM)) {
2989
+ if (
2990
+ state->_form_element != NULL
2991
+ && !has_open_element(parser, GUMBO_TAG_TEMPLATE)
2992
+ ) {
2993
+ gumbo_debug("Ignoring nested form.\n");
2994
+ parser_add_parse_error(parser, token);
2995
+ ignore_token(parser);
2996
+ return;
2997
+ }
2998
+ maybe_implicitly_close_p_tag(parser, token);
2999
+ GumboNode* form_element = insert_element_from_token(parser, token);
3000
+ if (!has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
3001
+ state->_form_element = form_element;
3002
+ }
3003
+ return;
3004
+ }
3005
+ if (tag_is(token, kStartTag, GUMBO_TAG_LI)) {
3006
+ maybe_implicitly_close_list_tag(parser, token, true);
3007
+ maybe_implicitly_close_p_tag(parser, token);
3008
+ insert_element_from_token(parser, token);
3009
+ return;
3010
+ }
3011
+ if (tag_in(token, kStartTag, &dd_dt_tags)) {
3012
+ maybe_implicitly_close_list_tag(parser, token, false);
3013
+ maybe_implicitly_close_p_tag(parser, token);
3014
+ insert_element_from_token(parser, token);
3015
+ return;
3016
+ }
3017
+ if (tag_is(token, kStartTag, GUMBO_TAG_PLAINTEXT)) {
3018
+ maybe_implicitly_close_p_tag(parser, token);
3019
+ insert_element_from_token(parser, token);
3020
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_PLAINTEXT);
3021
+ return;
3022
+ }
3023
+ if (tag_is(token, kStartTag, GUMBO_TAG_BUTTON)) {
3024
+ if (has_an_element_in_scope(parser, GUMBO_TAG_BUTTON)) {
3025
+ parser_add_parse_error(parser, token);
3026
+ // We don't want to use implicitly_close_tags here because it may add an
3027
+ // error and we've already added the only error the standard specifies.
3028
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3029
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_BUTTON))
3030
+ ;
3031
+ }
3032
+ reconstruct_active_formatting_elements(parser);
3033
+ insert_element_from_token(parser, token);
3034
+ set_frameset_not_ok(parser);
3035
+ return;
3036
+ }
3037
+ if (
3038
+ tag_in(token, kEndTag, &(const TagSet) {
3039
+ TAG(ADDRESS), TAG(ARTICLE), TAG(ASIDE), TAG(BLOCKQUOTE), TAG(BUTTON),
3040
+ TAG(CENTER), TAG(DETAILS), TAG(DIALOG), TAG(DIR), TAG(DIV), TAG(DL),
3041
+ TAG(FIELDSET), TAG(FIGCAPTION), TAG(FIGURE), TAG(FOOTER), TAG(HEADER),
3042
+ TAG(HGROUP), TAG(LISTING), TAG(MAIN), TAG(MENU), TAG(NAV), TAG(OL),
3043
+ TAG(PRE), TAG(SECTION), TAG(SUMMARY), TAG(UL), TAG(SEARCH)
3044
+ })
3045
+ ) {
3046
+ GumboTag tag = token->v.end_tag.tag;
3047
+ if (!has_an_element_in_scope(parser, tag)) {
3048
+ parser_add_parse_error(parser, token);
3049
+ ignore_token(parser);
3050
+ return;
3051
+ }
3052
+ return implicitly_close_tags (
3053
+ parser,
3054
+ token,
3055
+ GUMBO_NAMESPACE_HTML,
3056
+ token->v.end_tag.tag
3057
+ );
3058
+ }
3059
+ if (tag_is(token, kEndTag, GUMBO_TAG_FORM)) {
3060
+ if (has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
3061
+ if (!has_an_element_in_scope(parser, GUMBO_TAG_FORM)) {
3062
+ parser_add_parse_error(parser, token);
3063
+ ignore_token(parser);
3064
+ return;
3065
+ }
3066
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3067
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_FORM))
3068
+ parser_add_parse_error(parser, token);
3069
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_FORM))
3070
+ ;
3071
+ return;
3072
+ } else {
3073
+ GumboNode* node = state->_form_element;
3074
+ assert(!node || node->type == GUMBO_NODE_ELEMENT);
3075
+ state->_form_element = NULL;
3076
+ if (!node || !has_node_in_scope(parser, node)) {
3077
+ gumbo_debug("Closing an unopened form.\n");
3078
+ parser_add_parse_error(parser, token);
3079
+ ignore_token(parser);
3080
+ return;
3081
+ }
3082
+ // Since we remove the form node without popping, we need to make sure
3083
+ // that we flush any text nodes at the end of the form.
3084
+ maybe_flush_text_node_buffer(parser);
3085
+ // This differs from implicitly_close_tags because we remove *only* the
3086
+ // <form> element; other nodes are left in scope.
3087
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3088
+ if (get_current_node(parser) != node)
3089
+ parser_add_parse_error(parser, token);
3090
+ else
3091
+ record_end_of_element(token, &node->v.element);
3092
+
3093
+ GumboVector* open_elements = &state->_open_elements;
3094
+ int index = gumbo_vector_index_of(open_elements, node);
3095
+ assert(index >= 0);
3096
+ gumbo_vector_remove_at(index, open_elements);
3097
+ return;
3098
+ }
3099
+ }
3100
+ if (tag_is(token, kEndTag, GUMBO_TAG_P)) {
3101
+ if (!has_an_element_in_button_scope(parser, GUMBO_TAG_P)) {
3102
+ parser_add_parse_error(parser, token);
3103
+ // reconstruct_active_formatting_elements(parser);
3104
+ insert_element_of_tag_type (
3105
+ parser,
3106
+ GUMBO_TAG_P,
3107
+ GUMBO_INSERTION_CONVERTED_FROM_END_TAG
3108
+ );
3109
+ }
3110
+ implicitly_close_tags (
3111
+ parser,
3112
+ token,
3113
+ GUMBO_NAMESPACE_HTML,
3114
+ GUMBO_TAG_P
3115
+ );
3116
+ return;
3117
+ }
3118
+ if (tag_is(token, kEndTag, GUMBO_TAG_LI)) {
3119
+ if (!has_an_element_in_list_scope(parser, GUMBO_TAG_LI)) {
3120
+ parser_add_parse_error(parser, token);
3121
+ ignore_token(parser);
3122
+ return;
3123
+ }
3124
+ implicitly_close_tags (
3125
+ parser,
3126
+ token,
3127
+ GUMBO_NAMESPACE_HTML,
3128
+ GUMBO_TAG_LI
3129
+ );
3130
+ return;
3131
+ }
3132
+ if (tag_in(token, kEndTag, &dd_dt_tags)) {
3133
+ GumboTag token_tag = token->v.end_tag.tag;
3134
+ if (!has_an_element_in_scope(parser, token_tag)) {
3135
+ parser_add_parse_error(parser, token);
3136
+ ignore_token(parser);
3137
+ return;
3138
+ }
3139
+ implicitly_close_tags (
3140
+ parser,
3141
+ token,
3142
+ GUMBO_NAMESPACE_HTML,
3143
+ token_tag
3144
+ );
3145
+ return;
3146
+ }
3147
+ if (tag_in(token, kEndTag, &heading_tags)) {
3148
+ if (
3149
+ !has_an_element_in_scope_with_tagname(parser, 6, (GumboTag[]) {
3150
+ GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3, GUMBO_TAG_H4,
3151
+ GUMBO_TAG_H5, GUMBO_TAG_H6
3152
+ })
3153
+ ) {
3154
+ // No heading open; ignore the token entirely.
3155
+ parser_add_parse_error(parser, token);
3156
+ ignore_token(parser);
3157
+ return;
3158
+ }
3159
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3160
+ const GumboNode* current_node = get_current_node(parser);
3161
+ if (!node_html_tag_is(current_node, token->v.end_tag.tag)) {
3162
+ // There're children of the heading currently open; close them below and
3163
+ // record a parse error.
3164
+ // TODO(jdtang): Add a way to distinguish this error case from the one
3165
+ // above.
3166
+ parser_add_parse_error(parser, token);
3167
+ }
3168
+ do {
3169
+ current_node = pop_current_node(parser);
3170
+ } while (!node_tag_in_set(current_node, &heading_tags));
3171
+ return;
3172
+ }
3173
+ if (tag_is(token, kStartTag, GUMBO_TAG_A)) {
3174
+ int last_a;
3175
+ int has_matching_a = find_last_anchor_index(parser, &last_a);
3176
+ if (has_matching_a) {
3177
+ assert(has_matching_a == 1);
3178
+ parser_add_parse_error(parser, token);
3179
+ (void)adoption_agency_algorithm(parser, token);
3180
+ // The adoption agency algorithm usually removes all instances of <a>
3181
+ // from the list of active formatting elements, but in case it doesn't,
3182
+ // we're supposed to do this. (The conditions where it might not are
3183
+ // listed in the spec.)
3184
+ if (find_last_anchor_index(parser, &last_a)) {
3185
+ void* last_element = gumbo_vector_remove_at (
3186
+ last_a,
3187
+ &state->_active_formatting_elements
3188
+ );
3189
+ gumbo_vector_remove(last_element, &state->_open_elements);
3190
+ }
3191
+ }
3192
+ reconstruct_active_formatting_elements(parser);
3193
+ add_formatting_element(parser, insert_element_from_token(parser, token));
3194
+ return;
3195
+ }
3196
+ if (
3197
+ tag_in(token, kStartTag, &(const TagSet) {
3198
+ TAG(B), TAG(BIG), TAG(CODE), TAG(EM), TAG(FONT), TAG(I), TAG(S),
3199
+ TAG(SMALL), TAG(STRIKE), TAG(STRONG), TAG(TT), TAG(U)
3200
+ })
3201
+ ) {
3202
+ reconstruct_active_formatting_elements(parser);
3203
+ add_formatting_element(parser, insert_element_from_token(parser, token));
3204
+ return;
3205
+ }
3206
+ if (tag_is(token, kStartTag, GUMBO_TAG_NOBR)) {
3207
+ reconstruct_active_formatting_elements(parser);
3208
+ if (has_an_element_in_scope(parser, GUMBO_TAG_NOBR)) {
3209
+ parser_add_parse_error(parser, token);
3210
+ adoption_agency_algorithm(parser, token);
3211
+ reconstruct_active_formatting_elements(parser);
3212
+ }
3213
+ insert_element_from_token(parser, token);
3214
+ add_formatting_element(parser, get_current_node(parser));
3215
+ return;
3216
+ }
3217
+ if (
3218
+ tag_in(token, kEndTag, &(const TagSet) {
3219
+ TAG(A), TAG(B), TAG(BIG), TAG(CODE), TAG(EM), TAG(FONT), TAG(I),
3220
+ TAG(NOBR), TAG(S), TAG(SMALL), TAG(STRIKE), TAG(STRONG), TAG(TT),
3221
+ TAG(U)
3222
+ })
3223
+ ) {
3224
+ adoption_agency_algorithm(parser, token);
3225
+ return;
3226
+ }
3227
+ if (
3228
+ tag_in(token, kStartTag, &(const TagSet){TAG(APPLET), TAG(MARQUEE), TAG(OBJECT)})
3229
+ ) {
3230
+ reconstruct_active_formatting_elements(parser);
3231
+ insert_element_from_token(parser, token);
3232
+ add_formatting_element(parser, &kActiveFormattingScopeMarker);
3233
+ set_frameset_not_ok(parser);
3234
+ return;
3235
+ }
3236
+ if (
3237
+ tag_in(token, kEndTag, &(const TagSet){TAG(APPLET), TAG(MARQUEE), TAG(OBJECT)})
3238
+ ) {
3239
+ GumboTag token_tag = token->v.end_tag.tag;
3240
+ if (!has_an_element_in_scope(parser, token_tag)) {
3241
+ parser_add_parse_error(parser, token);
3242
+ ignore_token(parser);
3243
+ return;
3244
+ }
3245
+ implicitly_close_tags(parser, token, GUMBO_NAMESPACE_HTML, token_tag);
3246
+ clear_active_formatting_elements(parser);
3247
+ return;
3248
+ }
3249
+ if (tag_is(token, kStartTag, GUMBO_TAG_TABLE)) {
3250
+ if (
3251
+ get_document_node(parser)->v.document.doc_type_quirks_mode
3252
+ != GUMBO_DOCTYPE_QUIRKS
3253
+ ) {
3254
+ maybe_implicitly_close_p_tag(parser, token);
3255
+ }
3256
+ insert_element_from_token(parser, token);
3257
+ set_frameset_not_ok(parser);
3258
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3259
+ return;
3260
+ }
3261
+ if (tag_is(token, kEndTag, GUMBO_TAG_BR)) {
3262
+ parser_add_parse_error(parser, token);
3263
+ reconstruct_active_formatting_elements(parser);
3264
+ insert_element_of_tag_type (
3265
+ parser,
3266
+ GUMBO_TAG_BR,
3267
+ GUMBO_INSERTION_CONVERTED_FROM_END_TAG
3268
+ );
3269
+ pop_current_node(parser);
3270
+ acknowledge_self_closing_tag(parser);
3271
+ set_frameset_not_ok(parser);
3272
+ return;
3273
+ }
3274
+ if (
3275
+ tag_in(token, kStartTag, &(const TagSet) {
3276
+ TAG(AREA), TAG(BR), TAG(EMBED), TAG(IMG), TAG(IMAGE), TAG(KEYGEN),
3277
+ TAG(WBR)
3278
+ })
3279
+ ) {
3280
+ bool is_image = tag_is(token, kStartTag, GUMBO_TAG_IMAGE);
3281
+ if (is_image) {
3282
+ parser_add_parse_error(parser, token);
3283
+ token->v.start_tag.tag = GUMBO_TAG_IMG;
3284
+ }
3285
+ reconstruct_active_formatting_elements(parser);
3286
+ GumboNode* node = insert_element_from_token(parser, token);
3287
+ if (is_image)
3288
+ node->parse_flags |= GUMBO_INSERTION_FROM_IMAGE;
3289
+ pop_current_node(parser);
3290
+ acknowledge_self_closing_tag(parser);
3291
+ set_frameset_not_ok(parser);
3292
+ return;
3293
+ }
3294
+ if (tag_is(token, kStartTag, GUMBO_TAG_INPUT)) {
3295
+ reconstruct_active_formatting_elements(parser);
3296
+ GumboNode *input = insert_element_from_token(parser, token);
3297
+ pop_current_node(parser);
3298
+ acknowledge_self_closing_tag(parser);
3299
+ if (!attribute_matches(&input->v.element.attributes, "type", "hidden"))
3300
+ set_frameset_not_ok(parser);
3301
+ return;
3302
+ }
3303
+ if (
3304
+ tag_in(token, kStartTag, &(const TagSet){TAG(PARAM), TAG(SOURCE), TAG(TRACK)})
3305
+ ) {
3306
+ insert_element_from_token(parser, token);
3307
+ pop_current_node(parser);
3308
+ acknowledge_self_closing_tag(parser);
3309
+ return;
3310
+ }
3311
+ if (tag_is(token, kStartTag, GUMBO_TAG_HR)) {
3312
+ maybe_implicitly_close_p_tag(parser, token);
3313
+ insert_element_from_token(parser, token);
3314
+ pop_current_node(parser);
3315
+ acknowledge_self_closing_tag(parser);
3316
+ set_frameset_not_ok(parser);
3317
+ return;
3318
+ }
3319
+ if (tag_is(token, kStartTag, GUMBO_TAG_TEXTAREA)) {
3320
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RCDATA);
3321
+ parser->_parser_state->_ignore_next_linefeed = true;
3322
+ set_frameset_not_ok(parser);
3323
+ return;
3324
+ }
3325
+ if (tag_is(token, kStartTag, GUMBO_TAG_XMP)) {
3326
+ maybe_implicitly_close_p_tag(parser, token);
3327
+ reconstruct_active_formatting_elements(parser);
3328
+ set_frameset_not_ok(parser);
3329
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
3330
+ return;
3331
+ }
3332
+ if (tag_is(token, kStartTag, GUMBO_TAG_IFRAME)) {
3333
+ set_frameset_not_ok(parser);
3334
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
3335
+ return;
3336
+ }
3337
+ if (
3338
+ tag_is(token, kStartTag, GUMBO_TAG_NOEMBED)
3339
+ || (tag_is(token, kStartTag, GUMBO_TAG_NOSCRIPT) && parser->_options->parse_noscript_content_as_text)
3340
+ ) {
3341
+ run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
3342
+ return;
3343
+ }
3344
+ if (tag_is(token, kStartTag, GUMBO_TAG_SELECT)) {
3345
+ reconstruct_active_formatting_elements(parser);
3346
+ insert_element_from_token(parser, token);
3347
+ set_frameset_not_ok(parser);
3348
+ GumboInsertionMode state = parser->_parser_state->_insertion_mode;
3349
+ if (
3350
+ state == GUMBO_INSERTION_MODE_IN_TABLE
3351
+ || state == GUMBO_INSERTION_MODE_IN_CAPTION
3352
+ || state == GUMBO_INSERTION_MODE_IN_TABLE_BODY
3353
+ || state == GUMBO_INSERTION_MODE_IN_ROW
3354
+ || state == GUMBO_INSERTION_MODE_IN_CELL
3355
+ ) {
3356
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_SELECT_IN_TABLE);
3357
+ } else {
3358
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_SELECT);
3359
+ }
3360
+ return;
3361
+ }
3362
+ if (
3363
+ tag_in(token, kStartTag, &(const TagSet){TAG(OPTGROUP), TAG(OPTION)})
3364
+ ) {
3365
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
3366
+ pop_current_node(parser);
3367
+ }
3368
+ reconstruct_active_formatting_elements(parser);
3369
+ insert_element_from_token(parser, token);
3370
+ return;
3371
+ }
3372
+ if (tag_in(token, kStartTag, &(const TagSet){TAG(RB), TAG(RTC)})) {
3373
+ if (has_an_element_in_scope(parser, GUMBO_TAG_RUBY)) {
3374
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3375
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_RUBY))
3376
+ parser_add_parse_error(parser, token);
3377
+ }
3378
+ insert_element_from_token(parser, token);
3379
+ return;
3380
+ }
3381
+ if (tag_in(token, kStartTag, &(const TagSet){TAG(RP), TAG(RT)})) {
3382
+ if (has_an_element_in_scope(parser, GUMBO_TAG_RUBY)) {
3383
+ generate_implied_end_tags(parser, GUMBO_TAG_RTC, NULL);
3384
+ GumboNode* current = get_current_node(parser);
3385
+ if (!node_html_tag_is(current, GUMBO_TAG_RUBY) &&
3386
+ !node_html_tag_is(current, GUMBO_TAG_RTC)) {
3387
+ parser_add_parse_error(parser, token);
3388
+ }
3389
+ }
3390
+ insert_element_from_token(parser, token);
3391
+ return;
3392
+ }
3393
+ if (tag_is(token, kStartTag, GUMBO_TAG_MATH)) {
3394
+ reconstruct_active_formatting_elements(parser);
3395
+ adjust_mathml_attributes(token);
3396
+ adjust_foreign_attributes(token);
3397
+ insert_foreign_element(parser, token, GUMBO_NAMESPACE_MATHML);
3398
+ if (token->v.start_tag.is_self_closing) {
3399
+ pop_current_node(parser);
3400
+ acknowledge_self_closing_tag(parser);
3401
+ }
3402
+ return;
3403
+ }
3404
+ if (tag_is(token, kStartTag, GUMBO_TAG_SVG)) {
3405
+ reconstruct_active_formatting_elements(parser);
3406
+ adjust_svg_attributes(token);
3407
+ adjust_foreign_attributes(token);
3408
+ insert_foreign_element(parser, token, GUMBO_NAMESPACE_SVG);
3409
+ if (token->v.start_tag.is_self_closing) {
3410
+ pop_current_node(parser);
3411
+ acknowledge_self_closing_tag(parser);
3412
+ }
3413
+ return;
3414
+ }
3415
+ if (
3416
+ tag_in(token, kStartTag, &(const TagSet) {
3417
+ TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(FRAME), TAG(HEAD),
3418
+ TAG(TBODY), TAG(TD), TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
3419
+ })
3420
+ ) {
3421
+ parser_add_parse_error(parser, token);
3422
+ ignore_token(parser);
3423
+ return;
3424
+ }
3425
+ if (token->type == GUMBO_TOKEN_START_TAG) {
3426
+ reconstruct_active_formatting_elements(parser);
3427
+ insert_element_from_token(parser, token);
3428
+ return;
3429
+ }
3430
+ in_body_any_other_end_tag(parser, token);
3431
+ }
3432
+
3433
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incdata
3434
+ static void handle_text(GumboParser* parser, GumboToken* token) {
3435
+ if (
3436
+ token->type == GUMBO_TOKEN_CHARACTER
3437
+ || token->type == GUMBO_TOKEN_WHITESPACE
3438
+ ) {
3439
+ insert_text_token(parser, token);
3440
+ return;
3441
+ }
3442
+ // We provide only bare-bones script handling that doesn't involve any of
3443
+ // the parser-pause/already-started/script-nesting flags or re-entrant
3444
+ // invocations of the tokenizer. Because the intended usage of this library
3445
+ // is mostly for templating, refactoring, and static-analysis libraries, we
3446
+ // provide the script body as a text-node child of the <script> element.
3447
+ // This behavior doesn't support document.write of partial HTML elements,
3448
+ // but should be adequate for almost all other scripting support.
3449
+ if (token->type == GUMBO_TOKEN_EOF) {
3450
+ parser_add_parse_error(parser, token);
3451
+ parser->_parser_state->_reprocess_current_token = true;
3452
+ }
3453
+ pop_current_node(parser);
3454
+ set_insertion_mode(parser, parser->_parser_state->_original_insertion_mode);
3455
+ }
3456
+
3457
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable
3458
+ static void handle_in_table(GumboParser* parser, GumboToken* token) {
3459
+ GumboParserState* state = parser->_parser_state;
3460
+ if (
3461
+ (token->type == GUMBO_TOKEN_CHARACTER
3462
+ || token->type == GUMBO_TOKEN_WHITESPACE
3463
+ || token->type == GUMBO_TOKEN_NULL)
3464
+ && node_tag_in_set(get_current_node(parser), &(const TagSet) {
3465
+ TAG(TABLE), TAG(TBODY), TAG(TEMPLATE), TAG(TFOOT), TAG(THEAD), TAG(TR)
3466
+ })
3467
+ ) {
3468
+ // The "pending table character tokens" list described in the spec is
3469
+ // nothing more than the TextNodeBufferState. We accumulate text tokens as
3470
+ // normal, except that when we go to flush them in the handle_in_table_text,
3471
+ // we set _foster_parent_insertions if there're non-whitespace characters in
3472
+ // the buffer.
3473
+ assert(state->_text_node._buffer.length == 0);
3474
+ assert(state->_table_character_tokens.length == 0);
3475
+ state->_original_insertion_mode = state->_insertion_mode;
3476
+ state->_reprocess_current_token = true;
3477
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_TEXT);
3478
+ return;
3479
+ }
3480
+ if (token->type == GUMBO_TOKEN_COMMENT) {
3481
+ append_comment_node(parser, get_current_node(parser), token);
3482
+ return;
3483
+ }
3484
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
3485
+ parser_add_parse_error(parser, token);
3486
+ ignore_token(parser);
3487
+ return;
3488
+ }
3489
+ if (tag_is(token, kStartTag, GUMBO_TAG_CAPTION)) {
3490
+ clear_stack_to_table_context(parser);
3491
+ add_formatting_element(parser, &kActiveFormattingScopeMarker);
3492
+ insert_element_from_token(parser, token);
3493
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_CAPTION);
3494
+ return;
3495
+ }
3496
+ if (tag_is(token, kStartTag, GUMBO_TAG_COLGROUP)) {
3497
+ clear_stack_to_table_context(parser);
3498
+ insert_element_from_token(parser, token);
3499
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
3500
+ return;
3501
+ }
3502
+ if (tag_is(token, kStartTag, GUMBO_TAG_COL)) {
3503
+ clear_stack_to_table_context(parser);
3504
+ insert_element_of_tag_type (
3505
+ parser,
3506
+ GUMBO_TAG_COLGROUP,
3507
+ GUMBO_INSERTION_IMPLIED
3508
+ );
3509
+ state->_reprocess_current_token = true;
3510
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
3511
+ return;
3512
+ }
3513
+ if (
3514
+ tag_in(token, kStartTag, &(const TagSet) {
3515
+ TAG(TBODY), TAG(TFOOT), TAG(THEAD)
3516
+ })
3517
+ ) {
3518
+ clear_stack_to_table_context(parser);
3519
+ insert_element_from_token(parser, token);
3520
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
3521
+ return;
3522
+ }
3523
+ if (
3524
+ tag_in(token, kStartTag, &(const TagSet) {
3525
+ TAG(TD), TAG(TH), TAG(TR)
3526
+ })
3527
+ ) {
3528
+ clear_stack_to_table_context(parser);
3529
+ insert_element_of_tag_type (
3530
+ parser,
3531
+ GUMBO_TAG_TBODY,
3532
+ GUMBO_INSERTION_IMPLIED
3533
+ );
3534
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
3535
+ state->_reprocess_current_token = true;
3536
+ return;
3537
+ }
3538
+ if (tag_is(token, kStartTag, GUMBO_TAG_TABLE)) {
3539
+ parser_add_parse_error(parser, token);
3540
+ if (close_table(parser)) {
3541
+ state->_reprocess_current_token = true;
3542
+ } else {
3543
+ ignore_token(parser);
3544
+ }
3545
+ return;
3546
+ }
3547
+ if (tag_is(token, kEndTag, GUMBO_TAG_TABLE)) {
3548
+ if (!close_table(parser)) {
3549
+ parser_add_parse_error(parser, token);
3550
+ return;
3551
+ }
3552
+ return;
3553
+ }
3554
+ if (
3555
+ tag_in(token, kEndTag, &(const TagSet) {
3556
+ TAG(BODY), TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(HTML),
3557
+ TAG(TBODY), TAG(TD), TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
3558
+ })
3559
+ ) {
3560
+ parser_add_parse_error(parser, token);
3561
+ ignore_token(parser);
3562
+ return;
3563
+ }
3564
+ if (
3565
+ tag_in(token, kStartTag, &(const TagSet){TAG(STYLE), TAG(SCRIPT), TAG(TEMPLATE)})
3566
+ || (tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE))
3567
+ ) {
3568
+ handle_in_head(parser, token);
3569
+ return;
3570
+ }
3571
+ if (
3572
+ tag_is(token, kStartTag, GUMBO_TAG_INPUT)
3573
+ && attribute_matches(&token->v.start_tag.attributes, "type", "hidden")
3574
+ ) {
3575
+ parser_add_parse_error(parser, token);
3576
+ insert_element_from_token(parser, token);
3577
+ pop_current_node(parser);
3578
+ acknowledge_self_closing_tag(parser);
3579
+ return;
3580
+ }
3581
+ if (tag_is(token, kStartTag, GUMBO_TAG_FORM)) {
3582
+ parser_add_parse_error(parser, token);
3583
+ if (state->_form_element || has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
3584
+ ignore_token(parser);
3585
+ return;
3586
+ }
3587
+ state->_form_element = insert_element_from_token(parser, token);
3588
+ pop_current_node(parser);
3589
+ return;
3590
+ }
3591
+ if (token->type == GUMBO_TOKEN_EOF) {
3592
+ handle_in_body(parser, token);
3593
+ return;
3594
+ }
3595
+ // foster-parenting-start-tag or foster-parenting-end-tag error
3596
+ parser_add_parse_error(parser, token);
3597
+ state->_foster_parent_insertions = true;
3598
+ handle_in_body(parser, token);
3599
+ state->_foster_parent_insertions = false;
3600
+ }
3601
+
3602
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intabletext
3603
+ static void handle_in_table_text(GumboParser* parser, GumboToken* token) {
3604
+ if (token->type == GUMBO_TOKEN_NULL) {
3605
+ parser_add_parse_error(parser, token);
3606
+ ignore_token(parser);
3607
+ return;
3608
+ }
3609
+ GumboParserState* state = parser->_parser_state;
3610
+ // Non-whitespace tokens will cause parse errors later.
3611
+ // It's not entirely clear from the spec how this is supposed to work.
3612
+ // https://github.com/whatwg/html/issues/4046
3613
+ if (token->type == GUMBO_TOKEN_WHITESPACE
3614
+ || token->type == GUMBO_TOKEN_CHARACTER) {
3615
+ insert_text_token(parser, token);
3616
+ gumbo_character_token_buffer_append(token, &state->_table_character_tokens);
3617
+ return;
3618
+ }
3619
+
3620
+ GumboCharacterTokenBuffer* buffer = &state->_table_character_tokens;
3621
+ if (state->_text_node._type != GUMBO_NODE_WHITESPACE) {
3622
+ // Each character in buffer is an error. Unfortunately, that means we need
3623
+ // to emit a bunch of errors at the appropriate locations.
3624
+ for (size_t i = 0, n = buffer->length; i < n; ++i) {
3625
+ GumboToken tok;
3626
+ gumbo_character_token_buffer_get(buffer, i, &tok);
3627
+ // foster-parenting-character error
3628
+ parser_add_parse_error(parser, &tok);
3629
+ }
3630
+ state->_foster_parent_insertions = true;
3631
+ set_frameset_not_ok(parser);
3632
+ reconstruct_active_formatting_elements(parser);
3633
+ }
3634
+ maybe_flush_text_node_buffer(parser);
3635
+ gumbo_character_token_buffer_clear(buffer);
3636
+ state->_foster_parent_insertions = false;
3637
+ state->_reprocess_current_token = true;
3638
+ state->_insertion_mode = state->_original_insertion_mode;
3639
+ }
3640
+
3641
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incaption
3642
+ static void handle_in_caption(GumboParser* parser, GumboToken* token) {
3643
+ if (tag_is(token, kEndTag, GUMBO_TAG_CAPTION)) {
3644
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_CAPTION)) {
3645
+ parser_add_parse_error(parser, token);
3646
+ ignore_token(parser);
3647
+ return;
3648
+ }
3649
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3650
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_CAPTION))
3651
+ parser_add_parse_error(parser, token);
3652
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_CAPTION))
3653
+ ;
3654
+ clear_active_formatting_elements(parser);
3655
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3656
+ return;
3657
+ }
3658
+ if (
3659
+ tag_in(token, kStartTag, &(const TagSet) {
3660
+ TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(TBODY), TAG(TD),
3661
+ TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
3662
+ })
3663
+ || (tag_is(token, kEndTag, GUMBO_TAG_TABLE))
3664
+ ) {
3665
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_CAPTION)) {
3666
+ parser_add_parse_error(parser, token);
3667
+ ignore_token(parser);
3668
+ return;
3669
+ }
3670
+ generate_implied_end_tags(parser, GUMBO_TAG_LAST, NULL);
3671
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_CAPTION))
3672
+ parser_add_parse_error(parser, token);
3673
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_CAPTION))
3674
+ ;
3675
+ clear_active_formatting_elements(parser);
3676
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3677
+ parser->_parser_state->_reprocess_current_token = true;
3678
+ return;
3679
+ }
3680
+ if (
3681
+ tag_in(token, kEndTag, &(const TagSet) {
3682
+ TAG(BODY), TAG(COL), TAG(COLGROUP), TAG(HTML), TAG(TBODY), TAG(TD),
3683
+ TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
3684
+ })
3685
+ ) {
3686
+ parser_add_parse_error(parser, token);
3687
+ ignore_token(parser);
3688
+ return;
3689
+ }
3690
+ handle_in_body(parser, token);
3691
+ }
3692
+
3693
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incolgroup
3694
+ static void handle_in_column_group(GumboParser* parser, GumboToken* token) {
3695
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
3696
+ insert_text_token(parser, token);
3697
+ return;
3698
+ }
3699
+ if (token->type == GUMBO_TOKEN_COMMENT) {
3700
+ append_comment_node(parser, get_current_node(parser), token);
3701
+ return;
3702
+ }
3703
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
3704
+ parser_add_parse_error(parser, token);
3705
+ ignore_token(parser);
3706
+ return;
3707
+ }
3708
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
3709
+ handle_in_body(parser, token);
3710
+ return;
3711
+ }
3712
+ if (tag_is(token, kStartTag, GUMBO_TAG_COL)) {
3713
+ insert_element_from_token(parser, token);
3714
+ pop_current_node(parser);
3715
+ acknowledge_self_closing_tag(parser);
3716
+ return;
3717
+ }
3718
+ if (tag_is(token, kEndTag, GUMBO_TAG_COLGROUP)) {
3719
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_COLGROUP)) {
3720
+ parser_add_parse_error(parser, token);
3721
+ ignore_token(parser);
3722
+ return;
3723
+ }
3724
+ pop_current_node(parser);
3725
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3726
+ return;
3727
+ }
3728
+ if (tag_is(token, kEndTag, GUMBO_TAG_COL)) {
3729
+ parser_add_parse_error(parser, token);
3730
+ ignore_token(parser);
3731
+ return;
3732
+ }
3733
+ if (
3734
+ tag_is(token, kStartTag, GUMBO_TAG_TEMPLATE)
3735
+ || tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)
3736
+ ) {
3737
+ handle_in_head(parser, token);
3738
+ return;
3739
+ }
3740
+ if (token->type == GUMBO_TOKEN_EOF) {
3741
+ handle_in_body(parser, token);
3742
+ return;
3743
+ }
3744
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_COLGROUP)) {
3745
+ parser_add_parse_error(parser, token);
3746
+ ignore_token(parser);
3747
+ return;
3748
+ }
3749
+ pop_current_node(parser);
3750
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3751
+ parser->_parser_state->_reprocess_current_token = true;
3752
+ }
3753
+
3754
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intbody
3755
+ static void handle_in_table_body(GumboParser* parser, GumboToken* token) {
3756
+ if (tag_is(token, kStartTag, GUMBO_TAG_TR)) {
3757
+ clear_stack_to_table_body_context(parser);
3758
+ insert_element_from_token(parser, token);
3759
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
3760
+ return;
3761
+ }
3762
+ if (tag_in(token, kStartTag, &td_th_tags)) {
3763
+ parser_add_parse_error(parser, token);
3764
+ clear_stack_to_table_body_context(parser);
3765
+ insert_element_of_tag_type(parser, GUMBO_TAG_TR, GUMBO_INSERTION_IMPLIED);
3766
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
3767
+ parser->_parser_state->_reprocess_current_token = true;
3768
+ return;
3769
+ }
3770
+ if (
3771
+ tag_in(token, kEndTag, &(const TagSet){TAG(TBODY), TAG(TFOOT), TAG(THEAD)})
3772
+ ) {
3773
+ if (!has_an_element_in_table_scope(parser, token->v.end_tag.tag)) {
3774
+ parser_add_parse_error(parser, token);
3775
+ ignore_token(parser);
3776
+ return;
3777
+ }
3778
+ clear_stack_to_table_body_context(parser);
3779
+ pop_current_node(parser);
3780
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3781
+ return;
3782
+ }
3783
+ if (
3784
+ tag_in(token, kStartTag, &(const TagSet) {
3785
+ TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(TBODY), TAG(TFOOT),
3786
+ TAG(THEAD)
3787
+ })
3788
+ || tag_is(token, kEndTag, GUMBO_TAG_TABLE)
3789
+ ) {
3790
+ if (
3791
+ !(
3792
+ has_an_element_in_table_scope(parser, GUMBO_TAG_TBODY)
3793
+ || has_an_element_in_table_scope(parser, GUMBO_TAG_THEAD)
3794
+ || has_an_element_in_table_scope(parser, GUMBO_TAG_TFOOT)
3795
+ )
3796
+ ) {
3797
+ parser_add_parse_error(parser, token);
3798
+ ignore_token(parser);
3799
+ return;
3800
+ }
3801
+ clear_stack_to_table_body_context(parser);
3802
+ pop_current_node(parser);
3803
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
3804
+ parser->_parser_state->_reprocess_current_token = true;
3805
+ return;
3806
+ }
3807
+ if (
3808
+ tag_in(token, kEndTag, &(const TagSet) {
3809
+ TAG(BODY), TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(HTML), TAG(TD),
3810
+ TAG(TH), TAG(TR)
3811
+ })
3812
+ ) {
3813
+ parser_add_parse_error(parser, token);
3814
+ ignore_token(parser);
3815
+ return;
3816
+ }
3817
+ handle_in_table(parser, token);
3818
+ }
3819
+
3820
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intr
3821
+ static void handle_in_row(GumboParser* parser, GumboToken* token) {
3822
+ if (tag_in(token, kStartTag, &td_th_tags)) {
3823
+ clear_stack_to_table_row_context(parser);
3824
+ insert_element_from_token(parser, token);
3825
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_CELL);
3826
+ add_formatting_element(parser, &kActiveFormattingScopeMarker);
3827
+ return;
3828
+ }
3829
+ if (tag_is(token, kEndTag, GUMBO_TAG_TR)) {
3830
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TR)) {
3831
+ parser_add_parse_error(parser, token);
3832
+ ignore_token(parser);
3833
+ return;
3834
+ }
3835
+ clear_stack_to_table_row_context(parser);
3836
+ pop_current_node(parser);
3837
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
3838
+ return;
3839
+ }
3840
+ if (
3841
+ tag_in(token, kStartTag, &(const TagSet) {
3842
+ TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(TBODY), TAG(TFOOT),
3843
+ TAG(THEAD), TAG(TR)
3844
+ })
3845
+ || tag_is(token, kEndTag, GUMBO_TAG_TABLE)
3846
+ ) {
3847
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TR)) {
3848
+ parser_add_parse_error(parser, token);
3849
+ ignore_token(parser);
3850
+ return;
3851
+ }
3852
+ clear_stack_to_table_row_context(parser);
3853
+ pop_current_node(parser);
3854
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
3855
+ parser->_parser_state->_reprocess_current_token = true;
3856
+ return;
3857
+ }
3858
+ if (
3859
+ tag_in(token, kEndTag, &(const TagSet) {TAG(TBODY), TAG(TFOOT), TAG(THEAD)})
3860
+ ) {
3861
+ if (!has_an_element_in_table_scope(parser, token->v.end_tag.tag)) {
3862
+ parser_add_parse_error(parser, token);
3863
+ ignore_token(parser);
3864
+ return;
3865
+ }
3866
+ if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TR)) {
3867
+ ignore_token(parser);
3868
+ return;
3869
+ }
3870
+ clear_stack_to_table_row_context(parser);
3871
+ pop_current_node(parser);
3872
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
3873
+ parser->_parser_state->_reprocess_current_token = true;
3874
+ return;
3875
+ }
3876
+ if (
3877
+ tag_in(token, kEndTag, &(const TagSet) {
3878
+ TAG(BODY), TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(HTML),
3879
+ TAG(TD), TAG(TH)
3880
+ })
3881
+ ) {
3882
+ parser_add_parse_error(parser, token);
3883
+ ignore_token(parser);
3884
+ return;
3885
+ }
3886
+ handle_in_table(parser, token);
3887
+ }
3888
+
3889
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intd
3890
+ static void handle_in_cell(GumboParser* parser, GumboToken* token) {
3891
+ if (tag_in(token, kEndTag, &td_th_tags)) {
3892
+ GumboTag token_tag = token->v.end_tag.tag;
3893
+ if (!has_an_element_in_table_scope(parser, token_tag)) {
3894
+ parser_add_parse_error(parser, token);
3895
+ ignore_token(parser);
3896
+ return;
3897
+ }
3898
+ close_table_cell(parser, token, token_tag);
3899
+ return;
3900
+ }
3901
+ if (
3902
+ tag_in(token, kStartTag, &(const TagSet) {
3903
+ TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(TBODY), TAG(TD),
3904
+ TAG(TFOOT), TAG(TH), TAG(THEAD), TAG(TR)
3905
+ })
3906
+ ) {
3907
+ gumbo_debug("Handling <td> in cell.\n");
3908
+ if (
3909
+ !has_an_element_in_table_scope(parser, GUMBO_TAG_TH)
3910
+ && !has_an_element_in_table_scope(parser, GUMBO_TAG_TD)
3911
+ ) {
3912
+ gumbo_debug("Bailing out because there's no <td> or <th> in scope.\n");
3913
+ parser_add_parse_error(parser, token);
3914
+ ignore_token(parser);
3915
+ return;
3916
+ }
3917
+ parser->_parser_state->_reprocess_current_token = true;
3918
+ close_current_cell(parser, token);
3919
+ return;
3920
+ }
3921
+ if (
3922
+ tag_in(token, kEndTag, &(const TagSet) {
3923
+ TAG(BODY), TAG(CAPTION), TAG(COL), TAG(COLGROUP), TAG(HTML)
3924
+ })
3925
+ ) {
3926
+ parser_add_parse_error(parser, token);
3927
+ ignore_token(parser);
3928
+ return;
3929
+ }
3930
+ if (
3931
+ tag_in(token, kEndTag, &(const TagSet) {
3932
+ TAG(TABLE), TAG(TBODY), TAG(TFOOT), TAG(THEAD), TAG(TR)
3933
+ })
3934
+ ) {
3935
+ if (!has_an_element_in_table_scope(parser, token->v.end_tag.tag)) {
3936
+ parser_add_parse_error(parser, token);
3937
+ ignore_token(parser);
3938
+ return;
3939
+ }
3940
+ parser->_parser_state->_reprocess_current_token = true;
3941
+ close_current_cell(parser, token);
3942
+ return;
3943
+ }
3944
+ handle_in_body(parser, token);
3945
+ }
3946
+
3947
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inselect
3948
+ static void handle_in_select(GumboParser* parser, GumboToken* token) {
3949
+ if (token->type == GUMBO_TOKEN_NULL) {
3950
+ parser_add_parse_error(parser, token);
3951
+ ignore_token(parser);
3952
+ return;
3953
+ }
3954
+ if (
3955
+ token->type == GUMBO_TOKEN_CHARACTER
3956
+ || token->type == GUMBO_TOKEN_WHITESPACE
3957
+ ) {
3958
+ insert_text_token(parser, token);
3959
+ return;
3960
+ }
3961
+ if (token->type == GUMBO_TOKEN_COMMENT) {
3962
+ append_comment_node(parser, get_current_node(parser), token);
3963
+ return;
3964
+ }
3965
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
3966
+ parser_add_parse_error(parser, token);
3967
+ ignore_token(parser);
3968
+ return;
3969
+ }
3970
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
3971
+ handle_in_body(parser, token);
3972
+ return;
3973
+ }
3974
+ if (tag_is(token, kStartTag, GUMBO_TAG_OPTION)) {
3975
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
3976
+ pop_current_node(parser);
3977
+ }
3978
+ insert_element_from_token(parser, token);
3979
+ return;
3980
+ }
3981
+ if (tag_is(token, kStartTag, GUMBO_TAG_OPTGROUP)) {
3982
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
3983
+ pop_current_node(parser);
3984
+ }
3985
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTGROUP)) {
3986
+ pop_current_node(parser);
3987
+ }
3988
+ insert_element_from_token(parser, token);
3989
+ return;
3990
+ }
3991
+ if (tag_is(token, kStartTag, GUMBO_TAG_HR)) {
3992
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
3993
+ pop_current_node(parser);
3994
+ }
3995
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTGROUP)) {
3996
+ pop_current_node(parser);
3997
+ }
3998
+ insert_element_from_token(parser, token);
3999
+ pop_current_node(parser);
4000
+ acknowledge_self_closing_tag(parser);
4001
+ return;
4002
+ }
4003
+ if (tag_is(token, kEndTag, GUMBO_TAG_OPTGROUP)) {
4004
+ GumboVector* open_elements = &parser->_parser_state->_open_elements;
4005
+ if (
4006
+ node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)
4007
+ && node_html_tag_is (
4008
+ open_elements->data[open_elements->length - 2],
4009
+ GUMBO_TAG_OPTGROUP
4010
+ )
4011
+ ) {
4012
+ pop_current_node(parser);
4013
+ }
4014
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTGROUP)) {
4015
+ pop_current_node(parser);
4016
+ return;
4017
+ }
4018
+ parser_add_parse_error(parser, token);
4019
+ ignore_token(parser);
4020
+ return;
4021
+ }
4022
+ if (tag_is(token, kEndTag, GUMBO_TAG_OPTION)) {
4023
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
4024
+ pop_current_node(parser);
4025
+ return;
4026
+ }
4027
+ parser_add_parse_error(parser, token);
4028
+ ignore_token(parser);
4029
+ return;
4030
+ }
4031
+ if (tag_is(token, kEndTag, GUMBO_TAG_SELECT)) {
4032
+ if (!has_an_element_in_select_scope(parser, GUMBO_TAG_SELECT)) {
4033
+ parser_add_parse_error(parser, token);
4034
+ ignore_token(parser);
4035
+ return;
4036
+ }
4037
+ close_current_select(parser);
4038
+ return;
4039
+ }
4040
+ if (tag_is(token, kStartTag, GUMBO_TAG_SELECT)) {
4041
+ parser_add_parse_error(parser, token);
4042
+ ignore_token(parser);
4043
+ if (has_an_element_in_select_scope(parser, GUMBO_TAG_SELECT)) {
4044
+ close_current_select(parser);
4045
+ }
4046
+ return;
4047
+ }
4048
+ if (
4049
+ tag_in(token, kStartTag, &(const TagSet) {TAG(INPUT), TAG(KEYGEN), TAG(TEXTAREA)})
4050
+ ) {
4051
+ parser_add_parse_error(parser, token);
4052
+ if (!has_an_element_in_select_scope(parser, GUMBO_TAG_SELECT)) {
4053
+ ignore_token(parser);
4054
+ } else {
4055
+ close_current_select(parser);
4056
+ parser->_parser_state->_reprocess_current_token = true;
4057
+ }
4058
+ return;
4059
+ }
4060
+ if (
4061
+ tag_in(token, kStartTag, &(const TagSet){TAG(SCRIPT), TAG(TEMPLATE)})
4062
+ || tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)
4063
+ ) {
4064
+ handle_in_head(parser, token);
4065
+ return;
4066
+ }
4067
+ if (token->type == GUMBO_TOKEN_EOF) {
4068
+ handle_in_body(parser, token);
4069
+ return;
4070
+ }
4071
+ parser_add_parse_error(parser, token);
4072
+ ignore_token(parser);
4073
+ }
4074
+
4075
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inselectintable
4076
+ static void handle_in_select_in_table(GumboParser* parser, GumboToken* token) {
4077
+ static const TagSet tags = {
4078
+ TAG(CAPTION), TAG(TABLE), TAG(TBODY), TAG(TFOOT), TAG(THEAD),
4079
+ TAG(TR), TAG(TD), TAG(TH)
4080
+ };
4081
+ if (tag_in(token, kStartTag, &tags)) {
4082
+ parser_add_parse_error(parser, token);
4083
+ close_current_select(parser);
4084
+ parser->_parser_state->_reprocess_current_token = true;
4085
+ return;
4086
+ }
4087
+ if (tag_in(token, kEndTag, &tags)) {
4088
+ parser_add_parse_error(parser, token);
4089
+ if (!has_an_element_in_table_scope(parser, token->v.end_tag.tag)) {
4090
+ ignore_token(parser);
4091
+ return;
4092
+ }
4093
+ close_current_select(parser);
4094
+ parser->_parser_state->_reprocess_current_token = true;
4095
+ return;
4096
+ }
4097
+ handle_in_select(parser, token);
4098
+ }
4099
+
4100
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intemplate
4101
+ static void handle_in_template(GumboParser* parser, GumboToken* token) {
4102
+ GumboParserState* state = parser->_parser_state;
4103
+ switch (token->type) {
4104
+ case GUMBO_TOKEN_WHITESPACE:
4105
+ case GUMBO_TOKEN_CHARACTER:
4106
+ case GUMBO_TOKEN_COMMENT:
4107
+ case GUMBO_TOKEN_NULL:
4108
+ case GUMBO_TOKEN_DOCTYPE:
4109
+ handle_in_body(parser, token);
4110
+ return;
4111
+ default:
4112
+ break;
4113
+ }
4114
+ if (
4115
+ tag_in(token, kStartTag, &(const TagSet) {
4116
+ TAG(BASE), TAG(BASEFONT), TAG(BGSOUND), TAG(LINK), TAG(META),
4117
+ TAG(NOFRAMES), TAG(SCRIPT), TAG(STYLE), TAG(TEMPLATE), TAG(TITLE)
4118
+ })
4119
+ || tag_is(token, kEndTag, GUMBO_TAG_TEMPLATE)
4120
+ ) {
4121
+ handle_in_head(parser, token);
4122
+ return;
4123
+ }
4124
+ if (
4125
+ tag_in(token, kStartTag, &(const TagSet) {
4126
+ TAG(CAPTION), TAG(COLGROUP), TAG(TBODY), TAG(TFOOT), TAG(THEAD)
4127
+ })
4128
+ ) {
4129
+ pop_template_insertion_mode(parser);
4130
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
4131
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
4132
+ state->_reprocess_current_token = true;
4133
+ return;
4134
+ }
4135
+ if (tag_is(token, kStartTag, GUMBO_TAG_COL)) {
4136
+ pop_template_insertion_mode(parser);
4137
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
4138
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
4139
+ state->_reprocess_current_token = true;
4140
+ return;
4141
+ }
4142
+ if (tag_is(token, kStartTag, GUMBO_TAG_TR)) {
4143
+ pop_template_insertion_mode(parser);
4144
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
4145
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
4146
+ state->_reprocess_current_token = true;
4147
+ return;
4148
+ }
4149
+ if (tag_in(token, kStartTag, &td_th_tags)) {
4150
+ pop_template_insertion_mode(parser);
4151
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
4152
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
4153
+ state->_reprocess_current_token = true;
4154
+ return;
4155
+ }
4156
+ if (token->type == GUMBO_TOKEN_START_TAG) {
4157
+ pop_template_insertion_mode(parser);
4158
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
4159
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
4160
+ state->_reprocess_current_token = true;
4161
+ return;
4162
+ }
4163
+ if (token->type == GUMBO_TOKEN_END_TAG) {
4164
+ parser_add_parse_error(parser, token);
4165
+ ignore_token(parser);
4166
+ return;
4167
+ }
4168
+ if (token->type == GUMBO_TOKEN_EOF) {
4169
+ if (!has_open_element(parser, GUMBO_TAG_TEMPLATE)) {
4170
+ // Stop parsing.
4171
+ return;
4172
+ }
4173
+ parser_add_parse_error(parser, token);
4174
+ while (!node_html_tag_is(pop_current_node(parser), GUMBO_TAG_TEMPLATE))
4175
+ ;
4176
+ clear_active_formatting_elements(parser);
4177
+ pop_template_insertion_mode(parser);
4178
+ reset_insertion_mode_appropriately(parser);
4179
+ state->_reprocess_current_token = true;
4180
+ return;
4181
+ }
4182
+ assert(0 && "unreachable");
4183
+ }
4184
+
4185
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-afterbody
4186
+ static void handle_after_body(GumboParser* parser, GumboToken* token) {
4187
+ if (
4188
+ token->type == GUMBO_TOKEN_WHITESPACE
4189
+ || tag_is(token, kStartTag, GUMBO_TAG_HTML)
4190
+ ) {
4191
+ handle_in_body(parser, token);
4192
+ return;
4193
+ }
4194
+ if (token->type == GUMBO_TOKEN_COMMENT) {
4195
+ GumboNode* html_node = parser->_output->root;
4196
+ assert(html_node != NULL);
4197
+ append_comment_node(parser, html_node, token);
4198
+ return;
4199
+ }
4200
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
4201
+ parser_add_parse_error(parser, token);
4202
+ ignore_token(parser);
4203
+ return;
4204
+ }
4205
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
4206
+ handle_in_body(parser, token);
4207
+ return;
4208
+ }
4209
+ if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
4210
+ /* fragment case: ignore the closing HTML token */
4211
+ if (is_fragment_parser(parser)) {
4212
+ parser_add_parse_error(parser, token);
4213
+ ignore_token(parser);
4214
+ return;
4215
+ }
4216
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_AFTER_BODY);
4217
+ GumboNode* html = parser->_parser_state->_open_elements.data[0];
4218
+ assert(node_html_tag_is(html, GUMBO_TAG_HTML));
4219
+ record_end_of_element (
4220
+ parser->_parser_state->_current_token,
4221
+ &html->v.element
4222
+ );
4223
+ return;
4224
+ }
4225
+ if (token->type == GUMBO_TOKEN_EOF) {
4226
+ return;
4227
+ }
4228
+ parser_add_parse_error(parser, token);
4229
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
4230
+ parser->_parser_state->_reprocess_current_token = true;
4231
+ }
4232
+
4233
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inframeset
4234
+ static void handle_in_frameset(GumboParser* parser, GumboToken* token) {
4235
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
4236
+ insert_text_token(parser, token);
4237
+ return;
4238
+ }
4239
+ if (token->type == GUMBO_TOKEN_COMMENT) {
4240
+ append_comment_node(parser, get_current_node(parser), token);
4241
+ return;
4242
+ }
4243
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
4244
+ parser_add_parse_error(parser, token);
4245
+ ignore_token(parser);
4246
+ return;
4247
+ }
4248
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
4249
+ handle_in_body(parser, token);
4250
+ return;
4251
+ }
4252
+ if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
4253
+ insert_element_from_token(parser, token);
4254
+ return;
4255
+ }
4256
+ if (tag_is(token, kEndTag, GUMBO_TAG_FRAMESET)) {
4257
+ if (node_html_tag_is(get_current_node(parser), GUMBO_TAG_HTML)) {
4258
+ parser_add_parse_error(parser, token);
4259
+ ignore_token(parser);
4260
+ return;
4261
+ }
4262
+ pop_current_node(parser);
4263
+ if (
4264
+ !is_fragment_parser(parser)
4265
+ && !node_html_tag_is(get_current_node(parser), GUMBO_TAG_FRAMESET)
4266
+ ) {
4267
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_FRAMESET);
4268
+ }
4269
+ return;
4270
+ }
4271
+ if (tag_is(token, kStartTag, GUMBO_TAG_FRAME)) {
4272
+ insert_element_from_token(parser, token);
4273
+ pop_current_node(parser);
4274
+ acknowledge_self_closing_tag(parser);
4275
+ return;
4276
+ }
4277
+ if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
4278
+ handle_in_head(parser, token);
4279
+ return;
4280
+ }
4281
+ if (token->type == GUMBO_TOKEN_EOF) {
4282
+ if (!node_html_tag_is(get_current_node(parser), GUMBO_TAG_HTML))
4283
+ parser_add_parse_error(parser, token);
4284
+ return;
4285
+ }
4286
+ parser_add_parse_error(parser, token);
4287
+ ignore_token(parser);
4288
+ }
4289
+
4290
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-afterframeset
4291
+ static void handle_after_frameset(GumboParser* parser, GumboToken* token) {
4292
+ if (token->type == GUMBO_TOKEN_WHITESPACE) {
4293
+ insert_text_token(parser, token);
4294
+ return;
4295
+ }
4296
+ if (token->type == GUMBO_TOKEN_COMMENT) {
4297
+ append_comment_node(parser, get_current_node(parser), token);
4298
+ return;
4299
+ }
4300
+ if (token->type == GUMBO_TOKEN_DOCTYPE) {
4301
+ parser_add_parse_error(parser, token);
4302
+ ignore_token(parser);
4303
+ return;
4304
+ }
4305
+ if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
4306
+ handle_in_body(parser, token);
4307
+ return;
4308
+ }
4309
+ if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
4310
+ GumboNode* html = parser->_parser_state->_open_elements.data[0];
4311
+ assert(node_html_tag_is(html, GUMBO_TAG_HTML));
4312
+ record_end_of_element (
4313
+ parser->_parser_state->_current_token,
4314
+ &html->v.element
4315
+ );
4316
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_AFTER_FRAMESET);
4317
+ return;
4318
+ }
4319
+ if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
4320
+ return handle_in_head(parser, token);
4321
+ }
4322
+ if (token->type == GUMBO_TOKEN_EOF) {
4323
+ return;
4324
+ }
4325
+ parser_add_parse_error(parser, token);
4326
+ ignore_token(parser);
4327
+ }
4328
+
4329
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-body-insertion-mode
4330
+ static void handle_after_after_body(GumboParser* parser, GumboToken* token) {
4331
+ if (token->type == GUMBO_TOKEN_COMMENT) {
4332
+ append_comment_node(parser, get_document_node(parser), token);
4333
+ return;
4334
+ }
4335
+ if (
4336
+ token->type == GUMBO_TOKEN_DOCTYPE
4337
+ || token->type == GUMBO_TOKEN_WHITESPACE
4338
+ || tag_is(token, kStartTag, GUMBO_TAG_HTML)
4339
+ ) {
4340
+ handle_in_body(parser, token);
4341
+ return;
4342
+ }
4343
+ if (token->type == GUMBO_TOKEN_EOF) {
4344
+ return;
4345
+ }
4346
+ parser_add_parse_error(parser, token);
4347
+ set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
4348
+ parser->_parser_state->_reprocess_current_token = true;
4349
+ }
4350
+
4351
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-frameset-insertion-mode
4352
+ static void handle_after_after_frameset (
4353
+ GumboParser* parser,
4354
+ GumboToken* token
4355
+ ) {
4356
+ if (token->type == GUMBO_TOKEN_COMMENT) {
4357
+ append_comment_node(parser, get_document_node(parser), token);
4358
+ return;
4359
+ }
4360
+ if (
4361
+ token->type == GUMBO_TOKEN_DOCTYPE
4362
+ || token->type == GUMBO_TOKEN_WHITESPACE
4363
+ || tag_is(token, kStartTag, GUMBO_TAG_HTML)
4364
+ ) {
4365
+ handle_in_body(parser, token);
4366
+ return;
4367
+ }
4368
+ if (token->type == GUMBO_TOKEN_EOF) {
4369
+ return;
4370
+ }
4371
+ if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
4372
+ handle_in_head(parser, token);
4373
+ return;
4374
+ }
4375
+ parser_add_parse_error(parser, token);
4376
+ ignore_token(parser);
4377
+ }
4378
+
4379
+ // Function pointers for each insertion mode.
4380
+ // Keep in sync with insertion_mode.h.
4381
+ typedef void (*TokenHandler)(GumboParser* parser, GumboToken* token);
4382
+ static const TokenHandler kTokenHandlers[] = {
4383
+ handle_initial,
4384
+ handle_before_html,
4385
+ handle_before_head,
4386
+ handle_in_head,
4387
+ handle_in_head_noscript,
4388
+ handle_after_head,
4389
+ handle_in_body,
4390
+ handle_text,
4391
+ handle_in_table,
4392
+ handle_in_table_text,
4393
+ handle_in_caption,
4394
+ handle_in_column_group,
4395
+ handle_in_table_body,
4396
+ handle_in_row,
4397
+ handle_in_cell,
4398
+ handle_in_select,
4399
+ handle_in_select_in_table,
4400
+ handle_in_template,
4401
+ handle_after_body,
4402
+ handle_in_frameset,
4403
+ handle_after_frameset,
4404
+ handle_after_after_body,
4405
+ handle_after_after_frameset
4406
+ };
4407
+
4408
+ static void handle_html_content(GumboParser* parser, GumboToken* token) {
4409
+ const GumboInsertionMode mode = parser->_parser_state->_insertion_mode;
4410
+ const TokenHandler handler = kTokenHandlers[mode];
4411
+ handler(parser, token);
4412
+ }
4413
+
4414
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign
4415
+ static void handle_in_foreign_content(GumboParser* parser, GumboToken* token) {
4416
+ gumbo_debug("Handling foreign content.\n");
4417
+ switch (token->type) {
4418
+ case GUMBO_TOKEN_NULL:
4419
+ parser_add_parse_error(parser, token);
4420
+ token->v.character = kUtf8ReplacementChar;
4421
+ insert_text_token(parser, token);
4422
+ return;
4423
+ case GUMBO_TOKEN_WHITESPACE:
4424
+ insert_text_token(parser, token);
4425
+ return;
4426
+ case GUMBO_TOKEN_CDATA:
4427
+ case GUMBO_TOKEN_CHARACTER:
4428
+ insert_text_token(parser, token);
4429
+ set_frameset_not_ok(parser);
4430
+ return;
4431
+ case GUMBO_TOKEN_COMMENT:
4432
+ append_comment_node(parser, get_current_node(parser), token);
4433
+ return;
4434
+ case GUMBO_TOKEN_DOCTYPE:
4435
+ parser_add_parse_error(parser, token);
4436
+ ignore_token(parser);
4437
+ return;
4438
+ default:
4439
+ // Fall through to the if-statements below.
4440
+ break;
4441
+ }
4442
+ // Order matters for these clauses.
4443
+ if (
4444
+ tag_in(token, kStartTag, &(const TagSet) {
4445
+ TAG(B), TAG(BIG), TAG(BLOCKQUOTE), TAG(BODY), TAG(BR), TAG(CENTER),
4446
+ TAG(CODE), TAG(DD), TAG(DIV), TAG(DL), TAG(DT), TAG(EM), TAG(EMBED),
4447
+ TAG(H1), TAG(H2), TAG(H3), TAG(H4), TAG(H5), TAG(H6), TAG(HEAD),
4448
+ TAG(HR), TAG(I), TAG(IMG), TAG(LI), TAG(LISTING), TAG(MENU), TAG(META),
4449
+ TAG(NOBR), TAG(OL), TAG(P), TAG(PRE), TAG(RUBY), TAG(S), TAG(SMALL),
4450
+ TAG(SPAN), TAG(STRONG), TAG(STRIKE), TAG(SUB), TAG(SUP), TAG(TABLE),
4451
+ TAG(TT), TAG(U), TAG(UL), TAG(VAR)
4452
+ })
4453
+ || (
4454
+ tag_is(token, kStartTag, GUMBO_TAG_FONT)
4455
+ && (
4456
+ token_has_attribute(token, "color")
4457
+ || token_has_attribute(token, "face")
4458
+ || token_has_attribute(token, "size")
4459
+ )
4460
+ )
4461
+ || tag_in(token, kEndTag, &(const TagSet) { TAG(BR), TAG(P) })
4462
+ ) {
4463
+ /* Parse error */
4464
+ parser_add_parse_error(parser, token);
4465
+
4466
+ while (
4467
+ !(
4468
+ is_mathml_integration_point(get_current_node(parser))
4469
+ || is_html_integration_point(get_current_node(parser))
4470
+ || get_current_node(parser)->v.element.tag_namespace == GUMBO_NAMESPACE_HTML
4471
+ )
4472
+ ) {
4473
+ pop_current_node(parser);
4474
+ }
4475
+ handle_html_content(parser, token);
4476
+ return;
4477
+ }
4478
+
4479
+ if (token->type == GUMBO_TOKEN_START_TAG) {
4480
+ const GumboNamespaceEnum current_namespace =
4481
+ get_adjusted_current_node(parser)->v.element.tag_namespace;
4482
+ if (current_namespace == GUMBO_NAMESPACE_MATHML) {
4483
+ adjust_mathml_attributes(token);
4484
+ }
4485
+ if (current_namespace == GUMBO_NAMESPACE_SVG) {
4486
+ adjust_svg_tag(token);
4487
+ adjust_svg_attributes(token);
4488
+ }
4489
+ adjust_foreign_attributes(token);
4490
+ insert_foreign_element(parser, token, current_namespace);
4491
+ if (token->v.start_tag.is_self_closing) {
4492
+ pop_current_node(parser);
4493
+ acknowledge_self_closing_tag(parser);
4494
+ }
4495
+ return;
4496
+ // </script> tags are handled like any other end tag, putting the script's
4497
+ // text into a text node child and closing the current node.
4498
+ }
4499
+ assert(token->type == GUMBO_TOKEN_END_TAG);
4500
+ GumboNode* node = get_current_node(parser);
4501
+ GumboTag tag = token->v.end_tag.tag;
4502
+ const char* name = token->v.end_tag.name;
4503
+ assert(node != NULL);
4504
+
4505
+ if (!node_tagname_is(node, tag, name))
4506
+ parser_add_parse_error(parser, token);
4507
+ int i = parser->_parser_state->_open_elements.length;
4508
+ for (--i; i > 0;) {
4509
+ // Here we move up the stack until we find an HTML element (in which
4510
+ // case we do nothing) or we find the element that we're about to
4511
+ // close (in which case we pop everything we've seen until that
4512
+ // point.)
4513
+ gumbo_debug("Foreign %s node at %d.\n", node->v.element.name, i);
4514
+ if (node_tagname_is(node, tag, name)) {
4515
+ gumbo_debug("Matches.\n");
4516
+ while (node != pop_current_node(parser)) {
4517
+ // Pop all the nodes below the current one. Node is guaranteed to
4518
+ // be an element on the stack of open elements (set below), so
4519
+ // this loop is guaranteed to terminate.
4520
+ }
4521
+ return;
4522
+ }
4523
+ --i;
4524
+ node = parser->_parser_state->_open_elements.data[i];
4525
+ if (node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML) {
4526
+ // The loop continues only in foreign namespaces.
4527
+ break;
4528
+ }
4529
+ }
4530
+ assert(node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML);
4531
+ if (i == 0)
4532
+ return;
4533
+ // We can't call handle_token directly because the current node is still in
4534
+ // a foreign namespace, so it would re-enter this and result in infinite
4535
+ // recursion.
4536
+ handle_html_content(parser, token);
4537
+ }
4538
+
4539
+ // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction
4540
+ static void handle_token(GumboParser* parser, GumboToken* token) {
4541
+ if (
4542
+ parser->_parser_state->_ignore_next_linefeed
4543
+ && token->type == GUMBO_TOKEN_WHITESPACE && token->v.character == '\n'
4544
+ ) {
4545
+ parser->_parser_state->_ignore_next_linefeed = false;
4546
+ ignore_token(parser);
4547
+ return;
4548
+ }
4549
+ // This needs to be reset both here and in the conditional above to catch both
4550
+ // the case where the next token is not whitespace (so we don't ignore
4551
+ // whitespace in the middle of <pre> tags) and where there are multiple
4552
+ // whitespace tokens (so we don't ignore the second one).
4553
+ parser->_parser_state->_ignore_next_linefeed = false;
4554
+
4555
+ if (tag_is(token, kEndTag, GUMBO_TAG_BODY)) {
4556
+ parser->_parser_state->_closed_body_tag = true;
4557
+ }
4558
+ if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
4559
+ parser->_parser_state->_closed_html_tag = true;
4560
+ }
4561
+
4562
+ const GumboNode* current_node = get_adjusted_current_node(parser);
4563
+ assert (
4564
+ !current_node
4565
+ || current_node->type == GUMBO_NODE_ELEMENT
4566
+ || current_node->type == GUMBO_NODE_TEMPLATE
4567
+ );
4568
+ if (current_node)
4569
+ gumbo_debug("Current node: <%s>.\n", current_node->v.element.name);
4570
+ if (!current_node ||
4571
+ current_node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML ||
4572
+ (is_mathml_integration_point(current_node) &&
4573
+ (token->type == GUMBO_TOKEN_CHARACTER ||
4574
+ token->type == GUMBO_TOKEN_WHITESPACE ||
4575
+ token->type == GUMBO_TOKEN_NULL ||
4576
+ (token->type == GUMBO_TOKEN_START_TAG &&
4577
+ !tag_in(token, kStartTag,
4578
+ &(const TagSet){TAG(MGLYPH), TAG(MALIGNMARK)})))) ||
4579
+ (current_node->v.element.tag_namespace == GUMBO_NAMESPACE_MATHML &&
4580
+ node_qualified_tag_is(
4581
+ current_node, GUMBO_NAMESPACE_MATHML, GUMBO_TAG_ANNOTATION_XML) &&
4582
+ tag_is(token, kStartTag, GUMBO_TAG_SVG)) ||
4583
+ (is_html_integration_point(current_node) &&
4584
+ (token->type == GUMBO_TOKEN_START_TAG ||
4585
+ token->type == GUMBO_TOKEN_CHARACTER ||
4586
+ token->type == GUMBO_TOKEN_NULL ||
4587
+ token->type == GUMBO_TOKEN_WHITESPACE)) ||
4588
+ token->type == GUMBO_TOKEN_EOF) {
4589
+ handle_html_content(parser, token);
4590
+ } else {
4591
+ handle_in_foreign_content(parser, token);
4592
+ }
4593
+ }
4594
+
4595
+ static GumboNode* create_fragment_ctx_element (
4596
+ const char* tag_name,
4597
+ GumboNamespaceEnum ns,
4598
+ const char* encoding
4599
+ ) {
4600
+ assert(tag_name);
4601
+ GumboTag tag = gumbo_tagn_enum(tag_name, strlen(tag_name));
4602
+ GumboNodeType type =
4603
+ ns == GUMBO_NAMESPACE_HTML && tag == GUMBO_TAG_TEMPLATE
4604
+ ? GUMBO_NODE_TEMPLATE : GUMBO_NODE_ELEMENT;
4605
+ GumboNode* node = create_node(type);
4606
+ GumboElement* element = &node->v.element;
4607
+ element->children = kGumboEmptyVector;
4608
+ if (encoding) {
4609
+ gumbo_vector_init(1, &element->attributes);
4610
+ GumboAttribute* attr = gumbo_alloc(sizeof(GumboAttribute));
4611
+ attr->attr_namespace = GUMBO_ATTR_NAMESPACE_NONE;
4612
+ attr->name = "encoding"; // Do not free this!
4613
+ attr->original_name = kGumboEmptyString;
4614
+ attr->value = encoding; // Do not free this!
4615
+ attr->original_value = kGumboEmptyString;
4616
+ attr->name_start = kGumboEmptySourcePosition;
4617
+ gumbo_vector_add(attr, &element->attributes);
4618
+ } else {
4619
+ element->attributes = kGumboEmptyVector;
4620
+ }
4621
+ element->tag = tag;
4622
+ element->tag_namespace = ns;
4623
+ element->name = tag_name; // Do not free this!
4624
+ element->original_tag = kGumboEmptyString;
4625
+ element->original_end_tag = kGumboEmptyString;
4626
+ element->start_pos = kGumboEmptySourcePosition;
4627
+ element->end_pos = kGumboEmptySourcePosition;
4628
+ return node;
4629
+ }
4630
+
4631
+ static void destroy_fragment_ctx_element(GumboNode* ctx) {
4632
+ assert(ctx->type == GUMBO_NODE_ELEMENT || ctx->type == GUMBO_NODE_TEMPLATE);
4633
+ GumboElement* element = &ctx->v.element;
4634
+ element->name = NULL; // Do not free.
4635
+ if (element->attributes.length > 0) {
4636
+ assert(element->attributes.length == 1);
4637
+ GumboAttribute* attr = gumbo_vector_pop(&element->attributes);
4638
+ // Do not free attr->name or attr->value, just free the attr.
4639
+ gumbo_free(attr);
4640
+ }
4641
+ destroy_node(ctx);
4642
+ }
4643
+
4644
+ static void fragment_parser_init (
4645
+ GumboParser* parser,
4646
+ const GumboOptions* options
4647
+ ) {
4648
+ assert(options->fragment_context != NULL);
4649
+ const char* fragment_ctx = options->fragment_context;
4650
+ GumboNamespaceEnum fragment_namespace = options->fragment_namespace;
4651
+ const char* fragment_encoding = options->fragment_encoding;
4652
+ GumboQuirksModeEnum quirks = options->quirks_mode;
4653
+ bool ctx_has_form_ancestor = options->fragment_context_has_form_ancestor;
4654
+ GumboNode* root;
4655
+
4656
+ // 1. [Create a new Document node, and mark it as being an HTML document.]
4657
+ // 2. [If the node document of the context element is in quirks mode, then
4658
+ // let the Document be in quirks mode. Otherwise, the node document of
4659
+ // the context element is in limited-quirks mode, then let the Document
4660
+ // be in limited-quirks mode. Otherwise, leave the Document in no-quirks
4661
+ // mode.]
4662
+ get_document_node(parser)->v.document.doc_type_quirks_mode = quirks;
4663
+
4664
+ // 3. [If allowDeclarativeShadowRoots is true, then set the Document's allow
4665
+ // declarative shadow roots to true.]
4666
+ // 4. [Create a new HTML parser, and associate it with the just created Document node.]
4667
+ // 5. [Set the state of the HTML parser's tokenization stage as follows, switching on the context element:]
4668
+ parser->_parser_state->_fragment_ctx =
4669
+ create_fragment_ctx_element(fragment_ctx, fragment_namespace, fragment_encoding);
4670
+ GumboTag ctx_tag = parser->_parser_state->_fragment_ctx->v.element.tag;
4671
+
4672
+ // 4.
4673
+ if (fragment_namespace == GUMBO_NAMESPACE_HTML) {
4674
+ // Non-HTML namespaces always start in the DATA state.
4675
+ switch (ctx_tag) {
4676
+ case GUMBO_TAG_TITLE:
4677
+ case GUMBO_TAG_TEXTAREA:
4678
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_RCDATA);
4679
+ break;
4680
+
4681
+ case GUMBO_TAG_STYLE:
4682
+ case GUMBO_TAG_XMP:
4683
+ case GUMBO_TAG_IFRAME:
4684
+ case GUMBO_TAG_NOEMBED:
4685
+ case GUMBO_TAG_NOFRAMES:
4686
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_RAWTEXT);
4687
+ break;
4688
+
4689
+ case GUMBO_TAG_SCRIPT:
4690
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_SCRIPT_DATA);
4691
+ break;
4692
+
4693
+ case GUMBO_TAG_NOSCRIPT:
4694
+ if (options->parse_noscript_content_as_text)
4695
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_RAWTEXT);
4696
+ break;
4697
+
4698
+ case GUMBO_TAG_PLAINTEXT:
4699
+ gumbo_tokenizer_set_state(parser, GUMBO_LEX_PLAINTEXT);
4700
+ break;
4701
+
4702
+ default:
4703
+ /* default data state */
4704
+ break;
4705
+ }
4706
+ }
4707
+
4708
+ // 5. 6. 7.
4709
+ root = insert_element_of_tag_type (
4710
+ parser,
4711
+ GUMBO_TAG_HTML,
4712
+ GUMBO_INSERTION_IMPLIED
4713
+ );
4714
+ parser->_output->root = root;
4715
+
4716
+ // 8.
4717
+ if (ctx_tag == GUMBO_TAG_TEMPLATE) {
4718
+ push_template_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TEMPLATE);
4719
+ }
4720
+
4721
+ // 10.
4722
+ reset_insertion_mode_appropriately(parser);
4723
+
4724
+ // 11.
4725
+ if (ctx_has_form_ancestor
4726
+ || (ctx_tag == GUMBO_TAG_FORM
4727
+ && fragment_namespace == GUMBO_NAMESPACE_HTML)) {
4728
+ static const GumboNode form_ancestor = {
4729
+ .type = GUMBO_NODE_ELEMENT,
4730
+ .parent = NULL,
4731
+ .index_within_parent = -1,
4732
+ .parse_flags = GUMBO_INSERTION_BY_PARSER,
4733
+ .v.element = {
4734
+ .children = GUMBO_EMPTY_VECTOR_INIT,
4735
+ .tag = GUMBO_TAG_FORM,
4736
+ .name = NULL,
4737
+ .tag_namespace = GUMBO_NAMESPACE_HTML,
4738
+ .original_tag = GUMBO_EMPTY_STRING_INIT,
4739
+ .original_end_tag = GUMBO_EMPTY_STRING_INIT,
4740
+ .start_pos = GUMBO_EMPTY_SOURCE_POSITION_INIT,
4741
+ .end_pos = GUMBO_EMPTY_SOURCE_POSITION_INIT,
4742
+ .attributes = GUMBO_EMPTY_VECTOR_INIT,
4743
+ },
4744
+ };
4745
+ // This cast is okay because _form_element is only modified if it is
4746
+ // in in the list of open elements. This will never be.
4747
+ parser->_parser_state->_form_element = (GumboNode *)&form_ancestor;
4748
+ }
4749
+ }
4750
+
4751
+ GumboOutput* gumbo_parse(const char* buffer) {
4752
+ return gumbo_parse_with_options (
4753
+ &kGumboDefaultOptions,
4754
+ buffer,
4755
+ strlen(buffer)
4756
+ );
4757
+ }
4758
+
4759
+ GumboOutput* gumbo_parse_with_options (
4760
+ const GumboOptions* options,
4761
+ const char* buffer,
4762
+ size_t length
4763
+ ) {
4764
+ GumboParser parser;
4765
+ parser._options = options;
4766
+ output_init(&parser);
4767
+ gumbo_tokenizer_state_init(&parser, buffer, length);
4768
+ parser_state_init(&parser);
4769
+
4770
+ if (options->fragment_context != NULL)
4771
+ fragment_parser_init(&parser, options);
4772
+
4773
+ GumboParserState* state = parser._parser_state;
4774
+ gumbo_debug (
4775
+ "Parsing %.*s.\n",
4776
+ (int) length,
4777
+ buffer
4778
+ );
4779
+
4780
+ // Sanity check so that infinite loops die with an assertion failure instead
4781
+ // of hanging the process before we ever get an error.
4782
+ uint_fast32_t loop_count = 0;
4783
+
4784
+ const unsigned int max_tree_depth = options->max_tree_depth;
4785
+ GumboToken token;
4786
+
4787
+ do {
4788
+ if (state->_reprocess_current_token) {
4789
+ state->_reprocess_current_token = false;
4790
+ } else {
4791
+ GumboNode* adjusted_current_node = get_adjusted_current_node(&parser);
4792
+ gumbo_tokenizer_set_is_adjusted_current_node_foreign (
4793
+ &parser,
4794
+ adjusted_current_node &&
4795
+ adjusted_current_node->v.element.tag_namespace != GUMBO_NAMESPACE_HTML
4796
+ );
4797
+ // If the maximum tree depth has been exceeded, proceed as if EOF has been reached.
4798
+ //
4799
+ // The parser is pretty fragile. Breaking out of the parsing loop in the middle of
4800
+ // the parse can leave the document in an inconsistent state.
4801
+ if (unlikely(state->_open_elements.length > max_tree_depth)) {
4802
+ parser._output->status = GUMBO_STATUS_TREE_TOO_DEEP;
4803
+ gumbo_debug("Tree depth limit exceeded.\n");
4804
+ token.type = GUMBO_TOKEN_EOF;
4805
+ } else {
4806
+ gumbo_lex(&parser, &token);
4807
+ }
4808
+
4809
+ }
4810
+
4811
+ const char* token_type = "text";
4812
+ switch (token.type) {
4813
+ case GUMBO_TOKEN_DOCTYPE:
4814
+ token_type = "doctype";
4815
+ break;
4816
+ case GUMBO_TOKEN_START_TAG:
4817
+ if (token.v.start_tag.tag == GUMBO_TAG_UNKNOWN)
4818
+ token_type = token.v.start_tag.name;
4819
+ else
4820
+ token_type = gumbo_normalized_tagname(token.v.start_tag.tag);
4821
+ break;
4822
+ case GUMBO_TOKEN_END_TAG:
4823
+ token_type = gumbo_normalized_tagname(token.v.end_tag.tag);
4824
+ break;
4825
+ case GUMBO_TOKEN_COMMENT:
4826
+ token_type = "comment";
4827
+ break;
4828
+ default:
4829
+ break;
4830
+ }
4831
+ gumbo_debug (
4832
+ "Handling %s token @%lu:%lu in insertion mode %u.\n",
4833
+ (char*) token_type,
4834
+ (unsigned long)token.position.line,
4835
+ (unsigned long)token.position.column,
4836
+ state->_insertion_mode
4837
+ );
4838
+
4839
+ state->_current_token = &token;
4840
+ state->_self_closing_flag_acknowledged = false;
4841
+
4842
+ handle_token(&parser, &token);
4843
+
4844
+ // Check for memory leaks when ownership is transferred from start tag
4845
+ // tokens to nodes.
4846
+ assert (
4847
+ state->_reprocess_current_token
4848
+ || token.type != GUMBO_TOKEN_START_TAG
4849
+ || (token.v.start_tag.attributes.data == NULL
4850
+ && token.v.start_tag.name == NULL)
4851
+ );
4852
+
4853
+ if (!state->_reprocess_current_token) {
4854
+ // If we're done with the token, check for unacknowledged self-closing
4855
+ // flags on start tags.
4856
+ if (token.type == GUMBO_TOKEN_START_TAG &&
4857
+ token.v.start_tag.is_self_closing &&
4858
+ !state->_self_closing_flag_acknowledged) {
4859
+ GumboError* error = gumbo_add_error(&parser);
4860
+ if (error) {
4861
+ // This is essentially a tokenizer error that's only caught during
4862
+ // tree construction.
4863
+ error->type = GUMBO_ERR_NON_VOID_HTML_ELEMENT_START_TAG_WITH_TRAILING_SOLIDUS;
4864
+ error->original_text = token.original_text;
4865
+ error->position = token.position;
4866
+ }
4867
+ }
4868
+ // Make sure we free the end tag's name since it doesn't get transferred
4869
+ // to a token.
4870
+ if (token.type == GUMBO_TOKEN_END_TAG &&
4871
+ token.v.end_tag.tag == GUMBO_TAG_UNKNOWN)
4872
+ {
4873
+ gumbo_free(token.v.end_tag.name);
4874
+ token.v.end_tag.name = NULL;
4875
+ }
4876
+ }
4877
+
4878
+
4879
+ ++loop_count;
4880
+ assert(loop_count < 1000000000UL);
4881
+
4882
+ } while (
4883
+ (token.type != GUMBO_TOKEN_EOF || state->_reprocess_current_token)
4884
+ && !(options->stop_on_first_error && parser._output->document_error)
4885
+ );
4886
+
4887
+ finish_parsing(&parser);
4888
+ // For API uniformity reasons, if the doctype still has nulls, convert them to
4889
+ // empty strings.
4890
+ GumboDocument* doc_type = &parser._output->document->v.document;
4891
+ if (doc_type->name == NULL) {
4892
+ doc_type->name = gumbo_strdup("");
4893
+ }
4894
+ if (doc_type->public_identifier == NULL) {
4895
+ doc_type->public_identifier = gumbo_strdup("");
4896
+ }
4897
+ if (doc_type->system_identifier == NULL) {
4898
+ doc_type->system_identifier = gumbo_strdup("");
4899
+ }
4900
+
4901
+ parser_state_destroy(&parser);
4902
+ gumbo_tokenizer_state_destroy(&parser);
4903
+ return parser._output;
4904
+ }
4905
+
4906
+ const char* gumbo_status_to_string(GumboOutputStatus status) {
4907
+ switch (status) {
4908
+ case GUMBO_STATUS_OK:
4909
+ return "OK";
4910
+ case GUMBO_STATUS_OUT_OF_MEMORY:
4911
+ return "System allocator returned NULL during parsing";
4912
+ case GUMBO_STATUS_TOO_MANY_ATTRIBUTES:
4913
+ return "Attributes per element limit exceeded";
4914
+ case GUMBO_STATUS_TREE_TOO_DEEP:
4915
+ return "Document tree depth limit exceeded";
4916
+ default:
4917
+ return "Unknown GumboOutputStatus value";
4918
+ }
4919
+ }
4920
+
4921
+ void gumbo_destroy_node(GumboNode* node) {
4922
+ destroy_node(node);
4923
+ }
4924
+
4925
+ void gumbo_destroy_output(GumboOutput* output) {
4926
+ destroy_node(output->document);
4927
+ for (unsigned int i = 0; i < output->errors.length; ++i) {
4928
+ gumbo_error_destroy(output->errors.data[i]);
4929
+ }
4930
+ gumbo_vector_destroy(&output->errors);
4931
+ gumbo_free(output);
4932
+ }