nokogiri-backupify 1.5.0.beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. data/.autotest +26 -0
  2. data/CHANGELOG.ja.rdoc +509 -0
  3. data/CHANGELOG.rdoc +490 -0
  4. data/Manifest.txt +274 -0
  5. data/README.ja.rdoc +106 -0
  6. data/README.rdoc +150 -0
  7. data/Rakefile +217 -0
  8. data/bin/nokogiri +54 -0
  9. data/deps.rip +5 -0
  10. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  11. data/ext/java/nokogiri/HtmlDocument.java +146 -0
  12. data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
  13. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  14. data/ext/java/nokogiri/HtmlSaxParserContext.java +256 -0
  15. data/ext/java/nokogiri/NokogiriService.java +466 -0
  16. data/ext/java/nokogiri/XmlAttr.java +183 -0
  17. data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
  18. data/ext/java/nokogiri/XmlCdata.java +89 -0
  19. data/ext/java/nokogiri/XmlComment.java +84 -0
  20. data/ext/java/nokogiri/XmlDocument.java +514 -0
  21. data/ext/java/nokogiri/XmlDocumentFragment.java +216 -0
  22. data/ext/java/nokogiri/XmlDtd.java +464 -0
  23. data/ext/java/nokogiri/XmlElement.java +221 -0
  24. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  25. data/ext/java/nokogiri/XmlElementDecl.java +147 -0
  26. data/ext/java/nokogiri/XmlEntityDecl.java +161 -0
  27. data/ext/java/nokogiri/XmlEntityReference.java +75 -0
  28. data/ext/java/nokogiri/XmlNamespace.java +127 -0
  29. data/ext/java/nokogiri/XmlNode.java +1392 -0
  30. data/ext/java/nokogiri/XmlNodeSet.java +284 -0
  31. data/ext/java/nokogiri/XmlProcessingInstruction.java +103 -0
  32. data/ext/java/nokogiri/XmlReader.java +409 -0
  33. data/ext/java/nokogiri/XmlRelaxng.java +199 -0
  34. data/ext/java/nokogiri/XmlSaxParserContext.java +353 -0
  35. data/ext/java/nokogiri/XmlSaxPushParser.java +182 -0
  36. data/ext/java/nokogiri/XmlSchema.java +175 -0
  37. data/ext/java/nokogiri/XmlSyntaxError.java +114 -0
  38. data/ext/java/nokogiri/XmlText.java +135 -0
  39. data/ext/java/nokogiri/XmlXpathContext.java +175 -0
  40. data/ext/java/nokogiri/XsltStylesheet.java +181 -0
  41. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +205 -0
  42. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
  43. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +80 -0
  44. data/ext/java/nokogiri/internals/NokogiriHandler.java +326 -0
  45. data/ext/java/nokogiri/internals/NokogiriHelpers.java +583 -0
  46. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +170 -0
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +118 -0
  48. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +73 -0
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  50. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
  51. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +120 -0
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +56 -0
  53. data/ext/java/nokogiri/internals/ParserContext.java +278 -0
  54. data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
  55. data/ext/java/nokogiri/internals/ReaderNode.java +473 -0
  56. data/ext/java/nokogiri/internals/SaveContext.java +282 -0
  57. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +68 -0
  58. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  59. data/ext/java/nokogiri/internals/XmlDomParser.java +77 -0
  60. data/ext/java/nokogiri/internals/XmlDomParserContext.java +233 -0
  61. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  62. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  63. data/ext/nokogiri/depend +358 -0
  64. data/ext/nokogiri/extconf.rb +124 -0
  65. data/ext/nokogiri/html_document.c +154 -0
  66. data/ext/nokogiri/html_document.h +10 -0
  67. data/ext/nokogiri/html_element_description.c +276 -0
  68. data/ext/nokogiri/html_element_description.h +10 -0
  69. data/ext/nokogiri/html_entity_lookup.c +32 -0
  70. data/ext/nokogiri/html_entity_lookup.h +8 -0
  71. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  72. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  73. data/ext/nokogiri/nokogiri.c +92 -0
  74. data/ext/nokogiri/nokogiri.h +160 -0
  75. data/ext/nokogiri/xml_attr.c +94 -0
  76. data/ext/nokogiri/xml_attr.h +9 -0
  77. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  78. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  79. data/ext/nokogiri/xml_cdata.c +56 -0
  80. data/ext/nokogiri/xml_cdata.h +9 -0
  81. data/ext/nokogiri/xml_comment.c +54 -0
  82. data/ext/nokogiri/xml_comment.h +9 -0
  83. data/ext/nokogiri/xml_document.c +478 -0
  84. data/ext/nokogiri/xml_document.h +23 -0
  85. data/ext/nokogiri/xml_document_fragment.c +48 -0
  86. data/ext/nokogiri/xml_document_fragment.h +10 -0
  87. data/ext/nokogiri/xml_dtd.c +202 -0
  88. data/ext/nokogiri/xml_dtd.h +10 -0
  89. data/ext/nokogiri/xml_element_content.c +123 -0
  90. data/ext/nokogiri/xml_element_content.h +10 -0
  91. data/ext/nokogiri/xml_element_decl.c +69 -0
  92. data/ext/nokogiri/xml_element_decl.h +9 -0
  93. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  94. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  95. data/ext/nokogiri/xml_entity_decl.c +110 -0
  96. data/ext/nokogiri/xml_entity_decl.h +10 -0
  97. data/ext/nokogiri/xml_entity_reference.c +52 -0
  98. data/ext/nokogiri/xml_entity_reference.h +9 -0
  99. data/ext/nokogiri/xml_io.c +31 -0
  100. data/ext/nokogiri/xml_io.h +11 -0
  101. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  102. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  103. data/ext/nokogiri/xml_namespace.c +84 -0
  104. data/ext/nokogiri/xml_namespace.h +13 -0
  105. data/ext/nokogiri/xml_node.c +1384 -0
  106. data/ext/nokogiri/xml_node.h +13 -0
  107. data/ext/nokogiri/xml_node_set.c +418 -0
  108. data/ext/nokogiri/xml_node_set.h +9 -0
  109. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  110. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  111. data/ext/nokogiri/xml_reader.c +684 -0
  112. data/ext/nokogiri/xml_reader.h +10 -0
  113. data/ext/nokogiri/xml_relax_ng.c +161 -0
  114. data/ext/nokogiri/xml_relax_ng.h +9 -0
  115. data/ext/nokogiri/xml_sax_parser.c +288 -0
  116. data/ext/nokogiri/xml_sax_parser.h +39 -0
  117. data/ext/nokogiri/xml_sax_parser_context.c +199 -0
  118. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  119. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  120. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  121. data/ext/nokogiri/xml_schema.c +205 -0
  122. data/ext/nokogiri/xml_schema.h +9 -0
  123. data/ext/nokogiri/xml_syntax_error.c +58 -0
  124. data/ext/nokogiri/xml_syntax_error.h +13 -0
  125. data/ext/nokogiri/xml_text.c +50 -0
  126. data/ext/nokogiri/xml_text.h +9 -0
  127. data/ext/nokogiri/xml_xpath_context.c +309 -0
  128. data/ext/nokogiri/xml_xpath_context.h +9 -0
  129. data/ext/nokogiri/xslt_stylesheet.c +258 -0
  130. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  131. data/lib/isorelax.jar +0 -0
  132. data/lib/jing.jar +0 -0
  133. data/lib/nekodtd.jar +0 -0
  134. data/lib/nekohtml.jar +0 -0
  135. data/lib/nokogiri.rb +143 -0
  136. data/lib/nokogiri/css.rb +23 -0
  137. data/lib/nokogiri/css/node.rb +99 -0
  138. data/lib/nokogiri/css/parser.rb +677 -0
  139. data/lib/nokogiri/css/parser.y +237 -0
  140. data/lib/nokogiri/css/parser_extras.rb +91 -0
  141. data/lib/nokogiri/css/syntax_error.rb +7 -0
  142. data/lib/nokogiri/css/tokenizer.rb +152 -0
  143. data/lib/nokogiri/css/tokenizer.rex +55 -0
  144. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  145. data/lib/nokogiri/decorators/slop.rb +35 -0
  146. data/lib/nokogiri/html.rb +36 -0
  147. data/lib/nokogiri/html/builder.rb +35 -0
  148. data/lib/nokogiri/html/document.rb +221 -0
  149. data/lib/nokogiri/html/document_fragment.rb +41 -0
  150. data/lib/nokogiri/html/element_description.rb +23 -0
  151. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  152. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  153. data/lib/nokogiri/html/sax/parser.rb +52 -0
  154. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  155. data/lib/nokogiri/syntax_error.rb +4 -0
  156. data/lib/nokogiri/version.rb +35 -0
  157. data/lib/nokogiri/xml.rb +67 -0
  158. data/lib/nokogiri/xml/attr.rb +14 -0
  159. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  160. data/lib/nokogiri/xml/builder.rb +418 -0
  161. data/lib/nokogiri/xml/cdata.rb +11 -0
  162. data/lib/nokogiri/xml/character_data.rb +7 -0
  163. data/lib/nokogiri/xml/document.rb +218 -0
  164. data/lib/nokogiri/xml/document_fragment.rb +84 -0
  165. data/lib/nokogiri/xml/dtd.rb +22 -0
  166. data/lib/nokogiri/xml/element_content.rb +36 -0
  167. data/lib/nokogiri/xml/element_decl.rb +13 -0
  168. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  169. data/lib/nokogiri/xml/namespace.rb +13 -0
  170. data/lib/nokogiri/xml/node.rb +907 -0
  171. data/lib/nokogiri/xml/node/save_options.rb +45 -0
  172. data/lib/nokogiri/xml/node_set.rb +350 -0
  173. data/lib/nokogiri/xml/notation.rb +6 -0
  174. data/lib/nokogiri/xml/parse_options.rb +85 -0
  175. data/lib/nokogiri/xml/pp.rb +2 -0
  176. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  177. data/lib/nokogiri/xml/pp/node.rb +56 -0
  178. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  179. data/lib/nokogiri/xml/reader.rb +112 -0
  180. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  181. data/lib/nokogiri/xml/sax.rb +4 -0
  182. data/lib/nokogiri/xml/sax/document.rb +164 -0
  183. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  184. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  185. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  186. data/lib/nokogiri/xml/schema.rb +57 -0
  187. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  188. data/lib/nokogiri/xml/text.rb +9 -0
  189. data/lib/nokogiri/xml/xpath.rb +10 -0
  190. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  191. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  192. data/lib/nokogiri/xslt.rb +52 -0
  193. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  194. data/lib/xercesImpl.jar +0 -0
  195. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  196. data/tasks/cross_compile.rb +177 -0
  197. data/tasks/test.rb +94 -0
  198. data/test/css/test_nthiness.rb +159 -0
  199. data/test/css/test_parser.rb +303 -0
  200. data/test/css/test_tokenizer.rb +198 -0
  201. data/test/css/test_xpath_visitor.rb +85 -0
  202. data/test/decorators/test_slop.rb +16 -0
  203. data/test/files/2ch.html +108 -0
  204. data/test/files/address_book.rlx +12 -0
  205. data/test/files/address_book.xml +10 -0
  206. data/test/files/bar/bar.xsd +4 -0
  207. data/test/files/dont_hurt_em_why.xml +422 -0
  208. data/test/files/exslt.xml +8 -0
  209. data/test/files/exslt.xslt +35 -0
  210. data/test/files/foo/foo.xsd +4 -0
  211. data/test/files/po.xml +32 -0
  212. data/test/files/po.xsd +66 -0
  213. data/test/files/shift_jis.html +10 -0
  214. data/test/files/shift_jis.xml +5 -0
  215. data/test/files/snuggles.xml +3 -0
  216. data/test/files/staff.dtd +10 -0
  217. data/test/files/staff.xml +59 -0
  218. data/test/files/staff.xslt +32 -0
  219. data/test/files/tlm.html +850 -0
  220. data/test/files/valid_bar.xml +2 -0
  221. data/test/helper.rb +171 -0
  222. data/test/html/sax/test_parser.rb +136 -0
  223. data/test/html/sax/test_parser_context.rb +48 -0
  224. data/test/html/test_builder.rb +164 -0
  225. data/test/html/test_document.rb +457 -0
  226. data/test/html/test_document_encoding.rb +123 -0
  227. data/test/html/test_document_fragment.rb +255 -0
  228. data/test/html/test_element_description.rb +100 -0
  229. data/test/html/test_named_characters.rb +14 -0
  230. data/test/html/test_node.rb +190 -0
  231. data/test/html/test_node_encoding.rb +27 -0
  232. data/test/test_convert_xpath.rb +135 -0
  233. data/test/test_css_cache.rb +45 -0
  234. data/test/test_encoding_handler.rb +46 -0
  235. data/test/test_memory_leak.rb +52 -0
  236. data/test/test_nokogiri.rb +132 -0
  237. data/test/test_reader.rb +403 -0
  238. data/test/test_soap4r_sax.rb +52 -0
  239. data/test/test_xslt_transforms.rb +189 -0
  240. data/test/xml/node/test_save_options.rb +20 -0
  241. data/test/xml/node/test_subclass.rb +44 -0
  242. data/test/xml/sax/test_parser.rb +338 -0
  243. data/test/xml/sax/test_parser_context.rb +113 -0
  244. data/test/xml/sax/test_push_parser.rb +156 -0
  245. data/test/xml/test_attr.rb +65 -0
  246. data/test/xml/test_attribute_decl.rb +86 -0
  247. data/test/xml/test_builder.rb +210 -0
  248. data/test/xml/test_cdata.rb +50 -0
  249. data/test/xml/test_comment.rb +29 -0
  250. data/test/xml/test_document.rb +675 -0
  251. data/test/xml/test_document_encoding.rb +26 -0
  252. data/test/xml/test_document_fragment.rb +192 -0
  253. data/test/xml/test_dtd.rb +107 -0
  254. data/test/xml/test_dtd_encoding.rb +33 -0
  255. data/test/xml/test_element_content.rb +56 -0
  256. data/test/xml/test_element_decl.rb +73 -0
  257. data/test/xml/test_entity_decl.rb +122 -0
  258. data/test/xml/test_entity_reference.rb +21 -0
  259. data/test/xml/test_namespace.rb +70 -0
  260. data/test/xml/test_node.rb +899 -0
  261. data/test/xml/test_node_attributes.rb +34 -0
  262. data/test/xml/test_node_encoding.rb +107 -0
  263. data/test/xml/test_node_reparenting.rb +321 -0
  264. data/test/xml/test_node_set.rb +708 -0
  265. data/test/xml/test_parse_options.rb +52 -0
  266. data/test/xml/test_processing_instruction.rb +30 -0
  267. data/test/xml/test_reader_encoding.rb +126 -0
  268. data/test/xml/test_relax_ng.rb +60 -0
  269. data/test/xml/test_schema.rb +89 -0
  270. data/test/xml/test_syntax_error.rb +12 -0
  271. data/test/xml/test_text.rb +47 -0
  272. data/test/xml/test_unparented_node.rb +381 -0
  273. data/test/xml/test_xpath.rb +237 -0
  274. data/test/xslt/test_custom_functions.rb +94 -0
  275. metadata +525 -0
