nokogiri 1.11.1 → 1.12.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +232 -11
  3. data/LICENSE.md +1 -1
  4. data/README.md +27 -21
  5. data/dependencies.yml +12 -12
  6. data/ext/nokogiri/depend +35 -474
  7. data/ext/nokogiri/extconf.rb +391 -243
  8. data/ext/nokogiri/gumbo.c +611 -0
  9. data/ext/nokogiri/{html_document.c → html4_document.c} +18 -23
  10. data/ext/nokogiri/html4_element_description.c +294 -0
  11. data/ext/nokogiri/html4_entity_lookup.c +37 -0
  12. data/ext/nokogiri/html4_sax_parser_context.c +119 -0
  13. data/ext/nokogiri/{html_sax_push_parser.c → html4_sax_push_parser.c} +29 -27
  14. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  15. data/ext/nokogiri/nokogiri.c +206 -66
  16. data/ext/nokogiri/nokogiri.h +166 -76
  17. data/ext/nokogiri/test_global_handlers.c +3 -4
  18. data/ext/nokogiri/xml_attr.c +15 -15
  19. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  20. data/ext/nokogiri/xml_cdata.c +13 -18
  21. data/ext/nokogiri/xml_comment.c +19 -26
  22. data/ext/nokogiri/xml_document.c +258 -200
  23. data/ext/nokogiri/xml_document_fragment.c +13 -15
  24. data/ext/nokogiri/xml_dtd.c +54 -48
  25. data/ext/nokogiri/xml_element_content.c +31 -26
  26. data/ext/nokogiri/xml_element_decl.c +22 -22
  27. data/ext/nokogiri/xml_encoding_handler.c +28 -17
  28. data/ext/nokogiri/xml_entity_decl.c +32 -30
  29. data/ext/nokogiri/xml_entity_reference.c +16 -18
  30. data/ext/nokogiri/xml_namespace.c +58 -49
  31. data/ext/nokogiri/xml_node.c +473 -414
  32. data/ext/nokogiri/xml_node_set.c +174 -162
  33. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  34. data/ext/nokogiri/xml_reader.c +193 -157
  35. data/ext/nokogiri/xml_relax_ng.c +29 -23
  36. data/ext/nokogiri/xml_sax_parser.c +111 -106
  37. data/ext/nokogiri/xml_sax_parser_context.c +102 -85
  38. data/ext/nokogiri/xml_sax_push_parser.c +34 -27
  39. data/ext/nokogiri/xml_schema.c +49 -41
  40. data/ext/nokogiri/xml_syntax_error.c +21 -23
  41. data/ext/nokogiri/xml_text.c +13 -17
  42. data/ext/nokogiri/xml_xpath_context.c +86 -77
  43. data/ext/nokogiri/xslt_stylesheet.c +157 -156
  44. data/gumbo-parser/CHANGES.md +63 -0
  45. data/gumbo-parser/Makefile +101 -0
  46. data/gumbo-parser/THANKS +27 -0
  47. data/gumbo-parser/src/Makefile +17 -0
  48. data/gumbo-parser/src/README.md +41 -0
  49. data/gumbo-parser/src/ascii.c +75 -0
  50. data/gumbo-parser/src/ascii.h +115 -0
  51. data/gumbo-parser/src/attribute.c +42 -0
  52. data/gumbo-parser/src/attribute.h +17 -0
  53. data/gumbo-parser/src/char_ref.c +22225 -0
  54. data/gumbo-parser/src/char_ref.h +29 -0
  55. data/gumbo-parser/src/char_ref.rl +2154 -0
  56. data/gumbo-parser/src/error.c +626 -0
  57. data/gumbo-parser/src/error.h +148 -0
  58. data/gumbo-parser/src/foreign_attrs.c +104 -0
  59. data/gumbo-parser/src/foreign_attrs.gperf +27 -0
  60. data/gumbo-parser/src/gumbo.h +943 -0
  61. data/gumbo-parser/src/insertion_mode.h +33 -0
  62. data/gumbo-parser/src/macros.h +91 -0
  63. data/gumbo-parser/src/parser.c +4886 -0
  64. data/gumbo-parser/src/parser.h +41 -0
  65. data/gumbo-parser/src/replacement.h +33 -0
  66. data/gumbo-parser/src/string_buffer.c +103 -0
  67. data/gumbo-parser/src/string_buffer.h +68 -0
  68. data/gumbo-parser/src/string_piece.c +48 -0
  69. data/gumbo-parser/src/svg_attrs.c +174 -0
  70. data/gumbo-parser/src/svg_attrs.gperf +77 -0
  71. data/gumbo-parser/src/svg_tags.c +137 -0
  72. data/gumbo-parser/src/svg_tags.gperf +55 -0
  73. data/gumbo-parser/src/tag.c +222 -0
  74. data/gumbo-parser/src/tag_lookup.c +382 -0
  75. data/gumbo-parser/src/tag_lookup.gperf +169 -0
  76. data/gumbo-parser/src/tag_lookup.h +13 -0
  77. data/gumbo-parser/src/token_buffer.c +79 -0
  78. data/gumbo-parser/src/token_buffer.h +71 -0
  79. data/gumbo-parser/src/token_type.h +17 -0
  80. data/gumbo-parser/src/tokenizer.c +3463 -0
  81. data/gumbo-parser/src/tokenizer.h +112 -0
  82. data/gumbo-parser/src/tokenizer_states.h +339 -0
  83. data/gumbo-parser/src/utf8.c +245 -0
  84. data/gumbo-parser/src/utf8.h +164 -0
  85. data/gumbo-parser/src/util.c +68 -0
  86. data/gumbo-parser/src/util.h +30 -0
  87. data/gumbo-parser/src/vector.c +111 -0
  88. data/gumbo-parser/src/vector.h +45 -0
  89. data/lib/nokogiri.rb +31 -50
  90. data/lib/nokogiri/css.rb +14 -14
  91. data/lib/nokogiri/css/parser.rb +2 -2
  92. data/lib/nokogiri/css/parser.y +1 -1
  93. data/lib/nokogiri/css/syntax_error.rb +1 -1
  94. data/lib/nokogiri/extension.rb +26 -0
  95. data/lib/nokogiri/gumbo.rb +14 -0
  96. data/lib/nokogiri/html.rb +31 -27
  97. data/lib/nokogiri/html4.rb +40 -0
  98. data/lib/nokogiri/{html → html4}/builder.rb +2 -2
  99. data/lib/nokogiri/{html → html4}/document.rb +4 -4
  100. data/lib/nokogiri/{html → html4}/document_fragment.rb +17 -17
  101. data/lib/nokogiri/{html → html4}/element_description.rb +1 -1
  102. data/lib/nokogiri/{html → html4}/element_description_defaults.rb +1 -1
  103. data/lib/nokogiri/{html → html4}/entity_lookup.rb +1 -1
  104. data/lib/nokogiri/{html → html4}/sax/parser.rb +11 -14
  105. data/lib/nokogiri/html4/sax/parser_context.rb +19 -0
  106. data/lib/nokogiri/{html → html4}/sax/push_parser.rb +5 -5
  107. data/lib/nokogiri/html5.rb +473 -0
  108. data/lib/nokogiri/html5/document.rb +74 -0
  109. data/lib/nokogiri/html5/document_fragment.rb +80 -0
  110. data/lib/nokogiri/html5/node.rb +93 -0
  111. data/lib/nokogiri/version/constant.rb +1 -1
  112. data/lib/nokogiri/version/info.rb +42 -9
  113. data/lib/nokogiri/xml.rb +35 -36
  114. data/lib/nokogiri/xml/document.rb +74 -28
  115. data/lib/nokogiri/xml/node.rb +45 -47
  116. data/lib/nokogiri/xml/parse_options.rb +2 -0
  117. data/lib/nokogiri/xml/pp.rb +2 -2
  118. data/lib/nokogiri/xml/reader.rb +2 -9
  119. data/lib/nokogiri/xml/sax.rb +4 -4
  120. data/lib/nokogiri/xml/sax/document.rb +24 -30
  121. data/lib/nokogiri/xml/xpath.rb +3 -5
  122. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  123. data/lib/nokogiri/xslt.rb +16 -16
  124. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  125. data/patches/libxml2/{0002-Remove-script-macro-support.patch → 0001-Remove-script-macro-support.patch} +0 -0
  126. data/patches/libxml2/{0003-Update-entities-to-remove-handling-of-ssi.patch → 0002-Update-entities-to-remove-handling-of-ssi.patch} +0 -0
  127. data/patches/libxml2/{0004-libxml2.la-is-in-top_builddir.patch → 0003-libxml2.la-is-in-top_builddir.patch} +1 -1
  128. data/patches/libxml2/{0008-use-glibc-strlen.patch → 0004-use-glibc-strlen.patch} +0 -0
  129. data/patches/libxml2/{0009-avoid-isnan-isinf.patch → 0005-avoid-isnan-isinf.patch} +4 -4
  130. data/patches/libxml2/0006-update-automake-files-for-arm64.patch +2511 -0
  131. data/patches/libxml2/0007-Fix-XPath-recursion-limit.patch +31 -0
  132. data/patches/libxslt/0001-update-automake-files-for-arm64.patch +2511 -0
  133. data/patches/libxslt/0002-Fix-xml2-config-check-in-configure-script.patch +19 -0
  134. data/ports/archives/libxml2-2.9.12.tar.gz +0 -0
  135. metadata +117 -109
  136. data/ext/nokogiri/html_document.h +0 -10
  137. data/ext/nokogiri/html_element_description.c +0 -279
  138. data/ext/nokogiri/html_element_description.h +0 -10
  139. data/ext/nokogiri/html_entity_lookup.c +0 -32
  140. data/ext/nokogiri/html_entity_lookup.h +0 -8
  141. data/ext/nokogiri/html_sax_parser_context.c +0 -118
  142. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  143. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  144. data/ext/nokogiri/xml_attr.h +0 -9
  145. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  146. data/ext/nokogiri/xml_cdata.h +0 -9
  147. data/ext/nokogiri/xml_comment.h +0 -9
  148. data/ext/nokogiri/xml_document.h +0 -23
  149. data/ext/nokogiri/xml_document_fragment.h +0 -10
  150. data/ext/nokogiri/xml_dtd.h +0 -10
  151. data/ext/nokogiri/xml_element_content.h +0 -10
  152. data/ext/nokogiri/xml_element_decl.h +0 -9
  153. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  154. data/ext/nokogiri/xml_entity_decl.h +0 -10
  155. data/ext/nokogiri/xml_entity_reference.h +0 -9
  156. data/ext/nokogiri/xml_io.c +0 -63
  157. data/ext/nokogiri/xml_io.h +0 -11
  158. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  159. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  160. data/ext/nokogiri/xml_namespace.h +0 -14
  161. data/ext/nokogiri/xml_node.h +0 -13
  162. data/ext/nokogiri/xml_node_set.h +0 -12
  163. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  164. data/ext/nokogiri/xml_reader.h +0 -10
  165. data/ext/nokogiri/xml_relax_ng.h +0 -9
  166. data/ext/nokogiri/xml_sax_parser.h +0 -39
  167. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  168. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  169. data/ext/nokogiri/xml_schema.h +0 -9
  170. data/ext/nokogiri/xml_syntax_error.h +0 -25
  171. data/ext/nokogiri/xml_text.h +0 -9
  172. data/ext/nokogiri/xml_xpath_context.h +0 -10
  173. data/ext/nokogiri/xslt_stylesheet.h +0 -14
  174. data/lib/nokogiri/html/sax/parser_context.rb +0 -17
  175. data/patches/libxml2/0001-Revert-Do-not-URI-escape-in-server-side-includes.patch +0 -78
  176. data/patches/libxml2/0005-Fix-infinite-loop-in-xmlStringLenDecodeEntities.patch +0 -32
  177. data/patches/libxml2/0006-htmlParseComment-treat-as-if-it-closed-the-comment.patch +0 -73
  178. data/patches/libxml2/0007-use-new-htmlParseLookupCommentEnd-to-find-comment-en.patch +0 -103
  179. data/ports/archives/libxml2-2.9.10.tar.gz +0 -0
