nokogiri 1.11.0.rc3-java → 1.11.4-java

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 (187) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1015 -947
  4. data/LICENSE.md +1 -1
  5. data/README.md +168 -91
  6. data/dependencies.yml +12 -12
  7. data/ext/java/nokogiri/EncodingHandler.java +76 -89
  8. data/ext/java/nokogiri/HtmlDocument.java +135 -144
  9. data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
  10. data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
  11. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
  12. data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
  13. data/ext/java/nokogiri/NokogiriService.java +595 -556
  14. data/ext/java/nokogiri/XmlAttr.java +118 -126
  15. data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
  16. data/ext/java/nokogiri/XmlCdata.java +35 -58
  17. data/ext/java/nokogiri/XmlComment.java +46 -67
  18. data/ext/java/nokogiri/XmlDocument.java +645 -572
  19. data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
  20. data/ext/java/nokogiri/XmlDtd.java +448 -414
  21. data/ext/java/nokogiri/XmlElement.java +23 -48
  22. data/ext/java/nokogiri/XmlElementContent.java +343 -316
  23. data/ext/java/nokogiri/XmlElementDecl.java +124 -125
  24. data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
  25. data/ext/java/nokogiri/XmlEntityReference.java +49 -72
  26. data/ext/java/nokogiri/XmlNamespace.java +175 -175
  27. data/ext/java/nokogiri/XmlNode.java +1843 -1622
  28. data/ext/java/nokogiri/XmlNodeSet.java +361 -331
  29. data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
  30. data/ext/java/nokogiri/XmlReader.java +513 -450
  31. data/ext/java/nokogiri/XmlRelaxng.java +89 -101
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  34. data/ext/java/nokogiri/XmlSchema.java +335 -242
  35. data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
  36. data/ext/java/nokogiri/XmlText.java +55 -76
  37. data/ext/java/nokogiri/XmlXpathContext.java +242 -210
  38. data/ext/java/nokogiri/XsltStylesheet.java +280 -269
  39. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  40. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -190
  41. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  42. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  43. data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
  44. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  45. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
  46. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
  47. data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
  48. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
  49. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +79 -89
  50. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
  51. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
  52. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
  53. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +119 -78
  54. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
  55. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
  56. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
  57. data/ext/java/nokogiri/internals/ParserContext.java +206 -211
  58. data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
  59. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
  60. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
  61. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  62. data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
  63. data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
  64. data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
  65. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  66. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  67. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  68. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  78. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  81. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  82. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  83. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  84. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  85. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  86. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  87. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  88. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  89. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  90. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  91. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  92. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  93. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
  94. data/ext/nokogiri/depend +37 -358
  95. data/ext/nokogiri/extconf.rb +581 -374
  96. data/ext/nokogiri/html_document.c +78 -82
  97. data/ext/nokogiri/html_element_description.c +84 -71
  98. data/ext/nokogiri/html_entity_lookup.c +21 -16
  99. data/ext/nokogiri/html_sax_parser_context.c +69 -66
  100. data/ext/nokogiri/html_sax_push_parser.c +42 -34
  101. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  102. data/ext/nokogiri/nokogiri.c +192 -93
  103. data/ext/nokogiri/test_global_handlers.c +40 -0
  104. data/ext/nokogiri/xml_attr.c +15 -15
  105. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  106. data/ext/nokogiri/xml_cdata.c +13 -18
  107. data/ext/nokogiri/xml_comment.c +19 -26
  108. data/ext/nokogiri/xml_document.c +246 -188
  109. data/ext/nokogiri/xml_document_fragment.c +13 -15
  110. data/ext/nokogiri/xml_dtd.c +54 -48
  111. data/ext/nokogiri/xml_element_content.c +30 -27
  112. data/ext/nokogiri/xml_element_decl.c +22 -22
  113. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  114. data/ext/nokogiri/xml_entity_decl.c +32 -30
  115. data/ext/nokogiri/xml_entity_reference.c +16 -18
  116. data/ext/nokogiri/xml_namespace.c +56 -49
  117. data/ext/nokogiri/xml_node.c +371 -320
  118. data/ext/nokogiri/xml_node_set.c +168 -156
  119. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  120. data/ext/nokogiri/xml_reader.c +191 -157
  121. data/ext/nokogiri/xml_relax_ng.c +52 -28
  122. data/ext/nokogiri/xml_sax_parser.c +118 -118
  123. data/ext/nokogiri/xml_sax_parser_context.c +103 -86
  124. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  125. data/ext/nokogiri/xml_schema.c +95 -47
  126. data/ext/nokogiri/xml_syntax_error.c +42 -21
  127. data/ext/nokogiri/xml_text.c +13 -17
  128. data/ext/nokogiri/xml_xpath_context.c +206 -123
  129. data/ext/nokogiri/xslt_stylesheet.c +158 -161
  130. data/lib/nokogiri.rb +3 -7
  131. data/lib/nokogiri/css/parser.rb +3 -3
  132. data/lib/nokogiri/css/parser.y +2 -2
  133. data/lib/nokogiri/css/xpath_visitor.rb +70 -42
  134. data/lib/nokogiri/extension.rb +26 -0
  135. data/lib/nokogiri/html/document.rb +12 -26
  136. data/lib/nokogiri/html/document_fragment.rb +15 -15
  137. data/lib/nokogiri/nokogiri.jar +0 -0
  138. data/lib/nokogiri/version.rb +2 -149
  139. data/lib/nokogiri/version/constant.rb +5 -0
  140. data/lib/nokogiri/version/info.rb +205 -0
  141. data/lib/nokogiri/xml/document.rb +91 -35
  142. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  143. data/lib/nokogiri/xml/node.rb +89 -69
  144. data/lib/nokogiri/xml/parse_options.rb +6 -0
  145. data/lib/nokogiri/xml/reader.rb +2 -9
  146. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  147. data/lib/nokogiri/xml/schema.rb +12 -4
  148. data/lib/nokogiri/xml/searchable.rb +3 -1
  149. data/lib/nokogiri/xml/xpath.rb +1 -3
  150. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  151. metadata +86 -177
  152. data/ext/nokogiri/html_document.h +0 -10
  153. data/ext/nokogiri/html_element_description.h +0 -10
  154. data/ext/nokogiri/html_entity_lookup.h +0 -8
  155. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  156. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  157. data/ext/nokogiri/nokogiri.h +0 -134
  158. data/ext/nokogiri/xml_attr.h +0 -9
  159. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  160. data/ext/nokogiri/xml_cdata.h +0 -9
  161. data/ext/nokogiri/xml_comment.h +0 -9
  162. data/ext/nokogiri/xml_document.h +0 -23
  163. data/ext/nokogiri/xml_document_fragment.h +0 -10
  164. data/ext/nokogiri/xml_dtd.h +0 -10
  165. data/ext/nokogiri/xml_element_content.h +0 -10
  166. data/ext/nokogiri/xml_element_decl.h +0 -9
  167. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  168. data/ext/nokogiri/xml_entity_decl.h +0 -10
  169. data/ext/nokogiri/xml_entity_reference.h +0 -9
  170. data/ext/nokogiri/xml_io.c +0 -63
  171. data/ext/nokogiri/xml_io.h +0 -11
  172. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  173. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  174. data/ext/nokogiri/xml_namespace.h +0 -14
  175. data/ext/nokogiri/xml_node.h +0 -13
  176. data/ext/nokogiri/xml_node_set.h +0 -12
  177. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  178. data/ext/nokogiri/xml_reader.h +0 -10
  179. data/ext/nokogiri/xml_relax_ng.h +0 -9
  180. data/ext/nokogiri/xml_sax_parser.h +0 -39
  181. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  182. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  183. data/ext/nokogiri/xml_schema.h +0 -9
  184. data/ext/nokogiri/xml_syntax_error.h +0 -13
  185. data/ext/nokogiri/xml_text.h +0 -9
  186. data/ext/nokogiri/xml_xpath_context.h +0 -10
  187. data/ext/nokogiri/xslt_stylesheet.h +0 -14
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2012:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
1
  package nokogiri;
