class Nokogumbo

Public Class Methods

parse(p1) click to toggle source
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);
}