@@ -1,4 +1,6 @@
1
- #include <html_sax_push_parser.h>
1
+ #include <nokogiri.h>
2
+
3
+ VALUE cNokogiriHtml4SaxPushParser;
2
4
 
3
5
  /*
4
6
  * call-seq:
@@ -6,17 +8,18 @@
6
8
  *
7
9
  * Write +chunk+ to PushParser. +last_chunk+ triggers the end_document handle
8
10
  */
9
- static VALUE native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)
11
+ static VALUE
12
+ native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)
10
13
  {
11
14
  xmlParserCtxtPtr ctx;
12
- const char * chunk = NULL;
15
+ const char *chunk = NULL;
13
16
  int size = 0;
14
17
  int status = 0;
15
18
  libxmlStructuredErrorHandlerState handler_state;
16
19
 
17
20
  Data_Get_Struct(self, xmlParserCtxt, ctx);
18
21
 
19
- if(Qnil != _chunk) {
22
+ if (Qnil != _chunk) {
20
23
  chunk = StringValuePtr(_chunk);
21
24
  size = (int)RSTRING_LEN(_chunk);
22
25
  }
@@ -42,34 +45,37 @@ static VALUE native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)
42
45
  *
43
46
  * Initialize the push parser with +xml_sax+ using +filename+
44
47
  */
45
- static VALUE initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename,
46
- VALUE encoding)
48
+ static VALUE
49
+ initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename,
50
+ VALUE encoding)
47
51
  {
48
52
  htmlSAXHandlerPtr sax;
49
- const char * filename = NULL;
53
+ const char *filename = NULL;
50
54
  htmlParserCtxtPtr ctx;
51
55
  xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
52
56
 
53
57
  Data_Get_Struct(_xml_sax, xmlSAXHandler, sax);
54
58
 
55
- if(_filename != Qnil) filename = StringValueCStr(_filename);
59
+ if (_filename != Qnil) { filename = StringValueCStr(_filename); }
56
60
 
57
61
  if (!NIL_P(encoding)) {
58
62
  enc = xmlParseCharEncoding(StringValueCStr(encoding));
59
- if (enc == XML_CHAR_ENCODING_ERROR)
63
+ if (enc == XML_CHAR_ENCODING_ERROR) {
60
64
  rb_raise(rb_eArgError, "Unsupported Encoding");
65
+ }
61
66
  }
62
67
 
63
68
  ctx = htmlCreatePushParserCtxt(
64
- sax,
65
- NULL,
66
- NULL,
67
- 0,
68
- filename,
69
- enc
70
- );
71
- if(ctx == NULL)
69
+ sax,
70
+ NULL,
71
+ NULL,
72
+ 0,
73
+ filename,
74
+ enc
75
+ );
76
+ if (ctx == NULL) {
72
77
  rb_raise(rb_eRuntimeError, "Could not create a parser context");
78
+ }
73
79
 
