libxml-ruby 4.1.2 → 6.0.0

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 (172) hide show
  1. checksums.yaml +4 -4
  2. data/{HISTORY → CHANGELOG.md} +875 -887
  3. data/LICENSE +20 -20
  4. data/README.md +67 -0
  5. data/Rakefile +77 -98
  6. data/ext/libxml/extconf.rb +39 -20
  7. data/ext/libxml/libxml.c +67 -79
  8. data/ext/libxml/ruby_libxml.h +43 -44
  9. data/ext/libxml/ruby_xml.c +556 -899
  10. data/ext/libxml/ruby_xml.h +9 -10
  11. data/ext/libxml/ruby_xml_attr.c +338 -333
  12. data/ext/libxml/ruby_xml_attr.h +13 -12
  13. data/ext/libxml/ruby_xml_attr_decl.c +19 -12
  14. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  15. data/ext/libxml/ruby_xml_attributes.c +18 -9
  16. data/ext/libxml/ruby_xml_attributes.h +17 -15
  17. data/ext/libxml/ruby_xml_document.c +1142 -1129
  18. data/ext/libxml/ruby_xml_document.h +12 -11
  19. data/ext/libxml/ruby_xml_dtd.c +287 -248
  20. data/ext/libxml/ruby_xml_dtd.h +10 -9
  21. data/ext/libxml/ruby_xml_encoding.c +250 -250
  22. data/ext/libxml/ruby_xml_encoding.h +20 -18
  23. data/ext/libxml/ruby_xml_error.c +1003 -996
  24. data/ext/libxml/ruby_xml_error.h +14 -14
  25. data/ext/libxml/ruby_xml_html_parser.c +91 -91
  26. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  27. data/ext/libxml/ruby_xml_html_parser_context.c +364 -337
  28. data/ext/libxml/ruby_xml_html_parser_context.h +11 -10
  29. data/ext/libxml/ruby_xml_html_parser_options.c +48 -48
  30. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  31. data/ext/libxml/ruby_xml_input_cbg.c +216 -188
  32. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  33. data/ext/libxml/ruby_xml_io.c +49 -47
  34. data/ext/libxml/ruby_xml_io.h +10 -10
  35. data/ext/libxml/ruby_xml_namespace.c +159 -154
  36. data/ext/libxml/ruby_xml_namespace.h +11 -10
  37. data/ext/libxml/ruby_xml_namespaces.c +313 -293
  38. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  39. data/ext/libxml/ruby_xml_node.c +1433 -1398
  40. data/ext/libxml/ruby_xml_node.h +15 -13
  41. data/ext/libxml/ruby_xml_parser.c +91 -91
  42. data/ext/libxml/ruby_xml_parser.h +10 -10
  43. data/ext/libxml/ruby_xml_parser_context.c +1016 -1001
  44. data/ext/libxml/ruby_xml_parser_context.h +11 -10
  45. data/ext/libxml/ruby_xml_parser_options.c +74 -66
  46. data/ext/libxml/ruby_xml_parser_options.h +10 -10
  47. data/ext/libxml/ruby_xml_reader.c +40 -31
  48. data/ext/libxml/ruby_xml_reader.h +14 -14
  49. data/ext/libxml/ruby_xml_registry.c +31 -0
  50. data/ext/libxml/ruby_xml_registry.h +22 -0
  51. data/ext/libxml/ruby_xml_relaxng.c +21 -5
  52. data/ext/libxml/ruby_xml_relaxng.h +9 -8
  53. data/ext/libxml/ruby_xml_sax2_handler.c +326 -326
  54. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  55. data/ext/libxml/ruby_xml_sax_parser.c +108 -116
  56. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  57. data/ext/libxml/ruby_xml_schema.c +22 -15
  58. data/ext/libxml/ruby_xml_schema.h +26 -25
  59. data/ext/libxml/ruby_xml_schema_attribute.c +7 -7
  60. data/ext/libxml/ruby_xml_schema_attribute.h +37 -37
  61. data/ext/libxml/ruby_xml_schema_element.c +8 -8
  62. data/ext/libxml/ruby_xml_schema_element.h +11 -11
  63. data/ext/libxml/ruby_xml_schema_facet.c +50 -50
  64. data/ext/libxml/ruby_xml_schema_facet.h +9 -9
  65. data/ext/libxml/ruby_xml_schema_type.c +12 -19
  66. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  67. data/ext/libxml/ruby_xml_version.h +9 -9
  68. data/ext/libxml/ruby_xml_writer.c +183 -185
  69. data/ext/libxml/ruby_xml_writer.h +6 -6
  70. data/ext/libxml/ruby_xml_xinclude.c +20 -20
  71. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  72. data/ext/libxml/ruby_xml_xpath.c +195 -195
  73. data/ext/libxml/ruby_xml_xpath.h +15 -15
  74. data/ext/libxml/ruby_xml_xpath_context.c +408 -362
  75. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  76. data/ext/libxml/ruby_xml_xpath_expression.c +12 -6
  77. data/ext/libxml/ruby_xml_xpath_expression.h +11 -10
  78. data/ext/libxml/ruby_xml_xpath_object.c +69 -54
  79. data/ext/libxml/ruby_xml_xpath_object.h +19 -17
  80. data/ext/vc/libxml_ruby/libxml_ruby.vcxproj +271 -0
  81. data/ext/xcode/libxml-ruby.xcodeproj/project.pbxproj +633 -0
  82. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  83. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  84. data/ext/xcode/libxml-ruby.xcodeproj/xcshareddata/xcschemes/libxml-ruby.xcscheme +80 -0
  85. data/lib/libxml/attr.rb +122 -122
  86. data/lib/libxml/attr_decl.rb +80 -80
  87. data/lib/libxml/attributes.rb +13 -13
  88. data/lib/libxml/document.rb +165 -194
  89. data/lib/libxml/error.rb +122 -95
  90. data/lib/libxml/html_parser.rb +80 -96
  91. data/lib/libxml/namespace.rb +61 -61
  92. data/lib/libxml/namespaces.rb +37 -37
  93. data/lib/libxml/node.rb +321 -323
  94. data/lib/libxml/parser.rb +94 -100
  95. data/lib/libxml/sax_callbacks.rb +179 -179
  96. data/lib/libxml/sax_parser.rb +40 -40
  97. data/lib/libxml/schema/attribute.rb +27 -19
  98. data/lib/libxml/schema/element.rb +20 -0
  99. data/lib/libxml/schema/type.rb +44 -21
  100. data/lib/libxml/schema.rb +47 -47
  101. data/lib/libxml/tree.rb +28 -28
  102. data/lib/libxml-ruby.rb +30 -30
  103. data/libxml-ruby.gemspec +46 -48
  104. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  105. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  106. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  107. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  108. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  109. data/test/c14n/result/with-comments/example-1 +5 -5
  110. data/test/c14n/result/with-comments/example-2 +10 -10
  111. data/test/c14n/result/with-comments/example-3 +13 -13
  112. data/test/c14n/result/with-comments/example-4 +8 -8
  113. data/test/c14n/result/with-comments/example-5 +3 -3
  114. data/test/c14n/result/without-comments/example-1 +3 -3
  115. data/test/c14n/result/without-comments/example-2 +10 -10
  116. data/test/c14n/result/without-comments/example-3 +13 -13
  117. data/test/c14n/result/without-comments/example-4 +8 -8
  118. data/test/c14n/result/without-comments/example-5 +2 -2
  119. data/test/test_attr.rb +179 -180
  120. data/test/test_attr_decl.rb +131 -131
  121. data/test/test_attributes.rb +135 -135
  122. data/test/test_canonicalize.rb +122 -120
  123. data/test/test_document.rb +138 -132
  124. data/test/test_document_write.rb +142 -145
  125. data/test/test_dtd.rb +134 -129
  126. data/test/test_encoding.rb +126 -129
  127. data/test/test_encoding_sax.rb +114 -114
  128. data/test/test_error.rb +197 -194
  129. data/test/test_helper.rb +21 -13
  130. data/test/test_html_parser.rb +166 -162
  131. data/test/test_html_parser_context.rb +22 -22
  132. data/test/test_input_callbacks.rb +36 -0
  133. data/test/test_namespace.rb +58 -60
  134. data/test/test_namespaces.rb +200 -200
  135. data/test/test_node.rb +251 -237
  136. data/test/test_node_cdata.rb +50 -50
  137. data/test/test_node_comment.rb +32 -32
  138. data/test/test_node_copy.rb +40 -40
  139. data/test/test_node_edit.rb +176 -158
  140. data/test/test_node_pi.rb +37 -37
  141. data/test/test_node_text.rb +69 -69
  142. data/test/test_node_write.rb +93 -96
  143. data/test/test_node_xlink.rb +28 -28
  144. data/test/test_parser.rb +297 -324
  145. data/test/test_parser_context.rb +198 -198
  146. data/test/test_properties.rb +38 -38
  147. data/test/test_reader.rb +413 -364
  148. data/test/test_relaxng.rb +59 -53
  149. data/test/test_sax_parser.rb +345 -326
  150. data/test/test_schema.rb +28 -0
  151. data/test/test_traversal.rb +152 -152
  152. data/test/test_writer.rb +469 -468
  153. data/test/test_xinclude.rb +20 -20
  154. data/test/test_xml.rb +164 -267
  155. data/test/test_xpath.rb +244 -244
  156. data/test/test_xpath_context.rb +87 -87
  157. data/test/test_xpath_expression.rb +37 -37
  158. metadata +49 -31
  159. data/README.rdoc +0 -217
  160. data/ext/libxml/extconf.h +0 -3
  161. data/ext/libxml/ruby_xml_cbg.c +0 -85
  162. data/lib/libxml/hpricot.rb +0 -78
  163. data/lib/libxml.rb +0 -5
  164. data/lib/xml/libxml.rb +0 -10
  165. data/lib/xml.rb +0 -14
  166. data/script/benchmark/depixelate +0 -634
  167. data/script/benchmark/hamlet.xml +0 -9055
  168. data/script/benchmark/parsecount +0 -170
  169. data/script/benchmark/sock_entries.xml +0 -507
  170. data/script/benchmark/throughput +0 -41
  171. data/script/test +0 -6
  172. data/test/test_deprecated_require.rb +0 -12
