Class: Nokogiri::HTML::SAX::ParserContext

Inherits:
XML::SAX::ParserContext show all
Defined in:
lib/nokogiri/html/sax/parser_context.rb,
ext/nokogiri/html_sax_parser_context.c

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XML::SAX::ParserContext

#column, io, #line, #replace_entities, #replace_entities=

Class Method Details

.file(filename, encoding) ⇒ Object



43
44
45
46
47
48
49
50
# File 'ext/nokogiri/html_sax_parser_context.c', line 43

static VALUE parse_file(VALUE klass, VALUE filename, VALUE encoding)
{
  htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(
      StringValuePtr(filename),
      StringValuePtr(encoding)
  );
  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}

.memory(data, encoding) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'ext/nokogiri/html_sax_parser_context.c', line 16

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(RTEST(encoding)) {
    xmlCharEncodingHandlerPtr enc = xmlFindCharEncodingHandler(StringValuePtr(encoding));
    if(enc != NULL) {
      xmlSwitchToEncoding(ctxt, enc);
      if(ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
        rb_raise(rb_eRuntimeError, "Unsupported encoding %s",
            StringValuePtr(encoding));
      }
    }
  }

  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}

.new(thing, encoding = 'UTF-8') ⇒ Object



9
10
11
12
# 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

Instance Method Details

#parse_with(sax_handler) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'ext/nokogiri/html_sax_parser_context.c', line 52

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);

  htmlParseDocument(ctxt);

  if(NULL != ctxt->myDoc) xmlFreeDoc(ctxt->myDoc);

  NOKOGIRI_SAX_TUPLE_DESTROY(ctxt->userData);
  return self;
}