forked from sparklemotion/nokogiri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html4_document.c
166 lines (137 loc) · 4.98 KB
/
html4_document.c
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <nokogiri.h>
VALUE cNokogiriHtml4Document ;
static ID id_encoding_found;
static ID id_to_s;
/*
* call-seq:
* new
*
* Create a new document
*/
static VALUE
rb_html_document_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE uri, external_id, rest, rb_doc;
htmlDocPtr doc;
rb_scan_args(argc, argv, "0*", &rest);
uri = rb_ary_entry(rest, (long)0);
external_id = rb_ary_entry(rest, (long)1);
doc = htmlNewDoc(
RTEST(uri) ? (const xmlChar *)StringValueCStr(uri) : NULL,
RTEST(external_id) ? (const xmlChar *)StringValueCStr(external_id) : NULL
);
rb_doc = noko_xml_document_wrap_with_init_args(klass, doc, argc, argv);
return rb_doc ;
}
/*
* call-seq:
* read_io(io, url, encoding, options)
*
* Read the HTML document from +io+ with given +url+, +encoding+,
* and +options+. See Nokogiri::HTML4.parse
*/
static VALUE
rb_html_document_s_read_io(VALUE klass, VALUE rb_io, VALUE rb_url, VALUE rb_encoding, VALUE rb_options)
{
VALUE rb_doc;
VALUE rb_error_list = rb_ary_new();
htmlDocPtr c_doc;
const char *c_url = NIL_P(rb_url) ? NULL : StringValueCStr(rb_url);
const char *c_encoding = NIL_P(rb_encoding) ? NULL : StringValueCStr(rb_encoding);
int options = NUM2INT(rb_options);
xmlSetStructuredErrorFunc((void *)rb_error_list, Nokogiri_error_array_pusher);
c_doc = htmlReadIO(noko_io_read, noko_io_close, (void *)rb_io, c_url, c_encoding, options);
xmlSetStructuredErrorFunc(NULL, NULL);
/*
* If EncodingFound has occurred in EncodingReader, make sure to do
* a cleanup and propagate the error.
*/
if (rb_respond_to(rb_io, id_encoding_found)) {
VALUE encoding_found = rb_funcall(rb_io, id_encoding_found, 0);
if (!NIL_P(encoding_found)) {
xmlFreeDoc(c_doc);
rb_exc_raise(encoding_found);
}
}
if ((c_doc == NULL) || (!(options & XML_PARSE_RECOVER) && (RARRAY_LEN(rb_error_list) > 0))) {
VALUE rb_error ;
xmlFreeDoc(c_doc);
rb_error = rb_ary_entry(rb_error_list, 0);
if (rb_error == Qnil) {
rb_raise(rb_eRuntimeError, "Could not parse document");
} else {
VALUE exception_message = rb_funcall(rb_error, id_to_s, 0);
exception_message = rb_str_concat(rb_str_new2("Parser without recover option encountered error or warning: "),
exception_message);
rb_exc_raise(rb_class_new_instance(1, &exception_message, cNokogiriXmlSyntaxError));
}
return Qnil;
}
rb_doc = noko_xml_document_wrap(klass, c_doc);
rb_iv_set(rb_doc, "@errors", rb_error_list);
return rb_doc;
}
/*
* call-seq:
* read_memory(string, url, encoding, options)
*
* Read the HTML document contained in +string+ with given +url+, +encoding+,
* and +options+. See Nokogiri::HTML4.parse
*/
static VALUE
rb_html_document_s_read_memory(VALUE klass, VALUE rb_html, VALUE rb_url, VALUE rb_encoding, VALUE rb_options)
{
VALUE rb_doc;
VALUE rb_error_list = rb_ary_new();
htmlDocPtr c_doc;
const char *c_buffer = StringValuePtr(rb_html);
const char *c_url = NIL_P(rb_url) ? NULL : StringValueCStr(rb_url);
const char *c_encoding = NIL_P(rb_encoding) ? NULL : StringValueCStr(rb_encoding);
int html_len = (int)RSTRING_LEN(rb_html);
int options = NUM2INT(rb_options);
xmlSetStructuredErrorFunc((void *)rb_error_list, Nokogiri_error_array_pusher);
c_doc = htmlReadMemory(c_buffer, html_len, c_url, c_encoding, options);
xmlSetStructuredErrorFunc(NULL, NULL);
if ((c_doc == NULL) || (!(options & XML_PARSE_RECOVER) && (RARRAY_LEN(rb_error_list) > 0))) {
VALUE rb_error ;
xmlFreeDoc(c_doc);
rb_error = rb_ary_entry(rb_error_list, 0);
if (rb_error == Qnil) {
rb_raise(rb_eRuntimeError, "Could not parse document");
} else {
VALUE exception_message = rb_funcall(rb_error, id_to_s, 0);
exception_message = rb_str_concat(rb_str_new2("Parser without recover option encountered error or warning: "),
exception_message);
rb_exc_raise(rb_class_new_instance(1, &exception_message, cNokogiriXmlSyntaxError));
}
return Qnil;
}
rb_doc = noko_xml_document_wrap(klass, c_doc);
rb_iv_set(rb_doc, "@errors", rb_error_list);
return rb_doc;
}
/*
* call-seq:
* type
*
* The type for this document
*/
static VALUE
rb_html_document_type(VALUE self)
{
htmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
return INT2NUM(doc->type);
}
void
noko_init_html_document(void)
{
assert(cNokogiriXmlDocument);
cNokogiriHtml4Document = rb_define_class_under(mNokogiriHtml4, "Document", cNokogiriXmlDocument);
rb_define_singleton_method(cNokogiriHtml4Document, "read_memory", rb_html_document_s_read_memory, 4);
rb_define_singleton_method(cNokogiriHtml4Document, "read_io", rb_html_document_s_read_io, 4);
rb_define_singleton_method(cNokogiriHtml4Document, "new", rb_html_document_s_new, -1);
rb_define_method(cNokogiriHtml4Document, "type", rb_html_document_type, 0);
id_encoding_found = rb_intern("encoding_found");
id_to_s = rb_intern("to_s");
}