Class: Nokogumbo

Inherits:
Object
  • Object
show all
Defined in:
ext/nokogumboc/nokogumbo.c

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object

Parse a string using gumbo_parse into a Nokogiri document



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
# File 'ext/nokogumboc/nokogumbo.c', line 184

static VALUE parse(VALUE self, VALUE string) {
  GumboOutput *output = gumbo_parse_with_options(
    &kGumboDefaultOptions, RSTRING_PTR(string),
    (size_t) RSTRING_LEN(string)
  );
  xmlDocPtr doc = xmlNewDoc(CONST_CAST "1.0");
#ifdef NGLIB
  doc->type = XML_HTML_DOCUMENT_NODE;
#endif
  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;
    xmlCreateIntSubset(doc, CONST_CAST name,
      (public[0] ? CONST_CAST public : NIL),
      (system[0] ? CONST_CAST system : NIL));
  }

  GumboVector *children = &output->document->v.document.children;
  for (int i=0; i < children->length; i++) {
    GumboNode *child = children->data[i];
    xmlNodePtr node = walk_tree(doc, child);
    if (node) {
      if (child == output->root)
        xmlDocSetRootElement(doc, node);
      else
        xmlAddChild((xmlNodePtr)doc, node);
    }
  }
  gumbo_destroy_output(&kGumboDefaultOptions, output);

  return Nokogiri_wrap_xml_document(Document, doc);
}