libxml-ruby 5.0.4 → 5.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY +17 -6
  3. data/README.rdoc +1 -1
  4. data/Rakefile +98 -98
  5. data/ext/libxml/extconf.h +1 -0
  6. data/ext/libxml/extconf.rb +5 -0
  7. data/ext/libxml/libxml.c +79 -79
  8. data/ext/libxml/ruby_xml.c +556 -556
  9. data/ext/libxml/ruby_xml_attr.c +333 -333
  10. data/ext/libxml/ruby_xml_attr.h +12 -12
  11. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  12. data/ext/libxml/ruby_xml_attributes.h +17 -17
  13. data/ext/libxml/ruby_xml_cbg.c +85 -85
  14. data/ext/libxml/ruby_xml_document.c +1129 -1129
  15. data/ext/libxml/ruby_xml_dtd.c +257 -257
  16. data/ext/libxml/ruby_xml_dtd.h +9 -9
  17. data/ext/libxml/ruby_xml_encoding.c +250 -250
  18. data/ext/libxml/ruby_xml_error.c +1003 -1003
  19. data/ext/libxml/ruby_xml_error.h +14 -14
  20. data/ext/libxml/ruby_xml_html_parser.c +91 -91
  21. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  22. data/ext/libxml/ruby_xml_html_parser_context.c +351 -351
  23. data/ext/libxml/ruby_xml_html_parser_context.h +10 -10
  24. data/ext/libxml/ruby_xml_html_parser_options.c +48 -48
  25. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  26. data/ext/libxml/ruby_xml_input_cbg.c +188 -188
  27. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  28. data/ext/libxml/ruby_xml_io.c +47 -47
  29. data/ext/libxml/ruby_xml_io.h +10 -10
  30. data/ext/libxml/ruby_xml_namespace.c +151 -151
  31. data/ext/libxml/ruby_xml_namespace.h +10 -10
  32. data/ext/libxml/ruby_xml_namespaces.c +293 -293
  33. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  34. data/ext/libxml/ruby_xml_node.h +13 -13
  35. data/ext/libxml/ruby_xml_parser.c +91 -91
  36. data/ext/libxml/ruby_xml_parser.h +10 -10
  37. data/ext/libxml/ruby_xml_parser_context.c +1009 -1009
  38. data/ext/libxml/ruby_xml_parser_context.h +10 -10
  39. data/ext/libxml/ruby_xml_parser_options.c +74 -74
  40. data/ext/libxml/ruby_xml_parser_options.h +10 -10
  41. data/ext/libxml/ruby_xml_reader.h +14 -14
  42. data/ext/libxml/ruby_xml_relaxng.h +8 -8
  43. data/ext/libxml/ruby_xml_sax2_handler.c +326 -326
  44. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  45. data/ext/libxml/ruby_xml_sax_parser.c +108 -108
  46. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  47. data/ext/libxml/ruby_xml_schema.h +25 -25
  48. data/ext/libxml/ruby_xml_schema_attribute.h +37 -37
  49. data/ext/libxml/ruby_xml_schema_element.h +11 -11
  50. data/ext/libxml/ruby_xml_schema_facet.c +50 -50
  51. data/ext/libxml/ruby_xml_schema_facet.h +9 -9
  52. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  53. data/ext/libxml/ruby_xml_version.h +9 -9
  54. data/ext/libxml/ruby_xml_writer.c +1124 -1124
  55. data/ext/libxml/ruby_xml_writer.h +6 -6
  56. data/ext/libxml/ruby_xml_xinclude.c +20 -20
  57. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  58. data/ext/libxml/ruby_xml_xpath.c +195 -195
  59. data/ext/libxml/ruby_xml_xpath.h +15 -15
  60. data/ext/libxml/ruby_xml_xpath_context.c +362 -362
  61. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  62. data/ext/libxml/ruby_xml_xpath_expression.h +10 -10
  63. data/ext/libxml/ruby_xml_xpath_object.h +17 -17
  64. data/lib/libxml/attr.rb +122 -122
  65. data/lib/libxml/attr_decl.rb +80 -80
  66. data/lib/libxml/attributes.rb +13 -13
  67. data/lib/libxml/document.rb +194 -194
  68. data/lib/libxml/error.rb +95 -95
  69. data/lib/libxml/hpricot.rb +77 -77
  70. data/lib/libxml/html_parser.rb +96 -96
  71. data/lib/libxml/namespace.rb +61 -61
  72. data/lib/libxml/namespaces.rb +37 -37
  73. data/lib/libxml/node.rb +323 -323
  74. data/lib/libxml/parser.rb +102 -102
  75. data/lib/libxml/sax_callbacks.rb +179 -179
  76. data/lib/libxml/sax_parser.rb +40 -40
  77. data/lib/libxml/tree.rb +28 -28
  78. data/lib/libxml-ruby.rb +2 -2
  79. data/lib/libxml.rb +4 -4
  80. data/lib/xml/libxml.rb +10 -10
  81. data/lib/xml.rb +13 -13
  82. data/libxml-ruby.gemspec +50 -49
  83. data/test/test_document.rb +140 -140
  84. data/test/test_document_write.rb +142 -142
  85. data/test/test_dtd.rb +126 -126
  86. data/test/test_encoding.rb +126 -126
  87. data/test/test_error.rb +197 -194
  88. data/test/test_helper.rb +21 -20
  89. data/test/test_namespace.rb +58 -58
  90. data/test/test_node.rb +235 -235
  91. data/test/test_node_write.rb +93 -93
  92. data/test/test_parser.rb +333 -333
  93. data/test/test_reader.rb +364 -364
  94. data/test/test_writer.rb +500 -468
  95. data/test/test_xml.rb +168 -168
  96. metadata +5 -5
  97. data/test/test.rb +0 -5