@@ -0,0 +1,708 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNodeSet < Nokogiri::TestCase
6
+ class TestNodeSetNamespaces < Nokogiri::TestCase
7
+ def setup
8
+ super
9
+ @xml = Nokogiri.XML('<foo xmlns:n0="http://example.com" />')
10
+ @list = @xml.xpath('//namespace::*')
11
+ end
12
+
13
+ def test_include?
14
+ assert @list.include?(@list.first), 'list should have item'
15
+ end
16
+
17
+ def test_push
18
+ @list.push @list.first
19
+ end
20
+
21
+ def test_delete
22
+ @list.push @list.first
23
+ @list.delete @list.first
24
+ end
25
+ end
26
+
27
+ def setup
28
+ super
29
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
30
+ @list = @xml.css('employee')
31
+ end
32
+
33
+ def test_filter
34
+ list = @xml.css('address').filter('*[domestic="Yes"]')
35
+ assert_equal(%w{ Yes } * 4, list.map { |n| n['domestic'] })
36
+ end
37
+
38
+ def test_remove_attr
39
+ @list.each { |x| x['class'] = 'blah' }
40
+ assert_equal @list, @list.remove_attr('class')
41
+ @list.each { |x| assert_nil x['class'] }
42
+ end
43
+
44
+ def test_add_class
45
+ assert_equal @list, @list.add_class('bar')
46
+ @list.each { |x| assert_equal 'bar', x['class'] }
47
+
48
+ @list.add_class('bar')
49
+ @list.each { |x| assert_equal 'bar', x['class'] }
50
+
51
+ @list.add_class('baz')
52
+ @list.each { |x| assert_equal 'bar baz', x['class'] }
53
+ end
54
+
55
+ def test_remove_class_with_no_class
56
+ assert_equal @list, @list.remove_class('bar')
57
+ @list.each { |e| assert_nil e['class'] }
58
+
59
+ @list.each { |e| e['class'] = '' }
60
+ assert_equal @list, @list.remove_class('bar')
61
+ @list.each { |e| assert_nil e['class'] }
62
+ end
63
+
64
+ def test_remove_class_single
65
+ @list.each { |e| e['class'] = 'foo bar' }
66
+
67
+ assert_equal @list, @list.remove_class('bar')
68
+ @list.each { |e| assert_equal 'foo', e['class'] }
69
+ end
70
+
71
+ def test_remove_class_completely
72
+ @list.each { |e| e['class'] = 'foo' }
73
+
74
+ assert_equal @list, @list.remove_class
75
+ @list.each { |e| assert_nil e['class'] }
76
+ end
77
+
78
+ def test_attribute_set
79
+ @list.each { |e| assert_nil e['foo'] }
80
+
81
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
82
+ @list.send(t.first.to_sym, 'foo', t.last)
83
+ @list.each { |e| assert_equal t.last, e['foo'] }
84
+ end
85
+ end
86
+
87
+ def test_attribute_set_with_block
88
+ @list.each { |e| assert_nil e['foo'] }
89
+
90
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
91
+ @list.send(t.first.to_sym, 'foo') { |x| t.last }
92
+ @list.each { |e| assert_equal t.last, e['foo'] }
93
+ end
94
+ end
95
+
96
+ def test_attribute_set_with_hash
97
+ @list.each { |e| assert_nil e['foo'] }
98
+
99
+ [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
100
+ @list.send(t.first.to_sym, 'foo' => t.last)
101
+ @list.each { |e| assert_equal t.last, e['foo'] }
102
+ end
103
+ end
104
+
105
+ def test_attribute_no_args
106
+ @list.first['foo'] = 'bar'
107
+ assert_equal @list.first.attribute('foo'), @list.attribute('foo')
108
+ end
109
+
110
+ def test_search_empty_node_set
111
+ set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
112
+ assert_equal 0, set.css('foo').length
113
+ assert_equal 0, set.xpath('.//foo').length
114
+ assert_equal 0, set.search('foo').length
115
+ end
116
+
117
+ def test_xpath_with_custom_object
118
+ set = @xml.xpath('//staff')
119
+ custom_employees = set.xpath('//*[awesome(.)]', Class.new {
120
+ def awesome ns
121
+ ns.select { |n| n.name == 'employee' }
122
+ end
123
+ }.new)
124
+
125
+ assert_equal @xml.xpath('//employee'), custom_employees
126
+ end
127
+
128
+ def test_css_with_custom_object
129
+ set = @xml.xpath('//staff')
130
+ custom_employees = set.css('*:awesome', Class.new {
131
+ def awesome ns
132
+ ns.select { |n| n.name == 'employee' }
133
+ end
134
+ }.new)
135
+
136
+ assert_equal @xml.xpath('//employee'), custom_employees
137
+ end
138
+
139
+ def test_search_with_custom_object
140
+ set = @xml.xpath('//staff')
141
+ custom_employees = set.search('//*[awesome(.)]', Class.new {
142
+ def awesome ns
143
+ ns.select { |n| n.name == 'employee' }
144
+ end
145
+ }.new)
146
+
147
+ assert_equal @xml.xpath('//employee'), custom_employees
148
+ end
149
+
150
+ def test_css_searches_match_self
151
+ html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
152
+ set = html.xpath("/html/body/div")
153
+ assert_equal set.first, set.css(".a").first
154
+ end
155
+
156
+ def test_search_with_css_matches_self
157
+ html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
158
+ set = html.xpath("/html/body/div")
159
+ assert_equal set.first, set.search(".a").first
160
+ end
161
+
162
+ def test_css_search_with_namespace
163
+ fragment = Nokogiri::XML.fragment(<<-eoxml)
164
+ <html xmlns="http://www.w3.org/1999/xhtml">
165
+ <head></head>
166
+ <body></body>
167
+ </html>
168
+ eoxml
169
+ assert_nothing_raised{ fragment.children.search( 'body', { 'xmlns' => 'http://www.w3.org/1999/xhtml' }) }
170
+ end
171
+
172
+ def test_double_equal
173
+ assert node_set_one = @xml.xpath('//employee')
174
+ assert node_set_two = @xml.xpath('//employee')
175
+
176
+ assert_not_equal node_set_one.object_id, node_set_two.object_id
177
+
178
+ assert_equal node_set_one, node_set_two
179
+ end
180
+
181
+ def test_node_set_not_equal_to_string
182
+ node_set_one = @xml.xpath('//employee')
183
+ assert_not_equal node_set_one, "asdfadsf"
184
+ end
185
+
186
+ def test_out_of_order_not_equal
187
+ one = @xml.xpath('//employee')
188
+ two = @xml.xpath('//employee')
189
+ two.push two.shift
190
+ assert_not_equal one, two
191
+ end
192
+
193
+ def test_shorter_is_not_equal
194
+ node_set_one = @xml.xpath('//employee')
195
+ node_set_two = @xml.xpath('//employee')
196
+ node_set_two.delete(node_set_two.first)
197
+
198
+ assert_not_equal node_set_one, node_set_two
199
+ end
200
+
201
+ def test_pop
202
+ set = @xml.xpath('//employee')
203
+ last = set.last
204
+ assert_equal last, set.pop
205
+ end
206
+
207
+ def test_shift
208
+ set = @xml.xpath('//employee')
209
+ first = set.first
210
+ assert_equal first, set.shift
211
+ end
212
+
213
+ def test_shift_empty
214
+ set = Nokogiri::XML::NodeSet.new(@xml)
215
+ assert_nil set.shift
216
+ end
217
+
218
+ def test_pop_empty
219
+ set = Nokogiri::XML::NodeSet.new(@xml)
220
+ assert_nil set.pop
221
+ end
222
+
223
+ def test_first_takes_arguments
224
+ assert node_set = @xml.xpath('//employee')
225
+ assert_equal 2, node_set.first(2).length
226
+ end
227
+
228
+ def test_dup
229
+ assert node_set = @xml.xpath('//employee')
230
+ dup = node_set.dup
231
+ assert_equal node_set.length, dup.length
232
+ node_set.zip(dup).each do |a,b|
233
+ assert_equal a, b
234
+ end
235
+ end
236
+
237
+ def test_xmlns_is_automatically_registered
238
+ doc = Nokogiri::XML(<<-eoxml)
239
+ <root xmlns="http://tenderlovemaking.com/">
240
+ <foo>
241
+ <bar/>
242
+ </foo>
243
+ </root>
244
+ eoxml
245
+ set = doc.css('foo')
246
+ assert_equal 1, set.css('xmlns|bar').length
247
+ assert_equal 0, set.css('|bar').length
248
+ assert_equal 1, set.xpath('//xmlns:bar').length
249
+ assert_equal 1, set.search('xmlns|bar').length
250
+ assert_equal 1, set.search('//xmlns:bar').length
251
+ assert set.at('//xmlns:bar')
252
+ assert set.at('xmlns|bar')
253
+ assert set.at('bar')
254
+ end
255
+
256
+ def test_children_has_document
257
+ set = @xml.root.children
258
+ assert_instance_of(NodeSet, set)
259
+ assert_equal @xml, set.document
260
+ end
261
+
262
+ def test_length_size
263
+ assert node_set = @xml.search('//employee')
264
+ assert_equal node_set.length, node_set.size
265
+ end
266
+
267
+ def test_to_xml
268
+ assert node_set = @xml.search('//employee')
269
+ assert node_set.to_xml
270
+ end
271
+
272
+ def test_inner_html
273
+ doc = Nokogiri::HTML(<<-eohtml)
274
+ <html>
275
+ <body>
276
+ <div>
277
+ <a>one</a>
278
+ </div>
279
+ <div>
280
+ <a>two</a>
281
+ </div>
282
+ </body>
283
+ </html>
284
+ eohtml
285
+ assert html = doc.css('div').inner_html
286
+ assert_match '<a>', html
287
+ end
288
+
289
+ def test_gt_string_arg
290
+ assert node_set = @xml.search('//employee')
291
+ assert_equal node_set.xpath('./employeeId'), (node_set > 'employeeId')
292
+ end
293
+
294
+ def test_at
295
+ assert node_set = @xml.search('//employee')
296
+ assert_equal node_set.first, node_set.at(0)
297
+ end
298
+
299
+ def test_at_xpath
300
+ assert node_set = @xml.search('//employee')
301
+ assert_equal node_set.first.first_element_child, node_set.at_xpath('./employeeId')
302
+ end
303
+
304
+ def test_at_css
305
+ assert node_set = @xml.search('//employee')
306
+ assert_equal node_set.first.first_element_child, node_set.at_css('employeeId')
307
+ end
308
+
309
+ def test_percent
310
+ assert node_set = @xml.search('//employee')
311
+ assert_equal node_set.first, node_set % 0
312
+ end
313
+
314
+ def test_to_ary
315
+ assert node_set = @xml.search('//employee')
316
+ foo = []
317
+ foo += node_set
318
+ assert_equal node_set.length, foo.length
319
+ end
320
+
321
+ def test_push
322
+ node = Nokogiri::XML::Node.new('foo', @xml)
323
+ node.content = 'bar'
324
+
325
+ assert node_set = @xml.search('//employee')
326
+ node_set.push(node)
327
+
328
+ assert node_set.include?(node)
329
+ end
330
+
331
+ def test_delete_with_invalid_argument
332
+ employees = @xml.search("//employee")
333
+ positions = @xml.search("//position")
334
+
335
+ assert_raises(ArgumentError) { employees.delete(positions) }
336
+ end
337
+
338
+ def test_delete_when_present
339
+ employees = @xml.search("//employee")
340
+ wally = employees.first
341
+ assert employees.include?(wally) # testing setup
342
+ length = employees.length
343
+
344
+ result = employees.delete(wally)
345
+ assert_equal result, wally
346
+ assert ! employees.include?(wally)
347
+ assert length-1, employees.length
348
+ end
349
+
350
+ def test_delete_when_not_present
351
+ employees = @xml.search("//employee")
352
+ phb = @xml.search("//position").first
353
+ assert ! employees.include?(phb) # testing setup
354
+ length = employees.length
355
+
356
+ result = employees.delete(phb)
357
+ assert_nil result
358
+ assert length, employees.length
359
+ end
360
+
361
+ def test_unlink
362
+ xml = Nokogiri::XML.parse(<<-eoxml)
363
+ <root>
364
+ <a class='foo bar'>Bar</a>
365
+ <a class='bar foo'>Bar</a>
366
+ <a class='bar'>Bar</a>
367
+ <a>Hello world</a>
368
+ <a class='baz bar foo'>Bar</a>
369
+ <a class='bazbarfoo'>Awesome</a>
370
+ <a class='bazbar'>Awesome</a>
371
+ </root>
372
+ eoxml
373
+ set = xml.xpath('//a')
374
+ set.unlink
375
+ set.each do |node|
376
+ assert !node.parent
377
+ #assert !node.document
378
+ assert !node.previous_sibling
379
+ assert !node.next_sibling
380
+ end
381
+ assert_no_match(/Hello world/, xml.to_s)
382
+ end
383
+
384
+ def test_nodeset_search_takes_namespace
385
+ @xml = Nokogiri::XML.parse(<<-eoxml)
386
+ <root>
387
+ <car xmlns:part="http://general-motors.com/">
388
+ <part:tire>Michelin Model XGV</part:tire>
389
+ </car>
390
+ <bicycle xmlns:part="http://schwinn.com/">
391
+ <part:tire>I'm a bicycle tire!</part:tire>
392
+ </bicycle>
393
+ </root>
394
+ eoxml
395
+ set = @xml/'root'
396
+ assert_equal 1, set.length
397
+ bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
398
+ assert_equal 1, bike_tire.length
399
+ end
400
+
401
+ def test_new_nodeset
402
+ node_set = Nokogiri::XML::NodeSet.new(@xml)
403
+ assert_equal(0, node_set.length)
404
+ node = Nokogiri::XML::Node.new('form', @xml)
405
+ node_set << node
406
+ assert_equal(1, node_set.length)
407
+ assert_equal(node, node_set.last)
408
+ end
409
+
410
+ def test_search_on_nodeset
411
+ assert node_set = @xml.search('//employee')
412
+ assert sub_set = node_set.search('.//name')
413
+ assert_equal(node_set.length, sub_set.length)
414
+ end
415
+
416
+ def test_negative_index_works
417
+ assert node_set = @xml.search('//employee')
418
+ assert_equal node_set.last, node_set[-1]
419
+ end
420
+
421
+ def test_large_negative_index_returns_nil
422
+ assert node_set = @xml.search('//employee')
423
+ assert_nil(node_set[-1 * (node_set.length + 1)])
424
+ end
425
+
426
+ def test_node_set_fetches_private_data
427
+ assert node_set = @xml.search('//employee')
428
+
429
+ set = node_set
430
+ assert_equal(set[0], set[0])
431
+ end
432
+
433
+ def test_node_set_returns_0
434
+ assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
435
+ assert_equal(0, node_set.length)
436
+ end
437
+
438
+ def test_wrap
439
+ employees = (@xml/"//employee").wrap("<wrapper/>")
440
+ assert_equal 'wrapper', employees[0].parent.name
441
+ assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
442
+ end
443
+
444
+ def test_wrap_a_fragment
445
+ frag = Nokogiri::XML::DocumentFragment.parse <<-EOXML
446
+ <employees>
447
+ <employee>hello</employee>
448
+ <employee>goodbye</employee>
449
+ </employees>
450
+ EOXML
451
+ employees = frag.xpath ".//employee"
452
+ employees.wrap("<wrapper/>")
453
+ assert_equal 'wrapper', employees[0].parent.name
454
+ assert_equal 'employee', frag.at(".//wrapper").children.first.name
455
+ end
456
+
457
+ def test_wrap_preserves_document_structure
458
+ assert_equal "employeeId",
459
+ @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
460
+ @xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
461
+ assert_equal "wrapper",
462
+ @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
463
+ end
464
+
465
+ def test_plus_operator
466
+ names = @xml.search("name")
467
+ positions = @xml.search("position")
468
+
469
+ names_len = names.length
470
+ positions_len = positions.length
471
+
472
+ assert_raises(ArgumentError) { names + positions.first }
473
+
474
+ result = names + positions
475
+ assert_equal names_len, names.length
476
+ assert_equal positions_len, positions.length
477
+ assert_equal names.length + positions.length, result.length
478
+
479
+ names += positions
480
+ assert_equal result.length, names.length
481
+ end
482
+
483
+ def test_union
484
+ names = @xml.search("name")
485
+
486
+ assert_equal(names.length, (names | @xml.search("name")).length)
487
+ end
488
+
489
+ def test_minus_operator
490
+ employees = @xml.search("//employee")
491
+ females = @xml.search("//employee[gender[text()='Female']]")
492
+
493
+ employees_len = employees.length
494
+ females_len = females.length
495
+
496
+ assert_raises(ArgumentError) { employees - females.first }
497
+
498
+ result = employees - females
499
+ assert_equal employees_len, employees.length
500
+ assert_equal females_len, females.length
501
+ assert_equal employees.length - females.length, result.length
502
+
503
+ employees -= females
504
+ assert_equal result.length, employees.length
505
+ end
506
+
507
+ def test_array_index
508
+ employees = @xml.search("//employee")
509
+ other = @xml.search("//position").first
510
+
511
+ assert_equal 3, employees.index(employees[3])
512
+ assert_nil employees.index(other)
513
+ end
514
+
515
+ def test_slice_too_far
516
+ employees = @xml.search("//employee")
517
+ assert_equal employees.length, employees[0, employees.length + 1].length
518
+ assert_equal employees.length, employees[0, employees.length].length
519
+ end
520
+
521
+ def test_slice_waaaaaay_off_the_end
522
+ xml = Nokogiri::XML::Builder.new {
523
+ root { 100.times { div } }
524
+ }.doc
525
+ nodes = xml.css "div"
526
+ assert_equal 1, nodes.slice(99, 100_000).length
527
+ assert_equal 0, nodes.slice(100, 100_000).length
528
+ end
529
+
530
+ def test_array_slice_with_start_and_end
531
+ employees = @xml.search("//employee")
532
+ assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
533
+ end
534
+
535
+ def test_array_index_bracket_equivalence
536
+ employees = @xml.search("//employee")
537
+ assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
538
+ assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
539
+ end
540
+
541
+ def test_array_slice_with_negative_start
542
+ employees = @xml.search("//employee")
543
+ assert_equal [employees[2]], employees[-3,1].to_a
544
+ assert_equal [employees[2], employees[3]], employees[-3,2].to_a
545
+ end
546
+
547
+ def test_array_slice_with_invalid_args
548
+ employees = @xml.search("//employee")
549
+ assert_nil employees[99, 1] # large start
550
+ assert_nil employees[1, -1] # negative len
551
+ assert_equal [], employees[1, 0].to_a # zero len
552
+ end
553
+
554
+ def test_array_slice_with_range
555
+ employees = @xml.search("//employee")
556
+ assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
557
+ assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
558
+ end
559
+
560
+ def test_intersection_with_no_overlap
561
+ employees = @xml.search("//employee")
562
+ positions = @xml.search("//position")
563
+
564
+ assert_equal [], (employees & positions).to_a
565
+ end
566
+
567
+ def test_intersection
568
+ employees = @xml.search("//employee")
569
+ first_set = employees[0..2]
570
+ second_set = employees[2..4]
571
+
572
+ assert_equal [employees[2]], (first_set & second_set).to_a
573
+ end
574
+
575
+ def test_include?
576
+ employees = @xml.search("//employee")
577
+ yes = employees.first
578
+ no = @xml.search("//position").first
579
+
580
+ assert employees.include?(yes)
581
+ assert ! employees.include?(no)
582
+ end
583
+
584
+ def test_children
585
+ employees = @xml.search("//employee")
586
+ count = 0
587
+ employees.each do |employee|
588
+ count += employee.children.length
589
+ end
590
+ set = employees.children
591
+ assert_equal count, set.length
592
+ end
593
+
594
+ def test_inspect
595
+ employees = @xml.search("//employee")
596
+ inspected = employees.inspect
597
+
598
+ assert_equal "[#{employees.map { |x| x.inspect }.join(', ')}]",
599
+ inspected
600
+ end
601
+
602
+ def test_should_not_splode_when_accessing_namespace_declarations_in_a_node_set
603
+ xml = Nokogiri::XML "<foo></foo>"
604
+ node_set = xml.xpath("//namespace::*")
605
+ assert_equal 1, node_set.size
606
+ node = node_set.first
607
+ node.to_s # segfaults in 1.4.0 and earlier
608
+
609
+ # if we haven't segfaulted, let's make sure we handled it correctly
610
+ assert_instance_of Nokogiri::XML::Namespace, node
611
+ end
612
+
613
+ def test_should_not_splode_when_arrayifying_node_set_containing_namespace_declarations
614
+ xml = Nokogiri::XML "<foo></foo>"
615
+ node_set = xml.xpath("//namespace::*")
616
+ assert_equal 1, node_set.size
617
+
618
+ node_array = node_set.to_a
619
+ node = node_array.first
620
+ node.to_s # segfaults in 1.4.0 and earlier
621
+
622
+ # if we haven't segfaulted, let's make sure we handled it correctly
623
+ assert_instance_of Nokogiri::XML::Namespace, node
624
+ end
625
+
626
+ def test_should_not_splode_when_unlinking_node_set_containing_namespace_declarations
627
+ xml = Nokogiri::XML "<foo></foo>"
628
+ node_set = xml.xpath("//namespace::*")
629
+ assert_equal 1, node_set.size
630
+
631
+ node_set.unlink
632
+ end
633
+
634
+ def test_reverse
635
+ xml = Nokogiri::XML "<root><a />b<c />d<e /></root>"
636
+ children = xml.root.children
637
+ assert_instance_of Nokogiri::XML::NodeSet, children
638
+
639
+ reversed = children.reverse
640
+ assert_equal reversed[0], children[4]
641
+ assert_equal reversed[1], children[3]
642
+ assert_equal reversed[2], children[2]
643
+ assert_equal reversed[3], children[1]
644
+ assert_equal reversed[4], children[0]
645
+
646
+ assert_equal children, children.reverse.reverse
647
+ end
648
+
649
+ def test_node_set_dup_result_has_document_and_is_decorated
650
+ x = Module.new do
651
+ def awesome! ; end
652
+ end
653
+ util_decorate(@xml, x)
654
+ node_set = @xml.css("address")
655
+ new_set = node_set.dup
656
+ assert_equal node_set.document, new_set.document
657
+ assert new_set.respond_to?(:awesome!)
658
+ end
659
+
660
+ def test_node_set_union_result_has_document_and_is_decorated
661
+ x = Module.new do
662
+ def awesome! ; end
663
+ end
664
+ util_decorate(@xml, x)
665
+ node_set1 = @xml.css("address")
666
+ node_set2 = @xml.css("address")
667
+ new_set = node_set1 | node_set2
668
+ assert_equal node_set1.document, new_set.document
669
+ assert new_set.respond_to?(:awesome!)
670
+ end
671
+
672
+ def test_node_set_intersection_result_has_document_and_is_decorated
673
+ x = Module.new do
674
+ def awesome! ; end
675
+ end
676
+ util_decorate(@xml, x)
677
+ node_set1 = @xml.css("address")
678
+ node_set2 = @xml.css("address")
679
+ new_set = node_set1 & node_set2
680
+ assert_equal node_set1.document, new_set.document
681
+ assert new_set.respond_to?(:awesome!)
682
+ end
683
+
684
+ def test_node_set_difference_result_has_document_and_is_decorated
685
+ x = Module.new do
686
+ def awesome! ; end
687
+ end
688
+ util_decorate(@xml, x)
689
+ node_set1 = @xml.css("address")
690
+ node_set2 = @xml.css("address")
691
+ new_set = node_set1 - node_set2
692
+ assert_equal node_set1.document, new_set.document
693
+ assert new_set.respond_to?(:awesome!)
694
+ end
695
+
696
+ def test_node_set_slice_result_has_document_and_is_decorated
697
+ x = Module.new do
698
+ def awesome! ; end
699
+ end
700
+ util_decorate(@xml, x)
701
+ node_set = @xml.css("address")
702
+ new_set = node_set[0..-1]
703
+ assert_equal node_set.document, new_set.document
704
+ assert new_set.respond_to?(:awesome!)
705
+ end
706
+ end
707
+ end
708
+ end