Class: Nokogiri::HTML4::SAX::ParserContext

Inherits:
XML::SAX::ParserContext show all
Defined in:
lib/nokogiri/html4/sax/parser_context.rb,
ext/nokogiri/html4_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::HTML4::SAX::Parser

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XML::SAX::ParserContext

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

Class Method Details

.file(filename, encoding) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ext/nokogiri/html4_sax_parser_context.c', line 37

static VALUE
parse_file(VALUE klass, VALUE filename, VALUE encoding)
{
  htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(
                             StringValueCStr(filename),
                             StringValueCStr(encoding)
                           );

  if (ctxt->sax) {
    xmlFree(ctxt->sax);
    ctxt->sax = NULL;
  }

  return noko_xml_sax_parser_context_wrap(klass, ctxt);
}

.memory(data, encoding) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'ext/nokogiri/html4_sax_parser_context.c', line 5

static VALUE
parse_memory(VALUE klass, VALUE data, VALUE encoding)
{
  htmlParserCtxtPtr ctxt;

  Check_Type(data, T_STRING);

  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 noko_xml_sax_parser_context_wrap(klass, ctxt);
}

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



10
11
12
13
14
15
16
# File 'lib/nokogiri/html4/sax/parser_context.rb', line 10

def self.new(thing, encoding = "UTF-8")
  if [:read, :close].all? { |x| thing.respond_to?(x) }
    super
  else
    memory(thing, encoding)
  end
end

Instance Method Details

#parse_with(sax_handler) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/nokogiri/html4_sax_parser_context.c', line 74

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

  ctxt = noko_xml_sax_parser_context_unwrap(self);
  sax = noko_sax_handler_unwrap(sax_handler);

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

  xmlSetStructuredErrorFunc(NULL, NULL);

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

  return self;
}