jwagener-nokogiri 1.4.1

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