rubyjedi-nokogiri_java 1.4.0.20100513161003-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (285) hide show
  1. data/.autotest +26 -0
  2. data/CHANGELOG.ja.rdoc +330 -0
  3. data/CHANGELOG.rdoc +341 -0
  4. data/Manifest.txt +277 -0
  5. data/README.ja.rdoc +105 -0
  6. data/README.rdoc +125 -0
  7. data/Rakefile +307 -0
  8. data/bin/nokogiri +49 -0
  9. data/deps.rip +5 -0
  10. data/ext/nokogiri/extconf.rb +149 -0
  11. data/ext/nokogiri/html_document.c +145 -0
  12. data/ext/nokogiri/html_document.h +10 -0
  13. data/ext/nokogiri/html_element_description.c +272 -0
  14. data/ext/nokogiri/html_element_description.h +10 -0
  15. data/ext/nokogiri/html_entity_lookup.c +32 -0
  16. data/ext/nokogiri/html_entity_lookup.h +8 -0
  17. data/ext/nokogiri/html_sax_parser_context.c +92 -0
  18. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  19. data/ext/nokogiri/nokogiri.c +96 -0
  20. data/ext/nokogiri/nokogiri.h +148 -0
  21. data/ext/nokogiri/xml_attr.c +92 -0
  22. data/ext/nokogiri/xml_attr.h +9 -0
  23. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  24. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  25. data/ext/nokogiri/xml_cdata.c +54 -0
  26. data/ext/nokogiri/xml_cdata.h +9 -0
  27. data/ext/nokogiri/xml_comment.c +52 -0
  28. data/ext/nokogiri/xml_comment.h +9 -0
  29. data/ext/nokogiri/xml_document.c +386 -0
  30. data/ext/nokogiri/xml_document.h +24 -0
  31. data/ext/nokogiri/xml_document_fragment.c +46 -0
  32. data/ext/nokogiri/xml_document_fragment.h +10 -0
  33. data/ext/nokogiri/xml_dtd.c +192 -0
  34. data/ext/nokogiri/xml_dtd.h +10 -0
  35. data/ext/nokogiri/xml_element_content.c +123 -0
  36. data/ext/nokogiri/xml_element_content.h +10 -0
  37. data/ext/nokogiri/xml_element_decl.c +69 -0
  38. data/ext/nokogiri/xml_element_decl.h +9 -0
  39. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  40. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  41. data/ext/nokogiri/xml_entity_decl.c +97 -0
  42. data/ext/nokogiri/xml_entity_decl.h +10 -0
  43. data/ext/nokogiri/xml_entity_reference.c +50 -0
  44. data/ext/nokogiri/xml_entity_reference.h +9 -0
  45. data/ext/nokogiri/xml_io.c +31 -0
  46. data/ext/nokogiri/xml_io.h +11 -0
  47. data/ext/nokogiri/xml_namespace.c +82 -0
  48. data/ext/nokogiri/xml_namespace.h +13 -0
  49. data/ext/nokogiri/xml_node.c +1080 -0
  50. data/ext/nokogiri/xml_node.h +13 -0
  51. data/ext/nokogiri/xml_node_set.c +405 -0
  52. data/ext/nokogiri/xml_node_set.h +9 -0
  53. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  54. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  55. data/ext/nokogiri/xml_reader.c +593 -0
  56. data/ext/nokogiri/xml_reader.h +10 -0
  57. data/ext/nokogiri/xml_relax_ng.c +159 -0
  58. data/ext/nokogiri/xml_relax_ng.h +9 -0
  59. data/ext/nokogiri/xml_sax_parser.c +283 -0
  60. data/ext/nokogiri/xml_sax_parser.h +43 -0
  61. data/ext/nokogiri/xml_sax_parser_context.c +157 -0
  62. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  63. data/ext/nokogiri/xml_sax_push_parser.c +114 -0
  64. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  65. data/ext/nokogiri/xml_schema.c +156 -0
  66. data/ext/nokogiri/xml_schema.h +9 -0
  67. data/ext/nokogiri/xml_syntax_error.c +52 -0
  68. data/ext/nokogiri/xml_syntax_error.h +13 -0
  69. data/ext/nokogiri/xml_text.c +48 -0
  70. data/ext/nokogiri/xml_text.h +9 -0
  71. data/ext/nokogiri/xml_xpath.c +53 -0
  72. data/ext/nokogiri/xml_xpath.h +11 -0
  73. data/ext/nokogiri/xml_xpath_context.c +239 -0
  74. data/ext/nokogiri/xml_xpath_context.h +9 -0
  75. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  76. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  77. data/lib/isorelax.jar +0 -0
  78. data/lib/jing.jar +0 -0
  79. data/lib/nekodtd.jar +0 -0
  80. data/lib/nekohtml.jar +0 -0
  81. data/lib/nokogiri.rb +123 -0
  82. data/lib/nokogiri/css.rb +25 -0
  83. data/lib/nokogiri/css/generated_parser.rb +659 -0
  84. data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
  85. data/lib/nokogiri/css/node.rb +99 -0
  86. data/lib/nokogiri/css/parser.rb +82 -0
  87. data/lib/nokogiri/css/parser.y +230 -0
  88. data/lib/nokogiri/css/syntax_error.rb +7 -0
  89. data/lib/nokogiri/css/tokenizer.rb +7 -0
  90. data/lib/nokogiri/css/tokenizer.rex +55 -0
  91. data/lib/nokogiri/css/xpath_visitor.rb +164 -0
  92. data/lib/nokogiri/decorators/slop.rb +33 -0
  93. data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
  94. data/lib/nokogiri/ffi/html/document.rb +28 -0
  95. data/lib/nokogiri/ffi/html/element_description.rb +81 -0
  96. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  97. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  98. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  99. data/lib/nokogiri/ffi/libxml.rb +372 -0
  100. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  101. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  102. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  103. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  104. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  105. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  106. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  107. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
  108. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  109. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  110. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  111. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  112. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  113. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  114. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  115. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  116. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  117. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  118. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  119. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  120. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  121. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
  122. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  123. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  124. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  125. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  126. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  127. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  128. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  129. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  130. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  131. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  132. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  133. data/lib/nokogiri/ffi/xml/document.rb +135 -0
  134. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  135. data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
  136. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  137. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  138. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  139. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  140. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  141. data/lib/nokogiri/ffi/xml/node.rb +465 -0
  142. data/lib/nokogiri/ffi/xml/node_set.rb +146 -0
  143. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  144. data/lib/nokogiri/ffi/xml/reader.rb +227 -0
  145. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  146. data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
  147. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  148. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +55 -0
  149. data/lib/nokogiri/ffi/xml/schema.rb +92 -0
  150. data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
  151. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  152. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  153. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  154. data/lib/nokogiri/ffi/xslt/stylesheet.rb +50 -0
  155. data/lib/nokogiri/html.rb +36 -0
  156. data/lib/nokogiri/html/builder.rb +35 -0
  157. data/lib/nokogiri/html/document.rb +88 -0
  158. data/lib/nokogiri/html/document_fragment.rb +15 -0
  159. data/lib/nokogiri/html/element_description.rb +23 -0
  160. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  161. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  162. data/lib/nokogiri/html/sax/parser.rb +48 -0
  163. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  164. data/lib/nokogiri/nokogiri.jar +0 -0
  165. data/lib/nokogiri/syntax_error.rb +4 -0
  166. data/lib/nokogiri/version.rb +33 -0
  167. data/lib/nokogiri/version_warning.rb +11 -0
  168. data/lib/nokogiri/xml.rb +67 -0
  169. data/lib/nokogiri/xml/attr.rb +14 -0
  170. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  171. data/lib/nokogiri/xml/builder.rb +405 -0
  172. data/lib/nokogiri/xml/cdata.rb +11 -0
  173. data/lib/nokogiri/xml/character_data.rb +7 -0
  174. data/lib/nokogiri/xml/document.rb +163 -0
  175. data/lib/nokogiri/xml/document_fragment.rb +73 -0
  176. data/lib/nokogiri/xml/dtd.rb +11 -0
  177. data/lib/nokogiri/xml/element_content.rb +36 -0
  178. data/lib/nokogiri/xml/element_decl.rb +13 -0
  179. data/lib/nokogiri/xml/entity_decl.rb +15 -0
  180. data/lib/nokogiri/xml/fragment_handler.rb +73 -0
  181. data/lib/nokogiri/xml/namespace.rb +13 -0
  182. data/lib/nokogiri/xml/node.rb +730 -0
  183. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  184. data/lib/nokogiri/xml/node_set.rb +318 -0
  185. data/lib/nokogiri/xml/notation.rb +6 -0
  186. data/lib/nokogiri/xml/parse_options.rb +85 -0
  187. data/lib/nokogiri/xml/pp.rb +2 -0
  188. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  189. data/lib/nokogiri/xml/pp/node.rb +56 -0
  190. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  191. data/lib/nokogiri/xml/reader.rb +74 -0
  192. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  193. data/lib/nokogiri/xml/sax.rb +4 -0
  194. data/lib/nokogiri/xml/sax/document.rb +160 -0
  195. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  196. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  197. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  198. data/lib/nokogiri/xml/schema.rb +61 -0
  199. data/lib/nokogiri/xml/syntax_error.rb +43 -0
  200. data/lib/nokogiri/xml/xpath.rb +10 -0
  201. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  202. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  203. data/lib/nokogiri/xslt.rb +48 -0
  204. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  205. data/lib/xercesImpl.jar +0 -0
  206. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  207. data/tasks/test.rb +100 -0
  208. data/test/css/test_nthiness.rb +159 -0
  209. data/test/css/test_parser.rb +282 -0
  210. data/test/css/test_tokenizer.rb +190 -0
  211. data/test/css/test_xpath_visitor.rb +76 -0
  212. data/test/ffi/test_document.rb +35 -0
  213. data/test/files/2ch.html +108 -0
  214. data/test/files/address_book.rlx +12 -0
  215. data/test/files/address_book.xml +10 -0
  216. data/test/files/bar/bar.xsd +4 -0
  217. data/test/files/dont_hurt_em_why.xml +422 -0
  218. data/test/files/exslt.xml +8 -0
  219. data/test/files/exslt.xslt +35 -0
  220. data/test/files/foo/foo.xsd +4 -0
  221. data/test/files/po.xml +32 -0
  222. data/test/files/po.xsd +66 -0
  223. data/test/files/shift_jis.html +10 -0
  224. data/test/files/shift_jis.xml +5 -0
  225. data/test/files/snuggles.xml +3 -0
  226. data/test/files/staff.dtd +10 -0
  227. data/test/files/staff.xml +59 -0
  228. data/test/files/staff.xslt +32 -0
  229. data/test/files/tlm.html +850 -0
  230. data/test/files/valid_bar.xml +2 -0
  231. data/test/helper.rb +137 -0
  232. data/test/html/sax/test_parser.rb +83 -0
  233. data/test/html/sax/test_parser_context.rb +48 -0
  234. data/test/html/test_builder.rb +164 -0
  235. data/test/html/test_document.rb +385 -0
  236. data/test/html/test_document_encoding.rb +77 -0
  237. data/test/html/test_document_fragment.rb +157 -0
  238. data/test/html/test_element_description.rb +98 -0
  239. data/test/html/test_named_characters.rb +14 -0
  240. data/test/html/test_node.rb +242 -0
  241. data/test/html/test_node_encoding.rb +27 -0
  242. data/test/test_convert_xpath.rb +135 -0
  243. data/test/test_css_cache.rb +45 -0
  244. data/test/test_encoding_handler.rb +46 -0
  245. data/test/test_jruby.rb +40 -0
  246. data/test/test_memory_leak.rb +87 -0
  247. data/test/test_nokogiri.rb +140 -0
  248. data/test/test_reader.rb +358 -0
  249. data/test/test_soap4r_sax.rb +52 -0
  250. data/test/test_xslt_transforms.rb +150 -0
  251. data/test/xml/node/test_save_options.rb +20 -0
  252. data/test/xml/node/test_subclass.rb +44 -0
  253. data/test/xml/sax/test_parser.rb +314 -0
  254. data/test/xml/sax/test_parser_context.rb +63 -0
  255. data/test/xml/sax/test_push_parser.rb +135 -0
  256. data/test/xml/test_attr.rb +38 -0
  257. data/test/xml/test_attribute_decl.rb +90 -0
  258. data/test/xml/test_builder.rb +167 -0
  259. data/test/xml/test_cdata.rb +38 -0
  260. data/test/xml/test_comment.rb +29 -0
  261. data/test/xml/test_document.rb +638 -0
  262. data/test/xml/test_document_encoding.rb +26 -0
  263. data/test/xml/test_document_fragment.rb +149 -0
  264. data/test/xml/test_dtd.rb +92 -0
  265. data/test/xml/test_dtd_encoding.rb +33 -0
  266. data/test/xml/test_element_content.rb +56 -0
  267. data/test/xml/test_element_decl.rb +73 -0
  268. data/test/xml/test_entity_decl.rb +83 -0
  269. data/test/xml/test_entity_reference.rb +21 -0
  270. data/test/xml/test_namespace.rb +70 -0
  271. data/test/xml/test_node.rb +740 -0
  272. data/test/xml/test_node_attributes.rb +34 -0
  273. data/test/xml/test_node_encoding.rb +107 -0
  274. data/test/xml/test_node_reparenting.rb +279 -0
  275. data/test/xml/test_node_set.rb +577 -0
  276. data/test/xml/test_parse_options.rb +52 -0
  277. data/test/xml/test_processing_instruction.rb +30 -0
  278. data/test/xml/test_reader_encoding.rb +126 -0
  279. data/test/xml/test_relax_ng.rb +60 -0
  280. data/test/xml/test_schema.rb +89 -0
  281. data/test/xml/test_syntax_error.rb +12 -0
  282. data/test/xml/test_text.rb +30 -0
  283. data/test/xml/test_unparented_node.rb +381 -0
  284. data/test/xml/test_xpath.rb +169 -0
  285. metadata +477 -0
