nokogiri 1.13.8 → 1.14.5

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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +39 -0
  3. data/LICENSE-DEPENDENCIES.md +830 -509
  4. data/LICENSE.md +1 -1
  5. data/README.md +18 -11
  6. data/dependencies.yml +33 -15
  7. data/ext/nokogiri/extconf.rb +98 -24
  8. data/ext/nokogiri/gumbo.c +20 -10
  9. data/ext/nokogiri/html4_document.c +2 -2
  10. data/ext/nokogiri/html4_element_description.c +1 -1
  11. data/ext/nokogiri/html4_entity_lookup.c +2 -2
  12. data/ext/nokogiri/html4_sax_parser_context.c +1 -6
  13. data/ext/nokogiri/html4_sax_push_parser.c +1 -1
  14. data/ext/nokogiri/nokogiri.c +38 -51
  15. data/ext/nokogiri/nokogiri.h +18 -14
  16. data/ext/nokogiri/test_global_handlers.c +1 -1
  17. data/ext/nokogiri/xml_attr.c +1 -1
  18. data/ext/nokogiri/xml_attribute_decl.c +2 -2
  19. data/ext/nokogiri/xml_cdata.c +2 -2
  20. data/ext/nokogiri/xml_comment.c +1 -1
  21. data/ext/nokogiri/xml_document.c +22 -13
  22. data/ext/nokogiri/xml_document_fragment.c +1 -1
  23. data/ext/nokogiri/xml_dtd.c +1 -1
  24. data/ext/nokogiri/xml_element_content.c +3 -3
  25. data/ext/nokogiri/xml_element_decl.c +2 -2
  26. data/ext/nokogiri/xml_encoding_handler.c +3 -3
  27. data/ext/nokogiri/xml_entity_decl.c +1 -1
  28. data/ext/nokogiri/xml_entity_reference.c +1 -1
  29. data/ext/nokogiri/xml_namespace.c +80 -14
  30. data/ext/nokogiri/xml_node.c +288 -28
  31. data/ext/nokogiri/xml_node_set.c +0 -2
  32. data/ext/nokogiri/xml_processing_instruction.c +1 -1
  33. data/ext/nokogiri/xml_reader.c +45 -21
  34. data/ext/nokogiri/xml_relax_ng.c +1 -3
  35. data/ext/nokogiri/xml_sax_parser.c +23 -17
  36. data/ext/nokogiri/xml_sax_parser_context.c +1 -6
  37. data/ext/nokogiri/xml_sax_push_parser.c +1 -3
  38. data/ext/nokogiri/xml_schema.c +1 -3
  39. data/ext/nokogiri/xml_syntax_error.c +1 -1
  40. data/ext/nokogiri/xml_text.c +1 -1
  41. data/ext/nokogiri/xml_xpath_context.c +90 -83
  42. data/ext/nokogiri/xslt_stylesheet.c +15 -14
  43. data/gumbo-parser/Makefile +10 -0
  44. data/gumbo-parser/src/attribute.h +1 -1
  45. data/gumbo-parser/src/error.c +2 -2
  46. data/gumbo-parser/src/error.h +1 -1
  47. data/gumbo-parser/src/foreign_attrs.c +2 -2
  48. data/gumbo-parser/src/{gumbo.h → nokogiri_gumbo.h} +1 -0
  49. data/gumbo-parser/src/parser.c +8 -5
  50. data/gumbo-parser/src/replacement.h +1 -1
  51. data/gumbo-parser/src/string_buffer.h +1 -1
  52. data/gumbo-parser/src/string_piece.c +1 -1
  53. data/gumbo-parser/src/svg_attrs.c +2 -2
  54. data/gumbo-parser/src/svg_tags.c +2 -2
  55. data/gumbo-parser/src/tag.c +2 -1
  56. data/gumbo-parser/src/tag_lookup.c +7 -7
  57. data/gumbo-parser/src/tag_lookup.gperf +1 -0
  58. data/gumbo-parser/src/tag_lookup.h +1 -1
  59. data/gumbo-parser/src/token_buffer.h +1 -1
  60. data/gumbo-parser/src/tokenizer.c +1 -1
  61. data/gumbo-parser/src/tokenizer.h +1 -1
  62. data/gumbo-parser/src/utf8.c +1 -1
  63. data/gumbo-parser/src/utf8.h +1 -1
  64. data/gumbo-parser/src/util.c +1 -3
  65. data/gumbo-parser/src/util.h +4 -0
  66. data/gumbo-parser/src/vector.h +1 -1
  67. data/lib/nokogiri/css/node.rb +2 -2
  68. data/lib/nokogiri/css/xpath_visitor.rb +5 -3
  69. data/lib/nokogiri/css.rb +6 -0
  70. data/lib/nokogiri/decorators/slop.rb +1 -1
  71. data/lib/nokogiri/encoding_handler.rb +57 -0
  72. data/lib/nokogiri/extension.rb +3 -2
  73. data/lib/nokogiri/html4/document.rb +2 -121
  74. data/lib/nokogiri/html4/element_description_defaults.rb +6 -12
  75. data/lib/nokogiri/html4/encoding_reader.rb +121 -0
  76. data/lib/nokogiri/html4.rb +1 -0
  77. data/lib/nokogiri/html5/document.rb +113 -36
  78. data/lib/nokogiri/html5/document_fragment.rb +9 -2
  79. data/lib/nokogiri/html5/node.rb +3 -5
  80. data/lib/nokogiri/html5.rb +127 -216
  81. data/lib/nokogiri/jruby/dependencies.rb +1 -19
  82. data/lib/nokogiri/jruby/nokogiri_jars.rb +43 -0
  83. data/lib/nokogiri/version/constant.rb +1 -1
  84. data/lib/nokogiri/version/info.rb +11 -10
  85. data/lib/nokogiri/xml/attr.rb +49 -0
  86. data/lib/nokogiri/xml/builder.rb +1 -1
  87. data/lib/nokogiri/xml/document.rb +103 -55
  88. data/lib/nokogiri/xml/document_fragment.rb +49 -6
  89. data/lib/nokogiri/xml/namespace.rb +42 -0
  90. data/lib/nokogiri/xml/node/save_options.rb +6 -4
  91. data/lib/nokogiri/xml/node.rb +190 -35
  92. data/lib/nokogiri/xml/node_set.rb +88 -9
  93. data/lib/nokogiri/xml/parse_options.rb +129 -50
  94. data/lib/nokogiri/xml/pp/node.rb +6 -4
  95. data/lib/nokogiri/xml/processing_instruction.rb +2 -1
  96. data/lib/nokogiri/xml/sax/parser.rb +2 -3
  97. data/lib/nokogiri/xslt.rb +1 -1
  98. data/lib/nokogiri.rb +3 -11
  99. data/lib/xsd/xmlparser/nokogiri.rb +3 -1
  100. data/ports/archives/libxml2-2.10.4.tar.xz +0 -0
  101. data/ports/archives/libxslt-1.1.37.tar.xz +0 -0
  102. metadata +13 -236
  103. data/patches/libxml2/0004-use-glibc-strlen.patch +0 -53
  104. data/patches/libxml2/0005-avoid-isnan-isinf.patch +0 -81
  105. data/patches/libxml2/0006-update-automake-files-for-arm64.patch +0 -3040
  106. data/patches/libxml2/0008-htmlParseComment-handle-abruptly-closed-comments.patch +0 -61
  107. data/ports/archives/libxml2-2.9.14.tar.xz +0 -0
  108. data/ports/archives/libxslt-1.1.35.tar.xz +0 -0