74
80
  ctx->userData = NOKOGIRI_SAX_TUPLE_NEW(ctx, self);
75
81
 
@@ -78,16 +84,12 @@ static VALUE initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename,
78
84
  return self;
79
85
  }
80
86
 
81
- VALUE cNokogiriHtmlSaxPushParser;
82
- void init_html_sax_push_parser()
87
+ void
88
+ noko_init_html_sax_push_parser()
83
89
  {
84
- VALUE nokogiri = rb_define_module("Nokogiri");
85
- VALUE html = rb_define_module_under(nokogiri, "HTML");
86
- VALUE sax = rb_define_module_under(html, "SAX");
87
- VALUE klass = rb_define_class_under(sax, "PushParser", cNokogiriXmlSaxPushParser);
88
-
89
- cNokogiriHtmlSaxPushParser = klass;
90
+ assert(cNokogiriXmlSaxPushParser);
91
+ cNokogiriHtml4SaxPushParser = rb_define_class_under(mNokogiriHtml4Sax, "PushParser", cNokogiriXmlSaxPushParser);
90
92
 
91
- rb_define_private_method(klass, "initialize_native", initialize_native, 3);
92
- rb_define_private_method(klass, "native_write", native_write, 2);
93
+ rb_define_private_method(cNokogiriHtml4SaxPushParser, "initialize_native", initialize_native, 3);
94
+ rb_define_private_method(cNokogiriHtml4SaxPushParser, "native_write", native_write, 2);
93
95
  }