34
2
 
35
3
  import static nokogiri.XmlNode.setDocumentAndDecorate;
@@ -57,378 +25,440 @@ import org.w3c.dom.NodeList;
57
25
  * @author sergio
58
26
  * @author Yoko Harada <yokolet@gmail.com>
59
27
  */
60
- @JRubyClass(name="Nokogiri::XML::NodeSet")
61
- public class XmlNodeSet extends RubyObject implements NodeList {
62
-
63
- IRubyObject[] nodes;
64
-
65
- public XmlNodeSet(Ruby ruby, RubyClass klazz) {
66
- super(ruby, klazz);
67
- nodes = IRubyObject.NULL_ARRAY;
28
+ @JRubyClass(name = "Nokogiri::XML::NodeSet")
29
+ public class XmlNodeSet extends RubyObject implements NodeList
30
+ {
31
+
32
+ IRubyObject[] nodes;
33
+
34
+ public
35
+ XmlNodeSet(Ruby ruby, RubyClass klazz)
36
+ {
37
+ super(ruby, klazz);
38
+ nodes = IRubyObject.NULL_ARRAY;
39
+ }
40
+
41
+ XmlNodeSet(Ruby ruby, RubyClass klazz, IRubyObject[] nodes)
42
+ {
43
+ super(ruby, klazz);
44
+ this.nodes = nodes;
45
+ }
46
+
47
+ public static XmlNodeSet
48
+ newEmptyNodeSet(ThreadContext context, XmlNodeSet docOwner)
49
+ {
50
+ final Ruby runtime = context.runtime;
51
+ XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
52
+ set.initializeFrom(context, docOwner);
53
+ return set;
54
+ }
55
+
56
+ public static XmlNodeSet
57
+ newEmptyNodeSet(ThreadContext context, XmlNode docOwner)
58
+ {
59
+ final Ruby runtime = context.runtime;
60
+ XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
61
+ set.initialize(runtime, docOwner);
62
+ return set;
63
+ }
64
+
65
+ public static XmlNodeSet
66
+ newNodeSet(Ruby runtime, IRubyObject[] nodes)
67
+ {
68
+ XmlNodeSet xmlNodeSet = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
69
+ xmlNodeSet.setNodes(nodes);
70
+ return xmlNodeSet;
71
+ }
72
+
73
+ public static XmlNodeSet
74
+ newNodeSet(Ruby runtime, IRubyObject[] nodes, XmlNode docOwner)
75
+ {
76
+ XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"), nodes);
77
+ set.initialize(runtime, docOwner);
78
+ return set;
79
+ }
80
+
81
+ /**
82
+ * Create and return a copy of this object.
83
+ *
84
+ * @return a clone of this object
85
+ */
86
+ @Override
87
+ public Object
88
+ clone() throws CloneNotSupportedException
89
+ {
90
+ return super.clone();
91
+ }
92
+
93
+ private void
94
+ setNodes(IRubyObject[] array)
95
+ {
96
+ this.nodes = array;
97
+
98
+ IRubyObject first = array.length > 0 ? array[0] : null;
99
+ initialize(getRuntime(), first);
100
+ }
101
+
102
+ private void
103
+ initializeFrom(ThreadContext context, XmlNodeSet ref)
104
+ {
105
+ IRubyObject document = ref.getInstanceVariable("@document");
106
+ if (document != null && !document.isNil()) {
107
+ initialize(context, (XmlDocument) document);
68
108
  }
69
-
70
- XmlNodeSet(Ruby ruby, RubyClass klazz, IRubyObject[] nodes) {
71
- super(ruby, klazz);
72
- this.nodes = nodes;
73
- }
74
-
75
- public static XmlNodeSet newEmptyNodeSet(ThreadContext context, XmlNodeSet docOwner) {
76
- final Ruby runtime = context.runtime;
77
- XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
78
- set.initializeFrom(context, docOwner);
79
- return set;
80
- }
81
-
82
- public static XmlNodeSet newEmptyNodeSet(ThreadContext context, XmlNode docOwner) {
83
- final Ruby runtime = context.runtime;
84
- XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
85
- set.initialize(runtime, docOwner);
86
- return set;
109
+ }
110
+
111
+ final void
112
+ initialize(Ruby runtime, IRubyObject refNode)
113
+ {
114
+ if (refNode instanceof XmlNode) {
115
+ XmlDocument doc = ((XmlNode) refNode).document(runtime);
116
+ setDocumentAndDecorate(runtime.getCurrentContext(), this, doc);
87
117
  }
88
-
89
- public static XmlNodeSet newNodeSet(Ruby runtime, IRubyObject[] nodes) {
90
- XmlNodeSet xmlNodeSet = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
91
- xmlNodeSet.setNodes(nodes);
92
- return xmlNodeSet;
93
- }
94
-
95
- public static XmlNodeSet newNodeSet(Ruby runtime, IRubyObject[] nodes, XmlNode docOwner) {
96
- XmlNodeSet set = new XmlNodeSet(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"), nodes);
97
- set.initialize(runtime, docOwner);
98
- return set;
118
+ }
119
+
120
+ private void
121
+ initialize(ThreadContext context, XmlDocument doc)
122
+ {
123
+ setDocumentAndDecorate(context, this, doc);
124
+ }
125
+
126
+ public int
127
+ length()
128
+ {
129
+ return nodes == null ? 0 : nodes.length;
130
+ }
131
+
132
+ @JRubyMethod(name = "&")
133
+ public IRubyObject
134
+ op_and(ThreadContext context, IRubyObject nodeSet)
135
+ {
136
+ IRubyObject[] otherNodes = getNodes(context, nodeSet);
137
+
138
+ if (otherNodes == null || otherNodes.length == 0) {
139
+ return newEmptyNodeSet(context, this);
99
140
  }
100
141
 
101
- /**
102
- * Create and return a copy of this object.
103
- *
104
- * @return a clone of this object
105
- */
106
- @Override
107
- public Object clone() throws CloneNotSupportedException {
108
- return super.clone();
142
+ if (nodes == null || nodes.length == 0) {
143
+ return newEmptyNodeSet(context, this);
109
144
  }
110
145
 
111
- private void setNodes(IRubyObject[] array) {
112
- this.nodes = array;
113
-
114
- IRubyObject first = array.length > 0 ? array[0] : null;
115
- initialize(getRuntime(), first);
116
- }
146
+ IRubyObject[] curr = nodes;
147
+ IRubyObject[] other = getNodes(context, nodeSet);
148
+ IRubyObject[] result = new IRubyObject[nodes.length];
117
149
 
118
- private void initializeFrom(ThreadContext context, XmlNodeSet ref) {
119
- IRubyObject document = ref.getInstanceVariable("@document");
120
- if (document != null && !document.isNil()) {
121
- initialize(context, (XmlDocument) document);
122
- }
123
- }
150
+ int last = 0;
151
+ outer:
152
+ for (int i = 0; i < curr.length; i++) {
153
+ IRubyObject n = curr[i];
124
154
 
125
- final void initialize(Ruby runtime, IRubyObject refNode) {
126
- if (refNode instanceof XmlNode) {
127
- XmlDocument doc = ((XmlNode) refNode).document(runtime);
128
- setDocumentAndDecorate(runtime.getCurrentContext(), this, doc);
155
+ for (int j = 0; j < other.length; j++) {
156
+ if (other[j] == n) {
157
+ result[last++] = n;
158
+ continue outer;
129
159
  }
160
+ }
130
161
  }
131
162
 
132
- private void initialize(ThreadContext context, XmlDocument doc) {
133
- setDocumentAndDecorate(context, this, doc);
134
- }
135
-
136
- public int length() {
137
- return nodes == null ? 0 : nodes.length;
138
- }
139
-
140
- @JRubyMethod(name="&")
141
- public IRubyObject op_and(ThreadContext context, IRubyObject nodeSet) {
142
- IRubyObject[] otherNodes = getNodes(context, nodeSet);
143
-
144
- if (otherNodes == null || otherNodes.length == 0) {
145
- return newEmptyNodeSet(context, this);
146
- }
147
-
148
- if (nodes == null || nodes.length == 0) {
149
- return newEmptyNodeSet(context, this);
150
- }
163
+ XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
164
+ newSet.initializeFrom(context, this);
165
+ return newSet;
166
+ }
151
167
 
152
- IRubyObject[] curr = nodes;
153
- IRubyObject[] other = getNodes(context, nodeSet);
154
- IRubyObject[] result = new IRubyObject[nodes.length];
155
-
156
- int last = 0;
157
- outer:
158
- for (int i = 0; i < curr.length; i++) {
159
- IRubyObject n = curr[i];
160
-
161
- for (int j = 0; j < other.length; j++) {
162
- if (other[j] == n) {
163
- result[last++] = n;
164
- continue outer;
165
- }
166
- }
167
- }
168
+ @JRubyMethod
169
+ public IRubyObject
170
+ delete (ThreadContext context, IRubyObject node_or_namespace)
171
+ {
172
+ IRubyObject nodeOrNamespace = asXmlNodeOrNamespace(context, node_or_namespace);
168
173
 
169
- XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
170
- newSet.initializeFrom(context, this);
171
- return newSet;
174
+ if (nodes.length == 0) {
175
+ return context.nil;
172
176
  }
173
177
 
174
- @JRubyMethod
175
- public IRubyObject delete(ThreadContext context, IRubyObject node_or_namespace) {
176
- IRubyObject nodeOrNamespace = asXmlNodeOrNamespace(context, node_or_namespace);
177
-
178
- if (nodes.length == 0) {
179
- return context.nil;
180
- }
181
-
182
- IRubyObject[] orig = nodes;
183
- IRubyObject[] result = new IRubyObject[nodes.length];
184
-
185
- int last = 0;
186
-
187
- for (int i = 0; i < orig.length; i++) {
188
- IRubyObject n = orig[i];
189
-
190
- if (n == nodeOrNamespace) {
191
- continue;
192
- }
193
-
194
- result[last++] = n;
195
- }
178
+ IRubyObject[] orig = nodes;
179
+ IRubyObject[] result = new IRubyObject[nodes.length];
196
180
 
197
- if (nodeOrNamespace instanceof XmlNamespace) {
198
- ((XmlNamespace) nodeOrNamespace).deleteHref();
199
- }
181
+ int last = 0;
200
182
 
201
- nodes = Arrays.copyOf(result, last);
183
+ for (int i = 0; i < orig.length; i++) {
184
+ IRubyObject n = orig[i];
202
185
 
203
- if (nodes.length < orig.length) {
204
- // if we found the node return it
205
- return nodeOrNamespace;
206
- }
186
+ if (n == nodeOrNamespace) {
187
+ continue;
188
+ }
207
189
 
208
- return context.nil;
190
+ result[last++] = n;
209
191
  }
210
192
 
211
- @JRubyMethod
212
- public IRubyObject dup(ThreadContext context) {
213
- XmlNodeSet dup = newNodeSet(context.runtime, nodes.clone());
214
- dup.initializeFrom(context, this);
215
- return dup;
193
+ if (nodeOrNamespace instanceof XmlNamespace) {
194
+ ((XmlNamespace) nodeOrNamespace).deleteHref();
216
195
  }
217
196
 
218
- @JRubyMethod(name = "include?")
219
- public IRubyObject include_p(ThreadContext context, IRubyObject node_or_namespace) {
220
- for (int i = 0; i < nodes.length; i++) {
221
- if (nodes[i] == node_or_namespace) {
222
- return context.tru;
223
- }
224
- }
197
+ nodes = Arrays.copyOf(result, last);
225
198
 
226
- return context.fals;
199
+ if (nodes.length < orig.length) {
200
+ // if we found the node return it
201
+ return nodeOrNamespace;
227
202
  }
228
203
 
229
- @JRubyMethod(name = {"length", "size"})
230
- public IRubyObject length(ThreadContext context) {
231
- return context.runtime.newFixnum(nodes.length);
204
+ return context.nil;
205
+ }
206
+
207
+ @JRubyMethod
208
+ public IRubyObject
209
+ dup(ThreadContext context)
210
+ {
211
+ XmlNodeSet dup = newNodeSet(context.runtime, nodes.clone());
212
+ dup.initializeFrom(context, this);
213
+ return dup;
214
+ }
215
+
216
+ @JRubyMethod(name = "include?")
217
+ public IRubyObject
218
+ include_p(ThreadContext context, IRubyObject node_or_namespace)
219
+ {
220
+ for (int i = 0; i < nodes.length; i++) {
221
+ if (nodes[i] == node_or_namespace) {
222
+ return context.tru;
223
+ }
232
224
  }
233
225
 
234
- @JRubyMethod(name="-")
235
- public IRubyObject op_diff(ThreadContext context, IRubyObject nodeSet) {
236
- IRubyObject[] otherNodes = getNodes(context, nodeSet);
237
-
238
- if (otherNodes.length == 0) {
239
- return dup(context);
240
- }
241
-
242
- if (nodes.length == 0) {
243
- return newEmptyNodeSet(context, this);
244
- }
245
-
246
- IRubyObject[] curr = nodes;
247
- IRubyObject[] other = getNodes(context, nodeSet);
248
- IRubyObject[] result = new IRubyObject[nodes.length];
226
+ return context.fals;
227
+ }
249
228
 
250
- int last = 0;
251
- outer:
252
- for (int i = 0; i < curr.length; i++) {
253
- IRubyObject n = curr[i];
229
+ @JRubyMethod(name = {"length", "size"})
230
+ public IRubyObject
231
+ length(ThreadContext context)
232
+ {
233
+ return context.runtime.newFixnum(nodes.length);
234
+ }
254
235
 
255
- for (int j = 0; j < other.length; j++) {
256
- if (other[j] == n) {
257
- continue outer;
258
- }
259
- }
236
+ @JRubyMethod(name = "-")
237
+ public IRubyObject
238
+ op_diff(ThreadContext context, IRubyObject nodeSet)
239
+ {
240
+ IRubyObject[] otherNodes = getNodes(context, nodeSet);
260
241
 
261
- result[last++] = n;
262
- }
242
+ if (otherNodes.length == 0) {
243
+ return dup(context);
244
+ }
263
245
 
264
- XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
265
- newSet.initializeFrom(context, this);
266
- return newSet;
246
+ if (nodes.length == 0) {
247
+ return newEmptyNodeSet(context, this);
267
248
  }
268
249
 
269
- @JRubyMethod(name={"|", "+"})
270
- public IRubyObject op_or(ThreadContext context, IRubyObject nodeSet) {
271
- IRubyObject[] otherNodes = getNodes(context, nodeSet);
250
+ IRubyObject[] curr = nodes;
251
+ IRubyObject[] other = getNodes(context, nodeSet);
252
+ IRubyObject[] result = new IRubyObject[nodes.length];
272
253
 
273
- if (nodes.length == 0) {
274
- return ((XmlNodeSet) nodeSet).dup(context);
275
- }
254
+ int last = 0;
255
+ outer:
256
+ for (int i = 0; i < curr.length; i++) {
257
+ IRubyObject n = curr[i];
276
258
 
277
- if (otherNodes.length == 0) {
278
- return dup(context);
259
+ for (int j = 0; j < other.length; j++) {
260
+ if (other[j] == n) {
261
+ continue outer;
279
262
  }
263
+ }
280
264
 
281
- IRubyObject[] curr = nodes;
282
- IRubyObject[] other = getNodes(context, nodeSet);
283
- IRubyObject[] result = Arrays.copyOf(curr, curr.length + other.length);
284
-
285
- int last = curr.length;
286
- outer:
287
- for (int i = 0; i < other.length; i++) {
288
- IRubyObject n = other[i];
265
+ result[last++] = n;
266
+ }
289
267
 
290
- for (int j = 0; j < curr.length; j++) {
291
- if (curr[j] == n) {
292
- continue outer;
293
- }
294
- }
268
+ XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
269
+ newSet.initializeFrom(context, this);
270
+ return newSet;
271
+ }
295
272
 
296
- result[last++] = n;
297
- }
273
+ @JRubyMethod(name = {"|", "+"})
274
+ public IRubyObject
275
+ op_or(ThreadContext context, IRubyObject nodeSet)
276
+ {
277
+ IRubyObject[] otherNodes = getNodes(context, nodeSet);
298
278
 
299
- XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
300
- newSet.initializeFrom(context, this);
301
- return newSet;
279
+ if (nodes.length == 0) {
280
+ return ((XmlNodeSet) nodeSet).dup(context);
302
281
  }
303
282
 
304
- @JRubyMethod(name = {"push", "<<"})
305
- public IRubyObject push(ThreadContext context, IRubyObject node_or_namespace) {
306
- nodes = Arrays.copyOf(nodes, nodes.length+1);
307
- nodes[nodes.length-1] = node_or_namespace;
308
- return this;
283
+ if (otherNodes.length == 0) {
284
+ return dup(context);
309
285
  }
310
286
 
311
- // replace with
312
- // https://github.com/jruby/jruby/blame/13a3ec76d883a162b9d46c374c6e9eeea27b3261/core/src/main/java/org/jruby/RubyRange.java#L974
313
- // once we upgraded the min JRuby version to >= 9.2
314
- private static IRubyObject rangeBeginLength(ThreadContext context, IRubyObject rangeMaybe, int len, int[] begLen) {
315
- RubyRange range = (RubyRange) rangeMaybe;
316
- int min = range.begin(context).convertToInteger().getIntValue();
317
- int max = range.end(context).convertToInteger().getIntValue();
318
-
319
- if (min < 0) {
320
- min += len;
321
- if (min < 0) {
322
- throw context.runtime.newRangeError(min + ".." + (range.isExcludeEnd() ? "." : "") + max + " out of range");
323
- }
324
- }
287
+ IRubyObject[] curr = nodes;
288
+ IRubyObject[] other = getNodes(context, nodeSet);
289
+ IRubyObject[] result = Arrays.copyOf(curr, curr.length + other.length);
325
290
 
326
- if (max < 0) {
327
- max += len;
328
- }
291
+ int last = curr.length;
292
+ outer:
293
+ for (int i = 0; i < other.length; i++) {
294
+ IRubyObject n = other[i];
329
295
 
330
- if (!range.isExcludeEnd()) {
331
- max++;
296
+ for (int j = 0; j < curr.length; j++) {
297
+ if (curr[j] == n) {
298
+ continue outer;
332
299
  }
300
+ }
333
301
 
334
- begLen[0] = min;
335
- begLen[1] = max;
336
- return context.tru;
302
+ result[last++] = n;
337
303
  }
338
304
 
339
-
340
- @JRubyMethod(name={"[]", "slice"})
341
- public IRubyObject slice(ThreadContext context, IRubyObject indexOrRange) {
342
- if (indexOrRange instanceof RubyFixnum) {
343
- return slice(context, ((RubyFixnum) indexOrRange).getIntValue());
344
- }
345
-
346
- int[] begLen = new int[2];
347
- rangeBeginLength(context, indexOrRange, nodes.length, begLen);
348
- int min = begLen[0];
349
- int max = begLen[1];
350
- return subseq(context, min, max - min);
305
+ XmlNodeSet newSet = newNodeSet(context.runtime, Arrays.copyOf(result, last));
306
+ newSet.initializeFrom(context, this);
307
+ return newSet;
308
+ }
309
+
310
+ @JRubyMethod(name = {"push", "<<"})
311
+ public IRubyObject
312
+ push(ThreadContext context, IRubyObject node_or_namespace)
313
+ {
314
+ nodes = Arrays.copyOf(nodes, nodes.length + 1);
315
+ nodes[nodes.length - 1] = node_or_namespace;
316
+ return this;
317
+ }
318
+
319
+ // replace with
320
+ // https://github.com/jruby/jruby/blame/13a3ec76d883a162b9d46c374c6e9eeea27b3261/core/src/main/java/org/jruby/RubyRange.java#L974
321
+ // once we upgraded the min JRuby version to >= 9.2
322
+ private static IRubyObject
323
+ rangeBeginLength(ThreadContext context, IRubyObject rangeMaybe, int len, int[] begLen)
324
+ {
325
+ RubyRange range = (RubyRange) rangeMaybe;
326
+ int min = range.begin(context).convertToInteger().getIntValue();
327
+ int max = range.end(context).convertToInteger().getIntValue();
328
+
329
+ if (min < 0) {
330
+ min += len;
331
+ if (min < 0) {
332
+ throw context.runtime.newRangeError(min + ".." + (range.isExcludeEnd() ? "." : "") + max + " out of range");
333
+ }
351
334
  }
352
335
 
353
- IRubyObject slice(ThreadContext context, int idx) {
354
- if (idx < 0) {
355
- idx += nodes.length;
356
- }
357
-
358
- if (idx >= nodes.length || idx < 0) {
359
- return context.nil;
360
- }
336
+ if (max < 0) {
337
+ max += len;
338
+ }
361
339
 
362
- return nodes[idx];
340
+ if (!range.isExcludeEnd()) {
341
+ max++;
363
342
  }
364
343
 
365
- @JRubyMethod(name={"[]", "slice"})
366
- public IRubyObject slice(ThreadContext context, IRubyObject start, IRubyObject length) {
367
- int s = ((RubyFixnum) start).getIntValue();
368
- int l = ((RubyFixnum) length).getIntValue();
344
+ begLen[0] = min;
345
+ begLen[1] = max;
346
+ return context.tru;
347
+ }
369
348
 
370
- if (s < 0) {
371
- s += nodes.length;
372
- }
373
349
 
374
- return subseq(context, s, l);
350
+ @JRubyMethod(name = {"[]", "slice"})
351
+ public IRubyObject
352
+ slice(ThreadContext context, IRubyObject indexOrRange)
353
+ {
354
+ if (indexOrRange instanceof RubyFixnum) {
355
+ return slice(context, ((RubyFixnum) indexOrRange).getIntValue());
375
356
  }
376
357
 
377
- public IRubyObject subseq(ThreadContext context, int start, int length) {
378
- if (start > nodes.length) {
379
- return context.nil;
380
- }
358
+ int[] begLen = new int[2];
359
+ rangeBeginLength(context, indexOrRange, nodes.length, begLen);
360
+ int min = begLen[0];
361
+ int max = begLen[1];
362
+ return subseq(context, min, max - min);
363
+ }
364
+
365
+ IRubyObject
366
+ slice(ThreadContext context, int idx)
367
+ {
368
+ if (idx < 0) {
369
+ idx += nodes.length;
370
+ }
381
371
 
382
- if (start < 0 || length < 0) {
383
- return context.nil;
384
- }
372
+ if (idx >= nodes.length || idx < 0) {
373
+ return context.nil;
374
+ }
385
375
 
386
- if (start + length > nodes.length) {
387
- length = nodes.length - start;
388
- }
376
+ return nodes[idx];
377
+ }
389
378
 
390
- int to = start + length;
379
+ @JRubyMethod(name = {"[]", "slice"})
380
+ public IRubyObject
381
+ slice(ThreadContext context, IRubyObject start, IRubyObject length)
382
+ {
383
+ int s = ((RubyFixnum) start).getIntValue();
384
+ int l = ((RubyFixnum) length).getIntValue();
391
385
 
392
- return newNodeSet(context.runtime, Arrays.copyOfRange(nodes, start, to));
386
+ if (s < 0) {
387
+ s += nodes.length;
393
388
  }
394
389
 
395
- @JRubyMethod(name = {"to_a", "to_ary"})
396
- public RubyArray to_a(ThreadContext context) {
397
- return context.runtime.newArrayNoCopy(nodes);
398
- }
390
+ return subseq(context, s, l);
391
+ }
399
392
 
400
- @JRubyMethod(name = {"unlink", "remove"})
401
- public IRubyObject unlink(ThreadContext context) {
402
- for (int i = 0; i < nodes.length; i++) {
403
- if (nodes[i] instanceof XmlNode) {
404
- ((XmlNode) nodes[i] ).unlink(context);
405
- }
406
- }
407
- return this;
393
+ public IRubyObject
394
+ subseq(ThreadContext context, int start, int length)
395
+ {
396
+ if (start > nodes.length) {
397
+ return context.nil;
408
398
  }
409
399
 
410
- private static IRubyObject asXmlNodeOrNamespace(ThreadContext context, IRubyObject possibleNode) {
411
- if (possibleNode instanceof XmlNode || possibleNode instanceof XmlNamespace) {
412
- return possibleNode;
413
- }
414
- throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace");
400
+ if (start < 0 || length < 0) {
401
+ return context.nil;
415
402
  }
416
403
 
417
- private static IRubyObject[] getNodes(ThreadContext context, IRubyObject possibleNodeSet) {
418
- if (possibleNodeSet instanceof XmlNodeSet) {
419
- return ((XmlNodeSet) possibleNodeSet).nodes;
420
- }
421
- throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
404
+ if (start + length > nodes.length) {
405
+ length = nodes.length - start;
422
406
  }
423
407
 
424
- public int getLength() {
425
- return nodes.length;
408
+ int to = start + length;
409
+
410
+ return newNodeSet(context.runtime, Arrays.copyOfRange(nodes, start, to));
411
+ }
412
+
413
+ @JRubyMethod(name = {"to_a", "to_ary"})
414
+ public RubyArray
415
+ to_a(ThreadContext context)
416
+ {
417
+ return context.runtime.newArrayNoCopy(nodes);
418
+ }
419
+
420
+ @JRubyMethod(name = {"unlink", "remove"})
421
+ public IRubyObject
422
+ unlink(ThreadContext context)
423
+ {
424
+ for (int i = 0; i < nodes.length; i++) {
425
+ if (nodes[i] instanceof XmlNode) {
426
+ ((XmlNode) nodes[i]).unlink(context);
427
+ }
426
428
  }
427
-
428
- public Node item(int index) {
429
- Object n = nodes[index];
430
- if (n instanceof XmlNode) return ((XmlNode) n).node;
431
- if (n instanceof XmlNamespace) return ((XmlNamespace) n).getNode();
432
- return null;
429
+ return this;
430
+ }
431
+
432
+ private static IRubyObject
433
+ asXmlNodeOrNamespace(ThreadContext context, IRubyObject possibleNode)
434
+ {
435
+ if (possibleNode instanceof XmlNode || possibleNode instanceof XmlNamespace) {
436
+ return possibleNode;
437
+ }
438
+ throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace");
439
+ }
440
+
441
+ private static IRubyObject[]
442
+ getNodes(ThreadContext context, IRubyObject possibleNodeSet)
443
+ {
444
+ if (possibleNodeSet instanceof XmlNodeSet) {
445
+ return ((XmlNodeSet) possibleNodeSet).nodes;
433
446
  }
447
+ throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
448
+ }
449
+
450
+ public int
451
+ getLength()
452
+ {
453
+ return nodes.length;
454
+ }
455
+
456
+ public Node
457
+ item(int index)
458
+ {
459
+ Object n = nodes[index];
460
+ if (n instanceof XmlNode) { return ((XmlNode) n).node; }
461
+ if (n instanceof XmlNamespace) { return ((XmlNamespace) n).getNode(); }
462
+ return null;
463
+ }
434
464
  }