@@ -49,34 +49,11 @@ void noko_init_html_sax_push_parser(void);
49
49
  void noko_init_gumbo(void);
50
50
  void noko_init_test_global_handlers(void);
51
51
 
52
- static ID id_read, id_write;
53
-
54
-
55
- #ifndef HAVE_VASPRINTF
56
- /*
57
- * Thank you Geoffroy Couprie for this implementation of vasprintf!
58
- */
59
- int
60
- vasprintf(char **strp, const char *fmt, va_list ap)
61
- {
62
- /* Mingw32/64 have a broken vsnprintf implementation that fails when
63
- * using a zero-byte limit in order to retrieve the required size for malloc.
64
- * So we use a one byte buffer instead.
65
- */
66
- char tmp[1];
67
- int len = vsnprintf(tmp, 1, fmt, ap) + 1;
68
- char *res = (char *)malloc((unsigned int)len);
69
- if (res == NULL) {
70
- return -1;
71
- }
72
- *strp = res;
73
- return vsnprintf(res, (unsigned int)len, fmt, ap);
74
- }
75
- #endif
52
+ static ID id_read, id_write, id_external_encoding;
76
53
 
77
54
 
78
55
  static VALUE
79
- read_check(VALUE val)
56
+ noko_io_read_check(VALUE val)
80
57
  {
81
58
  VALUE *args = (VALUE *)val;
82
59
  return rb_funcall(args[0], id_read, 1, args[1]);
@@ -84,75 +61,84 @@ read_check(VALUE val)
84
61
 
85
62
 
86
63
  static VALUE
87
- read_failed(VALUE arg, VALUE exc)
64
+ noko_io_read_failed(VALUE arg, VALUE exc)
88
65
  {
89
66
  return Qundef;
90
67
  }
91
68
 
92
69
 
93
70
  int
94
- noko_io_read(void *ctx, char *buffer, int len)
71
+ noko_io_read(void *io, char *c_buffer, int c_buffer_len)
95
72
  {
96
- VALUE string, args[2];
97
- size_t str_len, safe_len;
73
+ VALUE rb_io = (VALUE)io;
74
+ VALUE rb_read_string, rb_args[2];
75
+ size_t n_bytes_read, safe_len;
98
76
 
99
- args[0] = (VALUE)ctx;
100
- args[1] = INT2NUM(len);
77
+ rb_args[0] = rb_io;
78
+ rb_args[1] = INT2NUM(c_buffer_len);
101
79
 
102
- string = rb_rescue(read_check, (VALUE)args, read_failed, 0);
80
+ rb_read_string = rb_rescue(noko_io_read_check, (VALUE)rb_args, noko_io_read_failed, 0);
103
81
 
104
- if (NIL_P(string)) { return 0; }
105
- if (string == Qundef) { return -1; }
106
- if (TYPE(string) != T_STRING) { return -1; }
82
+ if (NIL_P(rb_read_string)) { return 0; }
83
+ if (rb_read_string == Qundef) { return -1; }
84
+ if (TYPE(rb_read_string) != T_STRING) { return -1; }
107
85
 
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);
86
+ n_bytes_read = (size_t)RSTRING_LEN(rb_read_string);
87
+ safe_len = (n_bytes_read > (size_t)c_buffer_len) ? (size_t)c_buffer_len : n_bytes_read;
88
+ memcpy(c_buffer, StringValuePtr(rb_read_string), safe_len);
111
89
 
112
90
  return (int)safe_len;
113
91
  }
114
92
 
115
93
 
116
94
  static VALUE
117
- write_check(VALUE val)
95
+ noko_io_write_check(VALUE rb_args)
118
96
  {
119
- VALUE *args = (VALUE *)val;
120
- return rb_funcall(args[0], id_write, 1, args[1]);
97
+ VALUE rb_io = ((VALUE *)rb_args)[0];
98
+ VALUE rb_output = ((VALUE *)rb_args)[1];
99
+ return rb_funcall(rb_io, id_write, 1, rb_output);
121
100
  }
122
101
 
123
102
 
124
103
  static VALUE
125
- write_failed(VALUE arg, VALUE exc)
104
+ noko_io_write_failed(VALUE arg, VALUE exc)
126
105
  {
127
106
  return Qundef;
128
107
  }
129
108
 
130
109
 
131
110
  int
132
- noko_io_write(void *ctx, char *buffer, int len)
111
+ noko_io_write(void *io, char *c_buffer, int c_buffer_len)
133
112
  {
134
- VALUE args[2], size;
113
+ VALUE rb_args[2], rb_n_bytes_written;
114
+ VALUE rb_io = (VALUE)io;
115
+ VALUE rb_enc = Qnil;
116
+ rb_encoding *io_encoding;
135
117
 
136
- args[0] = (VALUE)ctx;
137
- args[1] = rb_str_new(buffer, (long)len);
118
+ if (rb_respond_to(rb_io, id_external_encoding)) {
119
+ rb_enc = rb_funcall(rb_io, id_external_encoding, 0);
120
+ }
121
+ io_encoding = RB_NIL_P(rb_enc) ? rb_ascii8bit_encoding() : rb_to_encoding(rb_enc);
138
122
 
139
- size = rb_rescue(write_check, (VALUE)args, write_failed, 0);
123
+ rb_args[0] = rb_io;
124
+ rb_args[1] = rb_enc_str_new(c_buffer, (long)c_buffer_len, io_encoding);
140
125
 
141
- if (size == Qundef) { return -1; }
126
+ rb_n_bytes_written = rb_rescue(noko_io_write_check, (VALUE)rb_args, noko_io_write_failed, 0);
127
+ if (rb_n_bytes_written == Qundef) { return -1; }
142
128
 
143
- return NUM2INT(size);
129
+ return NUM2INT(rb_n_bytes_written);
144
130
  }
145
131
 
146
132
 
147
133
  int
148
- noko_io_close(void *ctx)
134
+ noko_io_close(void *io)
149
135
  {
150
136
  return 0;
151
137
  }
152
138
 
153
139
 
154
140
  void
155
- Init_nokogiri()
141
+ Init_nokogiri(void)
156
142
  {
157
143
  mNokogiri = rb_define_module("Nokogiri");
158
144
  mNokogiriGumbo = rb_define_module_under(mNokogiri, "Gumbo");
@@ -275,4 +261,5 @@ Init_nokogiri()
275
261
 
276
262
  id_read = rb_intern("read");
277
263
  id_write = rb_intern("write");
264
+ id_external_encoding = rb_intern("external_encoding");
278
265
  }
@@ -1,6 +1,8 @@
1
1
  #ifndef NOKOGIRI_NATIVE
2
2
  #define NOKOGIRI_NATIVE
3
3
 
4
+ #include <ruby/defines.h> // https://github.com/sparklemotion/nokogiri/issues/2696
5
+
4
6
  #ifdef _MSC_VER
5
7
  # ifndef WIN32_LEAN_AND_MEAN
6
8
  # define WIN32_LEAN_AND_MEAN
@@ -23,7 +25,6 @@
23
25
  # define NOKOPUBVAR extern
24
26
  #endif
25
27
 
26
-
27
28
  #include <stdlib.h>
28
29
  #include <string.h>
29
30
  #include <assert.h>
@@ -75,22 +76,25 @@ xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
75
76
  #define NOKOGIRI_STR_NEW(str, len) rb_external_str_new_with_enc((const char *)(str), (long)(len), rb_utf8_encoding())
76
77
  #define RBSTR_OR_QNIL(_str) (_str ? NOKOGIRI_STR_NEW2(_str) : Qnil)
77
78
 
78
- #ifdef DEBUG
79
- # define NOKOGIRI_DEBUG_START(p) if (getenv("NOKOGIRI_NO_FREE")) return ; if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p start\n", __FILE__, __LINE__, p);
80
- # define NOKOGIRI_DEBUG_END(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p end\n", __FILE__, __LINE__, p);
81
- #else
82
- # define NOKOGIRI_DEBUG_START(p)
83
- # define NOKOGIRI_DEBUG_END(p)
79
+ #ifndef NORETURN_DECL
80
+ # if defined(__GNUC__)
81
+ # define NORETURN_DECL __attribute__ ((noreturn))
82
+ # else
83
+ # define NORETURN_DECL
84
+ # endif
84
85
  #endif
85
86
 
86
- #ifndef NORETURN
87
+ #ifndef PRINTFLIKE_DECL
87
88
  # if defined(__GNUC__)
88
- # define NORETURN(name) __attribute__((noreturn)) name
89
+ # define PRINTFLIKE_DECL(stringidx, argidx) __attribute__ ((format(printf,stringidx,argidx)))
89
90
  # else
90
- # define NORETURN(name) name
91
+ # define PRINTFLIKE_DECL(stringidx, argidx)
91
92
  # endif
92
93
  #endif
93
94
 
95
+ #if defined(TRUFFLERUBY) && !defined(NOKOGIRI_PACKAGED_LIBRARIES)
96
+ # define TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
97
+ #endif
94
98
 
95
99
  NOKOPUBVAR VALUE mNokogiri ;
96
100
  NOKOPUBVAR VALUE mNokogiriGumbo ;
@@ -162,7 +166,6 @@ typedef struct _nokogiriXsltStylesheetTuple {
162
166
  VALUE func_instances;
163
167
  } nokogiriXsltStylesheetTuple;
164
168
 
165
- int vasprintf(char **strp, const char *fmt, va_list ap);
166
169
  void noko_xml_document_pin_node(xmlNodePtr);
167
170
  void noko_xml_document_pin_namespace(xmlNsPtr, xmlDocPtr);
168
171
 
@@ -171,6 +174,7 @@ int noko_io_write(void *ctx, char *buffer, int len);
171
174
  int noko_io_close(void *ctx);
172
175
 
173
176
  #define Noko_Node_Get_Struct(obj,type,sval) ((sval) = (type*)DATA_PTR(obj))
177
+ #define Noko_Namespace_Get_Struct(obj,type,sval) ((sval) = (type*)DATA_PTR(obj))
174
178
 
175
179
  VALUE noko_xml_node_wrap(VALUE klass, xmlNodePtr node) ;
176
180
  VALUE noko_xml_node_wrap_node_set_result(xmlNodePtr node, VALUE node_set) ;
