Nokogiri::HTML::SAX::ParserContext

class Nokogiri::HTML::SAX::ParserContext

Parent:
Nokogiri::XML::SAX::ParserContext

Context for HTML SAX parsers. This class is usually not instantiated by the user. Instead, you should be looking at Nokogiri::HTML::SAX::Parser

Public Class Methods

file(p1, p2) Show source
static VALUE parse_file(VALUE klass, VALUE filename, VALUE encoding)
{
  htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(
      StringValueCStr(filename),
      StringValueCStr(encoding)
  );
  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}
memory(p1, p2) Show source
static VALUE
parse_memory(VALUE klass, VALUE data, VALUE encoding)
{
    htmlParserCtxtPtr ctxt;

    if (NIL_P(data))
        rb_raise(rb_eArgError, "data cannot be nil");
    if (!(int)RSTRING_LEN(data))
        rb_raise(rb_eRuntimeError, "data cannot be empty");

    ctxt = htmlCreateMemoryParserCtxt(StringValuePtr(data),
                                      (int)RSTRING_LEN(data));
    if (ctxt->sax) {
        xmlFree(ctxt->sax);
        ctxt->sax = NULL;
    }

    if (RTEST(encoding)) {
        xmlCharEncodingHandlerPtr enc = xmlFindCharEncodingHandler(StringValueCStr(encoding));
        if (enc != NULL) {
            xmlSwitchToEncoding(ctxt, enc);
            if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
                rb_raise(rb_eRuntimeError, "Unsupported encoding %s",
                         StringValueCStr(encoding));
            }
        }
    }

    return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}
new(thing, encoding = 'UTF-8') Show source
Calls superclass method Nokogiri::XML::SAX::ParserContext.new
# File lib/nokogiri/html/sax/parser_context.rb, line 9
def self.new thing, encoding = 'UTF-8'
  [:read, :close].all? { |x| thing.respond_to?(x) } ?  super :
    memory(thing, encoding)
end

Public Instance Methods

parse_with(p1) Show source
static VALUE
parse_with(VALUE self, VALUE sax_handler)
{
    htmlParserCtxtPtr ctxt;
    htmlSAXHandlerPtr sax;

    if (!rb_obj_is_kind_of(sax_handler, cNokogiriXmlSaxParser))
        rb_raise(rb_eArgError, "argument must be a Nokogiri::XML::SAX::Parser");

    Data_Get_Struct(self, htmlParserCtxt, ctxt);
    Data_Get_Struct(sax_handler, htmlSAXHandler, sax);

    /* Free the sax handler since we'll assign our own */
    if (ctxt->sax && ctxt->sax != (xmlSAXHandlerPtr)&xmlDefaultSAXHandler)
        xmlFree(ctxt->sax);

    ctxt->sax = sax;
    ctxt->userData = (void *)NOKOGIRI_SAX_TUPLE_NEW(ctxt, sax_handler);

    rb_ensure(parse_doc, (VALUE)ctxt, parse_doc_finalize, (VALUE)ctxt);

    return self;
}

© 2008–2016 Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo
Patrick Mahoney, Yoko Harada, Akinori Musha, John Shahid
Licensed under the MIT License.

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部