@@ -0,0 +1,121 @@
1
+ #ifndef HAVE_XMLFIRSTELEMENTCHILD
2
+ #include <nokogiri.h>
3
+ /**
4
+ * xmlFirstElementChild:
5
+ * @parent: the parent node
6
+ *
7
+ * Finds the first child node of that element which is a Element node
8
+ * Note the handling of entities references is different than in
9
+ * the W3C DOM element traversal spec since we don't have back reference
10
+ * from entities content to entities references.
11
+ *
12
+ * Returns the first element child or NULL if not available
13
+ */
14
+ xmlNodePtr
15
+ xmlFirstElementChild(xmlNodePtr parent)
16
+ {
17
+ xmlNodePtr cur = NULL;
18
+
19
+ if (parent == NULL) {
20
+ return (NULL);
21
+ }
22
+ switch (parent->type) {
23
+ case XML_ELEMENT_NODE:
24
+ case XML_ENTITY_NODE:
25
+ case XML_DOCUMENT_NODE:
26
+ case XML_HTML_DOCUMENT_NODE:
27
+ cur = parent->children;
28
+ break;
29
+ default:
30
+ return (NULL);
31
+ }
32
+ while (cur != NULL) {
33
+ if (cur->type == XML_ELEMENT_NODE) {
34
+ return (cur);
35
+ }
36
+ cur = cur->next;
37
+ }
38
+ return (NULL);
39
+ }
40
+
41
+ /**
42
+ * xmlNextElementSibling:
43
+ * @node: the current node
44
+ *
45
+ * Finds the first closest next sibling of the node which is an
46
+ * element node.
47
+ * Note the handling of entities references is different than in
48
+ * the W3C DOM element traversal spec since we don't have back reference
49
+ * from entities content to entities references.
50
+ *
51
+ * Returns the next element sibling or NULL if not available
52
+ */
53
+ xmlNodePtr
54
+ xmlNextElementSibling(xmlNodePtr node)
55
+ {
56
+ if (node == NULL) {
57
+ return (NULL);
58
+ }
59
+ switch (node->type) {
60
+ case XML_ELEMENT_NODE:
61
+ case XML_TEXT_NODE:
62
+ case XML_CDATA_SECTION_NODE:
63
+ case XML_ENTITY_REF_NODE:
64
+ case XML_ENTITY_NODE:
65
+ case XML_PI_NODE:
66
+ case XML_COMMENT_NODE:
67
+ case XML_DTD_NODE:
68
+ case XML_XINCLUDE_START:
69
+ case XML_XINCLUDE_END:
70
+ node = node->next;
71
+ break;
72
+ default:
73
+ return (NULL);
74
+ }
75
+ while (node != NULL) {
76
+ if (node->type == XML_ELEMENT_NODE) {
77
+ return (node);
78
+ }
79
+ node = node->next;
80
+ }
81
+ return (NULL);
82
+ }
83
+
84
+ /**
85
+ * xmlLastElementChild:
86
+ * @parent: the parent node
87
+ *
88
+ * Finds the last child node of that element which is a Element node
89
+ * Note the handling of entities references is different than in
90
+ * the W3C DOM element traversal spec since we don't have back reference
91
+ * from entities content to entities references.
92
+ *
93
+ * Returns the last element child or NULL if not available
94
+ */
95
+ xmlNodePtr
96
+ xmlLastElementChild(xmlNodePtr parent)
97
+ {
98
+ xmlNodePtr cur = NULL;
99
+
100
+ if (parent == NULL) {
101
+ return (NULL);
102
+ }
103
+ switch (parent->type) {
104
+ case XML_ELEMENT_NODE:
105
+ case XML_ENTITY_NODE:
106
+ case XML_DOCUMENT_NODE:
107
+ case XML_HTML_DOCUMENT_NODE:
108
+ cur = parent->last;
109
+ break;
110
+ default:
111
+ return (NULL);
112
+ }
113
+ while (cur != NULL) {
114
+ if (cur->type == XML_ELEMENT_NODE) {
115
+ return (cur);
116
+ }
117
+ cur = cur->prev;
118
+ }
119
+ return (NULL);
120
+ }
121
+ #endif
@@ -1,26 +1,70 @@
1
1
  #include <nokogiri.h>
2
2
 
3
- void init_test_global_handlers(); /* in lieu of test_global_handlers.h */
4
-
5
3
  VALUE mNokogiri ;
4
+ VALUE mNokogiriGumbo ;
5
+ VALUE mNokogiriHtml4 ;
6
+ VALUE mNokogiriHtml4Sax ;
7
+ VALUE mNokogiriHtml5 ;
6
8
  VALUE mNokogiriXml ;
7
- VALUE mNokogiriHtml ;
8
- VALUE mNokogiriXslt ;
9
9
  VALUE mNokogiriXmlSax ;
10
- VALUE mNokogiriHtmlSax ;
10
+ VALUE mNokogiriXmlXpath ;
11
+ VALUE mNokogiriXslt ;
12
+
13
+ VALUE cNokogiriSyntaxError;
14
+ VALUE cNokogiriXmlCharacterData;
15
+ VALUE cNokogiriXmlElement;
16
+ VALUE cNokogiriXmlXpathSyntaxError;
17
+
18
+ void noko_init_xml_attr(void);
19
+ void noko_init_xml_attribute_decl(void);
20
+ void noko_init_xml_cdata(void);
21
+ void noko_init_xml_comment(void);
22
+ void noko_init_xml_document(void);
23
+ void noko_init_xml_document_fragment(void);
24
+ void noko_init_xml_dtd(void);
25
+ void noko_init_xml_element_content(void);
26
+ void noko_init_xml_element_decl(void);
27
+ void noko_init_xml_encoding_handler(void);
28
+ void noko_init_xml_entity_decl(void);
29
+ void noko_init_xml_entity_reference(void);
30
+ void noko_init_xml_namespace(void);
31
+ void noko_init_xml_node(void);
32
+ void noko_init_xml_node_set(void);
33
+ void noko_init_xml_processing_instruction(void);
34
+ void noko_init_xml_reader(void);
35
+ void noko_init_xml_relax_ng(void);
36
+ void noko_init_xml_sax_parser(void);
37
+ void noko_init_xml_sax_parser_context(void);
38
+ void noko_init_xml_sax_push_parser(void);
39
+ void noko_init_xml_schema(void);
40
+ void noko_init_xml_syntax_error(void);
41
+ void noko_init_xml_text(void);
42
+ void noko_init_xml_xpath_context(void);
43
+ void noko_init_xslt_stylesheet(void);
44
+ void noko_init_html_document(void);
45
+ void noko_init_html_element_description(void);
46
+ void noko_init_html_entity_lookup(void);
47
+ void noko_init_html_sax_parser_context(void);
48
+ void noko_init_html_sax_push_parser(void);
49
+ void noko_init_gumbo(void);
50
+ void noko_init_test_global_handlers(void);
51
+
52
+ static ID id_read, id_write;
53
+
11
54
 
12
55
  #ifndef HAVE_VASPRINTF
13
56
  /*
14
57
  * Thank you Geoffroy Couprie for this implementation of vasprintf!
15
58
  */
16
- int vasprintf(char **strp, const char *fmt, va_list ap)
59
+ int
60
+ vasprintf(char **strp, const char *fmt, va_list ap)
17
61
  {
18
62
  /* Mingw32/64 have a broken vsnprintf implementation that fails when
19
63
  * using a zero-byte limit in order to retrieve the required size for malloc.
20
64
  * So we use a one byte buffer instead.
21
65
  */
22
66
  char tmp[1];
23
- int len = vsnprintf (tmp, 1, fmt, ap) + 1;
67
+ int len = vsnprintf(tmp, 1, fmt, ap) + 1;
24
68
  char *res = (char *)malloc((unsigned int)len);
25
69
  if (res == NULL) {
26
70
  return -1;
@@ -30,43 +74,95 @@ int vasprintf(char **strp, const char *fmt, va_list ap)
30
74
  }
31
75
  #endif
32
76
 
33
- #include "ruby/util.h"
34
77
 
35
- void nokogiri_root_node(xmlNodePtr node)
78
+ static VALUE
79
+ read_check(VALUE val)
36
80
  {
37
- xmlDocPtr doc;
38
- nokogiriTuplePtr tuple;
81
+ VALUE *args = (VALUE *)val;
82
+ return rb_funcall(args[0], id_read, 1, args[1]);
83
+ }
84
+
39
85
 
40
- doc = node->doc;
41
- if (doc->type == XML_DOCUMENT_FRAG_NODE) { doc = doc->doc; }
42
- tuple = (nokogiriTuplePtr)doc->_private;
43
- st_insert(tuple->unlinkedNodes, (st_data_t)node, (st_data_t)node);
86
+ static VALUE
87
+ read_failed(VALUE arg, VALUE exc)
88
+ {
89
+ return Qundef;
44
90
  }
45
91
 
46
- void nokogiri_root_nsdef(xmlNsPtr ns, xmlDocPtr doc)
92
+
93
+ int
94
+ noko_io_read(void *ctx, char *buffer, int len)
47
95
  {
48
- nokogiriTuplePtr tuple;
96
+ VALUE string, args[2];
97
+ size_t str_len, safe_len;
98
+
99
+ args[0] = (VALUE)ctx;
100
+ args[1] = INT2NUM(len);
101
+
102
+ string = rb_rescue(read_check, (VALUE)args, read_failed, 0);
103
+
104
+ if (NIL_P(string)) { return 0; }
105
+ if (string == Qundef) { return -1; }
106
+ if (TYPE(string) != T_STRING) { return -1; }
49
107
 
50
- if (doc->type == XML_DOCUMENT_FRAG_NODE) { doc = doc->doc; }
51
- tuple = (nokogiriTuplePtr)doc->_private;
52
- st_insert(tuple->unlinkedNodes, (st_data_t)ns, (st_data_t)ns);
108
+ str_len = (size_t)RSTRING_LEN(string);
109
+ safe_len = str_len > (size_t)len ? (size_t)len : str_len;
110
+ memcpy(buffer, StringValuePtr(string), safe_len);
111
+
112
+ return (int)safe_len;
53
113
  }
54
114
 
55
- void Init_nokogiri()
115
+
116
+ static VALUE
117
+ write_check(VALUE val)
56
118
  {
57
- xmlMemSetup(
58
- (xmlFreeFunc)ruby_xfree,
59
- (xmlMallocFunc)ruby_xmalloc,
60
- (xmlReallocFunc)ruby_xrealloc,
61
- ruby_strdup
62
- );
119
+ VALUE *args = (VALUE *)val;
120
+ return rb_funcall(args[0], id_write, 1, args[1]);
121
+ }
122
+
123
+
124
+ static VALUE
125
+ write_failed(VALUE arg, VALUE exc)
126
+ {
127
+ return Qundef;
128
+ }
129
+
130
+
131
+ int
132
+ noko_io_write(void *ctx, char *buffer, int len)
133
+ {
134
+ VALUE args[2], size;
135
+
136
+ args[0] = (VALUE)ctx;
137
+ args[1] = rb_str_new(buffer, (long)len);
138
+
139
+ size = rb_rescue(write_check, (VALUE)args, write_failed, 0);
140
+
141
+ if (size == Qundef) { return -1; }
63
142
 
143
+ return NUM2INT(size);
144
+ }
145
+
146
+
147
+ int
148
+ noko_io_close(void *ctx)
149
+ {
150
+ return 0;
151
+ }
152
+
153
+
154
+ void
155
+ Init_nokogiri()
156
+ {
64
157
  mNokogiri = rb_define_module("Nokogiri");
158
+ mNokogiriGumbo = rb_define_module_under(mNokogiri, "Gumbo");
159
+ mNokogiriHtml4 = rb_define_module_under(mNokogiri, "HTML4");
160
+ mNokogiriHtml4Sax = rb_define_module_under(mNokogiriHtml4, "SAX");
161
+ mNokogiriHtml5 = rb_define_module_under(mNokogiri, "HTML5");
65
162
  mNokogiriXml = rb_define_module_under(mNokogiri, "XML");
66
- mNokogiriHtml = rb_define_module_under(mNokogiri, "HTML");
67
- mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT");
68
163
  mNokogiriXmlSax = rb_define_module_under(mNokogiriXml, "SAX");
69
- mNokogiriHtmlSax = rb_define_module_under(mNokogiriHtml, "SAX");
164
+ mNokogiriXmlXpath = rb_define_module_under(mNokogiriXml, "XPath");
165
+ mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT");
70
166
 
71
167
  rb_const_set(mNokogiri, rb_intern("LIBXML_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXML_DOTTED_VERSION));
72
168
  rb_const_set(mNokogiri, rb_intern("LIBXML_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xmlParserVersion));
@@ -76,11 +172,11 @@ void Init_nokogiri()
76
172
 
77
173
  #ifdef NOKOGIRI_PACKAGED_LIBRARIES
78
174
  rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qtrue);
79
- #ifdef NOKOGIRI_PRECOMPILED_LIBRARIES
175
+ # ifdef NOKOGIRI_PRECOMPILED_LIBRARIES
80
176
  rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qtrue);
81
- #else
177
+ # else
82
178
  rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qfalse);
83
- #endif
179
+ # endif
84
180
  rb_const_set(mNokogiri, rb_intern("LIBXML2_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATCHES), " "));
85
181
  rb_const_set(mNokogiri, rb_intern("LIBXSLT_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATCHES), " "));
86
182
  #else
@@ -100,39 +196,83 @@ void Init_nokogiri()
100
196
  rb_const_set(mNokogiri, rb_intern("OTHER_LIBRARY_VERSIONS"), NOKOGIRI_STR_NEW2(NOKOGIRI_OTHER_LIBRARY_VERSIONS));
101
197
  #endif
102
198
 
199
+ #if defined(_WIN32) && !defined(NOKOGIRI_PACKAGED_LIBRARIES)
200
+ /*
201
+ * We choose *not* to do use Ruby's memory management functions with windows DLLs because of this
202
+ * issue in libxml 2.9.12:
203
+ *
204
+ * https://github.com/sparklemotion/nokogiri/issues/2241
205
+ *
206
+ * If the atexit() issue gets fixed in a future version of libxml2, then we may be able to skip
207
+ * this config only for the specific libxml2 versions 2.9.12.
208
+ *
209
+ * Alternatively, now that Ruby has a generational GC, it might be OK to let libxml2 use its
210
+ * default memory management functions (recall that this config was introduced to reduce memory
211
+ * bloat and allow Ruby to GC more often); but we should *really* test with production workloads
212
+ * before making that kind of a potentially-invasive change.
213
+ */
214
+ rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("default"));
215
+ #else
216
+ rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("ruby"));
217
+ xmlMemSetup((xmlFreeFunc)ruby_xfree, (xmlMallocFunc)ruby_xmalloc, (xmlReallocFunc)ruby_xrealloc, ruby_strdup);
218
+ #endif
219
+
103
220
  xmlInitParser();
221
+ exsltRegisterAll();
222
+
223
+ if (xsltExtModuleFunctionLookup((xmlChar*)"date-time", EXSLT_DATE_NAMESPACE)) {
224
+ rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qtrue);
225
+ } else {
226
+ rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qfalse);
227
+ }
228
+
229
+ cNokogiriSyntaxError = rb_define_class_under(mNokogiri, "SyntaxError", rb_eStandardError);
230
+ noko_init_xml_syntax_error();
231
+ assert(cNokogiriXmlSyntaxError);
232
+ cNokogiriXmlXpathSyntaxError = rb_define_class_under(mNokogiriXmlXpath, "SyntaxError", cNokogiriXmlSyntaxError);
233
+
234
+ noko_init_xml_element_content();
235
+ noko_init_xml_encoding_handler();
236
+ noko_init_xml_namespace();
237
+ noko_init_xml_node_set();
238
+ noko_init_xml_reader();
239
+ noko_init_xml_sax_parser();
240
+ noko_init_xml_xpath_context();
241
+ noko_init_xslt_stylesheet();
242
+ noko_init_html_element_description();
243
+ noko_init_html_entity_lookup();
244
+
245
+ noko_init_xml_schema();
246
+ noko_init_xml_relax_ng();
247
+
248
+ noko_init_xml_sax_parser_context();
249
+ noko_init_html_sax_parser_context();
250
+
251
+ noko_init_xml_sax_push_parser();
252
+ noko_init_html_sax_push_parser();
253
+
254
+ noko_init_xml_node();
255
+ noko_init_xml_attr();
256
+ noko_init_xml_attribute_decl();
257
+ noko_init_xml_dtd();
258
+ noko_init_xml_element_decl();
259
+ noko_init_xml_entity_decl();
260
+ noko_init_xml_entity_reference();
261
+ noko_init_xml_processing_instruction();
262
+ assert(cNokogiriXmlNode);
263
+ cNokogiriXmlElement = rb_define_class_under(mNokogiriXml, "Element", cNokogiriXmlNode);
264
+ cNokogiriXmlCharacterData = rb_define_class_under(mNokogiriXml, "CharacterData", cNokogiriXmlNode);
265
+ noko_init_xml_comment();
266
+ noko_init_xml_text();
267
+ noko_init_xml_cdata();
268
+
269
+ noko_init_xml_document_fragment();
270
+ noko_init_xml_document();
271
+ noko_init_html_document();
272
+ noko_init_gumbo();
273
+
274
+ noko_init_test_global_handlers();
104
275
 
105
- init_xml_document();
106
- init_html_document();
107
- init_xml_node();
108
- init_xml_document_fragment();
109
- init_xml_text();
110
- init_xml_cdata();
111
- init_xml_processing_instruction();
112
- init_xml_attr();
113
- init_xml_entity_reference();
114
- init_xml_comment();
115
- init_xml_node_set();
116
- init_xml_xpath_context();
117
- init_xml_sax_parser_context();
118
- init_xml_sax_parser();
119
- init_xml_sax_push_parser();
120
- init_xml_reader();
121
- init_xml_dtd();
122
- init_xml_element_content();
123
- init_xml_attribute_decl();
124
- init_xml_element_decl();
125
- init_xml_entity_decl();
126
- init_xml_namespace();
127
- init_html_sax_parser_context();
128
- init_html_sax_push_parser();
129
- init_xslt_stylesheet();
130
- init_xml_syntax_error();
131
- init_html_entity_lookup();
132
- init_html_element_description();
133
- init_xml_schema();
134
- init_xml_relax_ng();
135
- init_nokogiri_io();
136
- init_xml_encoding_handler();
137
- init_test_global_handlers();
276
+ id_read = rb_intern("read");
277
+ id_write = rb_intern("write");
138
278
  }