@@ -197,7 +201,7 @@ NOKOPUBFUN VALUE Nokogiri_wrap_xml_document(VALUE klass,
197
201
  #define NOKOGIRI_SAX_SELF(_ctxt) ((nokogiriSAXTuplePtr)(_ctxt))->self
198
202
  #define NOKOGIRI_SAX_CTXT(_ctxt) ((nokogiriSAXTuplePtr)(_ctxt))->ctxt
199
203
  #define NOKOGIRI_SAX_TUPLE_NEW(_ctxt, _self) nokogiri_sax_tuple_new(_ctxt, _self)
200
- #define NOKOGIRI_SAX_TUPLE_DESTROY(_tuple) free(_tuple)
204
+ #define NOKOGIRI_SAX_TUPLE_DESTROY(_tuple) ruby_xfree(_tuple)
201
205
 
202
206
  #define DISCARD_CONST_QUAL(t, v) ((t)(uintptr_t)(v))
203
207
  #define DISCARD_CONST_QUAL_XMLCHAR(v) DISCARD_CONST_QUAL(xmlChar *, v)
@@ -214,7 +218,7 @@ void Nokogiri_structured_error_func_save_and_set(libxmlStructuredErrorHandlerSta
214
218
  void Nokogiri_structured_error_func_restore(libxmlStructuredErrorHandlerState *handler_state);
215
219
  VALUE Nokogiri_wrap_xml_syntax_error(xmlErrorPtr error);
216
220
  void Nokogiri_error_array_pusher(void *ctx, xmlErrorPtr error);
217
- NORETURN(void Nokogiri_error_raise(void *ctx, xmlErrorPtr error));
221
+ NORETURN_DECL void Nokogiri_error_raise(void *ctx, xmlErrorPtr error);
218
222
  void Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, int nargs, VALUE handler,
219
223
  const char *function_name) ;
220
224
 
@@ -222,7 +226,7 @@ static inline
222
226
  nokogiriSAXTuplePtr
223
227
  nokogiri_sax_tuple_new(xmlParserCtxtPtr ctxt, VALUE self)
224
228
  {
225
- nokogiriSAXTuplePtr tuple = malloc(sizeof(nokogiriSAXTuple));
229
+ nokogiriSAXTuplePtr tuple = ruby_xmalloc(sizeof(nokogiriSAXTuple));
226
230
  tuple->self = self;
227
231
  tuple->ctxt = ctxt;
228
232
  return tuple;
@@ -32,7 +32,7 @@ rb_foreign_error_handler(VALUE klass)
32
32
  * Do NOT use this outside of the Nokogiri test suite.
33
33
  */
34
34
  void
35
- noko_init_test_global_handlers()
35
+ noko_init_test_global_handlers(void)
36
36
  {
37
37
  VALUE mNokogiriTest = rb_define_module_under(mNokogiri, "Test");
38
38
 
@@ -89,7 +89,7 @@ new (int argc, VALUE *argv, VALUE klass)
89
89
  }
90
90
 
91
91
  void
92
- noko_init_xml_attr()
92
+ noko_init_xml_attr(void)
93
93
  {
94
94
  assert(cNokogiriXmlNode);
95
95
  /*
@@ -13,7 +13,7 @@ attribute_type(VALUE self)
13
13
  {
14
14
  xmlAttributePtr node;
15
15
  Noko_Node_Get_Struct(self, xmlAttribute, node);
16
- return INT2NUM((long)node->atype);
16
+ return INT2NUM(node->atype);
17
17
  }
18
18
 
19
19
  /*
@@ -59,7 +59,7 @@ enumeration(VALUE self)
59
59
  }
60
60
 
61
61
  void
62
- noko_init_xml_attribute_decl()
62
+ noko_init_xml_attribute_decl(void)
63
63
  {
64
64
  assert(cNokogiriXmlNode);
65
65
  cNokogiriXmlAttributeDecl = rb_define_class_under(mNokogiriXml, "AttributeDecl", cNokogiriXmlNode);
@@ -29,7 +29,7 @@ new (int argc, VALUE *argv, VALUE klass)
29
29
 
30
30
  if (!NIL_P(content)) {
31
31
  content_str = (xmlChar *)StringValuePtr(content);
32
- content_str_len = RSTRING_LEN(content);
32
+ content_str_len = RSTRING_LENINT(content);
33
33
  }
34
34
 
35
35
  node = xmlNewCDataBlock(xml_doc->doc, content_str, content_str_len);
@@ -45,7 +45,7 @@ new (int argc, VALUE *argv, VALUE klass)
45
45
  }
46
46
 
47
47
  void
48
- noko_init_xml_cdata()
48
+ noko_init_xml_cdata(void)
49
49
  {
50
50
  assert(cNokogiriXmlText);
51
51
  /*
@@ -48,7 +48,7 @@ new (int argc, VALUE *argv, VALUE klass)
48
48
  }
49
49
 
50
50
  void
51
- noko_init_xml_comment()
51
+ noko_init_xml_comment(void)
52
52
  {
53
53
  assert(cNokogiriXmlCharacterData);
54
54
  /*
@@ -65,14 +65,12 @@ dealloc(xmlDocPtr doc)
65
65
  {
66
66
  st_table *node_hash;
67
67
 
68
- NOKOGIRI_DEBUG_START(doc);
69
-
70
68
  node_hash = DOC_UNLINKED_NODE_HASH(doc);
71
69
 
72
70
  st_foreach(node_hash, dealloc_node_i, (st_data_t)doc);
73
71
  st_free_table(node_hash);
74
72
 
75
- free(doc->_private);
73
+ ruby_xfree(doc->_private);
76
74
 
77
75
  /* When both Nokogiri and libxml-ruby are loaded, make sure that all nodes
78
76
  * have their _private pointers cleared. This is to avoid libxml-ruby's
@@ -84,8 +82,6 @@ dealloc(xmlDocPtr doc)
84
82
  }
85
83
 
86
84
  xmlFreeDoc(doc);
87
-
88
- NOKOGIRI_DEBUG_END(doc);
89
85
  }
90
86
 
91
87
  static void
@@ -104,7 +100,11 @@ recursively_remove_namespaces_from_node(xmlNodePtr node)
104
100
  (node->type == XML_XINCLUDE_START) ||
105
101
  (node->type == XML_XINCLUDE_END)) &&
106
102
  node->nsDef) {
107
- xmlFreeNsList(node->nsDef);
103
+ xmlNsPtr curr = node->nsDef;
104
+ while (curr) {
105
+ noko_xml_document_pin_namespace(curr, node->doc);
106
+ curr = curr->next;
107
+ }
108
108
  node->nsDef = NULL;
109
109
  }
110
110
 
@@ -536,6 +536,7 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
536
536
  VALUE rb_mode;
537
537
  VALUE rb_namespaces;
538
538
  VALUE rb_comments_p;
539
+ int c_mode = 0;
539
540
  xmlChar **c_namespaces;
540
541
 
541
542
  xmlDocPtr c_doc;
@@ -547,8 +548,16 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
547
548
  VALUE rb_io;
548
549
 
549
550
  rb_scan_args(argc, argv, "03", &rb_mode, &rb_namespaces, &rb_comments_p);
550
- if (!NIL_P(rb_mode)) { Check_Type(rb_mode, T_FIXNUM); }
551
- if (!NIL_P(rb_namespaces)) { Check_Type(rb_namespaces, T_ARRAY); }
551
+ if (!NIL_P(rb_mode)) {
552
+ Check_Type(rb_mode, T_FIXNUM);
553
+ c_mode = NUM2INT(rb_mode);
554
+ }
555
+ if (!NIL_P(rb_namespaces)) {
556
+ Check_Type(rb_namespaces, T_ARRAY);
557
+ if (c_mode == XML_C14N_1_0 || c_mode == XML_C14N_1_1) {
558
+ rb_raise(rb_eRuntimeError, "This canonicalizer does not support this operation");
559
+ }
560
+ }
552
561
 
553
562
  Data_Get_Struct(self, xmlDoc, c_doc);
554
563
 
@@ -569,7 +578,7 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
569
578
  c_namespaces = NULL;
570
579
  } else {
571
580
  long ns_len = RARRAY_LEN(rb_namespaces);
572
- c_namespaces = calloc((size_t)ns_len + 1, sizeof(xmlChar *));
581
+ c_namespaces = ruby_xcalloc((size_t)ns_len + 1, sizeof(xmlChar *));
573
582
  for (int j = 0 ; j < ns_len ; j++) {
574
583
  VALUE entry = rb_ary_entry(rb_namespaces, j);
575
584
  c_namespaces[j] = (xmlChar *)StringValueCStr(entry);
@@ -577,12 +586,12 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
577
586
  }
578
587
 
579
588
  xmlC14NExecute(c_doc, c_callback_wrapper, rb_callback,
580
- (int)(NIL_P(rb_mode) ? 0 : NUM2INT(rb_mode)),
589
+ c_mode,
581
590
  c_namespaces,
582
591
  (int)RTEST(rb_comments_p),
583
592
  c_obuf);
584
593
 
585
- free(c_namespaces);
594
+ ruby_xfree(c_namespaces);
586
595
  xmlOutputBufferClose(c_obuf);
587
596
 
588
597
  return rb_funcall(rb_io, rb_intern("string"), 0);
@@ -600,7 +609,7 @@ noko_xml_document_wrap_with_init_args(VALUE klass, xmlDocPtr c_document, int arg
600
609
 
601
610
  rb_document = Data_Wrap_Struct(klass, mark, dealloc, c_document);
602
611
 
603
- tuple = (nokogiriTuplePtr)malloc(sizeof(nokogiriTuple));
612
+ tuple = (nokogiriTuplePtr)ruby_xmalloc(sizeof(nokogiriTuple));
604
613
  tuple->doc = rb_document;
605
614
  tuple->unlinkedNodes = st_init_numtable_with_size(128);
606
615
  tuple->node_cache = rb_ary_new();
@@ -655,7 +664,7 @@ noko_xml_document_pin_namespace(xmlNsPtr ns, xmlDocPtr doc)
655
664
 
656
665
 
657
666
  void
658
- noko_init_xml_document()
667
+ noko_init_xml_document(void)
659
668
  {
660
669
  assert(cNokogiriXmlNode);
661
670
  /*
@@ -32,7 +32,7 @@ new (int argc, VALUE *argv, VALUE klass)
32
32
  }
33
33
 
34
34
  void
35
- noko_init_xml_document_fragment()
35
+ noko_init_xml_document_fragment(void)
36
36
  {
37
37
  assert(cNokogiriXmlNode);
38
38
  /*
@@ -190,7 +190,7 @@ external_id(VALUE self)
190
190
  }
191
191
 
192
192
  void
193
- noko_init_xml_dtd()
193
+ noko_init_xml_dtd(void)
194
194
  {
195
195
  assert(cNokogiriXmlNode);
196
196
  /*
@@ -31,7 +31,7 @@ get_type(VALUE self)
31
31
  xmlElementContentPtr elem;
32
32
  Data_Get_Struct(self, xmlElementContent, elem);
33
33
 
34
- return INT2NUM((long)elem->type);
34
+ return INT2NUM(elem->type);
35
35
  }
36
36
 
37
37
  /*
@@ -79,7 +79,7 @@ get_occur(VALUE self)
79
79
  xmlElementContentPtr elem;
80
80
  Data_Get_Struct(self, xmlElementContent, elem);
81
81
 
82
- return INT2NUM((long)elem->ocur);
82
+ return INT2NUM(elem->ocur);
83
83
  }
84
84
 
85
85
  /*
@@ -112,7 +112,7 @@ noko_xml_element_content_wrap(VALUE doc, xmlElementContentPtr element)
112
112
  }
113
113
 
114
114
  void
115
- noko_init_xml_element_content()
115
+ noko_init_xml_element_content(void)
116
116
  {
117
117
  cNokogiriXmlElementContent = rb_define_class_under(mNokogiriXml, "ElementContent", rb_cObject);
118
118
 
@@ -15,7 +15,7 @@ element_type(VALUE self)
15
15
  {
16
16
  xmlElementPtr node;
17
17
  Noko_Node_Get_Struct(self, xmlElement, node);
18
- return INT2NUM((long)node->etype);
18
+ return INT2NUM(node->etype);
19
19
  }
20
20
 
21
21
  /*
@@ -56,7 +56,7 @@ prefix(VALUE self)
56
56
  }
57
57
 
58
58
  void
59
- noko_init_xml_element_decl()
59
+ noko_init_xml_element_decl(void)
60
60
  {
61
61
  assert(cNokogiriXmlNode);
62
62
  cNokogiriXmlElementDecl = rb_define_class_under(mNokogiriXml, "ElementDecl", cNokogiriXmlNode);
@@ -45,9 +45,9 @@ rb_xml_encoding_handler_s_delete(VALUE klass, VALUE name)
45
45
 
46
46
 
47
47
  /*
48
- * call-seq: Nokogiri::EncodingHandler.alias(from, to)
48
+ * call-seq: Nokogiri::EncodingHandler.alias(real_name, alias_name)
49
49
  *
50
- * Alias encoding handler with name +from+ to name +to+
50
+ * Alias encoding handler with name +real_name+ to name +alias_name+
51
51
  */
52
52
  static VALUE
53
53
  rb_xml_encoding_handler_s_alias(VALUE klass, VALUE from, VALUE to)
@@ -89,7 +89,7 @@ rb_xml_encoding_handler_name(VALUE self)
89
89
 
90
90
 
91
91
  void
92
- noko_init_xml_encoding_handler()
92
+ noko_init_xml_encoding_handler(void)
93
93
  {
94
94
  cNokogiriEncodingHandler = rb_define_class_under(mNokogiri, "EncodingHandler", rb_cObject);
95
95
 
@@ -86,7 +86,7 @@ system_id(VALUE self)
86
86
  }
87
87
 
88
88
  void
89
- noko_init_xml_entity_decl()
89
+ noko_init_xml_entity_decl(void)
90
90
  {
91
91
  assert(cNokogiriXmlNode);
92
92
  cNokogiriXmlEntityDecl = rb_define_class_under(mNokogiriXml, "EntityDecl", cNokogiriXmlNode);
@@ -38,7 +38,7 @@ new (int argc, VALUE *argv, VALUE klass)
38
38
  }
39
39
 
40
40
  void
41
- noko_init_xml_entity_reference()
41
+ noko_init_xml_entity_reference(void)
42
42
  {
43
43
  assert(cNokogiriXmlNode);
44
44
  /*
@@ -25,13 +25,14 @@
25
25
  VALUE cNokogiriXmlNamespace ;
26
26
 
27
27
  static void
28
- dealloc_namespace(xmlNsPtr ns)
28
+ _xml_namespace_dealloc(void *ptr)
29
29
  {
30
30
  /*
31
31
  * this deallocator is only used for namespace nodes that are part of an xpath
32
32
  * node set. see noko_xml_namespace_wrap().
33
33
  */
34
- NOKOGIRI_DEBUG_START(ns) ;
34
+ xmlNsPtr ns = ptr;
35
+
35
36
  if (ns->href) {
36
37
  xmlFree(DISCARD_CONST_QUAL_XMLCHAR(ns->href));
37
38
  }
@@ -39,39 +40,100 @@ dealloc_namespace(xmlNsPtr ns)
39
40
  xmlFree(DISCARD_CONST_QUAL_XMLCHAR(ns->prefix));
40
41
  }
41
42
  xmlFree(ns);
42
- NOKOGIRI_DEBUG_END(ns) ;
43
43
  }
44
44
 
45
+ #ifdef HAVE_RB_GC_LOCATION
46
+ static void
47
+ _xml_namespace_update_references(void *ptr)
48
+ {
49
+ xmlNsPtr ns = ptr;
50
+ if (ns->_private) {
51
+ ns->_private = (void *)rb_gc_location((VALUE)ns->_private);
52
+ }
53
+ }
54
+ #else
55
+ # define _xml_namespace_update_references 0
56
+ #endif
57
+
58
+ static const rb_data_type_t nokogiri_xml_namespace_type_with_dealloc = {
59
+ "Nokogiri/XMLNamespace/WithDealloc",
60
+ {0, _xml_namespace_dealloc, 0, _xml_namespace_update_references},
61
+ 0, 0,
62
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
63
+ RUBY_TYPED_FREE_IMMEDIATELY,
64
+ #endif
65
+ };
66
+
67
+ static const rb_data_type_t nokogiri_xml_namespace_type_without_dealloc = {
68
+ "Nokogiri/XMLNamespace/WithoutDealloc",
69
+ {0, 0, 0, _xml_namespace_update_references},
70
+ 0, 0,
71
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
72
+ RUBY_TYPED_FREE_IMMEDIATELY,
73
+ #endif
74
+ };
45
75
 
46
76
  /*
47
- * call-seq:
48
- * prefix
77
+ * :call-seq:
78
+ * prefix() → String or nil
79
+ *
80
+ * Return the prefix for this Namespace, or +nil+ if there is no prefix (e.g., default namespace).
81
+ *
82
+ * *Example*
49
83
  *
50
- * Get the prefix for this namespace. Returns +nil+ if there is no prefix.
84
+ * doc = Nokogiri::XML.parse(<<~XML)
85
+ * <?xml version="1.0"?>
86
+ * <root xmlns="http://nokogiri.org/ns/default" xmlns:noko="http://nokogiri.org/ns/noko">
87
+ * <child1 foo="abc" noko:bar="def"/>
88
+ * <noko:child2 foo="qwe" noko:bar="rty"/>
89
+ * </root>
90
+ * XML
91
+ *
92
+ * doc.root.elements.first.namespace.prefix
93
+ * # => nil
94
+ *
95
+ * doc.root.elements.last.namespace.prefix
96
+ * # => "noko"
51
97
  */
52
98
  static VALUE
53
99
  prefix(VALUE self)
54
100
  {
55
101
  xmlNsPtr ns;
56
102
 
57
- Data_Get_Struct(self, xmlNs, ns);
103
+ Noko_Namespace_Get_Struct(self, xmlNs, ns);
58
104
  if (!ns->prefix) { return Qnil; }
59
105
 
60
106
  return NOKOGIRI_STR_NEW2(ns->prefix);
61
107
  }
62
108
 
63
109
  /*
64
- * call-seq:
65
- * href
110
+ * :call-seq:
111
+ * href() → String
112
+ *
113
+ * Returns the URI reference for this Namespace.
114
+ *
115
+ * *Example*
116
+ *
117
+ * doc = Nokogiri::XML.parse(<<~XML)
118
+ * <?xml version="1.0"?>
119
+ * <root xmlns="http://nokogiri.org/ns/default" xmlns:noko="http://nokogiri.org/ns/noko">
120
+ * <child1 foo="abc" noko:bar="def"/>
121
+ * <noko:child2 foo="qwe" noko:bar="rty"/>
122
+ * </root>
123
+ * XML
124
+ *
125
+ * doc.root.elements.first.namespace.href
126
+ * # => "http://nokogiri.org/ns/default"
66
127
  *
67
- * Get the href for this namespace
128
+ * doc.root.elements.last.namespace.href
129
+ * # => "http://nokogiri.org/ns/noko"
68
130
  */
69
131
  static VALUE
70
132
  href(VALUE self)
71
133
  {
72
134
  xmlNsPtr ns;
73
135
 
74
- Data_Get_Struct(self, xmlNs, ns);
136
+ Noko_Namespace_Get_Struct(self, xmlNs, ns);
75
137
  if (!ns->href) { return Qnil; }
76
138
 
77
139
  return NOKOGIRI_STR_NEW2(ns->href);
@@ -87,14 +149,18 @@ noko_xml_namespace_wrap(xmlNsPtr c_namespace, xmlDocPtr c_document)
87
149
  }
88
150
 
89
151
  if (c_document) {
90
- rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, c_namespace);
152
+ rb_namespace = TypedData_Wrap_Struct(cNokogiriXmlNamespace,
153
+ &nokogiri_xml_namespace_type_without_dealloc,
154
+ c_namespace);
91
155
 
92
156
  if (DOC_RUBY_OBJECT_TEST(c_document)) {
93
157
  rb_iv_set(rb_namespace, "@document", DOC_RUBY_OBJECT(c_document));
94
158
  rb_ary_push(DOC_NODE_CACHE(c_document), rb_namespace);
95
159
  }
96
160
  } else {
97
- rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, dealloc_namespace, c_namespace);
161
+ rb_namespace = TypedData_Wrap_Struct(cNokogiriXmlNamespace,
162
+ &nokogiri_xml_namespace_type_with_dealloc,
163
+ c_namespace);
98
164
  }
99
165
 
100
166
  c_namespace->_private = (void *)rb_namespace;
@@ -109,7 +175,7 @@ noko_xml_namespace_wrap_xpath_copy(xmlNsPtr c_namespace)
109
175
  }
110
176
 
111
177
  void
112
- noko_init_xml_namespace()
178
+ noko_init_xml_namespace(void)
113
179
  {
114
180
  cNokogiriXmlNamespace = rb_define_class_under(mNokogiriXml, "Namespace", rb_cObject);
115
181