/* TODO: DOCUMENT ME */
static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
{
  xmlNodePtr node;
  xmlNodePtr list;
  xmlNodeSetPtr set;
  xmlParserErrors error;
  VALUE doc, err;

  Data_Get_Struct(self, xmlNode, node);

  doc = DOC_RUBY_OBJECT(node->doc);
  err = rb_iv_get(doc, "@errors");

  xmlSetStructuredErrorFunc((void *)err, Nokogiri_error_array_pusher);

  /* Twiddle global variable because of a bug in libxml2.
   * http://git.gnome.org/browse/libxml2/commit/?id=e20fb5a72c83cbfc8e4a8aa3943c6be8febadab7
   */
#ifndef HTML_PARSE_NOIMPLIED
  htmlHandleOmittedElem(0);
#endif

  error = xmlParseInNodeContext(
      node,
      StringValuePtr(_str),
      (int)RSTRING_LEN(_str),
      (int)NUM2INT(_options),
      &list);

#ifndef HTML_PARSE_NOIMPLIED
  htmlHandleOmittedElem(1);
#endif

  xmlSetStructuredErrorFunc(NULL, NULL);

  /* FIXME: This probably needs to handle more constants... */
  switch(error) {
    case XML_ERR_OK:
      break;

    case XML_ERR_INTERNAL_ERROR:
    case XML_ERR_NO_MEMORY:
      rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
      break;

    default:
      break;
  }

  set = xmlXPathNodeSetCreate(NULL);

  while(list) {
    xmlXPathNodeSetAddUnique(set, list);
    list = list->next;
  }

  return Nokogiri_wrap_xml_node_set(set, doc);
}