/
gumbo.c
623 lines (549 loc) · 18 KB
/
gumbo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//
// Copyright 2013-2021 Sam Ruby, Stephen Checkoway
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// nokogumbo.c defines the following:
//
// class Nokogumbo
// def parse(utf8_string) # returns Nokogiri::HTML5::Document
// end
//
// Processing starts by calling gumbo_parse_with_options. The resulting document tree
// is then walked, a parallel libxml2 tree is constructed, and the final document is
// then wrapped using Nokogiri_wrap_xml_document. This approach reduces memory and CPU
// requirements as Ruby objects are only built when necessary.
//
#include <nokogiri.h>
#include "gumbo.h"
VALUE cNokogiriHtml5Document;
// Interned symbols
static ID internal_subset;
static ID parent;
/* Backwards compatibility to Ruby 2.1.0 */
#if RUBY_API_VERSION_CODE < 20200
#define ONIG_ESCAPE_UCHAR_COLLISION 1
#include <ruby/encoding.h>
static VALUE
rb_utf8_str_new(const char *str, long length)
{
return rb_enc_str_new(str, length, rb_utf8_encoding());
}
static VALUE
rb_utf8_str_new_cstr(const char *str)
{
return rb_enc_str_new_cstr(str, rb_utf8_encoding());
}
static VALUE
rb_utf8_str_new_static(const char *str, long length)
{
return rb_enc_str_new(str, length, rb_utf8_encoding());
}
#endif
#include <nokogiri.h>
#include <libxml/tree.h>
#include <libxml/HTMLtree.h>
// URI = system id
// external id = public id
static xmlDocPtr
new_html_doc(const char *dtd_name, const char *system, const char *public)
{
// These two libxml2 functions take the public and system ids in
// opposite orders.
htmlDocPtr doc = htmlNewDocNoDtD(/* URI */ NULL, /* ExternalID */NULL);
assert(doc);
if (dtd_name) {
xmlCreateIntSubset(doc, BAD_CAST dtd_name, BAD_CAST public, BAD_CAST system);
}
return doc;
}
static xmlNodePtr
get_parent(xmlNodePtr node)
{
return node->parent;
}
static GumboOutput *
perform_parse(const GumboOptions *options, VALUE input)
{
GumboOutput *output;
const char *status_string;
assert(RTEST(input));
Check_Type(input, T_STRING);
output = gumbo_parse_with_options(
options,
RSTRING_PTR(input),
RSTRING_LEN(input)
);
status_string = gumbo_status_to_string(output->status);
switch (output->status) {
case GUMBO_STATUS_OK:
break;
case GUMBO_STATUS_TOO_MANY_ATTRIBUTES:
case GUMBO_STATUS_TREE_TOO_DEEP:
gumbo_destroy_output(output);
rb_raise(rb_eArgError, "%s", status_string);
case GUMBO_STATUS_OUT_OF_MEMORY:
gumbo_destroy_output(output);
rb_raise(rb_eNoMemError, "%s", status_string);
}
return output;
}
static xmlNsPtr
lookup_or_add_ns(
xmlDocPtr doc,
xmlNodePtr root,
const char *href,
const char *prefix
)
{
xmlNsPtr ns = xmlSearchNs(doc, root, BAD_CAST prefix);
if (ns) {
return ns;
}
return xmlNewNs(root, BAD_CAST href, BAD_CAST prefix);
}
static void
set_line(xmlNodePtr node, size_t line)
{
// libxml2 uses 65535 to mean look elsewhere for the line number on some
// nodes.
if (line < 65535) {
node->line = (unsigned short)line;
}
}
// Construct an XML tree rooted at xml_output_node from the Gumbo tree rooted
// at gumbo_node.
static void
build_tree(
xmlDocPtr doc,
xmlNodePtr xml_output_node,
const GumboNode *gumbo_node
)
{
xmlNodePtr xml_root = NULL;
xmlNodePtr xml_node = xml_output_node;
size_t child_index = 0;
while (true) {
const GumboVector *children;
const GumboNode *gumbo_child;
xmlNodePtr xml_child;
assert(gumbo_node != NULL);
children = gumbo_node->type == GUMBO_NODE_DOCUMENT ?
&gumbo_node->v.document.children : &gumbo_node->v.element.children;
if (child_index >= children->length) {
// Move up the tree and to the next child.
if (xml_node == xml_output_node) {
// We've built as much of the tree as we can.
return;
}
child_index = gumbo_node->index_within_parent + 1;
gumbo_node = gumbo_node->parent;
xml_node = get_parent(xml_node);
// Children of fragments don't share the same root, so reset it and
// it'll be set below. In the non-fragment case, this will only happen
// after the html element has been finished at which point there are no
// further elements.
if (xml_node == xml_output_node) {
xml_root = NULL;
}
continue;
}
gumbo_child = children->data[child_index++];
switch (gumbo_child->type) {
case GUMBO_NODE_DOCUMENT:
abort(); // Bug in Gumbo.
case GUMBO_NODE_TEXT:
case GUMBO_NODE_WHITESPACE:
xml_child = xmlNewDocText(doc, BAD_CAST gumbo_child->v.text.text);
set_line(xml_child, gumbo_child->v.text.start_pos.line);
xmlAddChild(xml_node, xml_child);
break;
case GUMBO_NODE_CDATA:
xml_child = xmlNewCDataBlock(doc, BAD_CAST gumbo_child->v.text.text,
(int) strlen(gumbo_child->v.text.text));
set_line(xml_child, gumbo_child->v.text.start_pos.line);
xmlAddChild(xml_node, xml_child);
break;
case GUMBO_NODE_COMMENT:
xml_child = xmlNewDocComment(doc, BAD_CAST gumbo_child->v.text.text);
set_line(xml_child, gumbo_child->v.text.start_pos.line);
xmlAddChild(xml_node, xml_child);
break;
case GUMBO_NODE_TEMPLATE:
// XXX: Should create a template element and a new DocumentFragment
case GUMBO_NODE_ELEMENT: {
xmlNsPtr ns = NULL;
const GumboVector *attrs;
size_t i;
xml_child = xmlNewDocNode(doc, NULL, BAD_CAST gumbo_child->v.element.name, NULL);
set_line(xml_child, gumbo_child->v.element.start_pos.line);
if (xml_root == NULL) {
xml_root = xml_child;
}
switch (gumbo_child->v.element.tag_namespace) {
case GUMBO_NAMESPACE_HTML:
break;
case GUMBO_NAMESPACE_SVG:
ns = lookup_or_add_ns(doc, xml_root, "http://www.w3.org/2000/svg", "svg");
break;
case GUMBO_NAMESPACE_MATHML:
ns = lookup_or_add_ns(doc, xml_root, "http://www.w3.org/1998/Math/MathML", "math");
break;
}
if (ns != NULL) {
xmlSetNs(xml_child, ns);
}
xmlAddChild(xml_node, xml_child);
// Add the attributes.
attrs = &gumbo_child->v.element.attributes;
for (i = 0; i < attrs->length; i++) {
const GumboAttribute *attr = attrs->data[i];
switch (attr->attr_namespace) {
case GUMBO_ATTR_NAMESPACE_XLINK:
ns = lookup_or_add_ns(doc, xml_root, "http://www.w3.org/1999/xlink", "xlink");
break;
case GUMBO_ATTR_NAMESPACE_XML:
ns = lookup_or_add_ns(doc, xml_root, "http://www.w3.org/XML/1998/namespace", "xml");
break;
case GUMBO_ATTR_NAMESPACE_XMLNS:
ns = lookup_or_add_ns(doc, xml_root, "http://www.w3.org/2000/xmlns/", "xmlns");
break;
default:
ns = NULL;
}
xmlNewNsProp(xml_child, ns, BAD_CAST attr->name, BAD_CAST attr->value);
}
// Add children for this element.
child_index = 0;
gumbo_node = gumbo_child;
xml_node = xml_child;
}
}
}
}
static void
add_errors(const GumboOutput *output, VALUE rdoc, VALUE input, VALUE url)
{
const char *input_str = RSTRING_PTR(input);
size_t input_len = RSTRING_LEN(input);
// Add parse errors to rdoc.
if (output->errors.length) {
size_t i;
const GumboVector *errors = &output->errors;
VALUE rerrors = rb_ary_new2(errors->length);
for (i = 0; i < errors->length; i++) {
GumboError *err;
GumboSourcePosition position;
char *msg;
size_t size;
VALUE err_str;
VALUE syntax_error;
const char *error_code;
VALUE str1;
err = errors->data[i];
position = gumbo_error_position(err);
size = gumbo_caret_diagnostic_to_string(err, input_str, input_len, &msg);
err_str = rb_utf8_str_new(msg, size);
free(msg);
syntax_error = rb_class_new_instance(1, &err_str, cNokogiriXmlSyntaxError);
error_code = gumbo_error_code(err);
str1 = error_code ? rb_utf8_str_new_static(error_code, strlen(error_code)) : Qnil;
rb_iv_set(syntax_error, "@domain", INT2NUM(1)); // XML_FROM_PARSER
rb_iv_set(syntax_error, "@code", INT2NUM(1)); // XML_ERR_INTERNAL_ERROR
rb_iv_set(syntax_error, "@level", INT2NUM(2)); // XML_ERR_ERROR
rb_iv_set(syntax_error, "@file", url);
rb_iv_set(syntax_error, "@line", INT2NUM(position.line));
rb_iv_set(syntax_error, "@str1", str1);
rb_iv_set(syntax_error, "@str2", Qnil);
rb_iv_set(syntax_error, "@str3", Qnil);
rb_iv_set(syntax_error, "@int1", INT2NUM(0));
rb_iv_set(syntax_error, "@column", INT2NUM(position.column));
rb_ary_push(rerrors, syntax_error);
}
rb_iv_set(rdoc, "@errors", rerrors);
}
}
typedef struct {
GumboOutput *output;
VALUE input;
VALUE url_or_frag;
xmlDocPtr doc;
} ParseArgs;
static VALUE
parse_cleanup(VALUE parse_args)
{
ParseArgs *args = (ParseArgs *)parse_args;
gumbo_destroy_output(args->output);
// Make sure garbage collection doesn't mark the objects as being live based
// on references from the ParseArgs. This may be unnecessary.
args->input = Qnil;
args->url_or_frag = Qnil;
if (args->doc != NULL) {
xmlFreeDoc(args->doc);
}
return Qnil;
}
static VALUE parse_continue(VALUE parse_args);
/*
* @!visibility protected
*/
static VALUE
parse(VALUE self, VALUE input, VALUE url, VALUE max_attributes, VALUE max_errors, VALUE max_depth)
{
GumboOutput *output;
GumboOptions options = kGumboDefaultOptions;
ParseArgs args;
options.max_attributes = NUM2INT(max_attributes);
options.max_errors = NUM2INT(max_errors);
options.max_tree_depth = NUM2INT(max_depth);
output = perform_parse(&options, input);
args.output = output;
args.input = input;
args.url_or_frag = url;
args.doc = NULL;
return rb_ensure(parse_continue, (VALUE)(&args), parse_cleanup, (VALUE)(&args));
}
static VALUE
parse_continue(VALUE parse_args)
{
ParseArgs *args = (ParseArgs *)parse_args;
GumboOutput *output = args->output;
xmlDocPtr doc;
VALUE rdoc;
if (output->document->v.document.has_doctype) {
const char *name = output->document->v.document.name;
const char *public = output->document->v.document.public_identifier;
const char *system = output->document->v.document.system_identifier;
public = public[0] ? public : NULL;
system = system[0] ? system : NULL;
doc = new_html_doc(name, system, public);
} else {
doc = new_html_doc(NULL, NULL, NULL);
}
args->doc = doc; // Make sure doc gets cleaned up if an error is thrown.
build_tree(doc, (xmlNodePtr)doc, output->document);
rdoc = Nokogiri_wrap_xml_document(cNokogiriHtml5Document, doc);
args->doc = NULL; // The Ruby runtime now owns doc so don't delete it.
add_errors(output, rdoc, args->input, args->url_or_frag);
return rdoc;
}
static int
lookup_namespace(VALUE node, bool require_known_ns)
{
VALUE ns;
ID namespace, href;
const char *href_ptr;
size_t href_len;
CONST_ID(namespace, "namespace");
CONST_ID(href, "href");
ns = rb_funcall(node, namespace, 0);
if (NIL_P(ns)) {
return GUMBO_NAMESPACE_HTML;
}
ns = rb_funcall(ns, href, 0);
assert(RTEST(ns));
Check_Type(ns, T_STRING);
href_ptr = RSTRING_PTR(ns);
href_len = RSTRING_LEN(ns);
#define NAMESPACE_P(uri) (href_len == sizeof uri - 1 && !memcmp(href_ptr, uri, href_len))
if (NAMESPACE_P("http://www.w3.org/1999/xhtml")) {
return GUMBO_NAMESPACE_HTML;
}
if (NAMESPACE_P("http://www.w3.org/1998/Math/MathML")) {
return GUMBO_NAMESPACE_MATHML;
}
if (NAMESPACE_P("http://www.w3.org/2000/svg")) {
return GUMBO_NAMESPACE_SVG;
}
#undef NAMESPACE_P
if (require_known_ns) {
rb_raise(rb_eArgError, "Unexpected namespace URI \"%*s\"", (int)href_len, href_ptr);
}
return -1;
}
static xmlNodePtr
extract_xml_node(VALUE node)
{
xmlNodePtr xml_node;
Data_Get_Struct(node, xmlNode, xml_node);
return xml_node;
}
static VALUE fragment_continue(VALUE parse_args);
/*
* @!visibility protected
*/
static VALUE
fragment(
VALUE self,
VALUE doc_fragment,
VALUE tags,
VALUE ctx,
VALUE max_attributes,
VALUE max_errors,
VALUE max_depth
)
{
ID name = rb_intern_const("name");
const char *ctx_tag;
GumboNamespaceEnum ctx_ns;
GumboQuirksModeEnum quirks_mode;
bool form = false;
const char *encoding = NULL;
VALUE doc, dtd;
int depth;
GumboOptions options = kGumboDefaultOptions;
GumboOutput *output;
ParseArgs args;
if (NIL_P(ctx)) {
ctx_tag = "body";
ctx_ns = GUMBO_NAMESPACE_HTML;
} else if (TYPE(ctx) == T_STRING) {
size_t len;
const char *colon;
ctx_tag = StringValueCStr(ctx);
ctx_ns = GUMBO_NAMESPACE_HTML;
len = RSTRING_LEN(ctx);
colon = memchr(ctx_tag, ':', len);
if (colon) {
switch (colon - ctx_tag) {
case 3:
if (st_strncasecmp(ctx_tag, "svg", 3) != 0) {
goto error;
}
ctx_ns = GUMBO_NAMESPACE_SVG;
break;
case 4:
if (st_strncasecmp(ctx_tag, "html", 4) == 0) {
ctx_ns = GUMBO_NAMESPACE_HTML;
} else if (st_strncasecmp(ctx_tag, "math", 4) == 0) {
ctx_ns = GUMBO_NAMESPACE_MATHML;
} else {
goto error;
}
break;
default:
error:
rb_raise(rb_eArgError, "Invalid context namespace '%*s'", (int)(colon - ctx_tag), ctx_tag);
}
ctx_tag = colon + 1;
} else {
// For convenience, put 'svg' and 'math' in their namespaces.
if (len == 3 && st_strncasecmp(ctx_tag, "svg", 3) == 0) {
ctx_ns = GUMBO_NAMESPACE_SVG;
} else if (len == 4 && st_strncasecmp(ctx_tag, "math", 4) == 0) {
ctx_ns = GUMBO_NAMESPACE_MATHML;
}
}
// Check if it's a form.
form = ctx_ns == GUMBO_NAMESPACE_HTML && st_strcasecmp(ctx_tag, "form") == 0;
} else {
VALUE node, element_name;
ID element_ = rb_intern_const("element?");
// Context fragment name.
VALUE tag_name = rb_funcall(ctx, name, 0);
assert(RTEST(tag_name));
Check_Type(tag_name, T_STRING);
ctx_tag = StringValueCStr(tag_name);
// Context fragment namespace.
ctx_ns = lookup_namespace(ctx, true);
// Check for a form ancestor, including self.
for (node = ctx;
!NIL_P(node);
node = rb_respond_to(node, parent) ? rb_funcall(node, parent, 0) : Qnil) {
if (!RTEST(rb_funcall(node, element_, 0))) {
continue;
}
element_name = rb_funcall(node, name, 0);
if (RSTRING_LEN(element_name) == 4
&& !st_strcasecmp(RSTRING_PTR(element_name), "form")
&& lookup_namespace(node, false) == GUMBO_NAMESPACE_HTML) {
form = true;
break;
}
}
// Encoding.
if (RSTRING_LEN(tag_name) == 14
&& !st_strcasecmp(ctx_tag, "annotation-xml")) {
VALUE enc = rb_funcall(ctx, rb_intern_const("[]"),
rb_utf8_str_new_static("encoding", 8));
if (RTEST(enc)) {
Check_Type(enc, T_STRING);
encoding = StringValueCStr(enc);
}
}
}
// Quirks mode.
doc = rb_funcall(doc_fragment, rb_intern_const("document"), 0);
dtd = rb_funcall(doc, internal_subset, 0);
if (NIL_P(dtd)) {
quirks_mode = GUMBO_DOCTYPE_NO_QUIRKS;
} else {
VALUE dtd_name = rb_funcall(dtd, name, 0);
VALUE pubid = rb_funcall(dtd, rb_intern_const("external_id"), 0);
VALUE sysid = rb_funcall(dtd, rb_intern_const("system_id"), 0);
quirks_mode = gumbo_compute_quirks_mode(
NIL_P(dtd_name) ? NULL : StringValueCStr(dtd_name),
NIL_P(pubid) ? NULL : StringValueCStr(pubid),
NIL_P(sysid) ? NULL : StringValueCStr(sysid)
);
}
// Perform a fragment parse.
depth = NUM2INT(max_depth);
options = kGumboDefaultOptions;
options.max_attributes = NUM2INT(max_attributes);
options.max_errors = NUM2INT(max_errors);
// Add one to account for the HTML element.
options.max_tree_depth = depth < 0 ? -1 : (depth + 1);
options.fragment_context = ctx_tag;
options.fragment_namespace = ctx_ns;
options.fragment_encoding = encoding;
options.quirks_mode = quirks_mode;
options.fragment_context_has_form_ancestor = form;
output = perform_parse(&options, tags);
args.output = output;
args.input = tags;
args.url_or_frag = doc_fragment;
args.doc = (xmlDocPtr)extract_xml_node(doc);
rb_ensure(fragment_continue, (VALUE)(&args), parse_cleanup, (VALUE)(&args));
return Qnil;
}
static VALUE
fragment_continue(VALUE parse_args)
{
ParseArgs *args = (ParseArgs *)parse_args;
GumboOutput *output = args->output;
VALUE doc_fragment = args->url_or_frag;
xmlDocPtr xml_doc = args->doc;
xmlNodePtr xml_frag;
args->doc = NULL; // The Ruby runtime owns doc so make sure we don't delete it.
xml_frag = extract_xml_node(doc_fragment);
build_tree(xml_doc, xml_frag, output->root);
add_errors(output, doc_fragment, args->input, rb_utf8_str_new_static("#fragment", 9));
return Qnil;
}
// Initialize the Nokogumbo class and fetch constants we will use later.
void
noko_init_gumbo()
{
// Class constants.
cNokogiriHtml5Document = rb_define_class_under(mNokogiriHtml5, "Document", cNokogiriHtml4Document);
rb_gc_register_mark_object(cNokogiriHtml5Document);
// Interned symbols.
internal_subset = rb_intern_const("internal_subset");
parent = rb_intern_const("parent");
// Define Nokogumbo module with parse and fragment methods.
rb_define_singleton_method(mNokogiriGumbo, "parse", parse, 5);
rb_define_singleton_method(mNokogiriGumbo, "fragment", fragment, 6);
}
// vim: set shiftwidth=2 softtabstop=2 tabstop=8 expandtab: