nokogiri 1.10.10 → 1.12.0

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 (216) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1173 -884
  4. data/LICENSE.md +1 -1
  5. data/README.md +176 -96
  6. data/dependencies.yml +12 -12
  7. data/ext/nokogiri/depend +38 -358
  8. data/ext/nokogiri/extconf.rb +712 -414
  9. data/ext/nokogiri/gumbo.c +584 -0
  10. data/ext/nokogiri/html4_document.c +166 -0
  11. data/ext/nokogiri/html4_element_description.c +294 -0
  12. data/ext/nokogiri/html4_entity_lookup.c +37 -0
  13. data/ext/nokogiri/html4_sax_parser_context.c +119 -0
  14. data/ext/nokogiri/html4_sax_push_parser.c +95 -0
  15. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  16. data/ext/nokogiri/nokogiri.c +228 -91
  17. data/ext/nokogiri/nokogiri.h +188 -89
  18. data/ext/nokogiri/test_global_handlers.c +40 -0
  19. data/ext/nokogiri/xml_attr.c +15 -15
  20. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  21. data/ext/nokogiri/xml_cdata.c +13 -18
  22. data/ext/nokogiri/xml_comment.c +19 -26
  23. data/ext/nokogiri/xml_document.c +267 -195
  24. data/ext/nokogiri/xml_document_fragment.c +13 -15
  25. data/ext/nokogiri/xml_dtd.c +54 -48
  26. data/ext/nokogiri/xml_element_content.c +31 -26
  27. data/ext/nokogiri/xml_element_decl.c +22 -22
  28. data/ext/nokogiri/xml_encoding_handler.c +28 -17
  29. data/ext/nokogiri/xml_entity_decl.c +32 -30
  30. data/ext/nokogiri/xml_entity_reference.c +16 -18
  31. data/ext/nokogiri/xml_namespace.c +58 -49
  32. data/ext/nokogiri/xml_node.c +489 -410
  33. data/ext/nokogiri/xml_node_set.c +174 -162
  34. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  35. data/ext/nokogiri/xml_reader.c +197 -172
  36. data/ext/nokogiri/xml_relax_ng.c +52 -28
  37. data/ext/nokogiri/xml_sax_parser.c +112 -112
  38. data/ext/nokogiri/xml_sax_parser_context.c +105 -86
  39. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  40. data/ext/nokogiri/xml_schema.c +96 -46
  41. data/ext/nokogiri/xml_syntax_error.c +42 -21
  42. data/ext/nokogiri/xml_text.c +13 -17
  43. data/ext/nokogiri/xml_xpath_context.c +158 -73
  44. data/ext/nokogiri/xslt_stylesheet.c +158 -164
  45. data/gumbo-parser/CHANGES.md +63 -0
  46. data/gumbo-parser/Makefile +101 -0
  47. data/gumbo-parser/THANKS +27 -0
  48. data/gumbo-parser/src/Makefile +17 -0
  49. data/gumbo-parser/src/README.md +41 -0
  50. data/gumbo-parser/src/ascii.c +75 -0
  51. data/gumbo-parser/src/ascii.h +115 -0
  52. data/gumbo-parser/src/attribute.c +42 -0
  53. data/gumbo-parser/src/attribute.h +17 -0
  54. data/gumbo-parser/src/char_ref.c +22225 -0
  55. data/gumbo-parser/src/char_ref.h +29 -0
  56. data/gumbo-parser/src/char_ref.rl +2154 -0
  57. data/gumbo-parser/src/error.c +626 -0
  58. data/gumbo-parser/src/error.h +148 -0
  59. data/gumbo-parser/src/foreign_attrs.c +104 -0
  60. data/gumbo-parser/src/foreign_attrs.gperf +27 -0
  61. data/gumbo-parser/src/gumbo.h +943 -0
  62. data/gumbo-parser/src/insertion_mode.h +33 -0
  63. data/gumbo-parser/src/macros.h +91 -0
  64. data/gumbo-parser/src/parser.c +4886 -0
  65. data/gumbo-parser/src/parser.h +41 -0
  66. data/gumbo-parser/src/replacement.h +33 -0
  67. data/gumbo-parser/src/string_buffer.c +103 -0
  68. data/gumbo-parser/src/string_buffer.h +68 -0
  69. data/gumbo-parser/src/string_piece.c +48 -0
  70. data/gumbo-parser/src/svg_attrs.c +174 -0
  71. data/gumbo-parser/src/svg_attrs.gperf +77 -0
  72. data/gumbo-parser/src/svg_tags.c +137 -0
  73. data/gumbo-parser/src/svg_tags.gperf +55 -0
  74. data/gumbo-parser/src/tag.c +222 -0
  75. data/gumbo-parser/src/tag_lookup.c +382 -0
  76. data/gumbo-parser/src/tag_lookup.gperf +169 -0
  77. data/gumbo-parser/src/tag_lookup.h +13 -0
  78. data/gumbo-parser/src/token_buffer.c +79 -0
  79. data/gumbo-parser/src/token_buffer.h +71 -0
  80. data/gumbo-parser/src/token_type.h +17 -0
  81. data/gumbo-parser/src/tokenizer.c +3463 -0
  82. data/gumbo-parser/src/tokenizer.h +112 -0
  83. data/gumbo-parser/src/tokenizer_states.h +339 -0
  84. data/gumbo-parser/src/utf8.c +245 -0
  85. data/gumbo-parser/src/utf8.h +164 -0
  86. data/gumbo-parser/src/util.c +68 -0
  87. data/gumbo-parser/src/util.h +30 -0
  88. data/gumbo-parser/src/vector.c +111 -0
  89. data/gumbo-parser/src/vector.h +45 -0
  90. data/lib/nokogiri.rb +32 -51
  91. data/lib/nokogiri/css.rb +15 -14
  92. data/lib/nokogiri/css/node.rb +1 -0
  93. data/lib/nokogiri/css/parser.rb +64 -63
  94. data/lib/nokogiri/css/parser.y +3 -3
  95. data/lib/nokogiri/css/parser_extras.rb +39 -36
  96. data/lib/nokogiri/css/syntax_error.rb +2 -1
  97. data/lib/nokogiri/css/tokenizer.rb +1 -0
  98. data/lib/nokogiri/css/xpath_visitor.rb +73 -43
  99. data/lib/nokogiri/decorators/slop.rb +1 -0
  100. data/lib/nokogiri/extension.rb +26 -0
  101. data/lib/nokogiri/gumbo.rb +14 -0
  102. data/lib/nokogiri/html.rb +32 -27
  103. data/lib/nokogiri/html4.rb +40 -0
  104. data/lib/nokogiri/{html → html4}/builder.rb +3 -2
  105. data/lib/nokogiri/{html → html4}/document.rb +17 -30
  106. data/lib/nokogiri/{html → html4}/document_fragment.rb +18 -17
  107. data/lib/nokogiri/{html → html4}/element_description.rb +2 -1
  108. data/lib/nokogiri/{html → html4}/element_description_defaults.rb +2 -1
  109. data/lib/nokogiri/{html → html4}/entity_lookup.rb +2 -1
  110. data/lib/nokogiri/{html → html4}/sax/parser.rb +12 -14
  111. data/lib/nokogiri/html4/sax/parser_context.rb +19 -0
  112. data/lib/nokogiri/{html → html4}/sax/push_parser.rb +6 -5
  113. data/lib/nokogiri/html5.rb +473 -0
  114. data/lib/nokogiri/html5/document.rb +74 -0
  115. data/lib/nokogiri/html5/document_fragment.rb +80 -0
  116. data/lib/nokogiri/html5/node.rb +93 -0
  117. data/lib/nokogiri/jruby/dependencies.rb +20 -0
  118. data/lib/nokogiri/syntax_error.rb +1 -0
  119. data/lib/nokogiri/version.rb +3 -109
  120. data/lib/nokogiri/version/constant.rb +5 -0
  121. data/lib/nokogiri/version/info.rb +215 -0
  122. data/lib/nokogiri/xml.rb +36 -36
  123. data/lib/nokogiri/xml/attr.rb +1 -0
  124. data/lib/nokogiri/xml/attribute_decl.rb +1 -0
  125. data/lib/nokogiri/xml/builder.rb +3 -2
  126. data/lib/nokogiri/xml/cdata.rb +1 -0
  127. data/lib/nokogiri/xml/character_data.rb +1 -0
  128. data/lib/nokogiri/xml/document.rb +92 -41
  129. data/lib/nokogiri/xml/document_fragment.rb +5 -6
  130. data/lib/nokogiri/xml/dtd.rb +1 -0
  131. data/lib/nokogiri/xml/element_content.rb +1 -0
  132. data/lib/nokogiri/xml/element_decl.rb +1 -0
  133. data/lib/nokogiri/xml/entity_decl.rb +1 -0
  134. data/lib/nokogiri/xml/entity_reference.rb +1 -0
  135. data/lib/nokogiri/xml/namespace.rb +1 -0
  136. data/lib/nokogiri/xml/node.rb +629 -293
  137. data/lib/nokogiri/xml/node/save_options.rb +1 -0
  138. data/lib/nokogiri/xml/node_set.rb +1 -0
  139. data/lib/nokogiri/xml/notation.rb +1 -0
  140. data/lib/nokogiri/xml/parse_options.rb +12 -3
  141. data/lib/nokogiri/xml/pp.rb +3 -2
  142. data/lib/nokogiri/xml/pp/character_data.rb +1 -0
  143. data/lib/nokogiri/xml/pp/node.rb +1 -0
  144. data/lib/nokogiri/xml/processing_instruction.rb +1 -0
  145. data/lib/nokogiri/xml/reader.rb +9 -12
  146. data/lib/nokogiri/xml/relax_ng.rb +7 -2
  147. data/lib/nokogiri/xml/sax.rb +5 -4
  148. data/lib/nokogiri/xml/sax/document.rb +25 -30
  149. data/lib/nokogiri/xml/sax/parser.rb +1 -0
  150. data/lib/nokogiri/xml/sax/parser_context.rb +1 -0
  151. data/lib/nokogiri/xml/sax/push_parser.rb +1 -0
  152. data/lib/nokogiri/xml/schema.rb +13 -4
  153. data/lib/nokogiri/xml/searchable.rb +25 -16
  154. data/lib/nokogiri/xml/syntax_error.rb +1 -0
  155. data/lib/nokogiri/xml/text.rb +1 -0
  156. data/lib/nokogiri/xml/xpath.rb +4 -5
  157. data/lib/nokogiri/xml/xpath/syntax_error.rb +2 -1
  158. data/lib/nokogiri/xml/xpath_context.rb +1 -0
  159. data/lib/nokogiri/xslt.rb +17 -16
  160. data/lib/nokogiri/xslt/stylesheet.rb +2 -1
  161. data/lib/xsd/xmlparser/nokogiri.rb +1 -0
  162. data/patches/libxml2/{0002-Remove-script-macro-support.patch → 0001-Remove-script-macro-support.patch} +0 -0
  163. data/patches/libxml2/{0003-Update-entities-to-remove-handling-of-ssi.patch → 0002-Update-entities-to-remove-handling-of-ssi.patch} +0 -0
  164. data/patches/libxml2/{0004-libxml2.la-is-in-top_builddir.patch → 0003-libxml2.la-is-in-top_builddir.patch} +1 -1
  165. data/patches/libxml2/0004-use-glibc-strlen.patch +53 -0
  166. data/patches/libxml2/0005-avoid-isnan-isinf.patch +81 -0
  167. data/patches/libxml2/0006-update-automake-files-for-arm64.patch +2511 -0
  168. data/patches/libxml2/0007-Fix-XPath-recursion-limit.patch +31 -0
  169. data/patches/libxslt/0001-update-automake-files-for-arm64.patch +2511 -0
  170. data/patches/libxslt/0002-Fix-xml2-config-check-in-configure-script.patch +19 -0
  171. data/ports/archives/libxml2-2.9.12.tar.gz +0 -0
  172. metadata +139 -161
  173. data/ext/nokogiri/html_document.c +0 -170
  174. data/ext/nokogiri/html_document.h +0 -10
  175. data/ext/nokogiri/html_element_description.c +0 -279
  176. data/ext/nokogiri/html_element_description.h +0 -10
  177. data/ext/nokogiri/html_entity_lookup.c +0 -32
  178. data/ext/nokogiri/html_entity_lookup.h +0 -8
  179. data/ext/nokogiri/html_sax_parser_context.c +0 -116
  180. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  181. data/ext/nokogiri/html_sax_push_parser.c +0 -87
  182. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  183. data/ext/nokogiri/xml_attr.h +0 -9
  184. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  185. data/ext/nokogiri/xml_cdata.h +0 -9
  186. data/ext/nokogiri/xml_comment.h +0 -9
  187. data/ext/nokogiri/xml_document.h +0 -23
  188. data/ext/nokogiri/xml_document_fragment.h +0 -10
  189. data/ext/nokogiri/xml_dtd.h +0 -10
  190. data/ext/nokogiri/xml_element_content.h +0 -10
  191. data/ext/nokogiri/xml_element_decl.h +0 -9
  192. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  193. data/ext/nokogiri/xml_entity_decl.h +0 -10
  194. data/ext/nokogiri/xml_entity_reference.h +0 -9
  195. data/ext/nokogiri/xml_io.c +0 -61
  196. data/ext/nokogiri/xml_io.h +0 -11
  197. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  198. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  199. data/ext/nokogiri/xml_namespace.h +0 -14
  200. data/ext/nokogiri/xml_node.h +0 -13
  201. data/ext/nokogiri/xml_node_set.h +0 -12
  202. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  203. data/ext/nokogiri/xml_reader.h +0 -10
  204. data/ext/nokogiri/xml_relax_ng.h +0 -9
  205. data/ext/nokogiri/xml_sax_parser.h +0 -39
  206. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  207. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  208. data/ext/nokogiri/xml_schema.h +0 -9
  209. data/ext/nokogiri/xml_syntax_error.h +0 -13
  210. data/ext/nokogiri/xml_text.h +0 -9
  211. data/ext/nokogiri/xml_xpath_context.h +0 -10
  212. data/ext/nokogiri/xslt_stylesheet.h +0 -14
  213. data/lib/nokogiri/html/sax/parser_context.rb +0 -16
  214. data/patches/libxml2/0001-Revert-Do-not-URI-escape-in-server-side-includes.patch +0 -78
  215. data/patches/libxml2/0005-Fix-infinite-loop-in-xmlStringLenDecodeEntities.patch +0 -32
  216. data/ports/archives/libxml2-2.9.10.tar.gz +0 -0
@@ -1,17 +1,22 @@
1
- #include <xml_encoding_handler.h>
1
+ #include <nokogiri.h>
2
+
3
+ VALUE cNokogiriEncodingHandler;
4
+
2
5
 
3
6
  /*
4
7
  * call-seq: Nokogiri::EncodingHandler.[](name)
5
8
  *
6
9
  * Get the encoding handler for +name+
7
10
  */
8
- static VALUE get(VALUE klass, VALUE key)
11
+ static VALUE
12
+ get(VALUE klass, VALUE key)
9
13
  {
10
14
  xmlCharEncodingHandlerPtr handler;
11
15
 
12
16
  handler = xmlFindCharEncodingHandler(StringValueCStr(key));
13
- if(handler)
17
+ if (handler) {
14
18
  return Data_Wrap_Struct(klass, NULL, NULL, handler);
19
+ }
15
20
 
16
21
  return Qnil;
17
22
  }
@@ -21,9 +26,10 @@ static VALUE get(VALUE klass, VALUE key)
21
26
  *
22
27
  * Delete the encoding alias named +name+
23
28
  */
24
- static VALUE delete(VALUE klass, VALUE name)
29
+ static VALUE
30
+ delete (VALUE klass, VALUE name)
25
31
  {
26
- if(xmlDelEncodingAlias(StringValueCStr(name))) return Qnil;
32
+ if (xmlDelEncodingAlias(StringValueCStr(name))) { return Qnil; }
27
33
 
28
34
  return Qtrue;
29
35
  }
@@ -33,7 +39,8 @@ static VALUE delete(VALUE klass, VALUE name)
33
39
  *
34
40
  * Alias encoding handler with name +from+ to name +to+
35
41
  */
36
- static VALUE alias(VALUE klass, VALUE from, VALUE to)
42
+ static VALUE
43
+ alias(VALUE klass, VALUE from, VALUE to)
37
44
  {
38
45
  xmlAddEncodingAlias(StringValueCStr(from), StringValueCStr(to));
39
46
 
@@ -45,7 +52,8 @@ static VALUE alias(VALUE klass, VALUE from, VALUE to)
45
52
  *
46
53
  * Remove all encoding aliases.
47
54
  */
48
- static VALUE clear_aliases(VALUE klass)
55
+ static VALUE
56
+ clear_aliases(VALUE klass)
49
57
  {
50
58
  xmlCleanupEncodingAliases();
51
59
 
@@ -57,7 +65,8 @@ static VALUE clear_aliases(VALUE klass)
57
65
  *
58
66
  * Get the name of this EncodingHandler
59
67
  */
60
- static VALUE name(VALUE self)
68
+ static VALUE
69
+ name(VALUE self)
61
70
  {
62
71
  xmlCharEncodingHandlerPtr handler;
63
72
 
@@ -66,14 +75,16 @@ static VALUE name(VALUE self)
66
75
  return NOKOGIRI_STR_NEW2(handler->name);
67
76
  }
68
77
 
69
- void init_xml_encoding_handler()
78
+ void
79
+ noko_init_xml_encoding_handler()
70
80
  {
71
- VALUE nokogiri = rb_define_module("Nokogiri");
72
- VALUE klass = rb_define_class_under(nokogiri, "EncodingHandler", rb_cObject);
73
-
74
- rb_define_singleton_method(klass, "[]", get, 1);
75
- rb_define_singleton_method(klass, "delete", delete, 1);
76
- rb_define_singleton_method(klass, "alias", alias, 2);
77
- rb_define_singleton_method(klass, "clear_aliases!", clear_aliases, 0);
78
- rb_define_method(klass, "name", name, 0);
81
+ cNokogiriEncodingHandler = rb_define_class_under(mNokogiri, "EncodingHandler", rb_cObject);
82
+
83
+ rb_undef_alloc_func(cNokogiriEncodingHandler);
84
+
85
+ rb_define_singleton_method(cNokogiriEncodingHandler, "[]", get, 1);
86
+ rb_define_singleton_method(cNokogiriEncodingHandler, "delete", delete, 1);
87
+ rb_define_singleton_method(cNokogiriEncodingHandler, "alias", alias, 2);
88
+ rb_define_singleton_method(cNokogiriEncodingHandler, "clear_aliases!", clear_aliases, 0);
89
+ rb_define_method(cNokogiriEncodingHandler, "name", name, 0);
79
90
  }
@@ -1,4 +1,6 @@
1
- #include <xml_entity_decl.h>
1
+ #include <nokogiri.h>
2
+
3
+ VALUE cNokogiriXmlEntityDecl;
2
4
 
3
5
  /*
4
6
  * call-seq:
@@ -6,12 +8,13 @@
6
8
  *
7
9
  * Get the original_content before ref substitution
8
10
  */
9
- static VALUE original_content(VALUE self)
11
+ static VALUE
12
+ original_content(VALUE self)
10
13
  {
11
14
  xmlEntityPtr node;
12
15
  Data_Get_Struct(self, xmlEntity, node);
13
16
 
14
- if(!node->orig) return Qnil;
17
+ if (!node->orig) { return Qnil; }
15
18
 
16
19
  return NOKOGIRI_STR_NEW2(node->orig);
17
20
  }
@@ -22,12 +25,13 @@ static VALUE original_content(VALUE self)
22
25
  *
23
26
  * Get the content
24
27
  */
25
- static VALUE get_content(VALUE self)
28
+ static VALUE
29
+ get_content(VALUE self)
26
30
  {
27
31
  xmlEntityPtr node;
28
32
  Data_Get_Struct(self, xmlEntity, node);
29
33
 
30
- if(!node->content) return Qnil;
34
+ if (!node->content) { return Qnil; }
31
35
 
32
36
  return NOKOGIRI_STR_NEW(node->content, node->length);
33
37
  }
@@ -38,7 +42,8 @@ static VALUE get_content(VALUE self)
38
42
  *
39
43
  * Get the entity type
40
44
  */
41
- static VALUE entity_type(VALUE self)
45
+ static VALUE
46
+ entity_type(VALUE self)
42
47
  {
43
48
  xmlEntityPtr node;
44
49
  Data_Get_Struct(self, xmlEntity, node);
@@ -52,12 +57,13 @@ static VALUE entity_type(VALUE self)
52
57
  *
53
58
  * Get the external identifier for PUBLIC
54
59
  */
55
- static VALUE external_id(VALUE self)
60
+ static VALUE
61
+ external_id(VALUE self)
56
62
  {
57
63
  xmlEntityPtr node;
58
64
  Data_Get_Struct(self, xmlEntity, node);
59
65
 
60
- if(!node->ExternalID) return Qnil;
66
+ if (!node->ExternalID) { return Qnil; }
61
67
 
62
68
  return NOKOGIRI_STR_NEW2(node->ExternalID);
63
69
  }
@@ -68,43 +74,39 @@ static VALUE external_id(VALUE self)
68
74
  *
69
75
  * Get the URI for a SYSTEM or PUBLIC Entity
70
76
  */
71
- static VALUE system_id(VALUE self)
77
+ static VALUE
78
+ system_id(VALUE self)
72
79
  {
73
80
  xmlEntityPtr node;
74
81
  Data_Get_Struct(self, xmlEntity, node);
75
82
 
76
- if(!node->SystemID) return Qnil;
83
+ if (!node->SystemID) { return Qnil; }
77
84
 
78
85
  return NOKOGIRI_STR_NEW2(node->SystemID);
79
86
  }
80
87
 
81
- VALUE cNokogiriXmlEntityDecl;
82
-
83
- void init_xml_entity_decl()
88
+ void
89
+ noko_init_xml_entity_decl()
84
90
  {
85
- VALUE nokogiri = rb_define_module("Nokogiri");
86
- VALUE xml = rb_define_module_under(nokogiri, "XML");
87
- VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
88
- VALUE klass = rb_define_class_under(xml, "EntityDecl", node);
89
-
90
- cNokogiriXmlEntityDecl = klass;
91
+ assert(cNokogiriXmlNode);
92
+ cNokogiriXmlEntityDecl = rb_define_class_under(mNokogiriXml, "EntityDecl", cNokogiriXmlNode);
91
93
 
92
- rb_define_method(klass, "original_content", original_content, 0);
93
- rb_define_method(klass, "content", get_content, 0);
94
- rb_define_method(klass, "entity_type", entity_type, 0);
95
- rb_define_method(klass, "external_id", external_id, 0);
96
- rb_define_method(klass, "system_id", system_id, 0);
94
+ rb_define_method(cNokogiriXmlEntityDecl, "original_content", original_content, 0);
95
+ rb_define_method(cNokogiriXmlEntityDecl, "content", get_content, 0);
96
+ rb_define_method(cNokogiriXmlEntityDecl, "entity_type", entity_type, 0);
97
+ rb_define_method(cNokogiriXmlEntityDecl, "external_id", external_id, 0);
98
+ rb_define_method(cNokogiriXmlEntityDecl, "system_id", system_id, 0);
97
99
 
98
100
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("INTERNAL_GENERAL"),
99
- INT2NUM(XML_INTERNAL_GENERAL_ENTITY));
101
+ INT2NUM(XML_INTERNAL_GENERAL_ENTITY));
100
102
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("EXTERNAL_GENERAL_PARSED"),
101
- INT2NUM(XML_EXTERNAL_GENERAL_PARSED_ENTITY));
103
+ INT2NUM(XML_EXTERNAL_GENERAL_PARSED_ENTITY));
102
104
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("EXTERNAL_GENERAL_UNPARSED"),
103
- INT2NUM(XML_EXTERNAL_GENERAL_UNPARSED_ENTITY));
105
+ INT2NUM(XML_EXTERNAL_GENERAL_UNPARSED_ENTITY));
104
106
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("INTERNAL_PARAMETER"),
105
- INT2NUM(XML_INTERNAL_PARAMETER_ENTITY));
107
+ INT2NUM(XML_INTERNAL_PARAMETER_ENTITY));
106
108
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("EXTERNAL_PARAMETER"),
107
- INT2NUM(XML_EXTERNAL_PARAMETER_ENTITY));
109
+ INT2NUM(XML_EXTERNAL_PARAMETER_ENTITY));
108
110
  rb_const_set(cNokogiriXmlEntityDecl, rb_intern("INTERNAL_PREDEFINED"),
109
- INT2NUM(XML_INTERNAL_PREDEFINED_ENTITY));
111
+ INT2NUM(XML_INTERNAL_PREDEFINED_ENTITY));
110
112
  }
@@ -1,4 +1,6 @@
1
- #include <xml_entity_reference.h>
1
+ #include <nokogiri.h>
2
+
3
+ VALUE cNokogiriXmlEntityReference;
2
4
 
3
5
  /*
4
6
  * call-seq:
@@ -6,7 +8,8 @@
6
8
  *
7
9
  * Create a new EntityReference element on the +document+ with +name+
8
10
  */
9
- static VALUE new(int argc, VALUE *argv, VALUE klass)
11
+ static VALUE
12
+ new (int argc, VALUE *argv, VALUE klass)
10
13
  {
11
14
  xmlDocPtr xml_doc;
12
15
  xmlNodePtr node;
@@ -20,33 +23,28 @@ static VALUE new(int argc, VALUE *argv, VALUE klass)
20
23
  Data_Get_Struct(document, xmlDoc, xml_doc);
21
24
 
22
25
  node = xmlNewReference(
23
- xml_doc,
24
- (const xmlChar *)StringValueCStr(name)
25
- );
26
+ xml_doc,
27
+ (const xmlChar *)StringValueCStr(name)
28
+ );
26
29
 
27
- nokogiri_root_node(node);
30
+ noko_xml_document_pin_node(node);
28
31
 
29
- rb_node = Nokogiri_wrap_xml_node(klass, node);
32
+ rb_node = noko_xml_node_wrap(klass, node);
30
33
  rb_obj_call_init(rb_node, argc, argv);
31
34
 
32
- if(rb_block_given_p()) rb_yield(rb_node);
35
+ if (rb_block_given_p()) { rb_yield(rb_node); }
33
36
 
34
37
  return rb_node;
35
38
  }
36
39
 
37
- VALUE cNokogiriXmlEntityReference;
38
- void init_xml_entity_reference()
40
+ void
41
+ noko_init_xml_entity_reference()
39
42
  {
40
- VALUE nokogiri = rb_define_module("Nokogiri");
41
- VALUE xml = rb_define_module_under(nokogiri, "XML");
42
- VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
43
-
43
+ assert(cNokogiriXmlNode);
44
44
  /*
45
45
  * EntityReference represents an EntityReference node in an xml document.
46
46
  */
47
- VALUE klass = rb_define_class_under(xml, "EntityReference", node);
48
-
49
- cNokogiriXmlEntityReference = klass;
47
+ cNokogiriXmlEntityReference = rb_define_class_under(mNokogiriXml, "EntityReference", cNokogiriXmlNode);
50
48
 
51
- rb_define_singleton_method(klass, "new", new, -1);
49
+ rb_define_singleton_method(cNokogiriXmlEntityReference, "new", new, -1);
52
50
  }
@@ -1,16 +1,35 @@
1
- #include <xml_namespace.h>
1
+ #include <nokogiri.h>
2
+
3
+ /*
4
+ * The lifecycle of a Namespace node is more complicated than other Nodes, for two reasons:
5
+ *
6
+ * 1. the underlying C structure has a different layout than all the other node structs, with the
7
+ * `_private` member where we store a pointer to Ruby object data not being in first position.
8
+ * 2. xmlNs structures returned in an xmlNodeset from an XPath query are copies of the document's
9
+ * namespaces, and so do not share the same memory lifecycle as everything else in a document.
10
+ *
11
+ * As a result of 1, you may see special handling of XML_NAMESPACE_DECL node types throughout the
12
+ * Nokogiri C code, though I intend to wrap up that logic in ruby_object_{get,set} functions
13
+ * shortly.
14
+ *
15
+ * As a result of 2, you will see we have special handling in this file and in xml_node_set.c to
16
+ * carefully manage the memory lifecycle of xmlNs structs to match the Ruby object's GC
17
+ * lifecycle. In xml_node_set.c we have local versions of xmlXPathNodeSetDel() and
18
+ * xmlXPathFreeNodeSet() that avoid freeing xmlNs structs in the node set. In this file, we decide
19
+ * whether or not to call dealloc_namespace() depending on whether the xmlNs struct appears to be
20
+ * in an xmlNodeSet (and thus the result of an XPath query) or not.
21
+ *
22
+ * Yes, this is madness.
23
+ */
2
24
 
3
25
  VALUE cNokogiriXmlNamespace ;
4
26
 
5
- static void dealloc_namespace(xmlNsPtr ns)
27
+ static void
28
+ dealloc_namespace(xmlNsPtr ns)
6
29
  {
7
30
  /*
8
- *
9
31
  * this deallocator is only used for namespace nodes that are part of an xpath
10
- * node set.
11
- *
12
- * see Nokogiri_wrap_xml_namespace() for more details.
13
- *
32
+ * node set. see noko_xml_namespace_wrap().
14
33
  */
15
34
  NOKOGIRI_DEBUG_START(ns) ;
16
35
  if (ns->href) {
@@ -30,12 +49,13 @@ static void dealloc_namespace(xmlNsPtr ns)
30
49
  *
31
50
  * Get the prefix for this namespace. Returns +nil+ if there is no prefix.
32
51
  */
33
- static VALUE prefix(VALUE self)
52
+ static VALUE
53
+ prefix(VALUE self)
34
54
  {
35
55
  xmlNsPtr ns;
36
56
 
37
57
  Data_Get_Struct(self, xmlNs, ns);
38
- if(!ns->prefix) return Qnil;
58
+ if (!ns->prefix) { return Qnil; }
39
59
 
40
60
  return NOKOGIRI_STR_NEW2(ns->prefix);
41
61
  }
@@ -46,66 +66,55 @@ static VALUE prefix(VALUE self)
46
66
  *
47
67
  * Get the href for this namespace
48
68
  */
49
- static VALUE href(VALUE self)
69
+ static VALUE
70
+ href(VALUE self)
50
71
  {
51
72
  xmlNsPtr ns;
52
73
 
53
74
  Data_Get_Struct(self, xmlNs, ns);
54
- if(!ns->href) return Qnil;
75
+ if (!ns->href) { return Qnil; }
55
76
 
56
77
  return NOKOGIRI_STR_NEW2(ns->href);
57
78
  }
58
79
 
59
- static int part_of_an_xpath_node_set_eh(xmlNsPtr node)
80
+ VALUE
81
+ noko_xml_namespace_wrap(xmlNsPtr c_namespace, xmlDocPtr c_document)
60
82
  {
61
- return (node->next && ! NOKOGIRI_NAMESPACE_EH(node->next));
62
- }
83
+ VALUE rb_namespace;
63
84
 
64
- VALUE Nokogiri_wrap_xml_namespace(xmlDocPtr doc, xmlNsPtr node)
65
- {
66
- VALUE ns = 0, document, node_cache;
67
-
68
- assert(doc->type == XML_DOCUMENT_NODE || doc->type == XML_HTML_DOCUMENT_NODE);
69
-
70
- if (node->_private) return (VALUE)node->_private;
71
-
72
- if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;
85
+ if (c_namespace->_private) {
86
+ return (VALUE)c_namespace->_private;
87
+ }
73
88
 
74
- if (DOC_RUBY_OBJECT_TEST(doc)) {
75
- document = DOC_RUBY_OBJECT(doc);
89
+ if (c_document) {
90
+ rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, c_namespace);
76
91
 
77
- if (part_of_an_xpath_node_set_eh(node)) {
78
- /*
79
- * this is a duplicate returned as part of an xpath query node set, and so
80
- * we need to make sure we manage this memory.
81
- *
82
- * see comments in xml_node_set.c for more details.
83
- */
84
- ns = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, dealloc_namespace, node);
85
- } else {
86
- ns = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, node);
87
- node_cache = rb_iv_get(document, "@node_cache");
88
- rb_ary_push(node_cache, ns);
92
+ if (DOC_RUBY_OBJECT_TEST(c_document)) {
93
+ rb_iv_set(rb_namespace, "@document", DOC_RUBY_OBJECT(c_document));
94
+ rb_ary_push(DOC_NODE_CACHE(c_document), rb_namespace);
89
95
  }
90
-
91
- rb_iv_set(ns, "@document", document);
92
96
  } else {
93
- ns = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, node);
97
+ rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, dealloc_namespace, c_namespace);
94
98
  }
95
99
 
96
- node->_private = (void *)ns;
100
+ c_namespace->_private = (void *)rb_namespace;
101
+
102
+ return rb_namespace;
103
+ }
97
104
 
98
- return ns;
105
+ VALUE
106
+ noko_xml_namespace_wrap_xpath_copy(xmlNsPtr c_namespace)
107
+ {
108
+ return noko_xml_namespace_wrap(c_namespace, NULL);
99
109
  }
100
110
 
101
- void init_xml_namespace()
111
+ void
112
+ noko_init_xml_namespace()
102
113
  {
103
- VALUE nokogiri = rb_define_module("Nokogiri");
104
- VALUE xml = rb_define_module_under(nokogiri, "XML");
105
- VALUE klass = rb_define_class_under(xml, "Namespace", rb_cObject);
114
+ cNokogiriXmlNamespace = rb_define_class_under(mNokogiriXml, "Namespace", rb_cObject);
106
115
 
107
- cNokogiriXmlNamespace = klass;
116
+ rb_undef_alloc_func(cNokogiriXmlNamespace);
108
117
 
109
- rb_define_method(klass, "prefix", prefix, 0);
110
- rb_define_method(klass, "href", href, 0);
118
+ rb_define_method(cNokogiriXmlNamespace, "prefix", prefix, 0);
119
+ rb_define_method(cNokogiriXmlNamespace, "href", href, 0);
111
120
  }
@@ -1,9 +1,12 @@
1
- #include <xml_node.h>
1
+ #include <nokogiri.h>
2
2
 
3
- static ID decorate, decorate_bang;
3
+ VALUE cNokogiriXmlNode ;
4
+
5
+ static ID id_decorate, id_decorate_bang;
4
6
 
5
7
  #ifdef DEBUG
6
- static void debug_node_dealloc(xmlNodePtr x)
8
+ static void
9
+ debug_node_dealloc(xmlNodePtr x)
7
10
  {
8
11
  NOKOGIRI_DEBUG_START(x)
9
12
  NOKOGIRI_DEBUG_END(x)
@@ -12,25 +15,28 @@ static void debug_node_dealloc(xmlNodePtr x)
12
15
  # define debug_node_dealloc 0
13
16
  #endif
14
17
 
15
- static void mark(xmlNodePtr node)
18
+ static void
19
+ mark(xmlNodePtr node)
16
20
  {
17
21
  xmlDocPtr doc = node->doc;
18
- if(doc->type == XML_DOCUMENT_NODE || doc->type == XML_HTML_DOCUMENT_NODE) {
19
- if(DOC_RUBY_OBJECT_TEST(doc)) {
22
+ if (doc->type == XML_DOCUMENT_NODE || doc->type == XML_HTML_DOCUMENT_NODE) {
23
+ if (DOC_RUBY_OBJECT_TEST(doc)) {
20
24
  rb_gc_mark(DOC_RUBY_OBJECT(doc));
21
25
  }
22
- } else if(node->doc->_private) {
26
+ } else if (node->doc->_private) {
23
27
  rb_gc_mark((VALUE)doc->_private);
24
28
  }
25
29
  }
26
30
 
27
31
  /* :nodoc: */
28
- typedef xmlNodePtr (*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);
32
+ typedef xmlNodePtr(*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);
29
33
 
30
34
  /* :nodoc: */
31
- static void relink_namespace(xmlNodePtr reparented)
35
+ static void
36
+ relink_namespace(xmlNodePtr reparented)
32
37
  {
33
38
  xmlNodePtr child;
39
+ xmlAttrPtr attr;
34
40
 
35
41
  if (reparented->type != XML_ATTRIBUTE_NODE &&
36
42
  reparented->type != XML_ELEMENT_NODE) { return; }
@@ -42,7 +48,7 @@ static void relink_namespace(xmlNodePtr reparented)
42
48
  name = xmlSplitQName2(reparented->name, &prefix);
43
49
 
44
50
  if (reparented->type == XML_ATTRIBUTE_NODE) {
45
- if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) {
51
+ if (prefix == NULL || strcmp((char *)prefix, XMLNS_PREFIX) == 0) {
46
52
  xmlFree(name);
47
53
  xmlFree(prefix);
48
54
  return;
@@ -63,11 +69,6 @@ static void relink_namespace(xmlNodePtr reparented)
63
69
  /* Avoid segv when relinking against unlinked nodes. */
64
70
  if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) { return; }
65
71
 
66
- /* Make sure that our reparented node has the correct namespaces */
67
- if (!reparented->ns && reparented->doc != (xmlDocPtr)reparented->parent) {
68
- xmlSetNs(reparented, reparented->parent->ns);
69
- }
70
-
71
72
  /* Search our parents for an existing definition */
72
73
  if (reparented->nsDef) {
73
74
  xmlNsPtr curr = reparented->nsDef;
@@ -87,7 +88,7 @@ static void relink_namespace(xmlNodePtr reparented)
87
88
  } else {
88
89
  reparented->nsDef = curr->next;
89
90
  }
90
- nokogiri_root_nsdef(curr, reparented->doc);
91
+ noko_xml_document_pin_namespace(curr, reparented->doc);
91
92
  } else {
92
93
  prev = curr;
93
94
  }
@@ -127,16 +128,17 @@ static void relink_namespace(xmlNodePtr reparented)
127
128
  }
128
129
 
129
130
  if (reparented->type == XML_ELEMENT_NODE) {
130
- child = (xmlNodePtr)((xmlElementPtr)reparented)->attributes;
131
- while(NULL != child) {
132
- relink_namespace(child);
133
- child = child->next;
131
+ attr = reparented->properties;
132
+ while (NULL != attr) {
133
+ relink_namespace((xmlNodePtr)attr);
134
+ attr = attr->next;
134
135
  }
135
136
  }
136
137
  }
137
138
 
138
139
  /* :nodoc: */
139
- static xmlNodePtr xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
140
+ static xmlNodePtr
141
+ xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
140
142
  {
141
143
  xmlNodePtr retval ;
142
144
 
@@ -160,16 +162,17 @@ static xmlNodePtr xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
160
162
  }
161
163
 
162
164
  /* :nodoc: */
163
- static VALUE reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func prf)
165
+ static VALUE
166
+ reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func prf)
164
167
  {
165
168
  VALUE reparented_obj ;
166
- xmlNodePtr reparentee, pivot, reparented, next_text, new_next_text, parent ;
169
+ xmlNodePtr reparentee, original_reparentee, pivot, reparented, next_text, new_next_text, parent ;
167
170
  int original_ns_prefix_is_default = 0 ;
168
171
 
169
- if(!rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlNode)) {
172
+ if (!rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlNode)) {
170
173
  rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
171
174
  }
172
- if(rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlDocument)) {
175
+ if (rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlDocument)) {
173
176
  rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
174
177
  }
175
178
 
@@ -190,66 +193,66 @@ static VALUE reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_rep
190
193
 