@@ -1,351 +1,351 @@
1
- /* Please see the LICENSE file for copyright and distribution information */
2
-
3
- #include "ruby_libxml.h"
4
- #include "ruby_xml_html_parser_context.h"
5
-
6
- #include <libxml/parserInternals.h>
7
-
8
- /*
9
- * Document-class: LibXML::XML::HTMLParser::Context
10
- *
11
- * The XML::HTMLParser::Context class provides in-depth control over how
12
- * a document is parsed.
13
- */
14
-
15
- VALUE cXMLHtmlParserContext;
16
- static ID IO_ATTR;
17
-
18
- /* OS X 10.5 ships with libxml2 version 2.6.16 which does not expose the
19
- htmlNewParserCtxt (or htmlInitParserCtxt which it uses) method. htmlNewParserCtxt
20
- wasn't added to the libxml2 header files until 2.6.27. So the next two
21
- methods are simply copied from a newer version of libxml2 (2.7.2). */
22
- #if LIBXML_VERSION < 20627
23
- #define XML_CTXT_FINISH_DTD_0 0xabcd1234
24
- static int htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
25
- {
26
- htmlSAXHandler *sax;
27
- if (ctxt == NULL) return(-1);
28
-
29
- memset(ctxt, 0, sizeof(htmlParserCtxt));
30
- ctxt->dict = xmlDictCreate();
31
- if (ctxt->dict == NULL) {
32
- rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
33
- return(-1);
34
- }
35
- sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
36
- if (sax == NULL) {
37
- rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
38
- return(-1);
39
- }
40
- else
41
- memset(sax, 0, sizeof(htmlSAXHandler));
42
-
43
- ctxt->inputTab = (htmlParserInputPtr *) xmlMalloc(5 * sizeof(htmlParserInputPtr));
44
- if (ctxt->inputTab == NULL) {
45
- rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
46
- ctxt->inputNr = 0;
47
- ctxt->inputMax = 0;
48
- ctxt->input = NULL;
49
- return(-1);
50
- }
51
- ctxt->inputNr = 0;
52
- ctxt->inputMax = 5;
53
- ctxt->input = NULL;
54
- ctxt->version = NULL;
55
- ctxt->encoding = NULL;
56
- ctxt->standalone = -1;
57
- ctxt->instate = XML_PARSER_START;
58
-
59
- ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
60
- if (ctxt->nodeTab == NULL) {
61
- rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
62
- ctxt->nodeNr = 0;
63
- ctxt->nodeMax = 0;
64
- ctxt->node = NULL;
65
- ctxt->inputNr = 0;
66
- ctxt->inputMax = 0;
67
- ctxt->input = NULL;
68
- return(-1);
69
- }
70
- ctxt->nodeNr = 0;
71
- ctxt->nodeMax = 10;
72
- ctxt->node = NULL;
73
-
74
- ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
75
- if (ctxt->nameTab == NULL) {
76
- rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
77
- ctxt->nameNr = 0;
78
- ctxt->nameMax = 10;
79
- ctxt->name = NULL;
80
- ctxt->nodeNr = 0;
81
- ctxt->nodeMax = 0;
82
- ctxt->node = NULL;
83
- ctxt->inputNr = 0;
84
- ctxt->inputMax = 0;
85
- ctxt->input = NULL;
86
- return(-1);
87
- }
88
- ctxt->nameNr = 0;
89
- ctxt->nameMax = 10;
90
- ctxt->name = NULL;
91
-
92
- if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
93
- else {
94
- ctxt->sax = sax;
95
- memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
96
- }
97
- ctxt->userData = ctxt;
98
- ctxt->myDoc = NULL;
99
- ctxt->wellFormed = 1;
100
- ctxt->replaceEntities = 0;
101
- ctxt->linenumbers = xmlLineNumbersDefaultValue;
102
- ctxt->html = 1;
103
- ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
104
- ctxt->vctxt.userData = ctxt;
105
- ctxt->vctxt.error = xmlParserValidityError;
106
- ctxt->vctxt.warning = xmlParserValidityWarning;
107
- ctxt->record_info = 0;
108
- ctxt->validate = 0;
109
- ctxt->nbChars = 0;
110
- ctxt->checkIndex = 0;
111
- ctxt->catalogs = NULL;
112
- xmlInitNodeInfoSeq(&ctxt->node_seq);
113
- return(0);
114
- }
115
-
116
- static htmlParserCtxtPtr htmlNewParserCtxt(void)
117
- {
118
- xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
119
- if (ctxt == NULL) {
120
- rb_raise(rb_eNoMemError, "NewParserCtxt: out of memory\n");
121
- return(NULL);
122
- }
123
- memset(ctxt, 0, sizeof(xmlParserCtxt));
124
- if (htmlInitParserCtxt(ctxt) < 0) {
125
- htmlFreeParserCtxt(ctxt);
126
- return(NULL);
127
- }
128
- return(ctxt);
129
- }
130
- #endif
131
-
132
- static void rxml_html_parser_context_free(htmlParserCtxtPtr ctxt)
133
- {
134
- htmlFreeParserCtxt(ctxt);
135
- }
136
-
137
- static VALUE rxml_html_parser_context_wrap(htmlParserCtxtPtr ctxt)
138
- {
139
- return Data_Wrap_Struct(cXMLHtmlParserContext, NULL, rxml_html_parser_context_free, ctxt);
140
- }
141
-
142
- /* call-seq:
143
- * XML::HTMLParser::Context.file(file) -> XML::HTMLParser::Context
144
- *
145
- * Creates a new parser context based on the specified file or uri.
146
- *
147
- * Parameters:
148
- *
149
- * file - A filename or uri
150
- * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
151
- */
152
- static VALUE rxml_html_parser_context_file(int argc, VALUE* argv, VALUE klass)
153
- {
154
- VALUE file, options;
155
- rb_scan_args(argc, argv, "11", &file, &options);
156
-
157
- htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
158
- if (!ctxt)
159
- rxml_raise(xmlGetLastError());
160
-
161
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
162
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
163
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
164
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
165
- htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
166
-
167
- return rxml_html_parser_context_wrap(ctxt);
168
- }
169
-
170
- /* call-seq:
171
- * XML::HTMLParser::Context.io(io) -> XML::HTMLParser::Context
172
- *
173
- * Creates a new parser context based on the specified io object.
174
- *
175
- * Parameters:
176
- *
177
- * io - A ruby IO object
178
- * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
179
- */
180
- static VALUE rxml_html_parser_context_io(int argc, VALUE* argv, VALUE klass)
181
- {
182
- VALUE io, options;
183
- rb_scan_args(argc, argv, "11", &io, &options);
184
-
185
- VALUE result;
186
- htmlParserCtxtPtr ctxt;
187
- xmlParserInputBufferPtr input;
188
- xmlParserInputPtr stream;
189
-
190
- if (NIL_P(io))
191
- rb_raise(rb_eTypeError, "Must pass in an IO object");
192
-
193
- input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
194
- (void*)io, XML_CHAR_ENCODING_NONE);
195
-
196
- ctxt = htmlNewParserCtxt();
197
- if (!ctxt)
198
- {
199
- xmlFreeParserInputBuffer(input);
200
- rxml_raise(xmlGetLastError());
201
- }
202
-
203
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
204
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
205
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
206
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
207
- htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
208
-
209
- stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
210
-
211
- if (!stream)
212
- {
213
- xmlFreeParserInputBuffer(input);
214
- xmlFreeParserCtxt(ctxt);
215
- rxml_raise(xmlGetLastError());
216
- }
217
- inputPush(ctxt, stream);
218
- result = rxml_html_parser_context_wrap(ctxt);
219
-
220
- /* Attach io object to parser so it won't get freed.*/
221
- rb_ivar_set(result, IO_ATTR, io);
222
-
223
- return result;
224
- }
225
-
226
- /* call-seq:
227
- * XML::HTMLParser::Context.string(string) -> XML::HTMLParser::Context
228
- *
229
- * Creates a new parser context based on the specified string.
230
- *
231
- * Parameters:
232
- *
233
- * string - A string that contains the data to parse
234
- * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
235
- */
236
- static VALUE rxml_html_parser_context_string(int argc, VALUE* argv, VALUE klass)
237
- {
238
- VALUE string, options;
239
- rb_scan_args(argc, argv, "11", &string, &options);
240
-
241
- Check_Type(string, T_STRING);
242
-
243
- if (RSTRING_LEN(string) == 0)
244
- rb_raise(rb_eArgError, "Must specify a string with one or more characters");
245
-
246
- htmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
247
- (int)RSTRING_LEN(string));
248
- if (!ctxt)
249
- rxml_raise(xmlGetLastError());
250
-
251
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
252
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
253
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
254
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
255
- htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
256
-
257
- // Setup sax handler
258
- // TODO - there must be a better way? The sax handler is initialized for XML, but we want
259
- // to use HTML
260
- memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
261
- xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
262
-
263
- return rxml_html_parser_context_wrap(ctxt);
264
- }
265
-
266
- /*
267
- * call-seq:
268
- * context.close -> nil
269
- *
270
- * Closes the underlying input streams. This is useful when parsing a large amount of
271
- * files and you want to close the files without relying on Ruby's garbage collector
272
- * to run.
273
- */
274
- static VALUE rxml_html_parser_context_close(VALUE self)
275
- {
276
- htmlParserCtxtPtr ctxt;
277
- xmlParserInputPtr xinput;
278
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
279
-
280
- while ((xinput = inputPop(ctxt)) != NULL)
281
- {
282
- xmlFreeInputStream(xinput);
283
- }
284
- return Qnil;
285
- }
286
-
287
- /*
288
- * call-seq:
289
- * context.disable_cdata = (true|false)
290
- *
291
- * Control whether the CDATA nodes will be created in this context.
292
- */
293
- static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE value)
294
- {
295
- htmlParserCtxtPtr ctxt;
296
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
297
-
298
- if (ctxt->sax == NULL)
299
- rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
300
-
301
- /* LibXML controls this internally with the default SAX handler. */
302
- if (value)
303
- ctxt->sax->cdataBlock = NULL;
304
- else
305
- ctxt->sax->cdataBlock = xmlSAX2CDataBlock;
306
-
307
- return value;
308
- }
309
-
310
- /*
311
- * call-seq:
312
- * context.options = XML::Parser::Options::NOENT |
313
- XML::Parser::Options::NOCDATA
314
- *
315
- * Provides control over the execution of a parser. Valid values
316
- * are the constants defined on XML::Parser::Options. Multiple
317
- * options can be combined by using Bitwise OR (|).
318
- */
319
- static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
320
- {
321
- int xml_options = NUM2INT(options);
322
- htmlParserCtxtPtr ctxt;
323
- Check_Type(options, T_FIXNUM);
324
-
325
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
326
- htmlCtxtUseOptions(ctxt, xml_options);
327
-
328
- #if LIBXML_VERSION >= 20707
329
- /* Big hack here, but htmlCtxtUseOptions doens't support HTML_PARSE_NOIMPLIED.
330
- So do it ourselves. There must be a better way??? */
331
- if (xml_options & HTML_PARSE_NOIMPLIED)
332
- {
333
- ctxt->options |= HTML_PARSE_NOIMPLIED;
334
- }
335
- #endif
336
-
337
- return self;
338
- }
339
-
340
- void rxml_init_html_parser_context(void)
341
- {
342
- IO_ATTR = ID2SYM(rb_intern("@io"));
343
- cXMLHtmlParserContext = rb_define_class_under(cXMLHtmlParser, "Context", cXMLParserContext);
344
-
345
- rb_define_singleton_method(cXMLHtmlParserContext, "file", rxml_html_parser_context_file, -1);
346
- rb_define_singleton_method(cXMLHtmlParserContext, "io", rxml_html_parser_context_io, -1);
347
- rb_define_singleton_method(cXMLHtmlParserContext, "string", rxml_html_parser_context_string, -1);
348
- rb_define_method(cXMLHtmlParserContext, "close", rxml_html_parser_context_close, 0);
349
- rb_define_method(cXMLHtmlParserContext, "disable_cdata=", rxml_html_parser_context_disable_cdata_set, 1);
350
- rb_define_method(cXMLHtmlParserContext, "options=", rxml_html_parser_context_options_set, 1);
351
- }
1
+ /* Please see the LICENSE file for copyright and distribution information */
2
+
3
+ #include "ruby_libxml.h"
4
+ #include "ruby_xml_html_parser_context.h"
5
+
6
+ #include <libxml/parserInternals.h>
7
+
8
+ /*
9
+ * Document-class: LibXML::XML::HTMLParser::Context
10
+ *
11
+ * The XML::HTMLParser::Context class provides in-depth control over how
12
+ * a document is parsed.
13
+ */
14
+
15
+ VALUE cXMLHtmlParserContext;
16
+ static ID IO_ATTR;
17
+
18
+ /* OS X 10.5 ships with libxml2 version 2.6.16 which does not expose the
19
+ htmlNewParserCtxt (or htmlInitParserCtxt which it uses) method. htmlNewParserCtxt
20
+ wasn't added to the libxml2 header files until 2.6.27. So the next two
21
+ methods are simply copied from a newer version of libxml2 (2.7.2). */
22
+ #if LIBXML_VERSION < 20627
23
+ #define XML_CTXT_FINISH_DTD_0 0xabcd1234
24
+ static int htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
25
+ {
26
+ htmlSAXHandler *sax;
27
+ if (ctxt == NULL) return(-1);
28
+
29
+ memset(ctxt, 0, sizeof(htmlParserCtxt));
30
+ ctxt->dict = xmlDictCreate();
31
+ if (ctxt->dict == NULL) {
32
+ rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
33
+ return(-1);
34
+ }
35
+ sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
36
+ if (sax == NULL) {
37
+ rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
38
+ return(-1);
39
+ }
40
+ else
41
+ memset(sax, 0, sizeof(htmlSAXHandler));
42
+
43
+ ctxt->inputTab = (htmlParserInputPtr *) xmlMalloc(5 * sizeof(htmlParserInputPtr));
44
+ if (ctxt->inputTab == NULL) {
45
+ rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
46
+ ctxt->inputNr = 0;
47
+ ctxt->inputMax = 0;
48
+ ctxt->input = NULL;
49
+ return(-1);
50
+ }
51
+ ctxt->inputNr = 0;
52
+ ctxt->inputMax = 5;
53
+ ctxt->input = NULL;
54
+ ctxt->version = NULL;
55
+ ctxt->encoding = NULL;
56
+ ctxt->standalone = -1;
57
+ ctxt->instate = XML_PARSER_START;
58
+
59
+ ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
60
+ if (ctxt->nodeTab == NULL) {
61
+ rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
62
+ ctxt->nodeNr = 0;
63
+ ctxt->nodeMax = 0;
64
+ ctxt->node = NULL;
65
+ ctxt->inputNr = 0;
66
+ ctxt->inputMax = 0;
67
+ ctxt->input = NULL;
68
+ return(-1);
69
+ }
70
+ ctxt->nodeNr = 0;
71
+ ctxt->nodeMax = 10;
72
+ ctxt->node = NULL;
73
+
74
+ ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
75
+ if (ctxt->nameTab == NULL) {
76
+ rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
77
+ ctxt->nameNr = 0;
78
+ ctxt->nameMax = 10;
79
+ ctxt->name = NULL;
80
+ ctxt->nodeNr = 0;
81
+ ctxt->nodeMax = 0;
82
+ ctxt->node = NULL;
83
+ ctxt->inputNr = 0;
84
+ ctxt->inputMax = 0;
85
+ ctxt->input = NULL;
86
+ return(-1);
87
+ }
88
+ ctxt->nameNr = 0;
89
+ ctxt->nameMax = 10;
90
+ ctxt->name = NULL;
91
+
92
+ if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
93
+ else {
94
+ ctxt->sax = sax;
95
+ memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
96
+ }
97
+ ctxt->userData = ctxt;
98
+ ctxt->myDoc = NULL;
99
+ ctxt->wellFormed = 1;
100
+ ctxt->replaceEntities = 0;
101
+ ctxt->linenumbers = xmlLineNumbersDefaultValue;
102
+ ctxt->html = 1;
103
+ ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
104
+ ctxt->vctxt.userData = ctxt;
105
+ ctxt->vctxt.error = xmlParserValidityError;
106
+ ctxt->vctxt.warning = xmlParserValidityWarning;
107
+ ctxt->record_info = 0;
108
+ ctxt->validate = 0;
109
+ ctxt->nbChars = 0;
110
+ ctxt->checkIndex = 0;
111
+ ctxt->catalogs = NULL;
112
+ xmlInitNodeInfoSeq(&ctxt->node_seq);
113
+ return(0);
114
+ }
115
+
116
+ static htmlParserCtxtPtr htmlNewParserCtxt(void)
117
+ {
118
+ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
119
+ if (ctxt == NULL) {
120
+ rb_raise(rb_eNoMemError, "NewParserCtxt: out of memory\n");
121
+ return(NULL);
122
+ }
123
+ memset(ctxt, 0, sizeof(xmlParserCtxt));
124
+ if (htmlInitParserCtxt(ctxt) < 0) {
125
+ htmlFreeParserCtxt(ctxt);
126
+ return(NULL);
127
+ }
128
+ return(ctxt);
129
+ }
130
+ #endif
131
+
132
+ static void rxml_html_parser_context_free(htmlParserCtxtPtr ctxt)
133
+ {
134
+ htmlFreeParserCtxt(ctxt);
135
+ }
136
+
137
+ static VALUE rxml_html_parser_context_wrap(htmlParserCtxtPtr ctxt)
138
+ {
139
+ return Data_Wrap_Struct(cXMLHtmlParserContext, NULL, rxml_html_parser_context_free, ctxt);
140
+ }
141
+
142
+ /* call-seq:
143
+ * XML::HTMLParser::Context.file(file) -> XML::HTMLParser::Context
144
+ *
145
+ * Creates a new parser context based on the specified file or uri.
146
+ *
147
+ * Parameters:
148
+ *
149
+ * file - A filename or uri
150
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
151
+ */
152
+ static VALUE rxml_html_parser_context_file(int argc, VALUE* argv, VALUE klass)
153
+ {
154
+ VALUE file, options;
155
+ rb_scan_args(argc, argv, "11", &file, &options);
156
+
157
+ htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
158
+ if (!ctxt)
159
+ rxml_raise(xmlGetLastError());
160
+
161
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
162
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
163
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
164
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
165
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
166
+
167
+ return rxml_html_parser_context_wrap(ctxt);
168
+ }
169
+
170
+ /* call-seq:
171
+ * XML::HTMLParser::Context.io(io) -> XML::HTMLParser::Context
172
+ *
173
+ * Creates a new parser context based on the specified io object.
174
+ *
175
+ * Parameters:
176
+ *
177
+ * io - A ruby IO object
178
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
179
+ */
180
+ static VALUE rxml_html_parser_context_io(int argc, VALUE* argv, VALUE klass)
181
+ {
182
+ VALUE io, options;
183
+ rb_scan_args(argc, argv, "11", &io, &options);
184
+
185
+ VALUE result;
186
+ htmlParserCtxtPtr ctxt;
187
+ xmlParserInputBufferPtr input;
188
+ xmlParserInputPtr stream;
189
+
190
+ if (NIL_P(io))
191
+ rb_raise(rb_eTypeError, "Must pass in an IO object");
192
+
193
+ input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
194
+ (void*)io, XML_CHAR_ENCODING_NONE);
195
+
196
+ ctxt = htmlNewParserCtxt();
197
+ if (!ctxt)
198
+ {
199
+ xmlFreeParserInputBuffer(input);
200
+ rxml_raise(xmlGetLastError());
201
+ }
202
+
203
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
204
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
205
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
206
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
207
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
208
+
209
+ stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
210
+
211
+ if (!stream)
212
+ {
213
+ xmlFreeParserInputBuffer(input);
214
+ xmlFreeParserCtxt(ctxt);
215
+ rxml_raise(xmlGetLastError());
216
+ }
217
+ inputPush(ctxt, stream);
218
+ result = rxml_html_parser_context_wrap(ctxt);
219
+
220
+ /* Attach io object to parser so it won't get freed.*/
221
+ rb_ivar_set(result, IO_ATTR, io);
222
+
223
+ return result;
224
+ }
225
+
226
+ /* call-seq:
227
+ * XML::HTMLParser::Context.string(string) -> XML::HTMLParser::Context
228
+ *
229
+ * Creates a new parser context based on the specified string.
230
+ *
231
+ * Parameters:
232
+ *
233
+ * string - A string that contains the data to parse
234
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
235
+ */
236
+ static VALUE rxml_html_parser_context_string(int argc, VALUE* argv, VALUE klass)
237
+ {
238
+ VALUE string, options;
239
+ rb_scan_args(argc, argv, "11", &string, &options);
240
+
241
+ Check_Type(string, T_STRING);
242
+
243
+ if (RSTRING_LEN(string) == 0)
244
+ rb_raise(rb_eArgError, "Must specify a string with one or more characters");
245
+
246
+ htmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
247
+ (int)RSTRING_LEN(string));
248
+ if (!ctxt)
249
+ rxml_raise(xmlGetLastError());
250
+
251
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
252
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
253
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
254
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
255
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
256
+
257
+ // Setup sax handler
258
+ // TODO - there must be a better way? The sax handler is initialized for XML, but we want
259
+ // to use HTML
260
+ memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
261
+ xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
262
+
263
+ return rxml_html_parser_context_wrap(ctxt);
264
+ }
265
+
266
+ /*
267
+ * call-seq:
268
+ * context.close -> nil
269
+ *
270
+ * Closes the underlying input streams. This is useful when parsing a large amount of
271
+ * files and you want to close the files without relying on Ruby's garbage collector
272
+ * to run.
273
+ */
274
+ static VALUE rxml_html_parser_context_close(VALUE self)
275
+ {
276
+ htmlParserCtxtPtr ctxt;
277
+ xmlParserInputPtr xinput;
278
+ Data_Get_Struct(self, htmlParserCtxt, ctxt);
279
+
280
+ while ((xinput = inputPop(ctxt)) != NULL)
281
+ {
282
+ xmlFreeInputStream(xinput);
283
+ }
284
+ return Qnil;
285
+ }
286
+
287
+ /*
288
+ * call-seq:
289
+ * context.disable_cdata = (true|false)
290
+ *
291
+ * Control whether the CDATA nodes will be created in this context.
292
+ */
293
+ static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE value)
294
+ {
295
+ htmlParserCtxtPtr ctxt;
296
+ Data_Get_Struct(self, htmlParserCtxt, ctxt);
297
+
298
+ if (ctxt->sax == NULL)
299
+ rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
300
+
301
+ /* LibXML controls this internally with the default SAX handler. */
302
+ if (value)
303
+ ctxt->sax->cdataBlock = NULL;
304
+ else
305
+ ctxt->sax->cdataBlock = xmlSAX2CDataBlock;
306
+
307
+ return value;
308
+ }
309
+
310
+ /*
311
+ * call-seq:
312
+ * context.options = XML::Parser::Options::NOENT |
313
+ XML::Parser::Options::NOCDATA
314
+ *
315
+ * Provides control over the execution of a parser. Valid values
316
+ * are the constants defined on XML::Parser::Options. Multiple
317
+ * options can be combined by using Bitwise OR (|).
318
+ */
319
+ static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
320
+ {
321
+ int xml_options = NUM2INT(options);
322
+ htmlParserCtxtPtr ctxt;
323
+ Check_Type(options, T_FIXNUM);
324
+
325
+ Data_Get_Struct(self, htmlParserCtxt, ctxt);
326
+ htmlCtxtUseOptions(ctxt, xml_options);
327
+
328
+ #if LIBXML_VERSION >= 20707
329
+ /* Big hack here, but htmlCtxtUseOptions doens't support HTML_PARSE_NOIMPLIED.
330
+ So do it ourselves. There must be a better way??? */
331
+ if (xml_options & HTML_PARSE_NOIMPLIED)
332
+ {
333
+ ctxt->options |= HTML_PARSE_NOIMPLIED;
334
+ }
335
+ #endif
336
+
337
+ return self;
338
+ }
339
+
340
+ void rxml_init_html_parser_context(void)
341
+ {
342
+ IO_ATTR = ID2SYM(rb_intern("@io"));
343
+ cXMLHtmlParserContext = rb_define_class_under(cXMLHtmlParser, "Context", cXMLParserContext);
344
+
345
+ rb_define_singleton_method(cXMLHtmlParserContext, "file", rxml_html_parser_context_file, -1);
346
+ rb_define_singleton_method(cXMLHtmlParserContext, "io", rxml_html_parser_context_io, -1);
347
+ rb_define_singleton_method(cXMLHtmlParserContext, "string", rxml_html_parser_context_string, -1);
348
+ rb_define_method(cXMLHtmlParserContext, "close", rxml_html_parser_context_close, 0);
349
+ rb_define_method(cXMLHtmlParserContext, "disable_cdata=", rxml_html_parser_context_disable_cdata_set, 1);
350
+ rb_define_method(cXMLHtmlParserContext, "options=", rxml_html_parser_context_options_set, 1);
351
+ }