class Nokogiri::HTML::SAX::PushParser - nokogiri-1.8.0 Documentation

# class Nokogiri::HTML::SAX::PushParser

### Attributes

document[RW]

The Nokogiri::HTML::SAX::Document on which the PushParser will be operating

### Public Class Methods

new(doc = HTML::SAX::Document.new, file_name = nil, encoding = 'UTF-8') click to toggle source

    # File lib/nokogiri/html/sax/push_parser.rb, line 10
    def initialize(doc = HTML::SAX::Document.new, file_name = nil, encoding = 'UTF-8')
      @document = doc
      @encoding = encoding
      @sax_parser = HTML::SAX::Parser.new(doc, @encoding)

      ## Create our push parser context
      initialize_native(@sax_parser, file_name, encoding)
    end

### Public Instance Methods

<<(chunk, last_chunk = false)
Alias for: write
finish() click to toggle source

Finish the parsing. This method is only necessary for Nokogiri::HTML::SAX::Document#end_document to be called.

    # File lib/nokogiri/html/sax/push_parser.rb, line 30
    def finish
      write '', true
    end

write(chunk, last_chunk = false) click to toggle source

Write a `chunk` of HTML to the PushParser. Any callback methods that can be called will be called immediately.

    # File lib/nokogiri/html/sax/push_parser.rb, line 22
    def write chunk, last_chunk = false
      native_write(chunk, last_chunk)
    end

Also aliased as: <<

### Private Instance Methods

initialize_native(xml_sax, filename) click to toggle source

Initialize the push parser with `xml_sax` using `filename`

    static VALUE initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename,
                                   VALUE encoding)
    {
      htmlSAXHandlerPtr sax;
      const char * filename = NULL;
      htmlParserCtxtPtr ctx;
      xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;

      Data_Get_Struct(_xml_sax, xmlSAXHandler, sax);

      if(_filename != Qnil) filename = StringValueCStr(_filename);

      if (!NIL_P(encoding)) {
        enc = xmlParseCharEncoding(StringValueCStr(encoding));
        if (enc == XML_CHAR_ENCODING_ERROR)
          rb_raise(rb_eArgError, "Unsupported Encoding");
      }

      ctx = htmlCreatePushParserCtxt(
          sax,
          NULL,
          NULL,
          0,
          filename,
          enc
      );
      if(ctx == NULL)
        rb_raise(rb_eRuntimeError, "Could not create a parser context");

      ctx->userData = NOKOGIRI_SAX_TUPLE_NEW(ctx, self);

      ctx->sax2 = 1;
      DATA_PTR(self) = ctx;
      return self;
    }

native_write(chunk, last_chunk) click to toggle source

Write `chunk` to PushParser. `last_chunk` triggers the end_document handle

    static VALUE native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)
    {
      xmlParserCtxtPtr ctx;
      const char * chunk  = NULL;
      int size            = 0;

      Data_Get_Struct(self, xmlParserCtxt, ctx);

      if(Qnil != _chunk) {
        chunk = StringValuePtr(_chunk);
        size = (int)RSTRING_LEN(_chunk);
      }

      if(htmlParseChunk(ctx, chunk, size, Qtrue == _last_chunk ? 1 : 0)) {
        if (!(ctx->options & XML_PARSE_RECOVER)) {
          xmlErrorPtr e = xmlCtxtGetLastError(ctx);
          Nokogiri_error_raise(NULL, e);
        }
      }

      return self;
    }