191
194
  if (parent) {
192
195
  switch (parent->type) {
193
- case XML_DOCUMENT_NODE:
194
- case XML_HTML_DOCUMENT_NODE:
195
- switch (reparentee->type) {
196
- case XML_ELEMENT_NODE:
197
- case XML_PI_NODE:
198
- case XML_COMMENT_NODE:
199
- case XML_DOCUMENT_TYPE_NODE:
200
- /*
201
- * The DOM specification says no to adding text-like nodes
202
- * directly to a document, but we allow it for compatibility.
203
- */
204
- case XML_TEXT_NODE:
205
- case XML_CDATA_SECTION_NODE:
206
- case XML_ENTITY_REF_NODE:
207
- goto ok;
208
- default:
196
+ case XML_DOCUMENT_NODE:
197
+ case XML_HTML_DOCUMENT_NODE:
198
+ switch (reparentee->type) {
199
+ case XML_ELEMENT_NODE:
200
+ case XML_PI_NODE:
201
+ case XML_COMMENT_NODE:
202
+ case XML_DOCUMENT_TYPE_NODE:
203
+ /*
204
+ * The DOM specification says no to adding text-like nodes
205
+ * directly to a document, but we allow it for compatibility.
206
+ */
207
+ case XML_TEXT_NODE:
208
+ case XML_CDATA_SECTION_NODE:
209
+ case XML_ENTITY_REF_NODE:
210
+ goto ok;
211
+ default:
212
+ break;
213
+ }
209
214
  break;
210
- }
211
- break;
212
- case XML_DOCUMENT_FRAG_NODE:
213
- case XML_ENTITY_REF_NODE:
214
- case XML_ELEMENT_NODE:
215
- switch (reparentee->type) {
216
- case XML_ELEMENT_NODE:
217
- case XML_PI_NODE:
218
- case XML_COMMENT_NODE:
219
- case XML_TEXT_NODE:
220
- case XML_CDATA_SECTION_NODE:
215
+ case XML_DOCUMENT_FRAG_NODE:
221
216
  case XML_ENTITY_REF_NODE:
222
- goto ok;
223
- default:
217
+ case XML_ELEMENT_NODE:
218
+ switch (reparentee->type) {
219
+ case XML_ELEMENT_NODE:
220
+ case XML_PI_NODE:
221
+ case XML_COMMENT_NODE:
222
+ case XML_TEXT_NODE:
223
+ case XML_CDATA_SECTION_NODE:
224
+ case XML_ENTITY_REF_NODE:
225
+ goto ok;
226
+ default:
227
+ break;
228
+ }
229
+ break;
230
+ case XML_ATTRIBUTE_NODE:
231
+ switch (reparentee->type) {
232
+ case XML_TEXT_NODE:
233
+ case XML_ENTITY_REF_NODE:
234
+ goto ok;
235
+ default:
236
+ break;
237
+ }
224
238
  break;
225
- }
226
- break;
227
- case XML_ATTRIBUTE_NODE:
228
- switch (reparentee->type) {
229
239
  case XML_TEXT_NODE:
230
- case XML_ENTITY_REF_NODE:
231
- goto ok;
240
+ /*
241
+ * xmlAddChild() breaks the DOM specification in that it allows
242
+ * adding a text node to another, in which case text nodes are
243
+ * coalesced, but since our JRuby version does not support such
244
+ * operation, we should inhibit it.
245
+ */
246
+ break;
232
247
  default:
233
248
  break;
234
- }
235
- break;
236
- case XML_TEXT_NODE:
237
- /*
238
- * xmlAddChild() breaks the DOM specification in that it allows
239
- * adding a text node to another, in which case text nodes are
240
- * coalesced, but since our JRuby version does not support such
241
- * operation, we should inhibit it.
242
- */
243
- break;
244
- default:
245
- break;
246
249
  }
247
250
 
248
251
  rb_raise(rb_eArgError, "cannot reparent %s there", rb_obj_classname(reparentee_obj));
249
252
  }
250
253
 
251
254
  ok:
252
- xmlUnlinkNode(reparentee);
255
+ original_reparentee = reparentee;
253
256
 
254
257
  if (reparentee->doc != pivot->doc || reparentee->type == XML_TEXT_NODE) {
255
258
  /*
@@ -290,7 +293,7 @@ ok:
290
293
  original_ns_prefix_is_default = 1;
291
294
  }
292
295
 
293
- nokogiri_root_node(reparentee);
296
+ noko_xml_document_pin_node(reparentee);
294
297
 
295
298
  if (!(reparentee = xmlDocCopyNode(reparentee, pivot->doc, 1))) {
296
299
  rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
@@ -301,11 +304,13 @@ ok:
301
304
  * issue #391, where new node's prefix may become the string "default"
302
305
  * see libxml2 tree.c xmlNewReconciliedNs which implements this behavior.
303
306
  */
304
- xmlFree(reparentee->ns->prefix);
307
+ xmlFree((xmlChar *)reparentee->ns->prefix);
305
308
  reparentee->ns->prefix = NULL;
306
309
  }
307
310
  }
308
311
 
312
+ xmlUnlinkNode(original_reparentee);
313
+
309
314
  if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling
310
315
  && reparentee->type == XML_TEXT_NODE && pivot->next && pivot->next->type == XML_TEXT_NODE) {
311
316
  /*
@@ -330,12 +335,12 @@ ok:
330
335
  new_next_text = xmlDocCopyNode(next_text, pivot->doc, 1) ;
331
336
 
332
337
  xmlUnlinkNode(next_text);
333
- nokogiri_root_node(next_text);
338
+ noko_xml_document_pin_node(next_text);
334
339
 
335
340
  xmlAddNextSibling(pivot, new_next_text);
336
341
  }
337
342
 
338
- if(!(reparented = (*prf)(pivot, reparentee))) {
343
+ if (!(reparented = (*prf)(pivot, reparentee))) {
339
344
  rb_raise(rb_eRuntimeError, "Could not reparent node");
340
345
  }
341
346
 
@@ -348,9 +353,9 @@ ok:
348
353
 
349
354
  relink_namespace(reparented);
350
355
 
351
- reparented_obj = Nokogiri_wrap_xml_node(Qnil, reparented);
356
+ reparented_obj = noko_xml_node_wrap(Qnil, reparented);
352
357
 
353
- rb_funcall(reparented_obj, decorate_bang, 0);
358
+ rb_funcall(reparented_obj, id_decorate_bang, 0);
354
359
 
355
360
  return reparented_obj ;
356
361
  }
@@ -362,7 +367,8 @@ ok:
362
367
  *
363
368
  * Get the document for this Node
364
369
  */
365
- static VALUE document(VALUE self)
370
+ static VALUE
371
+ document(VALUE self)
366
372
  {
367
373
  xmlNodePtr node;
368
374
  Data_Get_Struct(self, xmlNode, node);
@@ -375,7 +381,8 @@ static VALUE document(VALUE self)
375
381
  *
376
382
  * Get the internal pointer number
377
383
  */
378
- static VALUE pointer_id(VALUE self)
384
+ static VALUE
385
+ pointer_id(VALUE self)
379
386
  {
380
387
  xmlNodePtr node;
381
388
  Data_Get_Struct(self, xmlNode, node);
@@ -389,7 +396,8 @@ static VALUE pointer_id(VALUE self)
389
396
  *
390
397
  * Encode any special characters in +string+
391
398
  */
392
- static VALUE encode_special_chars(VALUE self, VALUE string)
399
+ static VALUE
400
+ encode_special_chars(VALUE self, VALUE string)
393
401
  {
394
402
  xmlNodePtr node;
395
403
  xmlChar *encoded;
@@ -419,7 +427,8 @@ static VALUE encode_special_chars(VALUE self, VALUE string)
419
427
  * doc.create_internal_subset("chapter", nil, "chapter.dtd")
420
428
  * # => <!DOCTYPE chapter SYSTEM "chapter.dtd">
421
429
  */
422
- static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
430
+ static VALUE
431
+ create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
423
432
  {
424
433
  xmlNodePtr node;
425
434
  xmlDocPtr doc;
@@ -429,7 +438,7 @@ static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, V
429
438
 
430
439
  doc = node->doc;
431
440
 
432
- if(xmlGetIntSubset(doc)) {
441
+ if (xmlGetIntSubset(doc)) {
433
442
  rb_raise(rb_eRuntimeError, "Document already has an internal subset");
434
443
  }
435
444
 
@@ -440,9 +449,9 @@ static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, V
440
449
  NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
441
450
  );
442
451
 
443
- if(!dtd) { return Qnil; }
452
+ if (!dtd) { return Qnil; }
444
453
 
445
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
454
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
446
455
  }
447
456
 
448
457
  /*
@@ -451,7 +460,8 @@ static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, V
451
460
  *
452
461
  * Create an external subset
453
462
  */
454
- static VALUE create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
463
+ static VALUE
464
+ create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
455
465
  {
456
466
  xmlNodePtr node;
457
467
  xmlDocPtr doc;
@@ -461,7 +471,7 @@ static VALUE create_external_subset(VALUE self, VALUE name, VALUE external_id, V
461
471
 
462
472
  doc = node->doc;
463
473
 
464
- if(doc->extSubset) {
474
+ if (doc->extSubset) {
465
475
  rb_raise(rb_eRuntimeError, "Document already has an external subset");
466
476
  }
467
477
 
@@ -472,9 +482,9 @@ static VALUE create_external_subset(VALUE self, VALUE name, VALUE external_id, V
472
482
  NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
473
483
  );
474
484
 
475
- if(!dtd) { return Qnil; }
485
+ if (!dtd) { return Qnil; }
476
486
 
477
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
487
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
478
488
  }
479
489
 
480
490
  /*
@@ -483,7 +493,8 @@ static VALUE create_external_subset(VALUE self, VALUE name, VALUE external_id, V
483
493
  *
484
494
  * Get the external subset
485
495
  */
486
- static VALUE external_subset(VALUE self)
496
+ static VALUE
497
+ external_subset(VALUE self)
487
498
  {
488
499
  xmlNodePtr node;
489
500
  xmlDocPtr doc;
@@ -491,14 +502,14 @@ static VALUE external_subset(VALUE self)
491
502
 
492
503
  Data_Get_Struct(self, xmlNode, node);
493
504
 
494
- if(!node->doc) { return Qnil; }
505
+ if (!node->doc) { return Qnil; }
495
506
 
496
507
  doc = node->doc;
497
508
  dtd = doc->extSubset;
498
509
 
499
- if(!dtd) { return Qnil; }
510
+ if (!dtd) { return Qnil; }
500
511
 
501
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
512
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
502
513
  }
503
514
 
504
515
  /*
@@ -507,7 +518,8 @@ static VALUE external_subset(VALUE self)
507
518
  *
508
519
  * Get the internal subset
509
520
  */
510
- static VALUE internal_subset(VALUE self)
521
+ static VALUE
522
+ internal_subset(VALUE self)
511
523
  {
512
524
  xmlNodePtr node;
513
525
  xmlDocPtr doc;
@@ -515,14 +527,14 @@ static VALUE internal_subset(VALUE self)
515
527
 
516
528
  Data_Get_Struct(self, xmlNode, node);
517
529
 
518
- if(!node->doc) { return Qnil; }
530
+ if (!node->doc) { return Qnil; }
519
531
 
520
532
  doc = node->doc;
521
533
  dtd = xmlGetIntSubset(doc);
522
534
 
523
- if(!dtd) { return Qnil; }
535
+ if (!dtd) { return Qnil; }
524
536
 
525
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
537
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
526
538
  }
527
539
 
528
540
  /*
@@ -537,7 +549,8 @@ static VALUE internal_subset(VALUE self)
537
549
  * node's parent document. Defaults to the current node's document.
538
550
  * current document.
539
551
  */
540
- static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
552
+ static VALUE
553
+ duplicate_node(int argc, VALUE *argv, VALUE self)
541
554
  {
542
555
  VALUE r_level, r_new_parent_doc;
543
556
  int level;
@@ -561,11 +574,11 @@ static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
561
574
  }
562
575
 
563
576
  dup = xmlDocCopyNode(node, new_parent_doc, level);
564
- if(dup == NULL) { return Qnil; }
577
+ if (dup == NULL) { return Qnil; }
565
578
 
566
- nokogiri_root_node(dup);
579
+ noko_xml_document_pin_node(dup);
567
580
 
568
- return Nokogiri_wrap_xml_node(rb_obj_class(self), dup);
581
+ return noko_xml_node_wrap(rb_obj_class(self), dup);
569
582
  }
570
583
 
571
584
  /*
@@ -574,12 +587,13 @@ static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
574
587
  *
575
588
  * Unlink this node from its current context.
576
589
  */
577
- static VALUE unlink_node(VALUE self)
590
+ static VALUE
591
+ unlink_node(VALUE self)
578
592
  {
579
593
  xmlNodePtr node;
580
594
  Data_Get_Struct(self, xmlNode, node);
581
595
  xmlUnlinkNode(node);
582
- nokogiri_root_node(node);
596
+ noko_xml_document_pin_node(node);
583
597
  return self;
584
598
  }
585
599
 
@@ -589,7 +603,8 @@ static VALUE unlink_node(VALUE self)
589
603
  *
590
604
  * Is this node blank?
591
605
  */
592
- static VALUE blank_eh(VALUE self)
606
+ static VALUE
607
+ blank_eh(VALUE self)
593
608
  {
594
609
  xmlNodePtr node;
595
610
  Data_Get_Struct(self, xmlNode, node);
@@ -602,15 +617,16 @@ static VALUE blank_eh(VALUE self)
602
617
  *
603
618
  * Returns the next sibling node
604
619
  */
605
- static VALUE next_sibling(VALUE self)
620
+ static VALUE
621
+ next_sibling(VALUE self)
606
622
  {
607
623
  xmlNodePtr node, sibling;
608
624
  Data_Get_Struct(self, xmlNode, node);
609
625
 
610
626
  sibling = node->next;
611
- if(!sibling) { return Qnil; }
627
+ if (!sibling) { return Qnil; }
612
628
 
613
- return Nokogiri_wrap_xml_node(Qnil, sibling) ;
629
+ return noko_xml_node_wrap(Qnil, sibling) ;
614
630
  }
615
631
 
616
632
  /*
@@ -619,15 +635,16 @@ static VALUE next_sibling(VALUE self)
619
635
  *
620
636
  * Returns the previous sibling node
621
637
  */
622
- static VALUE previous_sibling(VALUE self)
638
+ static VALUE
639
+ previous_sibling(VALUE self)
623
640
  {
624
641
  xmlNodePtr node, sibling;
625
642
  Data_Get_Struct(self, xmlNode, node);
626
643
 
627
644
  sibling = node->prev;
628
- if(!sibling) { return Qnil; }
645
+ if (!sibling) { return Qnil; }
629
646
 
630
- return Nokogiri_wrap_xml_node(Qnil, sibling);
647
+ return noko_xml_node_wrap(Qnil, sibling);
631
648
  }
632
649
 
633
650
  /*
@@ -636,15 +653,16 @@ static VALUE previous_sibling(VALUE self)
636
653
  *
637
654
  * Returns the next Nokogiri::XML::Element type sibling node.
638
655
  */
639
- static VALUE next_element(VALUE self)
656
+ static VALUE
657
+ next_element(VALUE self)
640
658
  {
641
659
  xmlNodePtr node, sibling;
642
660
  Data_Get_Struct(self, xmlNode, node);
643
661
 
644
662
  sibling = xmlNextElementSibling(node);
645
- if(!sibling) { return Qnil; }
663
+ if (!sibling) { return Qnil; }
646
664
 
647
- return Nokogiri_wrap_xml_node(Qnil, sibling);
665
+ return noko_xml_node_wrap(Qnil, sibling);
648
666
  }
649
667
 
650
668
  /*
@@ -653,7 +671,8 @@ static VALUE next_element(VALUE self)
653
671
  *
654
672
  * Returns the previous Nokogiri::XML::Element type sibling node.
655
673
  */
656
- static VALUE previous_element(VALUE self)
674
+ static VALUE
675
+ previous_element(VALUE self)
657
676
  {
658
677
  xmlNodePtr node, sibling;
659
678
  Data_Get_Struct(self, xmlNode, node);
@@ -662,23 +681,24 @@ static VALUE previous_element(VALUE self)
662
681
  * note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
663
682
  */
664
683
  sibling = node->prev;
665
- if(!sibling) { return Qnil; }
684
+ if (!sibling) { return Qnil; }
666
685
 
667
- while(sibling && sibling->type != XML_ELEMENT_NODE) {
686
+ while (sibling && sibling->type != XML_ELEMENT_NODE) {
668
687
  sibling = sibling->prev;
669
688
  }
670
689
 
671
- return sibling ? Nokogiri_wrap_xml_node(Qnil, sibling) : Qnil ;
690
+ return sibling ? noko_xml_node_wrap(Qnil, sibling) : Qnil ;
672
691
  }
673
692
 
674
693
  /* :nodoc: */
675
- static VALUE replace(VALUE self, VALUE new_node)
694
+ static VALUE
695
+ replace(VALUE self, VALUE new_node)
676
696
  {
677
697
  VALUE reparent = reparent_node_with(self, new_node, xmlReplaceNodeWrapper);
678
698
 
679
699
  xmlNodePtr pivot;
680
700
  Data_Get_Struct(self, xmlNode, pivot);
681
- nokogiri_root_node(pivot);
701
+ noko_xml_document_pin_node(pivot);
682
702
 
683
703
  return reparent;
684
704
  }
@@ -689,7 +709,8 @@ static VALUE replace(VALUE self, VALUE new_node)
689
709
  *
690
710
  * Get the list of children for this node as a NodeSet
691
711
  */
692
- static VALUE children(VALUE self)
712
+ static VALUE
713
+ children(VALUE self)
693
714
  {
694
715
  xmlNodePtr node;
695
716
  xmlNodePtr child;
@@ -704,15 +725,15 @@ static VALUE children(VALUE self)
704
725
 
705
726
  document = DOC_RUBY_OBJECT(node->doc);
706
727
 
707
- if(!child) { return Nokogiri_wrap_xml_node_set(set, document); }
728
+ if (!child) { return noko_xml_node_set_wrap(set, document); }
708
729
 
709
730
  child = child->next;
710
- while(NULL != child) {
731
+ while (NULL != child) {
711
732
  xmlXPathNodeSetAddUnique(set, child);
712
733
  child = child->next;
713
734
  }
714
735
 
715
- node_set = Nokogiri_wrap_xml_node_set(set, document);
736
+ node_set = noko_xml_node_set_wrap(set, document);
716
737
 
717
738
  return node_set;
718
739
  }
@@ -728,7 +749,8 @@ static VALUE children(VALUE self)
728
749
  *
729
750
  * @doc.root.element_children.all? { |x| x.element? } # => true
730
751
  */
731
- static VALUE element_children(VALUE self)
752
+ static VALUE
753
+ element_children(VALUE self)
732
754
  {
733
755
  xmlNodePtr node;
734
756
  xmlNodePtr child;
@@ -743,15 +765,15 @@ static VALUE element_children(VALUE self)
743
765
 
744
766
  document = DOC_RUBY_OBJECT(node->doc);
745
767
 
746
- if(!child) { return Nokogiri_wrap_xml_node_set(set, document); }
768
+ if (!child) { return noko_xml_node_set_wrap(set, document); }
747
769
 
748
770
  child = xmlNextElementSibling(child);
749
- while(NULL != child) {
771
+ while (NULL != child) {
750
772
  xmlXPathNodeSetAddUnique(set, child);
751
773
  child = xmlNextElementSibling(child);
752
774
  }
753
775
 
754
- node_set = Nokogiri_wrap_xml_node_set(set, document);
776
+ node_set = noko_xml_node_set_wrap(set, document);
755
777
 
756
778
  return node_set;
757
779
  }
@@ -762,15 +784,16 @@ static VALUE element_children(VALUE self)
762
784
  *
763
785
  * Returns the child node
764
786
  */
765
- static VALUE child(VALUE self)
787
+ static VALUE
788
+ child(VALUE self)
766
789
  {
767
790
  xmlNodePtr node, child;
768
791
  Data_Get_Struct(self, xmlNode, node);
769
792
 
770
793
  child = node->children;
771
- if(!child) { return Qnil; }
794
+ if (!child) { return Qnil; }
772
795
 
773
- return Nokogiri_wrap_xml_node(Qnil, child);
796
+ return noko_xml_node_wrap(Qnil, child);
774
797
  }
775
798
 
776
799
  /*
@@ -783,15 +806,16 @@ static VALUE child(VALUE self)
783
806
  *
784
807
  * @doc.root.first_element_child.element? # => true
785
808
  */
786
- static VALUE first_element_child(VALUE self)
809
+ static VALUE
810
+ first_element_child(VALUE self)
787
811
  {
788
812
  xmlNodePtr node, child;
789
813
  Data_Get_Struct(self, xmlNode, node);
790
814
 
791
815
  child = xmlFirstElementChild(node);
792
- if(!child) { return Qnil; }
816
+ if (!child) { return Qnil; }
793
817
 
794
- return Nokogiri_wrap_xml_node(Qnil, child);
818
+ return noko_xml_node_wrap(Qnil, child);
795
819
  }
796
820
 
797
821
  /*
@@ -804,15 +828,16 @@ static VALUE first_element_child(VALUE self)
804
828
  *
805
829
  * @doc.root.last_element_child.element? # => true
806
830
  */
807
- static VALUE last_element_child(VALUE self)
831
+ static VALUE
832
+ last_element_child(VALUE self)
808
833
  {
809
834
  xmlNodePtr node, child;
810
835
  Data_Get_Struct(self, xmlNode, node);
811
836
 
812
837
  child = xmlLastElementChild(node);
813
- if(!child) { return Qnil; }
838
+ if (!child) { return Qnil; }
814
839
 
815
- return Nokogiri_wrap_xml_node(Qnil, child);
840
+ return noko_xml_node_wrap(Qnil, child);
816
841
  }
817
842
 
818
843
  /*
@@ -821,11 +846,12 @@ static VALUE last_element_child(VALUE self)
821
846
  *
822
847
  * Returns true if +attribute+ is set
823
848
  */
824
- static VALUE key_eh(VALUE self, VALUE attribute)
849
+ static VALUE
850
+ key_eh(VALUE self, VALUE attribute)
825
851
  {
826
852
  xmlNodePtr node;
827
853
  Data_Get_Struct(self, xmlNode, node);
828
- if(xmlHasProp(node, (xmlChar *)StringValueCStr(attribute))) {
854
+ if (xmlHasProp(node, (xmlChar *)StringValueCStr(attribute))) {
829
855
  return Qtrue;
830
856
  }
831
857
  return Qfalse;
@@ -837,12 +863,13 @@ static VALUE key_eh(VALUE self, VALUE attribute)
837
863
  *
838
864
  * Returns true if +attribute+ is set with +namespace+
839
865
  */
840
- static VALUE namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
866
+ static VALUE
867
+ namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
841
868
  {
842
869
  xmlNodePtr node;
843
870
  Data_Get_Struct(self, xmlNode, node);
844
- if(xmlHasNsProp(node, (xmlChar *)StringValueCStr(attribute),
845
- NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace))) {
871
+ if (xmlHasNsProp(node, (xmlChar *)StringValueCStr(attribute),
872
+ NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace))) {
846
873
  return Qtrue;
847
874
  }
848
875
  return Qfalse;
@@ -854,7 +881,8 @@ static VALUE namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
854
881
  *
855
882
  * Set the +property+ to +value+
856
883
  */
857
- static VALUE set(VALUE self, VALUE property, VALUE value)
884
+ static VALUE
885
+ set(VALUE self, VALUE property, VALUE value)
858
886
  {
859
887
  xmlNodePtr node, cur;
860
888
  xmlAttrPtr prop;
@@ -867,13 +895,13 @@ static VALUE set(VALUE self, VALUE property, VALUE value)
867
895
  * We can avoid this by unlinking these nodes first.
868
896
  */
869
897
  if (node->type != XML_ELEMENT_NODE) {
870
- return(Qnil);
898
+ return (Qnil);
871
899
  }
872
900
  prop = xmlHasProp(node, (xmlChar *)StringValueCStr(property));
873
901
  if (prop && prop->children) {
874
902
  for (cur = prop->children; cur; cur = cur->next) {
875
903
  if (cur->_private) {
876
- nokogiri_root_node(cur);
904
+ noko_xml_document_pin_node(cur);
877
905
  xmlUnlinkNode(cur);
878
906
  }
879
907
  }
@@ -891,7 +919,8 @@ static VALUE set(VALUE self, VALUE property, VALUE value)
891
919
  *
892
920
  * Get the value for +attribute+
893
921
  */
894
- static VALUE get(VALUE self, VALUE rattribute)
922
+ static VALUE
923
+ get(VALUE self, VALUE rattribute)
895
924
  {
896
925
  xmlNodePtr node;
897
926
  xmlChar *value = 0;
@@ -917,7 +946,7 @@ static VALUE get(VALUE self, VALUE rattribute)
917
946
  if (ns) {
918
947
  value = xmlGetNsProp(node, attr_name, ns->href);
919
948
  } else {
920
- value = xmlGetProp(node, (xmlChar*)StringValueCStr(rattribute));
949
+ value = xmlGetProp(node, (xmlChar *)StringValueCStr(rattribute));
921
950
  }
922
951
  } else {
923
952
  value = xmlGetNoNsProp(node, attribute);
@@ -938,14 +967,15 @@ static VALUE get(VALUE self, VALUE rattribute)
938
967
  *
939
968
  * Set the namespace to +namespace+
940
969
  */
941
- static VALUE set_namespace(VALUE self, VALUE namespace)
970
+ static VALUE
971
+ set_namespace(VALUE self, VALUE namespace)
942
972
  {
943
973
  xmlNodePtr node;
944
974
  xmlNsPtr ns = NULL;
945
975
 
946
976
  Data_Get_Struct(self, xmlNode, node);
947
977
 
948
- if(!NIL_P(namespace)) {
978
+ if (!NIL_P(namespace)) {
949
979
  Data_Get_Struct(namespace, xmlNs, ns);
950
980
  }
951
981
 
@@ -960,15 +990,16 @@ static VALUE set_namespace(VALUE self, VALUE namespace)
960
990
  *
961
991
  * Get the attribute node with +name+
962
992
  */
963
- static VALUE attr(VALUE self, VALUE name)
993
+ static VALUE
994
+ attr(VALUE self, VALUE name)
964
995
  {
965
996
  xmlNodePtr node;
966
997
  xmlAttrPtr prop;
967
998
  Data_Get_Struct(self, xmlNode, node);
968
999
  prop = xmlHasProp(node, (xmlChar *)StringValueCStr(name));
969
1000
 
970
- if(! prop) { return Qnil; }
971
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
1001
+ if (! prop) { return Qnil; }
1002
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
972
1003
  }
973
1004
 
974
1005
  /*
@@ -977,7 +1008,8 @@ static VALUE attr(VALUE self, VALUE name)
977
1008
  *
978
1009
  * Get the attribute node with +name+ and +namespace+
979
1010
  */
980
- static VALUE attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
1011
+ static VALUE
1012
+ attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
981
1013
  {
982
1014
  xmlNodePtr node;
983
1015
  xmlAttrPtr prop;
@@ -985,28 +1017,23 @@ static VALUE attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
985
1017
  prop = xmlHasNsProp(node, (xmlChar *)StringValueCStr(name),
986
1018
  NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace));
987
1019
 
988
- if(! prop) { return Qnil; }
989
- return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
1020
+ if (! prop) { return Qnil; }
1021
+ return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
990
1022
  }
991
1023
 
992
1024
  /*
993
- * call-seq:
994
- * attribute_nodes()
995
- *
996
- * returns a list containing the Node attributes.
1025
+ * @overload attribute_nodes()
1026
+ * Get the attributes for a Node
1027
+ * @return [Array<Nokogiri::XML::Attr>] containing the Node's attributes.
997
1028
  */
998
- static VALUE attribute_nodes(VALUE self)
1029
+ static VALUE
1030
+ attribute_nodes(VALUE rb_node)
999
1031
  {
1000
- /* this code in the mode of xmlHasProp() */
1001
- xmlNodePtr node;
1002
- VALUE attr;
1032
+ xmlNodePtr c_node;
1003
1033
 
1004
- Data_Get_Struct(self, xmlNode, node);
1005
-
1006
- attr = rb_ary_new();
1007
- Nokogiri_xml_node_properties(node, attr);
1034
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1008
1035
 
1009
- return attr ;
1036
+ return noko_xml_node_attrs(c_node);
1010
1037
  }
1011
1038
 
1012
1039
 
@@ -1017,16 +1044,17 @@ static VALUE attribute_nodes(VALUE self)
1017
1044
  * returns the namespace of the element or attribute node as a Namespace
1018
1045
  * object, or nil if there is no namespace for the element or attribute.
1019
1046
  */
1020
- static VALUE namespace(VALUE self)
1047
+ static VALUE
1048
+ noko_xml_node_namespace(VALUE rb_node)
1021
1049
  {
1022
- xmlNodePtr node ;
1023
- Data_Get_Struct(self, xmlNode, node);
1050
+ xmlNodePtr c_node ;
1051
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1024
1052
 
1025
- if (node->ns) {
1026
- return Nokogiri_wrap_xml_namespace(node->doc, node->ns);
1027
- }
1053
+ if (c_node->ns) {
1054
+ return noko_xml_namespace_wrap(c_node->ns, c_node->doc);
1055
+ }
1028
1056
 
1029
- return Qnil ;
1057
+ return Qnil ;
1030
1058
  }
1031
1059
 
1032
1060
  /*
@@ -1035,27 +1063,27 @@ return Qnil ;
1035
1063
  *
1036
1064
  * returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in"xmlns="), and prefixed namespaces (as in "xmlns:prefix=").
1037
1065
  */
1038
- static VALUE namespace_definitions(VALUE self)
1066
+ static VALUE
1067
+ namespace_definitions(VALUE rb_node)
1039
1068
  {
1040
1069
  /* this code in the mode of xmlHasProp() */
1041
- xmlNodePtr node ;
1042
- VALUE list;
1043
- xmlNsPtr ns;
1044
-
1045
- Data_Get_Struct(self, xmlNode, node);
1046
-
1047
- list = rb_ary_new();
1070
+ xmlNodePtr c_node ;
1071
+ xmlNsPtr c_namespace;
1072
+ VALUE definitions = rb_ary_new();
1048
1073
 
1049
- ns = node->nsDef;
1074
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1050
1075
 
1051
- if(!ns) { return list; }
1076
+ c_namespace = c_node->nsDef;
1077
+ if (!c_namespace) {
1078
+ return definitions;
1079
+ }
1052
1080
 
1053
- while(NULL != ns) {
1054
- rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns));
1055
- ns = ns->next;
1081
+ while (c_namespace != NULL) {
1082
+ rb_ary_push(definitions, noko_xml_namespace_wrap(c_namespace, c_node->doc));
1083
+ c_namespace = c_namespace->next;
1056
1084
  }
1057
1085
 
1058
- return list;
1086
+ return definitions;
1059
1087
  }
1060
1088
 
1061
1089
  /*
@@ -1067,26 +1095,27 @@ static VALUE namespace_definitions(VALUE self)
1067
1095
  * namespaces ("xmlns=" style) for self are included in this array; Default
1068
1096
  * namespaces for ancestors, however, are not. See also #namespaces
1069
1097
  */
1070
- static VALUE namespace_scopes(VALUE self)
1098
+ static VALUE
1099
+ namespace_scopes(VALUE rb_node)
1071
1100
  {
1072
- xmlNodePtr node ;
1073
- VALUE list;
1074
- xmlNsPtr *ns_list;
1101
+ xmlNodePtr c_node ;
1102
+ xmlNsPtr *namespaces;
1103
+ VALUE scopes = rb_ary_new();
1075
1104
  int j;
1076
1105
 
1077
- Data_Get_Struct(self, xmlNode, node);
1078
-
1079
- list = rb_ary_new();
1080
- ns_list = xmlGetNsList(node->doc, node);
1106
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1081
1107
 
1082
- if(!ns_list) { return list; }
1108
+ namespaces = xmlGetNsList(c_node->doc, c_node);
1109
+ if (!namespaces) {
1110
+ return scopes;
1111
+ }
1083
1112
 
1084
- for (j = 0 ; ns_list[j] != NULL ; ++j) {
1085
- rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns_list[j]));
1113
+ for (j = 0 ; namespaces[j] != NULL ; ++j) {
1114
+ rb_ary_push(scopes, noko_xml_namespace_wrap(namespaces[j], c_node->doc));
1086
1115
  }
1087
1116
 
1088
- xmlFree(ns_list);
1089
- return list;
1117
+ xmlFree(namespaces);
1118
+ return scopes;
1090
1119
  }
1091
1120
 
1092
1121
  /*
@@ -1095,7 +1124,8 @@ static VALUE namespace_scopes(VALUE self)
1095
1124
  *
1096
1125
  * Get the type for this Node
1097
1126
  */
1098
- static VALUE node_type(VALUE self)
1127
+ static VALUE
1128
+ node_type(VALUE self)
1099
1129
  {
1100
1130
  xmlNodePtr node;
1101
1131
  Data_Get_Struct(self, xmlNode, node);
@@ -1108,7 +1138,8 @@ static VALUE node_type(VALUE self)
1108
1138
  *
1109
1139
  * Set the content for this Node
1110
1140
  */
1111
- static VALUE set_native_content(VALUE self, VALUE content)
1141
+ static VALUE
1142
+ set_native_content(VALUE self, VALUE content)
1112
1143
  {
1113
1144
  xmlNodePtr node, child, next ;
1114
1145
  Data_Get_Struct(self, xmlNode, node);
@@ -1117,7 +1148,7 @@ static VALUE set_native_content(VALUE self, VALUE content)
1117
1148
  while (NULL != child) {
1118
1149
  next = child->next ;
1119
1150
  xmlUnlinkNode(child) ;
1120
- nokogiri_root_node(child);
1151
+ noko_xml_document_pin_node(child);
1121
1152
  child = next ;
1122
1153
  }
1123
1154
 
@@ -1132,15 +1163,16 @@ static VALUE set_native_content(VALUE self, VALUE content)
1132
1163
  * Returns the plaintext content for this Node. Note that entities will always
1133
1164
  * be expanded in the returned string.
1134
1165
  */
1135
- static VALUE get_native_content(VALUE self)
1166
+ static VALUE
1167
+ get_native_content(VALUE self)
1136
1168
  {
1137
1169
  xmlNodePtr node;
1138
- xmlChar * content;
1170
+ xmlChar *content;
1139
1171
 
1140
1172
  Data_Get_Struct(self, xmlNode, node);
1141
1173
 
1142
1174
  content = xmlNodeGetContent(node);
1143
- if(content) {
1175
+ if (content) {
1144
1176
  VALUE rval = NOKOGIRI_STR_NEW2(content);
1145
1177
  xmlFree(content);
1146
1178
  return rval;
@@ -1154,13 +1186,14 @@ static VALUE get_native_content(VALUE self)
1154
1186
  *
1155
1187
  * Set the language of a node, i.e. the values of the xml:lang attribute.
1156
1188
  */
1157
- static VALUE set_lang(VALUE self_rb, VALUE lang_rb)
1189
+ static VALUE
1190
+ set_lang(VALUE self_rb, VALUE lang_rb)
1158
1191
  {
1159
1192
  xmlNodePtr self ;
1160
- xmlChar* lang ;
1193
+ xmlChar *lang ;
1161
1194
 
1162
1195
  Data_Get_Struct(self_rb, xmlNode, self);
1163
- lang = (xmlChar*)StringValueCStr(lang_rb);
1196
+ lang = (xmlChar *)StringValueCStr(lang_rb);
1164
1197
 
1165
1198
  xmlNodeSetLang(self, lang);
1166
1199
 
@@ -1174,10 +1207,11 @@ static VALUE set_lang(VALUE self_rb, VALUE lang_rb)
1174
1207
  * Searches the language of a node, i.e. the values of the xml:lang attribute or
1175
1208
  * the one carried by the nearest ancestor.
1176
1209
  */
1177
- static VALUE get_lang(VALUE self_rb)
1210
+ static VALUE
1211
+ get_lang(VALUE self_rb)
1178
1212
  {
1179
1213
  xmlNodePtr self ;
1180
- xmlChar* lang ;
1214
+ xmlChar *lang ;
1181
1215
  VALUE lang_rb ;
1182
1216
 
1183
1217
  Data_Get_Struct(self_rb, xmlNode, self);
@@ -1193,7 +1227,8 @@ static VALUE get_lang(VALUE self_rb)
1193
1227
  }
1194
1228
 
1195
1229
  /* :nodoc: */
1196
- static VALUE add_child(VALUE self, VALUE new_child)
1230
+ static VALUE
1231
+ add_child(VALUE self, VALUE new_child)
1197
1232
  {
1198
1233
  return reparent_node_with(self, new_child, xmlAddChild);
1199
1234
  }
@@ -1204,15 +1239,16 @@ static VALUE add_child(VALUE self, VALUE new_child)
1204
1239
  *
1205
1240
  * Get the parent Node for this Node
1206
1241
  */
1207
- static VALUE get_parent(VALUE self)
1242
+ static VALUE
1243
+ get_parent(VALUE self)
1208
1244
  {
1209
1245
  xmlNodePtr node, parent;
1210
1246
  Data_Get_Struct(self, xmlNode, node);
1211
1247
 
1212
1248
  parent = node->parent;
1213
- if(!parent) { return Qnil; }
1249
+ if (!parent) { return Qnil; }
1214
1250
 
1215
- return Nokogiri_wrap_xml_node(Qnil, parent) ;
1251
+ return noko_xml_node_wrap(Qnil, parent) ;
1216
1252
  }
1217
1253
 
1218
1254
  /*
@@ -1221,11 +1257,12 @@ static VALUE get_parent(VALUE self)
1221
1257
  *
1222
1258
  * Set the name for this Node
1223
1259
  */
1224
- static VALUE set_name(VALUE self, VALUE new_name)
1260
+ static VALUE
1261
+ set_name(VALUE self, VALUE new_name)
1225
1262
  {
1226
1263
  xmlNodePtr node;
1227
1264
  Data_Get_Struct(self, xmlNode, node);
1228
- xmlNodeSetName(node, (xmlChar*)StringValueCStr(new_name));
1265
+ xmlNodeSetName(node, (xmlChar *)StringValueCStr(new_name));
1229
1266
  return new_name;
1230
1267
  }
1231
1268
 
@@ -1235,11 +1272,12 @@ static VALUE set_name(VALUE self, VALUE new_name)
1235
1272
  *
1236
1273
  * Returns the name for this Node
1237
1274
  */
1238
- static VALUE get_name(VALUE self)
1275
+ static VALUE
1276
+ get_name(VALUE self)
1239
1277
  {
1240
1278
  xmlNodePtr node;
1241
1279
  Data_Get_Struct(self, xmlNode, node);
1242
- if(node->name) {
1280
+ if (node->name) {
1243
1281
  return NOKOGIRI_STR_NEW2(node->name);
1244
1282
  }
1245
1283
  return Qnil;
@@ -1251,28 +1289,39 @@ static VALUE get_name(VALUE self)
1251
1289
  *
1252
1290
  * Returns the path associated with this Node
1253
1291
  */
1254
- static VALUE path(VALUE self)
1292
+ static VALUE
1293
+ noko_xml_node_path(VALUE rb_node)
1255
1294
  {
1256
- xmlNodePtr node;
1257
- xmlChar *path ;
1295
+ xmlNodePtr c_node;
1296
+ xmlChar *c_path ;
1258
1297
  VALUE rval;
1259
1298
 
1260
- Data_Get_Struct(self, xmlNode, node);
1299
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1300
+
1301
+ c_path = xmlGetNodePath(c_node);
1302
+ if (c_path == NULL) {
1303
+ // see https://github.com/sparklemotion/nokogiri/issues/2250
1304
+ // this behavior is clearly undesirable, but is what libxml <= 2.9.10 returned, and so we
1305
+ // do this for now to preserve the behavior across libxml2 versions.
1306
+ rval = NOKOGIRI_STR_NEW2("?");
1307
+ } else {
1308
+ rval = NOKOGIRI_STR_NEW2(c_path);
1309
+ xmlFree(c_path);
1310
+ }
1261
1311
 
1262
- path = xmlGetNodePath(node);
1263
- rval = NOKOGIRI_STR_NEW2(path);
1264
- xmlFree(path);
1265
1312
  return rval ;
1266
1313
  }
1267
1314
 
1268
1315
  /* :nodoc: */
1269
- static VALUE add_next_sibling(VALUE self, VALUE new_sibling)
1316
+ static VALUE
1317
+ add_next_sibling(VALUE self, VALUE new_sibling)
1270
1318
  {
1271
1319
  return reparent_node_with(self, new_sibling, xmlAddNextSibling) ;
1272
1320
  }
1273
1321
 
1274
1322
  /* :nodoc: */
1275
- static VALUE add_previous_sibling(VALUE self, VALUE new_sibling)
1323
+ static VALUE
1324
+ add_previous_sibling(VALUE self, VALUE new_sibling)
1276
1325
  {
1277
1326
  return reparent_node_with(self, new_sibling, xmlAddPrevSibling) ;
1278
1327
  }
@@ -1283,7 +1332,8 @@ static VALUE add_previous_sibling(VALUE self, VALUE new_sibling)
1283
1332
  *
1284
1333
  * Write this Node to +io+ with +encoding+ and +options+
1285
1334
  */
1286
- static VALUE native_write_to(
1335
+ static VALUE
1336
+ native_write_to(
1287
1337
  VALUE self,
1288
1338
  VALUE io,
1289
1339
  VALUE encoding,
@@ -1292,7 +1342,7 @@ static VALUE native_write_to(
1292
1342
  )
1293
1343
  {
1294
1344
  xmlNodePtr node;
1295
- const char * before_indent;
1345
+ const char *before_indent;
1296
1346
  xmlSaveCtxtPtr savectx;
1297
1347
 
1298
1348
  Data_Get_Struct(self, xmlNode, node);
@@ -1304,8 +1354,8 @@ static VALUE native_write_to(
1304
1354
  xmlTreeIndentString = StringValueCStr(indent_string);
1305
1355
 
1306
1356
  savectx = xmlSaveToIO(
1307
- (xmlOutputWriteCallback)io_write_callback,
1308
- (xmlOutputCloseCallback)io_close_callback,
1357
+ (xmlOutputWriteCallback)noko_io_write,
1358
+ (xmlOutputCloseCallback)noko_io_close,
1309
1359
  (void *)io,
1310
1360
  RTEST(encoding) ? StringValueCStr(encoding) : NULL,
1311
1361
  (int)NUM2INT(options)
@@ -1324,7 +1374,8 @@ static VALUE native_write_to(
1324
1374
  *
1325
1375
  * Returns the line for this Node
1326
1376
  */
1327
- static VALUE line(VALUE self)
1377
+ static VALUE
1378
+ line(VALUE self)
1328
1379
  {
1329
1380
  xmlNodePtr node;
1330
1381
  Data_Get_Struct(self, xmlNode, node);
@@ -1332,6 +1383,26 @@ static VALUE line(VALUE self)
1332
1383
  return INT2NUM(xmlGetLineNo(node));
1333
1384
  }
1334
1385
 
1386
+ /*
1387
+ * call-seq:
1388
+ * line=(num)
1389
+ *
1390
+ * Sets the line for this Node. num must be less than 65535.
1391
+ */
1392
+ static VALUE
1393
+ set_line(VALUE self, VALUE num)
1394
+ {
1395
+ xmlNodePtr node;
1396
+ int value = NUM2INT(num);
1397
+
1398
+ Data_Get_Struct(self, xmlNode, node);
1399
+ if (value < 65535) {
1400
+ node->line = value;
1401
+ }
1402
+
1403
+ return num;
1404
+ }
1405
+
1335
1406
  /*
1336
1407
  * call-seq:
1337
1408
  * add_namespace_definition(prefix, href)
@@ -1343,45 +1414,47 @@ static VALUE line(VALUE self)
1343
1414
  * show up in #attributes, but they will be included as an xmlns attribute
1344
1415
  * when the node is serialized to XML.
1345
1416
  */
1346
- static VALUE add_namespace_definition(VALUE self, VALUE prefix, VALUE href)
1417
+ static VALUE
1418
+ add_namespace_definition(VALUE rb_node, VALUE rb_prefix, VALUE rb_href)
1347
1419
  {
1348
- xmlNodePtr node, namespace;
1349
- xmlNsPtr ns;
1420
+ xmlNodePtr c_node, element;
1421
+ xmlNsPtr c_namespace;
1422
+ const xmlChar *c_prefix = (const xmlChar *)(NIL_P(rb_prefix) ? NULL : StringValueCStr(rb_prefix));
1350
1423
 
1351
- Data_Get_Struct(self, xmlNode, node);
1352
- namespace = node ;
1424
+ Data_Get_Struct(rb_node, xmlNode, c_node);
1425
+ element = c_node ;
1353
1426
 
1354
- ns = xmlSearchNs(
1355
- node->doc,
1356
- node,
1357
- (const xmlChar *)(NIL_P(prefix) ? NULL : StringValueCStr(prefix))
1358
- );
1427
+ c_namespace = xmlSearchNs(c_node->doc, c_node, c_prefix);
1359
1428
 
1360
- if(!ns) {
1361
- if (node->type != XML_ELEMENT_NODE) {
1362
- namespace = node->parent;
1429
+ if (!c_namespace) {
1430
+ if (c_node->type != XML_ELEMENT_NODE) {
1431
+ element = c_node->parent;
1363
1432
  }
1364
- ns = xmlNewNs(
1365
- namespace,
1366
- (const xmlChar *)StringValueCStr(href),
1367
- (const xmlChar *)(NIL_P(prefix) ? NULL : StringValueCStr(prefix))
1368
- );
1433
+ c_namespace = xmlNewNs(element, (const xmlChar *)StringValueCStr(rb_href), c_prefix);
1369
1434
  }
1370
1435
 
1371
- if (!ns) { return Qnil ; }
1436
+ if (!c_namespace) {
1437
+ return Qnil ;
1438
+ }
1372
1439
 
1373
- if(NIL_P(prefix) || node != namespace) { xmlSetNs(node, ns); }
1440
+ if (NIL_P(rb_prefix) || c_node != element) {
1441
+ xmlSetNs(c_node, c_namespace);
1442
+ }
1374
1443
 
1375
- return Nokogiri_wrap_xml_namespace(node->doc, ns);
1444
+ return noko_xml_namespace_wrap(c_namespace, c_node->doc);
1376
1445
  }
1377
1446
 
1378
1447
  /*
1379
- * call-seq:
1380
- * new(name, document)
1381
- *
1382
- * Create a new node with +name+ sharing GC lifecycle with +document+
1448
+ * @overload new(name, document)
1449
+ * Create a new node with +name+ sharing GC lifecycle with +document+.
1450
+ * @param name [String]
1451
+ * @param document [Nokogiri::XML::Document]
1452
+ * @yieldparam node [Nokogiri::XML::Node]
1453
+ * @return [Nokogiri::XML::Node]
1454
+ * @see Nokogiri::XML::Node#initialize
1383
1455
  */
1384
- static VALUE new(int argc, VALUE *argv, VALUE klass)
1456
+ static VALUE
1457
+ rb_xml_node_new(int argc, VALUE *argv, VALUE klass)
1385
1458
  {
1386
1459
  xmlDocPtr doc;
1387
1460
  xmlNodePtr node;
@@ -1396,15 +1469,15 @@ static VALUE new(int argc, VALUE *argv, VALUE klass)
1396
1469
 
1397
1470
  node = xmlNewNode(NULL, (xmlChar *)StringValueCStr(name));
1398
1471
  node->doc = doc->doc;
1399
- nokogiri_root_node(node);
1472
+ noko_xml_document_pin_node(node);
1400
1473
 
1401
- rb_node = Nokogiri_wrap_xml_node(
1474
+ rb_node = noko_xml_node_wrap(
1402
1475
  klass == cNokogiriXmlNode ? (VALUE)NULL : klass,
1403
1476
  node
1404
1477
  );
1405
1478
  rb_obj_call_init(rb_node, argc, argv);
1406
1479
 
1407
- if(rb_block_given_p()) { rb_yield(rb_node); }
1480
+ if (rb_block_given_p()) { rb_yield(rb_node); }
1408
1481
 
1409
1482
  return rb_node;
1410
1483
  }
@@ -1415,7 +1488,8 @@ static VALUE new(int argc, VALUE *argv, VALUE klass)
1415
1488
  *
1416
1489
  * Returns the Node as html.
1417
1490
  */
1418
- static VALUE dump_html(VALUE self)
1491
+ static VALUE
1492
+ dump_html(VALUE self)
1419
1493
  {
1420
1494
  xmlBufferPtr buf ;
1421
1495
  xmlNodePtr node ;
@@ -1436,7 +1510,8 @@ static VALUE dump_html(VALUE self)
1436
1510
  *
1437
1511
  * Compare this Node to +other+ with respect to their Document
1438
1512
  */
1439
- static VALUE compare(VALUE self, VALUE _other)
1513
+ static VALUE
1514
+ compare(VALUE self, VALUE _other)
1440
1515
  {
1441
1516
  xmlNodePtr node, other;
1442
1517
  Data_Get_Struct(self, xmlNode, node);
@@ -1453,7 +1528,8 @@ static VALUE compare(VALUE self, VALUE _other)
1453
1528
  * Loads and substitutes all xinclude elements below the node. The
1454
1529
  * parser context will be initialized with +options+.
1455
1530
  */
1456
- static VALUE process_xincludes(VALUE self, VALUE options)
1531
+ static VALUE
1532
+ process_xincludes(VALUE self, VALUE options)
1457
1533
  {
1458
1534
  int rcode ;
1459
1535
  xmlNodePtr node;
@@ -1469,7 +1545,7 @@ static VALUE process_xincludes(VALUE self, VALUE options)
1469
1545
  xmlErrorPtr error;
1470
1546
 
1471
1547
  error = xmlGetLastError();
1472
- if(error) {
1548
+ if (error) {
1473
1549
  rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
1474
1550
  } else {
1475
1551
  rb_raise(rb_eRuntimeError, "Could not perform xinclude substitution");
@@ -1481,7 +1557,8 @@ static VALUE process_xincludes(VALUE self, VALUE options)
1481
1557
 
1482
1558
 
1483
1559
  /* TODO: DOCUMENT ME */
1484
- static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
1560
+ static VALUE
1561
+ in_context(VALUE self, VALUE _str, VALUE _options)
1485
1562
  {
1486
1563
  xmlNodePtr node, list = 0, tmp, child_iter, node_children, doc_children;
1487
1564
  xmlNodeSetPtr set;
@@ -1562,12 +1639,12 @@ static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
1562
1639
 
1563
1640
  /* FIXME: This probably needs to handle more constants... */
1564
1641
  switch (error) {
1565
- case XML_ERR_INTERNAL_ERROR:
1566
- case XML_ERR_NO_MEMORY:
1567
- rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
1568
- break;
1569
- default:
1570
- break;
1642
+ case XML_ERR_INTERNAL_ERROR:
1643
+ case XML_ERR_NO_MEMORY:
1644
+ rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
1645
+ break;
1646
+ default:
1647
+ break;
1571
1648
  }
1572
1649
 
1573
1650
  set = xmlXPathNodeSetCreate(NULL);
@@ -1576,178 +1653,180 @@ static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
1576
1653
  tmp = list->next;
1577
1654
  list->next = NULL;
1578
1655
  xmlXPathNodeSetAddUnique(set, list);
1579
- nokogiri_root_node(list);
1656
+ noko_xml_document_pin_node(list);
1580
1657
  list = tmp;
1581
1658
  }
1582
1659
 
1583
- return Nokogiri_wrap_xml_node_set(set, doc);
1660
+ return noko_xml_node_set_wrap(set, doc);
1584
1661
  }
1585
1662
 
1586
1663
 
1587
- VALUE Nokogiri_wrap_xml_node(VALUE klass, xmlNodePtr node)
1664
+ VALUE
1665
+ noko_xml_node_wrap(VALUE rb_class, xmlNodePtr c_node)
1588
1666
  {
1589
- VALUE document = Qnil ;
1590
- VALUE node_cache = Qnil ;
1591
- VALUE rb_node = Qnil ;
1667
+ VALUE rb_document, rb_node_cache, rb_node;
1592
1668
  nokogiriTuplePtr node_has_a_document;
1593
- xmlDocPtr doc;
1669
+ xmlDocPtr c_doc;
1594
1670
  void (*mark_method)(xmlNodePtr) = NULL ;
1595
1671
 
1596
- assert(node);
1672
+ assert(c_node);
1597
1673
 
1598
- if(node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
1599
- return DOC_RUBY_OBJECT(node->doc);
1674
+ if (c_node->type == XML_DOCUMENT_NODE || c_node->type == XML_HTML_DOCUMENT_NODE) {
1675
+ return DOC_RUBY_OBJECT(c_node->doc);
1600
1676
  }
1601
1677
 
1602
1678
  /* It's OK if the node doesn't have a fully-realized document (as in XML::Reader). */
1603
1679
  /* see https://github.com/sparklemotion/nokogiri/issues/95 */
1604
1680
  /* and https://github.com/sparklemotion/nokogiri/issues/439 */
1605
- doc = node->doc;
1606
- if (doc->type == XML_DOCUMENT_FRAG_NODE) { doc = doc->doc; }
1607
- node_has_a_document = DOC_RUBY_OBJECT_TEST(doc);
1681
+ c_doc = c_node->doc;
1682
+ if (c_doc->type == XML_DOCUMENT_FRAG_NODE) { c_doc = c_doc->doc; }
1683
+ node_has_a_document = DOC_RUBY_OBJECT_TEST(c_doc);
1608
1684
 
1609
- if(node->_private && node_has_a_document) {
1610
- return (VALUE)node->_private;
1685
+ if (c_node->_private && node_has_a_document) {
1686
+ return (VALUE)c_node->_private;
1611
1687
  }
1612
1688
 
1613
- if(!RTEST(klass)) {
1614
- switch(node->type) {
1615
- case XML_ELEMENT_NODE:
1616
- klass = cNokogiriXmlElement;
1617
- break;
1618
- case XML_TEXT_NODE:
1619
- klass = cNokogiriXmlText;
1620
- break;
1621
- case XML_ATTRIBUTE_NODE:
1622
- klass = cNokogiriXmlAttr;
1623
- break;
1624
- case XML_ENTITY_REF_NODE:
1625
- klass = cNokogiriXmlEntityReference;
1626
- break;
1627
- case XML_COMMENT_NODE:
1628
- klass = cNokogiriXmlComment;
1629
- break;
1630
- case XML_DOCUMENT_FRAG_NODE:
1631
- klass = cNokogiriXmlDocumentFragment;
1632
- break;
1633
- case XML_PI_NODE:
1634
- klass = cNokogiriXmlProcessingInstruction;
1635
- break;
1636
- case XML_ENTITY_DECL:
1637
- klass = cNokogiriXmlEntityDecl;
1638
- break;
1639
- case XML_CDATA_SECTION_NODE:
1640
- klass = cNokogiriXmlCData;
1641
- break;
1642
- case XML_DTD_NODE:
1643
- klass = cNokogiriXmlDtd;
1644
- break;
1645
- case XML_ATTRIBUTE_DECL:
1646
- klass = cNokogiriXmlAttributeDecl;
1647
- break;
1648
- case XML_ELEMENT_DECL:
1649
- klass = cNokogiriXmlElementDecl;
1650
- break;
1651
- default:
1652
- klass = cNokogiriXmlNode;
1689
+ if (!RTEST(rb_class)) {
1690
+ switch (c_node->type) {
1691
+ case XML_ELEMENT_NODE:
1692
+ rb_class = cNokogiriXmlElement;
1693
+ break;
1694
+ case XML_TEXT_NODE:
1695
+ rb_class = cNokogiriXmlText;
1696
+ break;
1697
+ case XML_ATTRIBUTE_NODE:
1698
+ rb_class = cNokogiriXmlAttr;
1699
+ break;
1700
+ case XML_ENTITY_REF_NODE:
1701
+ rb_class = cNokogiriXmlEntityReference;
1702
+ break;
1703
+ case XML_COMMENT_NODE:
1704
+ rb_class = cNokogiriXmlComment;
1705
+ break;
1706
+ case XML_DOCUMENT_FRAG_NODE:
1707
+ rb_class = cNokogiriXmlDocumentFragment;
1708
+ break;
1709
+ case XML_PI_NODE:
1710
+ rb_class = cNokogiriXmlProcessingInstruction;
1711
+ break;
1712
+ case XML_ENTITY_DECL:
1713
+ rb_class = cNokogiriXmlEntityDecl;
1714
+ break;
1715
+ case XML_CDATA_SECTION_NODE:
1716
+ rb_class = cNokogiriXmlCData;
1717
+ break;
1718
+ case XML_DTD_NODE:
1719
+ rb_class = cNokogiriXmlDtd;
1720
+ break;
1721
+ case XML_ATTRIBUTE_DECL:
1722
+ rb_class = cNokogiriXmlAttributeDecl;
1723
+ break;
1724
+ case XML_ELEMENT_DECL:
1725
+ rb_class = cNokogiriXmlElementDecl;
1726
+ break;
1727
+ default:
1728
+ rb_class = cNokogiriXmlNode;
1653
1729
  }
1654
1730
  }
1655
1731
 
1656
1732
  mark_method = node_has_a_document ? mark : NULL ;
1657
1733
 
1658
- rb_node = Data_Wrap_Struct(klass, mark_method, debug_node_dealloc, node) ;
1659
- node->_private = (void *)rb_node;
1734
+ rb_node = Data_Wrap_Struct(rb_class, mark_method, debug_node_dealloc, c_node) ;
1735
+ c_node->_private = (void *)rb_node;
1660
1736
 
1661
1737
  if (node_has_a_document) {
1662
- document = DOC_RUBY_OBJECT(doc);
1663
- node_cache = DOC_NODE_CACHE(doc);
1664
- rb_ary_push(node_cache, rb_node);
1665
- rb_funcall(document, decorate, 1, rb_node);
1738
+ rb_document = DOC_RUBY_OBJECT(c_doc);
1739
+ rb_node_cache = DOC_NODE_CACHE(c_doc);
1740
+ rb_ary_push(rb_node_cache, rb_node);
1741
+ rb_funcall(rb_document, id_decorate, 1, rb_node);
1666
1742
  }
1667
1743
 
1668
1744
  return rb_node ;
1669
1745
  }
1670
1746
 
1671
1747
 
1672
- void Nokogiri_xml_node_properties(xmlNodePtr node, VALUE attr_list)
1748
+ /*
1749
+ * return Array<Nokogiri::XML::Attr> containing the node's attributes
1750
+ */
1751
+ VALUE
1752
+ noko_xml_node_attrs(xmlNodePtr c_node)
1673
1753
  {
1674
- xmlAttrPtr prop;
1675
- prop = node->properties ;
1676
- while (prop != NULL) {
1677
- rb_ary_push(attr_list, Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop));
1678
- prop = prop->next ;
1754
+ VALUE rb_properties = rb_ary_new();
1755
+ xmlAttrPtr c_property;
1756
+
1757
+ c_property = c_node->properties ;
1758
+ while (c_property != NULL) {
1759
+ rb_ary_push(rb_properties, noko_xml_node_wrap(Qnil, (xmlNodePtr)c_property));
1760
+ c_property = c_property->next ;
1679
1761
  }
1680
- }
1681
1762
 
1682
- VALUE cNokogiriXmlNode ;
1683
- VALUE cNokogiriXmlElement ;
1763
+ return rb_properties;
1764
+ }
1684
1765
 
1685
- void init_xml_node()
1766
+ void
1767
+ noko_init_xml_node()
1686
1768
  {
1687
- VALUE nokogiri = rb_define_module("Nokogiri");
1688
- VALUE xml = rb_define_module_under(nokogiri, "XML");
1689
- VALUE klass = rb_define_class_under(xml, "Node", rb_cObject);
1690
-
1691
- cNokogiriXmlNode = klass;
1692
-
1693
- cNokogiriXmlElement = rb_define_class_under(xml, "Element", klass);
1694
-
1695
- rb_define_singleton_method(klass, "new", new, -1);
1696
-
1697
- rb_define_method(klass, "add_namespace_definition", add_namespace_definition, 2);
1698
- rb_define_method(klass, "node_name", get_name, 0);
1699
- rb_define_method(klass, "document", document, 0);
1700
- rb_define_method(klass, "node_name=", set_name, 1);
1701
- rb_define_method(klass, "parent", get_parent, 0);
1702
- rb_define_method(klass, "child", child, 0);
1703
- rb_define_method(klass, "first_element_child", first_element_child, 0);
1704
- rb_define_method(klass, "last_element_child", last_element_child, 0);
1705
- rb_define_method(klass, "children", children, 0);
1706
- rb_define_method(klass, "element_children", element_children, 0);
1707
- rb_define_method(klass, "next_sibling", next_sibling, 0);
1708
- rb_define_method(klass, "previous_sibling", previous_sibling, 0);
1709
- rb_define_method(klass, "next_element", next_element, 0);
1710
- rb_define_method(klass, "previous_element", previous_element, 0);
1711
- rb_define_method(klass, "node_type", node_type, 0);
1712
- rb_define_method(klass, "path", path, 0);
1713
- rb_define_method(klass, "key?", key_eh, 1);
1714
- rb_define_method(klass, "namespaced_key?", namespaced_key_eh, 2);
1715
- rb_define_method(klass, "blank?", blank_eh, 0);
1716
- rb_define_method(klass, "attribute_nodes", attribute_nodes, 0);
1717
- rb_define_method(klass, "attribute", attr, 1);
1718
- rb_define_method(klass, "attribute_with_ns", attribute_with_ns, 2);
1719
- rb_define_method(klass, "namespace", namespace, 0);
1720
- rb_define_method(klass, "namespace_definitions", namespace_definitions, 0);
1721
- rb_define_method(klass, "namespace_scopes", namespace_scopes, 0);
1722
- rb_define_method(klass, "encode_special_chars", encode_special_chars, 1);
1723
- rb_define_method(klass, "dup", duplicate_node, -1);
1724
- rb_define_method(klass, "unlink", unlink_node, 0);
1725
- rb_define_method(klass, "internal_subset", internal_subset, 0);
1726
- rb_define_method(klass, "external_subset", external_subset, 0);
1727
- rb_define_method(klass, "create_internal_subset", create_internal_subset, 3);
1728
- rb_define_method(klass, "create_external_subset", create_external_subset, 3);
1729
- rb_define_method(klass, "pointer_id", pointer_id, 0);
1730
- rb_define_method(klass, "line", line, 0);
1731
- rb_define_method(klass, "content", get_native_content, 0);
1732
- rb_define_method(klass, "native_content=", set_native_content, 1);
1733
- rb_define_method(klass, "lang", get_lang, 0);
1734
- rb_define_method(klass, "lang=", set_lang, 1);
1735
-
1736
- rb_define_private_method(klass, "process_xincludes", process_xincludes, 1);
1737
- rb_define_private_method(klass, "in_context", in_context, 2);
1738
- rb_define_private_method(klass, "add_child_node", add_child, 1);
1739
- rb_define_private_method(klass, "add_previous_sibling_node", add_previous_sibling, 1);
1740
- rb_define_private_method(klass, "add_next_sibling_node", add_next_sibling, 1);
1741
- rb_define_private_method(klass, "replace_node", replace, 1);
1742
- rb_define_private_method(klass, "dump_html", dump_html, 0);
1743
- rb_define_private_method(klass, "native_write_to", native_write_to, 4);
1744
- rb_define_private_method(klass, "get", get, 1);
1745
- rb_define_private_method(klass, "set", set, 2);
1746
- rb_define_private_method(klass, "set_namespace", set_namespace, 1);
1747
- rb_define_private_method(klass, "compare", compare, 1);
1748
-
1749
- decorate = rb_intern("decorate");
1750
- decorate_bang = rb_intern("decorate!");
1769
+ cNokogiriXmlNode = rb_define_class_under(mNokogiriXml, "Node", rb_cObject);
1770
+
1771
+ rb_undef_alloc_func(cNokogiriXmlNode);
1772
+
1773
+ rb_define_singleton_method(cNokogiriXmlNode, "new", rb_xml_node_new, -1);
1774
+
1775
+ rb_define_method(cNokogiriXmlNode, "add_namespace_definition", add_namespace_definition, 2);
1776
+ rb_define_method(cNokogiriXmlNode, "node_name", get_name, 0);
1777
+ rb_define_method(cNokogiriXmlNode, "document", document, 0);
1778
+ rb_define_method(cNokogiriXmlNode, "node_name=", set_name, 1);
1779
+ rb_define_method(cNokogiriXmlNode, "parent", get_parent, 0);
1780
+ rb_define_method(cNokogiriXmlNode, "child", child, 0);
1781
+ rb_define_method(cNokogiriXmlNode, "first_element_child", first_element_child, 0);
1782
+ rb_define_method(cNokogiriXmlNode, "last_element_child", last_element_child, 0);
1783
+ rb_define_method(cNokogiriXmlNode, "children", children, 0);
1784
+ rb_define_method(cNokogiriXmlNode, "element_children", element_children, 0);
1785
+ rb_define_method(cNokogiriXmlNode, "next_sibling", next_sibling, 0);
1786
+ rb_define_method(cNokogiriXmlNode, "previous_sibling", previous_sibling, 0);
1787
+ rb_define_method(cNokogiriXmlNode, "next_element", next_element, 0);
1788
+ rb_define_method(cNokogiriXmlNode, "previous_element", previous_element, 0);
1789
+ rb_define_method(cNokogiriXmlNode, "node_type", node_type, 0);
1790
+ rb_define_method(cNokogiriXmlNode, "path", noko_xml_node_path, 0);
1791
+ rb_define_method(cNokogiriXmlNode, "key?", key_eh, 1);
1792
+ rb_define_method(cNokogiriXmlNode, "namespaced_key?", namespaced_key_eh, 2);
1793
+ rb_define_method(cNokogiriXmlNode, "blank?", blank_eh, 0);
1794
+ rb_define_method(cNokogiriXmlNode, "attribute_nodes", attribute_nodes, 0);
1795
+ rb_define_method(cNokogiriXmlNode, "attribute", attr, 1);
1796
+ rb_define_method(cNokogiriXmlNode, "attribute_with_ns", attribute_with_ns, 2);
1797
+ rb_define_method(cNokogiriXmlNode, "namespace", noko_xml_node_namespace, 0);
1798
+ rb_define_method(cNokogiriXmlNode, "namespace_definitions", namespace_definitions, 0);
1799
+ rb_define_method(cNokogiriXmlNode, "namespace_scopes", namespace_scopes, 0);
1800
+ rb_define_method(cNokogiriXmlNode, "encode_special_chars", encode_special_chars, 1);
1801
+ rb_define_method(cNokogiriXmlNode, "dup", duplicate_node, -1);
1802
+ rb_define_method(cNokogiriXmlNode, "unlink", unlink_node, 0);
1803
+ rb_define_method(cNokogiriXmlNode, "internal_subset", internal_subset, 0);
1804
+ rb_define_method(cNokogiriXmlNode, "external_subset", external_subset, 0);
1805
+ rb_define_method(cNokogiriXmlNode, "create_internal_subset", create_internal_subset, 3);
1806
+ rb_define_method(cNokogiriXmlNode, "create_external_subset", create_external_subset, 3);
1807
+ rb_define_method(cNokogiriXmlNode, "pointer_id", pointer_id, 0);
1808
+ rb_define_method(cNokogiriXmlNode, "line", line, 0);
1809
+ rb_define_method(cNokogiriXmlNode, "line=", set_line, 1);
1810
+ rb_define_method(cNokogiriXmlNode, "content", get_native_content, 0);
1811
+ rb_define_method(cNokogiriXmlNode, "native_content=", set_native_content, 1);
1812
+ rb_define_method(cNokogiriXmlNode, "lang", get_lang, 0);
1813
+ rb_define_method(cNokogiriXmlNode, "lang=", set_lang, 1);
1814
+
1815
+ rb_define_private_method(cNokogiriXmlNode, "process_xincludes", process_xincludes, 1);
1816
+ rb_define_private_method(cNokogiriXmlNode, "in_context", in_context, 2);
1817
+ rb_define_private_method(cNokogiriXmlNode, "add_child_node", add_child, 1);
1818
+ rb_define_private_method(cNokogiriXmlNode, "add_previous_sibling_node", add_previous_sibling, 1);
1819
+ rb_define_private_method(cNokogiriXmlNode, "add_next_sibling_node", add_next_sibling, 1);
1820
+ rb_define_private_method(cNokogiriXmlNode, "replace_node", replace, 1);
1821
+ rb_define_private_method(cNokogiriXmlNode, "dump_html", dump_html, 0);
1822
+ rb_define_private_method(cNokogiriXmlNode, "native_write_to", native_write_to, 4);
1823
+ rb_define_private_method(cNokogiriXmlNode, "get", get, 1);
1824
+ rb_define_private_method(cNokogiriXmlNode, "set", set, 2);
1825
+ rb_define_private_method(cNokogiriXmlNode, "set_namespace", set_namespace, 1);
1826
+ rb_define_private_method(cNokogiriXmlNode, "compare", compare, 1);
1827
+
1828
+ id_decorate = rb_intern("decorate");
1829
+ id_decorate_bang = rb_intern("decorate!");
1751
1830
  }
1752
1831
 
1753
1832
  /* vim: set noet sw=4 sws=4 */