gumbo-html 0.2.3 → 0.3.0

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