@@ -0,0 +1,34 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNodeAttributes < Nokogiri::TestCase
6
+ def test_attribute_with_ns
7
+ doc = Nokogiri::XML <<-eoxml
8
+ <root xmlns:tlm='http://tenderlovemaking.com/'>
9
+ <node tlm:foo='bar' foo='baz' />
10
+ </root>
11
+ eoxml
12
+
13
+ node = doc.at('node')
14
+
15
+ assert_equal 'bar',
16
+ node.attribute_with_ns('foo', 'http://tenderlovemaking.com/').value
17
+ end
18
+
19
+ def test_namespace_key?
20
+ doc = Nokogiri::XML <<-eoxml
21
+ <root xmlns:tlm='http://tenderlovemaking.com/'>
22
+ <node tlm:foo='bar' foo='baz' />
23
+ </root>
24
+ eoxml
25
+
26
+ node = doc.at('node')
27
+
28
+ assert node.namespaced_key?('foo', 'http://tenderlovemaking.com/')
29
+ assert node.namespaced_key?('foo', nil)
30
+ assert !node.namespaced_key?('foo', 'foo')
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,107 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ if RUBY_VERSION =~ /^1\.9/
6
+ class TestNodeEncoding < Nokogiri::TestCase
7
+ def setup
8
+ super
9
+ @html = Nokogiri::HTML(File.read(HTML_FILE), HTML_FILE)
10
+ end
11
+
12
+ def test_get_attribute
13
+ node = @html.css('a').first
14
+ assert_equal @html.encoding, node['href'].encoding.name
15
+ end
16
+
17
+ def test_text_encoding_is_utf_8
18
+ @html = Nokogiri::HTML(File.open(NICH_FILE))
19
+ assert_equal 'UTF-8', @html.text.encoding.name
20
+ end
21
+
22
+ def test_serialize_encoding_html
23
+ @html = Nokogiri::HTML(File.open(NICH_FILE))
24
+ assert_equal @html.encoding.downcase,
25
+ @html.serialize.encoding.name.downcase
26
+
27
+ @doc = Nokogiri::HTML(@html.serialize)
28
+ assert_equal @html.serialize, @doc.serialize
29
+ end
30
+
31
+ def test_serialize_encoding_xml
32
+ @xml = Nokogiri::XML(File.open(SHIFT_JIS_XML))
33
+ assert_equal @xml.encoding.downcase,
34
+ @xml.serialize.encoding.name.downcase
35
+
36
+ @doc = Nokogiri::XML(@xml.serialize)
37
+ assert_equal @xml.serialize, @doc.serialize
38
+ end
39
+
40
+ def test_encode_special_chars
41
+ foo = @html.css('a').first.encode_special_chars('foo')
42
+ assert_equal @html.encoding, foo.encoding.name
43
+ end
44
+
45
+ def test_content
46
+ node = @html.css('a').first
47
+ assert_equal @html.encoding, node.content.encoding.name
48
+ end
49
+
50
+ def test_name
51
+ node = @html.css('a').first
52
+ assert_equal @html.encoding, node.name.encoding.name
53
+ end
54
+
55
+ def test_path
56
+ node = @html.css('a').first
57
+ assert_equal @html.encoding, node.path.encoding.name
58
+ end
59
+
60
+ def test_namespace
61
+ xml = <<-eoxml
62
+ <root>
63
+ <car xmlns:part="http://general-motors.com/">
64
+ <part:tire>Michelin Model XGV</part:tire>
65
+ </car>
66
+ <bicycle xmlns:part="http://schwinn.com/">
67
+ <part:tire>I'm a bicycle tire!</part:tire>
68
+ </bicycle>
69
+ </root>
70
+ eoxml
71
+ doc = Nokogiri::XML(xml, nil, 'UTF-8')
72
+ assert_equal 'UTF-8', doc.encoding
73
+ n = doc.xpath('//part:tire', { 'part' => 'http://schwinn.com/' }).first
74
+ assert n
75
+ assert_equal doc.encoding, n.namespace.href.encoding.name
76
+ assert_equal doc.encoding, n.namespace.prefix.encoding.name
77
+ end
78
+
79
+ def test_namespace_as_hash
80
+ xml = <<-eoxml
81
+ <root>
82
+ <car xmlns:part="http://general-motors.com/">
83
+ <part:tire>Michelin Model XGV</part:tire>
84
+ </car>
85
+ <bicycle xmlns:part="http://schwinn.com/">
86
+ <part:tire>I'm a bicycle tire!</part:tire>
87
+ </bicycle>
88
+ </root>
89
+ eoxml
90
+ doc = Nokogiri::XML(xml, nil, 'UTF-8')
91
+ assert_equal 'UTF-8', doc.encoding
92
+ assert n = doc.xpath('//car').first
93
+
94
+ n.namespace_definitions.each do |nd|
95
+ assert_equal doc.encoding, nd.href.encoding.name
96
+ assert_equal doc.encoding, nd.prefix.encoding.name
97
+ end
98
+
99
+ n.namespaces.each do |k,v|
100
+ assert_equal doc.encoding, k.encoding.name
101
+ assert_equal doc.encoding, v.encoding.name
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,279 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNodeReparenting < Nokogiri::TestCase
6
+
7
+ def setup
8
+ super
9
+ @xml = Nokogiri::XML "<root><a1>First node</a1><a2>Second node</a2><a3>Third node</a3></root>"
10
+ end
11
+
12
+ def test_add_child_should_insert_at_end_of_children
13
+ text_node = Nokogiri::XML::Text.new('hello', @xml)
14
+ assert_equal Nokogiri::XML::Node::TEXT_NODE, text_node.type
15
+
16
+ @xml.root.add_child text_node
17
+
18
+ assert_equal @xml.root.children.last.content, 'hello'
19
+ end
20
+
21
+ def test_add_child_fragment_should_insert_fragment_roots_at_end_of_children
22
+ node = @xml.root.children[1]
23
+ fragment = Nokogiri::XML.fragment("<b1>foo</b1><b2>bar</b2>")
24
+ fc1 = fragment.children[0]
25
+ fc2 = fragment.children[1]
26
+
27
+ node.add_child fragment
28
+
29
+ assert_equal 3, node.children.length
30
+ assert_equal fc1, node.children[1]
31
+ assert_equal fc2, node.children[2]
32
+ end
33
+
34
+ def test_chevron_works_as_add_child
35
+ text_node = Nokogiri::XML::Text.new('hello', @xml)
36
+ assert_equal Nokogiri::XML::Node::TEXT_NODE, text_node.type
37
+
38
+ @xml.root << text_node
39
+
40
+ assert_equal @xml.root.children.last.content, 'hello'
41
+ end
42
+
43
+ def test_add_child_already_in_the_document_should_move_the_node
44
+ third_node = @xml.root.children.last
45
+ first_node = @xml.root.children.first
46
+
47
+ @xml.root.add_child(first_node)
48
+
49
+ assert_equal 2, @xml.root.children.index(first_node)
50
+ assert_equal 1, @xml.root.children.index(third_node)
51
+ end
52
+
53
+ def test_add_child_from_other_document_should_remove_from_old_document
54
+ d1 = Nokogiri::XML("<root><item>1</item><item>2</item></root>")
55
+ d2 = Nokogiri::XML("<root><item>3</item><item>4</item></root>")
56
+
57
+ d2.at('root').search('item').each do |item|
58
+ d1.at('root').add_child item
59
+ end
60
+
61
+ assert_equal 0, d2.search('item').size
62
+ assert_equal 4, d1.search('item').size
63
+ end
64
+
65
+ def test_add_child_text_node_should_merge_with_adjacent_text_nodes
66
+ node = @xml.root.children.first
67
+ old_child = node.children.first
68
+ new_child = Nokogiri::XML::Text.new('text', @xml)
69
+
70
+ node.add_child new_child
71
+
72
+ assert_equal "First nodetext", node.children.first.content
73
+ assert_equal "First nodetext", new_child.content
74
+ assert_equal "First nodetext", old_child.content
75
+ end
76
+
77
+ def test_add_child_node_following_sequential_text_nodes_should_have_right_path
78
+ node = @xml.root.children.first
79
+ node.add_child(Nokogiri::XML::Text.new('text', @xml))
80
+
81
+ item = node.add_child(Nokogiri::XML::Element.new('item', @xml))
82
+
83
+ assert_equal '/root/a1/item', item.path
84
+ end
85
+
86
+ def test_add_child_node_with_namespace_should_keep_namespace
87
+ doc = Nokogiri::XML::Document.new
88
+ item = Nokogiri::XML::Element.new('item', doc)
89
+ doc.root = item
90
+
91
+ entry = Nokogiri::XML::Element.new('entry', doc)
92
+ entry.add_namespace('tlm', 'http://tenderlovemaking.com')
93
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
94
+ item.add_child(entry)
95
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
96
+ end
97
+
98
+ def test_add_child_node_should_inherit_namespace
99
+ doc = Nokogiri::XML(<<-eoxml)
100
+ <root xmlns="http://tenderlovemaking.com/">
101
+ <first>
102
+ </first>
103
+ </root>
104
+ eoxml
105
+ assert node = doc.at('//xmlns:first')
106
+ child = Nokogiri::XML::Node.new('second', doc)
107
+ node.add_child(child)
108
+ assert doc.at('//xmlns:second')
109
+ end
110
+
111
+ def test_add_child_node_should_not_inherit_namespace_if_it_has_one
112
+ doc = Nokogiri::XML(<<-eoxml)
113
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="http://flavorjon.es/">
114
+ <first>
115
+ </first>
116
+ </root>
117
+ eoxml
118
+ assert node = doc.at('//xmlns:first')
119
+ child = Nokogiri::XML::Node.new('second', doc)
120
+
121
+ ns = doc.root.namespace_definitions.detect { |x| x.prefix == "foo" }
122
+ child.namespace = ns
123
+
124
+ node.add_child(child)
125
+ assert doc.at('//foo:second', "foo" => "http://flavorjon.es/")
126
+ end
127
+
128
+ def test_replace_node_should_remove_previous_node_and_insert_new_node
129
+ second_node = @xml.root.children[1]
130
+
131
+ new_node = Nokogiri::XML::Node.new('foo', @xml)
132
+ second_node.replace(new_node)
133
+
134
+ assert_equal @xml.root.children[1], new_node
135
+ assert_nil second_node.parent
136
+ end
137
+
138
+ def test_replace_fragment_should_replace_node_with_fragment_roots
139
+ node = @xml.root.children[1]
140
+ fragment = Nokogiri::XML.fragment("<b1>foo</b1><b2>bar</b2>")
141
+ fc1 = fragment.children[0]
142
+ fc2 = fragment.children[1]
143
+
144
+ node.replace fragment
145
+
146
+ assert_equal 4, @xml.root.children.length
147
+ assert_equal fc1, @xml.root.children[1]
148
+ assert_equal fc2, @xml.root.children[2]
149
+ end
150
+
151
+ def test_replace_with_default_namespaces
152
+ fruits = Nokogiri::XML(<<-eoxml)
153
+ <fruit xmlns="http://fruits.org">
154
+ <apple />
155
+ </fruit>
156
+ eoxml
157
+
158
+ apple = fruits.css('apple').first
159
+
160
+ orange = Nokogiri::XML::Node.new('orange', fruits)
161
+ apple.replace(orange)
162
+
163
+ assert_equal orange, fruits.css('orange').first
164
+ end
165
+
166
+ def test_illegal_replace_of_node_with_doc
167
+ new_node = Nokogiri::XML.parse('<foo>bar</foo>')
168
+ old_node = @xml.at_css('a1')
169
+ assert_raises(ArgumentError){ old_node.replace new_node }
170
+ end
171
+
172
+ def test_replace_with_node_from_different_docs
173
+ xml1 = "<test> <caption>Original caption</caption> </test>"
174
+ xml2 = "<test> <caption>Replacement caption</caption> </test>"
175
+ doc1 = Nokogiri::XML(xml1)
176
+ doc2 = Nokogiri::XML(xml2)
177
+ caption1 = doc1.xpath("//caption")[0]
178
+ caption2 = doc2.xpath("//caption")[0]
179
+ caption1.replace(caption2) # this segfaulted under 1.4.0 and earlier
180
+ assert_equal "Replacement caption", doc1.css("caption").inner_text
181
+ end
182
+
183
+ def test_add_next_sibling_should_insert_after
184
+ node = @xml.root.children[1]
185
+ new_node = Nokogiri::XML::Node.new('foo', @xml)
186
+
187
+ node.add_next_sibling(new_node)
188
+ assert_equal 1, @xml.root.children.index(node)
189
+ assert_equal 2, @xml.root.children.index(new_node)
190
+ end
191
+
192
+ def test_add_next_sibling_fragment_should_insert_fragment_roots_after
193
+ node = @xml.root.children[1]
194
+ fragment = Nokogiri::XML.fragment("<b1>foo</b1><b2>bar</b2>")
195
+ fc1 = fragment.children[0]
196
+ fc2 = fragment.children[1]
197
+
198
+ node.add_next_sibling fragment
199
+
200
+ assert_equal 5, @xml.root.children.length
201
+ assert_equal fc1, @xml.root.children[2]
202
+ assert_equal fc2, @xml.root.children[3]
203
+ end
204
+
205
+ def test_add_next_sibling_text_node_should_merge_with_adjacent_text_nodes
206
+ node = @xml.root.children.first
207
+ text = node.children.first
208
+ new_text = Nokogiri::XML::Text.new('text', @xml)
209
+
210
+ text.add_next_sibling new_text
211
+
212
+ assert_equal "First nodetext", node.children.first.content
213
+ assert_equal "First nodetext", text.content
214
+ assert_equal "First nodetext", new_text.content
215
+ end
216
+
217
+ def test_add_previous_sibling_should_insert_before
218
+ node = @xml.root.children[1]
219
+ new_node = Nokogiri::XML::Node.new('foo', @xml)
220
+
221
+ node.add_previous_sibling(new_node)
222
+ assert_equal 1, @xml.root.children.index(new_node)
223
+ assert_equal 2, @xml.root.children.index(node)
224
+ end
225
+
226
+ def test_add_previous_sibling_fragment_should_insert_fragment_roots_before
227
+ node = @xml.root.children[1]
228
+ fragment = Nokogiri::XML.fragment("<b1>foo</b1><b2>bar</b2>")
229
+ fc1 = fragment.children[0]
230
+ fc2 = fragment.children[1]
231
+
232
+ node.add_previous_sibling fragment
233
+
234
+ assert_equal 5, @xml.root.children.length
235
+ assert_equal fc1, @xml.root.children[1]
236
+ assert_equal fc2, @xml.root.children[2]
237
+ end
238
+
239
+ def test_add_previous_sibling_text_node_should_merge_with_adjacent_text_nodes
240
+ node = @xml.root.children.first
241
+ text = node.children.first
242
+ new_text = Nokogiri::XML::Text.new('text', @xml)
243
+
244
+ text.add_previous_sibling new_text
245
+
246
+ assert_equal "textFirst node", node.children.first.content
247
+ assert_equal "textFirst node", text.content
248
+ assert_equal "textFirst node", new_text.content
249
+ end
250
+
251
+ def test_unlink_then_reparent
252
+ # see http://github.com/tenderlove/nokogiri/issues#issue/22
253
+ 10.times do
254
+ STDOUT.putc "."
255
+ STDOUT.flush
256
+ begin
257
+ doc = Nokogiri::XML <<-EOHTML
258
+ <root>
259
+ <a>
260
+ <b/>
261
+ <c/>
262
+ </a>
263
+ </root>
264
+ EOHTML
265
+
266
+ root = doc.at("root")
267
+ a = root.at("a")
268
+ b = a.at("b")
269
+ c = a.at("c")
270
+ a.add_next_sibling(b.unlink)
271
+ c.unlink
272
+ end
273
+ GC.start
274
+ end
275
+ end
276
+
277
+ end
278
+ end
279
+ end
@@ -0,0 +1,577 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNodeSet < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
9
+ @list = @xml.css('employee')
10
+ end
11
+
12
+ def test_remove_attr
13
+ @list.each { |x| x['class'] = 'blah' }
14
+ assert_equal @list, @list.remove_attr('class')
15
+ @list.each { |x| assert_nil x['class'] }
16
+ end
17
+
18
+ def test_add_class
19
+ assert_equal @list, @list.add_class('bar')
20
+ @list.each { |x| assert_equal 'bar', x['class'] }
21
+
22
+ @list.add_class('bar')
23
+ @list.each { |x| assert_equal 'bar', x['class'] }
24
+
25
+ @list.add_class('baz')
26
+ @list.each { |x| assert_equal 'bar baz', x['class'] }
27
+ end
28
+
29
+ def test_remove_class_with_no_class
30
+ assert_equal @list, @list.remove_class('bar')
31
+ @list.each { |e| assert_nil e['class'] }
32
+
33
+ @list.each { |e| e['class'] = '' }
34
+ assert_equal @list, @list.remove_class('bar')
35
+ @list.each { |e| assert_nil e['class'] }
36
+ end
37
+
38
+ def test_remove_class_single
39
+ @list.each { |e| e['class'] = 'foo bar' }
40
+
41
+ assert_equal @list, @list.remove_class('bar')
42
+ @list.each { |e| assert_equal 'foo', e['class'] }
43
+ end
44
+
45
+ def test_remove_class_completely
46
+ @list.each { |e| e['class'] = 'foo' }
47
+
48
+ assert_equal @list, @list.remove_class
49
+ @list.each { |e| assert_nil e['class'] }
50
+ end
51
+
52
+ def test_attribute_set
53
+ @list.each { |e| assert_nil e['foo'] }
54
+
55
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
56
+ @list.send(t.first.to_sym, 'foo', t.last)
57
+ @list.each { |e| assert_equal t.last, e['foo'] }
58
+ end
59
+ end
60
+
61
+ def test_attribute_set_with_block
62
+ @list.each { |e| assert_nil e['foo'] }
63
+
64
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
65
+ @list.send(t.first.to_sym, 'foo') { |x| t.last }
66
+ @list.each { |e| assert_equal t.last, e['foo'] }
67
+ end
68
+ end
69
+
70
+ def test_attribute_set_with_hash
71
+ @list.each { |e| assert_nil e['foo'] }
72
+
73
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
74
+ @list.send(t.first.to_sym, 'foo' => t.last)
75
+ @list.each { |e| assert_equal t.last, e['foo'] }
76
+ end
77
+ end
78
+
79
+ def test_attribute_no_args
80
+ @list.first['foo'] = 'bar'
81
+ assert_equal @list.first.attribute('foo'), @list.attribute('foo')
82
+ end
83
+
84
+ def test_search_empty_node_set
85
+ set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
86
+ assert_equal 0, set.css('foo').length
87
+ assert_equal 0, set.xpath('.//foo').length
88
+ assert_equal 0, set.search('foo').length
89
+ end
90
+
91
+ def test_xpath_with_custom_object
92
+ set = @xml.xpath('//staff')
93
+ custom_employees = set.xpath('//*[awesome(.)]', Class.new {
94
+ def awesome ns
95
+ ns.select { |n| n.name == 'employee' }
96
+ end
97
+ }.new)
98
+
99
+ assert_equal @xml.xpath('//employee'), custom_employees
100
+ end
101
+
102
+ def test_css_with_custom_object
103
+ set = @xml.xpath('//staff')
104
+ custom_employees = set.css('*:awesome', Class.new {
105
+ def awesome ns
106
+ ns.select { |n| n.name == 'employee' }
107
+ end
108
+ }.new)
109
+
110
+ assert_equal @xml.xpath('//employee'), custom_employees
111
+ end
112
+
113
+ def test_search_with_custom_object
114
+ set = @xml.xpath('//staff')
115
+ custom_employees = set.search('//*[awesome(.)]', Class.new {
116
+ def awesome ns
117
+ ns.select { |n| n.name == 'employee' }
118
+ end
119
+ }.new)
120
+
121
+ assert_equal @xml.xpath('//employee'), custom_employees
122
+ end
123
+
124
+ def test_css_searches_match_self
125
+ html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
126
+ set = html.xpath("/html/body/div")
127
+ assert_equal set.first, set.css(".a").first
128
+ end
129
+
130
+ def test_search_with_css_matches_self
131
+ html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
132
+ set = html.xpath("/html/body/div")
133
+ assert_equal set.first, set.search(".a").first
134
+ end
135
+
136
+ def test_double_equal
137
+ assert node_set_one = @xml.xpath('//employee')
138
+ assert node_set_two = @xml.xpath('//employee')
139
+
140
+ assert_not_equal node_set_one.object_id, node_set_two.object_id
141
+
142
+ assert_equal node_set_one, node_set_two
143
+ end
144
+
145
+ def test_node_set_not_equal_to_string
146
+ node_set_one = @xml.xpath('//employee')
147
+ assert_not_equal node_set_one, "asdfadsf"
148
+ end
149
+
150
+ def test_out_of_order_not_equal
151
+ one = @xml.xpath('//employee')
152
+ two = @xml.xpath('//employee')
153
+ two.push two.shift
154
+ assert_not_equal one, two
155
+ end
156
+
157
+ def test_shorter_is_not_equal
158
+ node_set_one = @xml.xpath('//employee')
159
+ node_set_two = @xml.xpath('//employee')
160
+ node_set_two.delete(node_set_two.first)
161
+
162
+ assert_not_equal node_set_one, node_set_two
163
+ end
164
+
165
+ def test_pop
166
+ set = @xml.xpath('//employee')
167
+ last = set.last
168
+ assert_equal last, set.pop
169
+ end
170
+
171
+ def test_shift
172
+ set = @xml.xpath('//employee')
173
+ first = set.first
174
+ assert_equal first, set.shift
175
+ end
176
+
177
+ def test_shift_empty
178
+ set = Nokogiri::XML::NodeSet.new(@xml)
179
+ assert_nil set.shift
180
+ end
181
+
182
+ def test_pop_empty
183
+ set = Nokogiri::XML::NodeSet.new(@xml)
184
+ assert_nil set.pop
185
+ end
186
+
187
+ def test_first_takes_arguments
188
+ assert node_set = @xml.xpath('//employee')
189
+ assert_equal 2, node_set.first(2).length
190
+ end
191
+
192
+ def test_dup
193
+ assert node_set = @xml.xpath('//employee')
194
+ dup = node_set.dup
195
+ assert_equal node_set.length, dup.length
196
+ node_set.zip(dup).each do |a,b|
197
+ assert_equal a, b
198
+ end
199
+ end
200
+
201
+ def test_xmlns_is_automatically_registered
202
+ doc = Nokogiri::XML(<<-eoxml)
203
+ <root xmlns="http://tenderlovemaking.com/">
204
+ <foo>
205
+ <bar/>
206
+ </foo>
207
+ </root>
208
+ eoxml
209
+ set = doc.css('foo')
210
+ assert_equal 1, set.css('xmlns|bar').length
211
+ assert_equal 0, set.css('|bar').length
212
+ assert_equal 1, set.xpath('//xmlns:bar').length
213
+ assert_equal 1, set.search('xmlns|bar').length
214
+ assert_equal 1, set.search('//xmlns:bar').length
215
+ assert set.at('//xmlns:bar')
216
+ assert set.at('xmlns|bar')
217
+ assert set.at('bar')
218
+ end
219
+
220
+ def test_children_has_document
221
+ set = @xml.root.children
222
+ assert_instance_of(NodeSet, set)
223
+ assert_equal @xml, set.document
224
+ end
225
+
226
+ def test_length_size
227
+ assert node_set = @xml.search('//employee')
228
+ assert_equal node_set.length, node_set.size
229
+ end
230
+
231
+ def test_to_xml
232
+ assert node_set = @xml.search('//employee')
233
+ assert node_set.to_xml
234
+ end
235
+
236
+ def test_inner_html
237
+ doc = Nokogiri::HTML(<<-eohtml)
238
+ <html>
239
+ <body>
240
+ <div>
241
+ <a>one</a>
242
+ </div>
243
+ <div>
244
+ <a>two</a>
245
+ </div>
246
+ </body>
247
+ </html>
248
+ eohtml
249
+ assert html = doc.css('div').inner_html
250
+ assert_match '<a>', html
251
+ end
252
+
253
+ def test_at
254
+ assert node_set = @xml.search('//employee')
255
+ assert_equal node_set.first, node_set.at(0)
256
+ end
257
+
258
+ def test_percent
259
+ assert node_set = @xml.search('//employee')
260
+ assert_equal node_set.first, node_set % 0
261
+ end
262
+
263
+ def test_to_ary
264
+ assert node_set = @xml.search('//employee')
265
+ foo = []
266
+ foo += node_set
267
+ assert_equal node_set.length, foo.length
268
+ end
269
+
270
+ def test_push
271
+ node = Nokogiri::XML::Node.new('foo', @xml)
272
+ node.content = 'bar'
273
+
274
+ assert node_set = @xml.search('//employee')
275
+ node_set.push(node)
276
+
277
+ assert node_set.include?(node)
278
+ end
279
+
280
+ def test_delete_with_invalid_argument
281
+ employees = @xml.search("//employee")
282
+ positions = @xml.search("//position")
283
+
284
+ assert_raises(ArgumentError) { employees.delete(positions) }
285
+ end
286
+
287
+ def test_delete_when_present
288
+ employees = @xml.search("//employee")
289
+ wally = employees.first
290
+ assert employees.include?(wally) # testing setup
291
+ length = employees.length
292
+
293
+ result = employees.delete(wally)
294
+ assert_equal result, wally
295
+ assert ! employees.include?(wally)
296
+ assert length-1, employees.length
297
+ end
298
+
299
+ def test_delete_when_not_present
300
+ employees = @xml.search("//employee")
301
+ phb = @xml.search("//position").first
302
+ assert ! employees.include?(phb) # testing setup
303
+ length = employees.length
304
+
305
+ result = employees.delete(phb)
306
+ assert_nil result
307
+ assert length, employees.length
308
+ end
309
+
310
+ def test_unlink
311
+ xml = Nokogiri::XML.parse(<<-eoxml)
312
+ <root>
313
+ <a class='foo bar'>Bar</a>
314
+ <a class='bar foo'>Bar</a>
315
+ <a class='bar'>Bar</a>
316
+ <a>Hello world</a>
317
+ <a class='baz bar foo'>Bar</a>
318
+ <a class='bazbarfoo'>Awesome</a>
319
+ <a class='bazbar'>Awesome</a>
320
+ </root>
321
+ eoxml
322
+ set = xml.xpath('//a')
323
+ set.unlink
324
+ set.each do |node|
325
+ assert !node.parent
326
+ #assert !node.document
327
+ assert !node.previous_sibling
328
+ assert !node.next_sibling
329
+ end
330
+ assert_no_match(/Hello world/, xml.to_s)
331
+ end
332
+
333
+ def test_nodeset_search_takes_namespace
334
+ @xml = Nokogiri::XML.parse(<<-eoxml)
335
+ <root>
336
+ <car xmlns:part="http://general-motors.com/">
337
+ <part:tire>Michelin Model XGV</part:tire>
338
+ </car>
339
+ <bicycle xmlns:part="http://schwinn.com/">
340
+ <part:tire>I'm a bicycle tire!</part:tire>
341
+ </bicycle>
342
+ </root>
343
+ eoxml
344
+ set = @xml/'root'
345
+ assert_equal 1, set.length
346
+ bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
347
+ assert_equal 1, bike_tire.length
348
+ end
349
+
350
+ def test_new_nodeset
351
+ node_set = Nokogiri::XML::NodeSet.new(@xml)
352
+ assert_equal(0, node_set.length)
353
+ node = Nokogiri::XML::Node.new('form', @xml)
354
+ node_set << node
355
+ assert_equal(1, node_set.length)
356
+ assert_equal(node, node_set.last)
357
+ end
358
+
359
+ def test_search_on_nodeset
360
+ assert node_set = @xml.search('//employee')
361
+ assert sub_set = node_set.search('.//name')
362
+ assert_equal(node_set.length, sub_set.length)
363
+ end
364
+
365
+ def test_negative_index_works
366
+ assert node_set = @xml.search('//employee')
367
+ assert_equal node_set.last, node_set[-1]
368
+ end
369
+
370
+ def test_large_negative_index_returns_nil
371
+ assert node_set = @xml.search('//employee')
372
+ assert_nil(node_set[-1 * (node_set.length + 1)])
373
+ end
374
+
375
+ def test_node_set_fetches_private_data
376
+ assert node_set = @xml.search('//employee')
377
+
378
+ set = node_set
379
+ assert_equal(set[0], set[0])
380
+ end
381
+
382
+ def test_node_set_returns_0
383
+ assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
384
+ assert_equal(0, node_set.length)
385
+ end
386
+
387
+ def test_wrap
388
+ employees = (@xml/"//employee").wrap("<wrapper/>")
389
+ assert_equal 'wrapper', employees[0].parent.name
390
+ assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
391
+ end
392
+
393
+ def test_wrap_preserves_document_structure
394
+ assert_equal "employeeId",
395
+ @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
396
+ @xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
397
+ assert_equal "wrapper",
398
+ @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
399
+ end
400
+
401
+ def test_plus_operator
402
+ names = @xml.search("name")
403
+ positions = @xml.search("position")
404
+
405
+ names_len = names.length
406
+ positions_len = positions.length
407
+
408
+ assert_raises(ArgumentError) { result = names + positions.first }
409
+
410
+ result = names + positions
411
+ assert_equal names_len, names.length
412
+ assert_equal positions_len, positions.length
413
+ assert_equal names.length + positions.length, result.length
414
+
415
+ names += positions
416
+ assert_equal result.length, names.length
417
+ end
418
+
419
+ def test_union
420
+ names = @xml.search("name")
421
+
422
+ assert_equal(names.length, (names | @xml.search("name")).length)
423
+ end
424
+
425
+ def test_minus_operator
426
+ employees = @xml.search("//employee")
427
+ females = @xml.search("//employee[gender[text()='Female']]")
428
+
429
+ employees_len = employees.length
430
+ females_len = females.length
431
+
432
+ assert_raises(ArgumentError) { result = employees - females.first }
433
+
434
+ result = employees - females
435
+ assert_equal employees_len, employees.length
436
+ assert_equal females_len, females.length
437
+ assert_equal employees.length - females.length, result.length
438
+
439
+ employees -= females
440
+ assert_equal result.length, employees.length
441
+ end
442
+
443
+ def test_array_index
444
+ employees = @xml.search("//employee")
445
+ other = @xml.search("//position").first
446
+
447
+ assert_equal 3, employees.index(employees[3])
448
+ assert_nil employees.index(other)
449
+ end
450
+
451
+ def test_slice_too_far
452
+ employees = @xml.search("//employee")
453
+ assert_equal employees.length, employees[0, employees.length + 1].length
454
+ assert_equal employees.length, employees[0, employees.length].length
455
+ end
456
+
457
+ def test_array_slice_with_start_and_end
458
+ employees = @xml.search("//employee")
459
+ assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
460
+ end
461
+
462
+ def test_array_index_bracket_equivalence
463
+ employees = @xml.search("//employee")
464
+ assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
465
+ assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
466
+ end
467
+
468
+ def test_array_slice_with_negative_start
469
+ employees = @xml.search("//employee")
470
+ assert_equal [employees[2]], employees[-3,1].to_a
471
+ assert_equal [employees[2], employees[3]], employees[-3,2].to_a
472
+ end
473
+
474
+ def test_array_slice_with_invalid_args
475
+ employees = @xml.search("//employee")
476
+ assert_nil employees[99, 1] # large start
477
+ assert_nil employees[1, -1] # negative len
478
+ assert_equal [], employees[1, 0].to_a # zero len
479
+ end
480
+
481
+ def test_array_slice_with_range
482
+ employees = @xml.search("//employee")
483
+ assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
484
+ assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
485
+ end
486
+
487
+ def test_intersection_with_no_overlap
488
+ employees = @xml.search("//employee")
489
+ positions = @xml.search("//position")
490
+
491
+ assert_equal [], (employees & positions).to_a
492
+ end
493
+
494
+ def test_intersection
495
+ employees = @xml.search("//employee")
496
+ first_set = employees[0..2]
497
+ second_set = employees[2..4]
498
+
499
+ assert_equal [employees[2]], (first_set & second_set).to_a
500
+ end
501
+
502
+ def test_include?
503
+ employees = @xml.search("//employee")
504
+ yes = employees.first
505
+ no = @xml.search("//position").first
506
+
507
+ assert employees.include?(yes)
508
+ assert ! employees.include?(no)
509
+ end
510
+
511
+ def test_children
512
+ employees = @xml.search("//employee")
513
+ count = 0
514
+ employees.each do |employee|
515
+ count += employee.children.length
516
+ end
517
+ set = employees.children
518
+ assert_equal count, set.length
519
+ end
520
+
521
+ def test_inspect
522
+ employees = @xml.search("//employee")
523
+ inspected = employees.inspect
524
+
525
+ assert_equal "[#{employees.map { |x| x.inspect }.join(', ')}]",
526
+ inspected
527
+ end
528
+
529
+ def test_should_not_splode_when_accessing_namespace_declarations_in_a_node_set
530
+ xml = Nokogiri::XML "<foo></foo>"
531
+ node_set = xml.xpath("//namespace::*")
532
+ assert_equal 1, node_set.size
533
+ node = node_set.first
534
+ node.to_s # segfaults in 1.4.0 and earlier
535
+
536
+ # if we haven't segfaulted, let's make sure we handled it correctly
537
+ assert_instance_of Nokogiri::XML::Namespace, node
538
+ end
539
+
540
+ def test_should_not_splode_when_arrayifying_node_set_containing_namespace_declarations
541
+ xml = Nokogiri::XML "<foo></foo>"
542
+ node_set = xml.xpath("//namespace::*")
543
+ assert_equal 1, node_set.size
544
+
545
+ node_array = node_set.to_a
546
+ node = node_array.first
547
+ node.to_s # segfaults in 1.4.0 and earlier
548
+
549
+ # if we haven't segfaulted, let's make sure we handled it correctly
550
+ assert_instance_of Nokogiri::XML::Namespace, node
551
+ end
552
+
553
+ def test_should_not_splode_when_unlinking_node_set_containing_namespace_declarations
554
+ xml = Nokogiri::XML "<foo></foo>"
555
+ node_set = xml.xpath("//namespace::*")
556
+ assert_equal 1, node_set.size
557
+
558
+ node_set.unlink
559
+ end
560
+
561
+ def test_reverse
562
+ xml = Nokogiri::XML "<root><a />b<c />d<e /></root>"
563
+ children = xml.root.children
564
+ assert_instance_of Nokogiri::XML::NodeSet, children
565
+
566
+ reversed = children.reverse
567
+ assert_equal reversed[0], children[4]
568
+ assert_equal reversed[1], children[3]
569
+ assert_equal reversed[2], children[2]
570
+ assert_equal reversed[3], children[1]
571
+ assert_equal reversed[4], children[0]
572
+
573
+ assert_equal children, children.reverse.reverse
574
+ end
575
+ end
576
+ end
577
+ end