@@ -1,337 +1,364 @@
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
- */
151
- static VALUE rxml_html_parser_context_file(VALUE klass, VALUE file)
152
- {
153
- htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
154
- if (!ctxt)
155
- rxml_raise(&xmlLastError);
156
-
157
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
158
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
159
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
160
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
161
- htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
162
-
163
- return rxml_html_parser_context_wrap(ctxt);
164
- }
165
-
166
- /* call-seq:
167
- * XML::HTMLParser::Context.io(io) -> XML::HTMLParser::Context
168
- *
169
- * Creates a new parser context based on the specified io object.
170
- *
171
- * Parameters:
172
- *
173
- * io - A ruby IO object.
174
- */
175
- static VALUE rxml_html_parser_context_io(VALUE klass, VALUE io)
176
- {
177
- VALUE result;
178
- htmlParserCtxtPtr ctxt;
179
- xmlParserInputBufferPtr input;
180
- xmlParserInputPtr stream;
181
-
182
- if (NIL_P(io))
183
- rb_raise(rb_eTypeError, "Must pass in an IO object");
184
-
185
- input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
186
- (void*)io, XML_CHAR_ENCODING_NONE);
187
-
188
- ctxt = htmlNewParserCtxt();
189
- if (!ctxt)
190
- {
191
- xmlFreeParserInputBuffer(input);
192
- rxml_raise(&xmlLastError);
193
- }
194
-
195
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
196
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
197
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
198
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
199
- htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
200
-
201
- stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
202
-
203
- if (!stream)
204
- {
205
- xmlFreeParserInputBuffer(input);
206
- xmlFreeParserCtxt(ctxt);
207
- rxml_raise(&xmlLastError);
208
- }
209
- inputPush(ctxt, stream);
210
- result = rxml_html_parser_context_wrap(ctxt);
211
-
212
- /* Attach io object to parser so it won't get freed.*/
213
- rb_ivar_set(result, IO_ATTR, io);
214
-
215
- return result;
216
- }
217
-
218
- /* call-seq:
219
- * XML::HTMLParser::Context.string(string) -> XML::HTMLParser::Context
220
- *
221
- * Creates a new parser context based on the specified string.
222
- *
223
- * Parameters:
224
- *
225
- * string - A string that contains the data to parse.
226
- */
227
- static VALUE rxml_html_parser_context_string(VALUE klass, VALUE string)
228
- {
229
- htmlParserCtxtPtr ctxt;
230
- Check_Type(string, T_STRING);
231
-
232
- if (RSTRING_LEN(string) == 0)
233
- rb_raise(rb_eArgError, "Must specify a string with one or more characters");
234
-
235
- ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
236
- (int)RSTRING_LEN(string));
237
- if (!ctxt)
238
- rxml_raise(&xmlLastError);
239
-
240
- /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
241
- xmlCtxtUseOptionsInternal (called below) initialize slightly different
242
- context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
243
- sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
244
- htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
245
-
246
- if (ctxt->sax != NULL)
247
- memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
248
-
249
- return rxml_html_parser_context_wrap(ctxt);
250
- }
251
-
252
- /*
253
- * call-seq:
254
- * context.close -> nil
255
- *
256
- * Closes the underlying input streams. This is useful when parsing a large amount of
257
- * files and you want to close the files without relying on Ruby's garbage collector
258
- * to run.
259
- */
260
- static VALUE rxml_html_parser_context_close(VALUE self)
261
- {
262
- htmlParserCtxtPtr ctxt;
263
- xmlParserInputPtr xinput;
264
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
265
-
266
- while ((xinput = inputPop(ctxt)) != NULL)
267
- {
268
- xmlFreeInputStream(xinput);
269
- }
270
- return Qnil;
271
- }
272
-
273
- /*
274
- * call-seq:
275
- * context.disable_cdata = (true|false)
276
- *
277
- * Control whether the CDATA nodes will be created in this context.
278
- */
279
- static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE value)
280
- {
281
- htmlParserCtxtPtr ctxt;
282
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
283
-
284
- if (ctxt->sax == NULL)
285
- rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
286
-
287
- /* LibXML controls this internally with the default SAX handler. */
288
- if (value)
289
- ctxt->sax->cdataBlock = NULL;
290
- else
291
- ctxt->sax->cdataBlock = htmlDefaultSAXHandler.cdataBlock;
292
-
293
- return value;
294
- }
295
-
296
- /*
297
- * call-seq:
298
- * context.options = XML::Parser::Options::NOENT |
299
- XML::Parser::Options::NOCDATA
300
- *
301
- * Provides control over the execution of a parser. Valid values
302
- * are the constants defined on XML::Parser::Options. Multiple
303
- * options can be combined by using Bitwise OR (|).
304
- */
305
- static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
306
- {
307
- int xml_options = NUM2INT(options);
308
- htmlParserCtxtPtr ctxt;
309
- Check_Type(options, T_FIXNUM);
310
-
311
- Data_Get_Struct(self, htmlParserCtxt, ctxt);
312
- htmlCtxtUseOptions(ctxt, xml_options);
313
-
314
- #if LIBXML_VERSION >= 20707
315
- /* Big hack here, but htmlCtxtUseOptions doens't support HTML_PARSE_NOIMPLIED.
316
- So do it ourselves. There must be a better way??? */
317
- if (xml_options & HTML_PARSE_NOIMPLIED)
318
- {
319
- ctxt->options |= HTML_PARSE_NOIMPLIED;
320
- }
321
- #endif
322
-
323
- return self;
324
- }
325
-
326
- void rxml_init_html_parser_context(void)
327
- {
328
- IO_ATTR = ID2SYM(rb_intern("@io"));
329
- cXMLHtmlParserContext = rb_define_class_under(cXMLHtmlParser, "Context", cXMLParserContext);
330
-
331
- rb_define_singleton_method(cXMLHtmlParserContext, "file", rxml_html_parser_context_file, 1);
332
- rb_define_singleton_method(cXMLHtmlParserContext, "io", rxml_html_parser_context_io, 1);
333
- rb_define_singleton_method(cXMLHtmlParserContext, "string", rxml_html_parser_context_string, 1);
334
- rb_define_method(cXMLHtmlParserContext, "close", rxml_html_parser_context_close, 0);
335
- rb_define_method(cXMLHtmlParserContext, "disable_cdata=", rxml_html_parser_context_disable_cdata_set, 1);
336
- rb_define_method(cXMLHtmlParserContext, "options=", rxml_html_parser_context_options_set, 1);
337
- }
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(void* data)
133
+ {
134
+ htmlParserCtxtPtr ctxt = (htmlParserCtxtPtr)data;
135
+ htmlFreeParserCtxt(ctxt);
136
+ }
137
+
138
+ const rb_data_type_t rxml_html_parser_context_type = {
139
+ "LibXML::XML::HTMLParser::Context",
140
+ {NULL, rxml_html_parser_context_free, NULL},
141
+ &rxml_parser_context_type, NULL, 0
142
+ };
143
+
144
+ static VALUE rxml_html_parser_context_wrap(htmlParserCtxtPtr ctxt)
145
+ {
146
+ return TypedData_Wrap_Struct(cXMLHtmlParserContext, &rxml_html_parser_context_type, ctxt);
147
+ }
148
+
149
+ /* call-seq:
150
+ * XML::HTMLParser::Context.file(file) -> XML::HTMLParser::Context
151
+ *
152
+ * Creates a new parser context based on the specified file or uri.
153
+ *
154
+ * Parameters:
155
+ *
156
+ * file - A filename or uri
157
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
158
+ */
159
+ static VALUE rxml_html_parser_context_file(int argc, VALUE* argv, VALUE klass)
160
+ {
161
+ VALUE file, options;
162
+ rb_scan_args(argc, argv, "11", &file, &options);
163
+
164
+ htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
165
+ if (!ctxt)
166
+ rxml_raise(xmlGetLastError());
167
+
168
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
169
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
170
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
171
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
172
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
173
+
174
+ return rxml_html_parser_context_wrap(ctxt);
175
+ }
176
+
177
+ /* call-seq:
178
+ * XML::HTMLParser::Context.io(io) -> XML::HTMLParser::Context
179
+ *
180
+ * Creates a new parser context based on the specified io object.
181
+ *
182
+ * Parameters:
183
+ *
184
+ * io - A ruby IO object
185
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
186
+ */
187
+ static VALUE rxml_html_parser_context_io(int argc, VALUE* argv, VALUE klass)
188
+ {
189
+ VALUE io, options;
190
+ rb_scan_args(argc, argv, "11", &io, &options);
191
+
192
+ VALUE result;
193
+ htmlParserCtxtPtr ctxt;
194
+ xmlParserInputBufferPtr input;
195
+ xmlParserInputPtr stream;
196
+
197
+ if (NIL_P(io))
198
+ rb_raise(rb_eTypeError, "Must pass in an IO object");
199
+
200
+ input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
201
+ (void*)io, XML_CHAR_ENCODING_NONE);
202
+
203
+ ctxt = htmlNewParserCtxt();
204
+ if (!ctxt)
205
+ {
206
+ xmlFreeParserInputBuffer(input);
207
+ rxml_raise(xmlGetLastError());
208
+ }
209
+
210
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
211
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
212
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
213
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
214
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
215
+
216
+ stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
217
+
218
+ if (!stream)
219
+ {
220
+ xmlFreeParserInputBuffer(input);
221
+ xmlFreeParserCtxt(ctxt);
222
+ rxml_raise(xmlGetLastError());
223
+ }
224
+ inputPush(ctxt, stream);
225
+ result = rxml_html_parser_context_wrap(ctxt);
226
+
227
+ /* Attach io object to parser so it won't get freed.*/
228
+ rb_ivar_set(result, IO_ATTR, io);
229
+
230
+ return result;
231
+ }
232
+
233
+ /* call-seq:
234
+ * XML::HTMLParser::Context.string(string) -> XML::HTMLParser::Context
235
+ *
236
+ * Creates a new parser context based on the specified string.
237
+ *
238
+ * Parameters:
239
+ *
240
+ * string - A string that contains the data to parse
241
+ * options - A or'ed together list of LibXML::XML::HTMLParser::Options values
242
+ */
243
+ static VALUE rxml_html_parser_context_string(int argc, VALUE* argv, VALUE klass)
244
+ {
245
+ VALUE string, options;
246
+ rb_scan_args(argc, argv, "11", &string, &options);
247
+
248
+ Check_Type(string, T_STRING);
249
+
250
+ if (RSTRING_LEN(string) == 0)
251
+ rb_raise(rb_eArgError, "Must specify a string with one or more characters");
252
+
253
+ htmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
254
+ (int)RSTRING_LEN(string));
255
+ if (!ctxt)
256
+ rxml_raise(xmlGetLastError());
257
+
258
+ /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
259
+ xmlCtxtUseOptionsInternal (called below) initialize slightly different
260
+ context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
261
+ sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
262
+ htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
263
+
264
+ // Setup sax handler
265
+ // TODO - there must be a better way? The sax handler is initialized for XML, but we want
266
+ // to use HTML
267
+ memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
268
+ xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
269
+
270
+ return rxml_html_parser_context_wrap(ctxt);
271
+ }
272
+
273
+ /*
274
+ * call-seq:
275
+ * context.close -> nil
276
+ *
277
+ * Closes the underlying input streams. This is useful when parsing a large amount of
278
+ * files and you want to close the files without relying on Ruby's garbage collector
279
+ * to run.
280
+ */
281
+ static VALUE rxml_html_parser_context_close(VALUE self)
282
+ {
283
+ htmlParserCtxtPtr ctxt;
284
+ xmlParserInputPtr xinput;
285
+ TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
286
+
287
+ while ((xinput = inputPop(ctxt)) != NULL)
288
+ {
289
+ xmlFreeInputStream(xinput);
290
+ }
291
+ return Qnil;
292
+ }
293
+
294
+ /*
295
+ * call-seq:
296
+ * context.disable_cdata = (true|false)
297
+ *
298
+ * Control whether the CDATA nodes will be created in this context.
299
+ */
300
+ static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE value)
301
+ {
302
+ htmlParserCtxtPtr ctxt;
303
+ TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
304
+
305
+ if (ctxt->sax == NULL)
306
+ rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
307
+
308
+ /* LibXML controls this internally with the default SAX handler. */
309
+ if (value)
310
+ ctxt->sax->cdataBlock = NULL;
311
+ else
312
+ ctxt->sax->cdataBlock = xmlSAX2CDataBlock;
313
+
314
+ return value;
315
+ }
316
+
317
+ /*
318
+ * call-seq:
319
+ * context.options = XML::Parser::Options::NOENT |
320
+ XML::Parser::Options::NOCDATA
321
+ *
322
+ * Provides control over the execution of a parser. Valid values
323
+ * are the constants defined on XML::Parser::Options. Multiple
324
+ * options can be combined by using Bitwise OR (|).
325
+ */
326
+ static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
327
+ {
328
+ int xml_options = NUM2INT(options);
329
+ htmlParserCtxtPtr ctxt;
330
+
331
+ TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
332
+ htmlCtxtUseOptions(ctxt, xml_options);
333
+
334
+ #if LIBXML_VERSION >= 20707
335
+ /* Big hack here, but htmlCtxtUseOptions doens't support HTML_PARSE_NOIMPLIED.
336
+ So do it ourselves. There must be a better way??? */
337
+ if (xml_options & HTML_PARSE_NOIMPLIED)
338
+ {
339
+ ctxt->options |= HTML_PARSE_NOIMPLIED;
340
+ }
341
+ #endif
342
+
343
+ return self;
344
+ }
345
+
346
+ static VALUE rxml_html_parser_context_alloc(VALUE klass)
347
+ {
348
+ xmlParserCtxtPtr ctxt = htmlNewParserCtxt();
349
+ return TypedData_Wrap_Struct(klass, &rxml_html_parser_context_type, ctxt);
350
+ }
351
+
352
+ void rxml_init_html_parser_context(void)
353
+ {
354
+ IO_ATTR = ID2SYM(rb_intern("@io"));
355
+ cXMLHtmlParserContext = rb_define_class_under(cXMLHtmlParser, "Context", cXMLParserContext);
356
+ rb_define_alloc_func(cXMLHtmlParserContext, rxml_html_parser_context_alloc);
357
+
358
+ rb_define_singleton_method(cXMLHtmlParserContext, "file", rxml_html_parser_context_file, -1);
359
+ rb_define_singleton_method(cXMLHtmlParserContext, "io", rxml_html_parser_context_io, -1);
360
+ rb_define_singleton_method(cXMLHtmlParserContext, "string", rxml_html_parser_context_string, -1);
361
+ rb_define_method(cXMLHtmlParserContext, "close", rxml_html_parser_context_close, 0);
362
+ rb_define_method(cXMLHtmlParserContext, "disable_cdata=", rxml_html_parser_context_disable_cdata_set, 1);
363
+ rb_define_method(cXMLHtmlParserContext, "options=", rxml_html_parser_context_options_set, 1);
364
+ }