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