nokogiri 1.2.3-x86-mswin32-60 → 1.4.5-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (319) hide show
  1. data/.autotest +18 -7
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.ja.rdoc +297 -3
  4. data/CHANGELOG.rdoc +289 -0
  5. data/Manifest.txt +148 -37
  6. data/README.ja.rdoc +20 -20
  7. data/README.rdoc +53 -22
  8. data/Rakefile +127 -211
  9. data/bin/nokogiri +54 -0
  10. data/ext/nokogiri/depend +358 -0
  11. data/ext/nokogiri/extconf.rb +89 -54
  12. data/ext/nokogiri/html_document.c +34 -27
  13. data/ext/nokogiri/html_document.h +1 -1
  14. data/ext/nokogiri/html_element_description.c +276 -0
  15. data/ext/nokogiri/html_element_description.h +10 -0
  16. data/ext/nokogiri/html_entity_lookup.c +7 -5
  17. data/ext/nokogiri/html_entity_lookup.h +1 -1
  18. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  19. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  20. data/ext/nokogiri/{native.c → nokogiri.c} +31 -7
  21. data/ext/nokogiri/{native.h → nokogiri.h} +68 -41
  22. data/ext/nokogiri/xml_attr.c +20 -9
  23. data/ext/nokogiri/xml_attr.h +1 -1
  24. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  25. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  26. data/ext/nokogiri/xml_cdata.c +21 -9
  27. data/ext/nokogiri/xml_cdata.h +1 -1
  28. data/ext/nokogiri/xml_comment.c +18 -6
  29. data/ext/nokogiri/xml_comment.h +1 -1
  30. data/ext/nokogiri/xml_document.c +247 -68
  31. data/ext/nokogiri/xml_document.h +5 -3
  32. data/ext/nokogiri/xml_document_fragment.c +15 -7
  33. data/ext/nokogiri/xml_document_fragment.h +1 -1
  34. data/ext/nokogiri/xml_dtd.c +110 -10
  35. data/ext/nokogiri/xml_dtd.h +3 -1
  36. data/ext/nokogiri/xml_element_content.c +123 -0
  37. data/ext/nokogiri/xml_element_content.h +10 -0
  38. data/ext/nokogiri/xml_element_decl.c +69 -0
  39. data/ext/nokogiri/xml_element_decl.h +9 -0
  40. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  41. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  42. data/ext/nokogiri/xml_entity_decl.c +110 -0
  43. data/ext/nokogiri/xml_entity_decl.h +10 -0
  44. data/ext/nokogiri/xml_entity_reference.c +16 -5
  45. data/ext/nokogiri/xml_entity_reference.h +1 -1
  46. data/ext/nokogiri/xml_io.c +40 -8
  47. data/ext/nokogiri/xml_io.h +2 -1
  48. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  49. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  50. data/ext/nokogiri/xml_namespace.c +84 -0
  51. data/ext/nokogiri/xml_namespace.h +13 -0
  52. data/ext/nokogiri/xml_node.c +782 -225
  53. data/ext/nokogiri/xml_node.h +2 -4
  54. data/ext/nokogiri/xml_node_set.c +253 -34
  55. data/ext/nokogiri/xml_node_set.h +2 -2
  56. data/ext/nokogiri/xml_processing_instruction.c +17 -5
  57. data/ext/nokogiri/xml_processing_instruction.h +1 -1
  58. data/ext/nokogiri/xml_reader.c +277 -85
  59. data/ext/nokogiri/xml_reader.h +1 -1
  60. data/ext/nokogiri/xml_relax_ng.c +168 -0
  61. data/ext/nokogiri/xml_relax_ng.h +9 -0
  62. data/ext/nokogiri/xml_sax_parser.c +183 -111
  63. data/ext/nokogiri/xml_sax_parser.h +30 -1
  64. data/ext/nokogiri/xml_sax_parser_context.c +199 -0
  65. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  66. data/ext/nokogiri/xml_sax_push_parser.c +42 -12
  67. data/ext/nokogiri/xml_sax_push_parser.h +1 -1
  68. data/ext/nokogiri/xml_schema.c +205 -0
  69. data/ext/nokogiri/xml_schema.h +9 -0
  70. data/ext/nokogiri/xml_syntax_error.c +28 -173
  71. data/ext/nokogiri/xml_syntax_error.h +2 -1
  72. data/ext/nokogiri/xml_text.c +16 -6
  73. data/ext/nokogiri/xml_text.h +1 -1
  74. data/ext/nokogiri/xml_xpath_context.c +104 -47
  75. data/ext/nokogiri/xml_xpath_context.h +1 -1
  76. data/ext/nokogiri/xslt_stylesheet.c +161 -19
  77. data/ext/nokogiri/xslt_stylesheet.h +1 -1
  78. data/lib/nokogiri.rb +47 -8
  79. data/lib/nokogiri/1.8/nokogiri.so +0 -0
  80. data/lib/nokogiri/1.9/nokogiri.so +0 -0
  81. data/lib/nokogiri/css.rb +6 -3
  82. data/lib/nokogiri/css/node.rb +14 -12
  83. data/lib/nokogiri/css/parser.rb +665 -62
  84. data/lib/nokogiri/css/parser.y +20 -10
  85. data/lib/nokogiri/css/parser_extras.rb +91 -0
  86. data/lib/nokogiri/css/tokenizer.rb +148 -5
  87. data/lib/nokogiri/css/tokenizer.rex +10 -9
  88. data/lib/nokogiri/css/xpath_visitor.rb +47 -44
  89. data/lib/nokogiri/decorators/slop.rb +8 -4
  90. data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
  91. data/lib/nokogiri/ffi/html/document.rb +28 -0
  92. data/lib/nokogiri/ffi/html/element_description.rb +81 -0
  93. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  94. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  95. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  96. data/lib/nokogiri/ffi/libxml.rb +420 -0
  97. data/lib/nokogiri/ffi/structs/common_node.rb +38 -0
  98. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  99. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  100. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  101. data/lib/nokogiri/ffi/structs/xml_attr.rb +20 -0
  102. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  103. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  104. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
  105. data/lib/nokogiri/ffi/structs/xml_document.rb +117 -0
  106. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  107. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  108. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  109. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  110. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  111. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  112. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  113. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  114. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  115. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +20 -0
  116. data/lib/nokogiri/ffi/structs/xml_parser_input.rb +19 -0
  117. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  118. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  119. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
  120. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  121. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  122. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  123. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +38 -0
  124. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  125. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  126. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  127. data/lib/nokogiri/ffi/weak_bucket.rb +40 -0
  128. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  129. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  130. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  131. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  132. data/lib/nokogiri/ffi/xml/document.rb +174 -0
  133. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  134. data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
  135. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  136. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  137. data/lib/nokogiri/ffi/xml/entity_decl.rb +36 -0
  138. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  139. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  140. data/lib/nokogiri/ffi/xml/node.rb +559 -0
  141. data/lib/nokogiri/ffi/xml/node_set.rb +150 -0
  142. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  143. data/lib/nokogiri/ffi/xml/reader.rb +236 -0
  144. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  145. data/lib/nokogiri/ffi/xml/sax/parser.rb +143 -0
  146. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +79 -0
  147. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
  148. data/lib/nokogiri/ffi/xml/schema.rb +109 -0
  149. data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
  150. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  151. data/lib/nokogiri/ffi/xml/xpath.rb +9 -0
  152. data/lib/nokogiri/ffi/xml/xpath_context.rb +153 -0
  153. data/lib/nokogiri/ffi/xslt/stylesheet.rb +77 -0
  154. data/lib/nokogiri/html.rb +13 -47
  155. data/lib/nokogiri/html/builder.rb +27 -1
  156. data/lib/nokogiri/html/document.rb +201 -7
  157. data/lib/nokogiri/html/document_fragment.rb +41 -0
  158. data/lib/nokogiri/html/element_description.rb +23 -0
  159. data/lib/nokogiri/html/entity_lookup.rb +2 -0
  160. data/lib/nokogiri/html/sax/parser.rb +34 -3
  161. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  162. data/lib/nokogiri/nokogiri.rb +1 -0
  163. data/lib/nokogiri/version.rb +40 -1
  164. data/lib/nokogiri/version_warning.rb +14 -0
  165. data/lib/nokogiri/xml.rb +32 -53
  166. data/lib/nokogiri/xml/attr.rb +5 -0
  167. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  168. data/lib/nokogiri/xml/builder.rb +349 -29
  169. data/lib/nokogiri/xml/cdata.rb +3 -1
  170. data/lib/nokogiri/xml/character_data.rb +7 -0
  171. data/lib/nokogiri/xml/document.rb +166 -14
  172. data/lib/nokogiri/xml/document_fragment.rb +76 -1
  173. data/lib/nokogiri/xml/dtd.rb +16 -3
  174. data/lib/nokogiri/xml/element_content.rb +36 -0
  175. data/lib/nokogiri/xml/element_decl.rb +13 -0
  176. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  177. data/lib/nokogiri/xml/namespace.rb +13 -0
  178. data/lib/nokogiri/xml/node.rb +561 -166
  179. data/lib/nokogiri/xml/node/save_options.rb +22 -2
  180. data/lib/nokogiri/xml/node_set.rb +202 -40
  181. data/lib/nokogiri/xml/parse_options.rb +93 -0
  182. data/lib/nokogiri/xml/pp.rb +2 -0
  183. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  184. data/lib/nokogiri/xml/pp/node.rb +56 -0
  185. data/lib/nokogiri/xml/processing_instruction.rb +2 -0
  186. data/lib/nokogiri/xml/reader.rb +93 -8
  187. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  188. data/lib/nokogiri/xml/sax.rb +1 -7
  189. data/lib/nokogiri/xml/sax/document.rb +107 -2
  190. data/lib/nokogiri/xml/sax/parser.rb +57 -7
  191. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  192. data/lib/nokogiri/xml/sax/push_parser.rb +13 -1
  193. data/lib/nokogiri/xml/schema.rb +63 -0
  194. data/lib/nokogiri/xml/syntax_error.rb +25 -1
  195. data/lib/nokogiri/xml/text.rb +4 -1
  196. data/lib/nokogiri/xml/xpath.rb +1 -1
  197. data/lib/nokogiri/xml/xpath/syntax_error.rb +3 -0
  198. data/lib/nokogiri/xml/xpath_context.rb +2 -0
  199. data/lib/nokogiri/xslt.rb +26 -2
  200. data/lib/nokogiri/xslt/stylesheet.rb +19 -0
  201. data/lib/xsd/xmlparser/nokogiri.rb +45 -9
  202. data/tasks/cross_compile.rb +173 -0
  203. data/tasks/test.rb +25 -69
  204. data/test/css/test_nthiness.rb +3 -4
  205. data/test/css/test_parser.rb +75 -20
  206. data/test/css/test_tokenizer.rb +23 -1
  207. data/test/css/test_xpath_visitor.rb +10 -1
  208. data/test/decorators/test_slop.rb +16 -0
  209. data/test/ffi/test_document.rb +35 -0
  210. data/test/files/2ch.html +108 -0
  211. data/test/files/address_book.rlx +12 -0
  212. data/test/files/address_book.xml +10 -0
  213. data/test/files/bar/bar.xsd +4 -0
  214. data/test/files/encoding.html +82 -0
  215. data/test/files/encoding.xhtml +84 -0
  216. data/test/files/foo/foo.xsd +4 -0
  217. data/test/files/po.xml +32 -0
  218. data/test/files/po.xsd +66 -0
  219. data/test/files/shift_jis.html +10 -0
  220. data/test/files/shift_jis.xml +5 -0
  221. data/test/files/snuggles.xml +3 -0
  222. data/test/files/staff.dtd +10 -0
  223. data/test/files/valid_bar.xml +2 -0
  224. data/test/helper.rb +101 -23
  225. data/test/html/sax/test_parser.rb +81 -2
  226. data/test/html/sax/test_parser_context.rb +48 -0
  227. data/test/html/test_builder.rb +39 -8
  228. data/test/html/test_document.rb +186 -23
  229. data/test/html/test_document_encoding.rb +78 -1
  230. data/test/html/test_document_fragment.rb +253 -0
  231. data/test/html/test_element_description.rb +98 -0
  232. data/test/html/test_named_characters.rb +1 -1
  233. data/test/html/test_node.rb +124 -36
  234. data/test/html/test_node_encoding.rb +27 -0
  235. data/test/test_convert_xpath.rb +1 -52
  236. data/test/test_css_cache.rb +2 -13
  237. data/test/test_encoding_handler.rb +46 -0
  238. data/test/test_memory_leak.rb +88 -19
  239. data/test/test_nokogiri.rb +38 -5
  240. data/test/test_reader.rb +188 -6
  241. data/test/test_soap4r_sax.rb +52 -0
  242. data/test/test_xslt_transforms.rb +183 -83
  243. data/test/xml/node/test_save_options.rb +1 -1
  244. data/test/xml/node/test_subclass.rb +44 -0
  245. data/test/xml/sax/test_parser.rb +175 -4
  246. data/test/xml/sax/test_parser_context.rb +113 -0
  247. data/test/xml/sax/test_push_parser.rb +90 -2
  248. data/test/xml/test_attr.rb +35 -1
  249. data/test/xml/test_attribute_decl.rb +82 -0
  250. data/test/xml/test_builder.rb +186 -1
  251. data/test/xml/test_cdata.rb +32 -1
  252. data/test/xml/test_comment.rb +13 -1
  253. data/test/xml/test_document.rb +415 -43
  254. data/test/xml/test_document_encoding.rb +1 -1
  255. data/test/xml/test_document_fragment.rb +173 -5
  256. data/test/xml/test_dtd.rb +61 -6
  257. data/test/xml/test_dtd_encoding.rb +3 -1
  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 +120 -0
  261. data/test/xml/test_entity_reference.rb +5 -1
  262. data/test/xml/test_namespace.rb +68 -0
  263. data/test/xml/test_node.rb +546 -201
  264. data/test/xml/test_node_attributes.rb +34 -0
  265. data/test/xml/test_node_encoding.rb +33 -3
  266. data/test/xml/test_node_reparenting.rb +321 -0
  267. data/test/xml/test_node_set.rb +538 -2
  268. data/test/xml/test_parse_options.rb +52 -0
  269. data/test/xml/test_processing_instruction.rb +6 -1
  270. data/test/xml/test_reader_encoding.rb +1 -1
  271. data/test/xml/test_relax_ng.rb +60 -0
  272. data/test/xml/test_schema.rb +94 -0
  273. data/test/xml/test_syntax_error.rb +12 -0
  274. data/test/xml/test_text.rb +35 -1
  275. data/test/xml/test_unparented_node.rb +5 -5
  276. data/test/xml/test_xpath.rb +142 -11
  277. data/test/xslt/test_custom_functions.rb +94 -0
  278. metadata +328 -92
  279. data/ext/nokogiri/html_sax_parser.c +0 -57
  280. data/ext/nokogiri/html_sax_parser.h +0 -11
  281. data/ext/nokogiri/iconv.dll +0 -0
  282. data/ext/nokogiri/libexslt.dll +0 -0
  283. data/ext/nokogiri/libxml2.dll +0 -0
  284. data/ext/nokogiri/libxslt.dll +0 -0
  285. data/ext/nokogiri/native.so +0 -0
  286. data/ext/nokogiri/xml_xpath.c +0 -53
  287. data/ext/nokogiri/xml_xpath.h +0 -11
  288. data/ext/nokogiri/zlib1.dll +0 -0
  289. data/lib/action-nokogiri.rb +0 -30
  290. data/lib/nokogiri/css/generated_parser.rb +0 -713
  291. data/lib/nokogiri/css/generated_tokenizer.rb +0 -144
  292. data/lib/nokogiri/decorators.rb +0 -2
  293. data/lib/nokogiri/decorators/hpricot.rb +0 -3
  294. data/lib/nokogiri/decorators/hpricot/node.rb +0 -56
  295. data/lib/nokogiri/decorators/hpricot/node_set.rb +0 -54
  296. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +0 -28
  297. data/lib/nokogiri/hpricot.rb +0 -51
  298. data/lib/nokogiri/xml/comment.rb +0 -6
  299. data/lib/nokogiri/xml/element.rb +0 -6
  300. data/lib/nokogiri/xml/entity_declaration.rb +0 -9
  301. data/lib/nokogiri/xml/fragment_handler.rb +0 -34
  302. data/test/hpricot/files/basic.xhtml +0 -17
  303. data/test/hpricot/files/boingboing.html +0 -2266
  304. data/test/hpricot/files/cy0.html +0 -3653
  305. data/test/hpricot/files/immob.html +0 -400
  306. data/test/hpricot/files/pace_application.html +0 -1320
  307. data/test/hpricot/files/tenderlove.html +0 -16
  308. data/test/hpricot/files/uswebgen.html +0 -220
  309. data/test/hpricot/files/utf8.html +0 -1054
  310. data/test/hpricot/files/week9.html +0 -1723
  311. data/test/hpricot/files/why.xml +0 -19
  312. data/test/hpricot/load_files.rb +0 -11
  313. data/test/hpricot/test_alter.rb +0 -68
  314. data/test/hpricot/test_builder.rb +0 -20
  315. data/test/hpricot/test_parser.rb +0 -426
  316. data/test/hpricot/test_paths.rb +0 -15
  317. data/test/hpricot/test_preserved.rb +0 -77
  318. data/test/hpricot/test_xml.rb +0 -30
  319. data/test/test_gc.rb +0 -15
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module XML
@@ -12,6 +12,10 @@ module Nokogiri
12
12
  assert ref = EntityReference.new(@xml, 'ent4')
13
13
  assert_instance_of EntityReference, ref
14
14
  end
15
+
16
+ def test_many_references
17
+ 100.times { EntityReference.new(@xml, 'foo') }
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -0,0 +1,68 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNamespace < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML <<-eoxml
9
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
10
+ <awesome/>
11
+ </root>
12
+ eoxml
13
+ end
14
+
15
+ def test_built_nodes_keep_namespace_decls
16
+ doc = Document.new
17
+ e = Node.new 'element', doc
18
+ c = Node.new 'child', doc
19
+ c.default_namespace = 'woop:de:doo'
20
+
21
+ assert c.namespace, 'has a namespace'
22
+ e.add_child c
23
+ assert c.namespace, 'has a namespace'
24
+
25
+ doc.add_child e
26
+ assert c.namespace, 'has a namespace'
27
+ end
28
+
29
+ def test_inspect
30
+ ns = @xml.root.namespace
31
+ assert_equal "#<#{ns.class.name}:#{sprintf("0x%x", ns.object_id)} href=#{ns.href.inspect}>", ns.inspect
32
+ end
33
+
34
+ def test_namespace_is_in_node_cache
35
+ node = @xml.root.namespace
36
+ assert @xml.instance_variable_get(:@node_cache).include?(node)
37
+ end
38
+
39
+ def test_namespace_node_prefix
40
+ namespaces = @xml.root.namespace_definitions
41
+ assert_equal [nil, 'foo'], namespaces.map { |x| x.prefix }
42
+ end
43
+
44
+ def test_namespace_node_href
45
+ namespaces = @xml.root.namespace_definitions
46
+ assert_equal [
47
+ 'http://tenderlovemaking.com/',
48
+ 'bar'
49
+ ], namespaces.map { |x| x.href }
50
+ end
51
+
52
+ def test_equality
53
+ namespaces = @xml.root.namespace_definitions
54
+ assert_equal namespaces, @xml.root.namespace_definitions
55
+ end
56
+
57
+ def test_add_definition
58
+ @xml.root.add_namespace_definition('baz', 'bar')
59
+ assert_equal 3, @xml.root.namespace_definitions.length
60
+ end
61
+
62
+ def test_add_definition_return
63
+ ns = @xml.root.add_namespace_definition('baz', 'bar')
64
+ assert_equal 'baz', ns.prefix
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  require 'stringio'
4
4
 
@@ -7,7 +7,292 @@ module Nokogiri
7
7
  class TestNode < Nokogiri::TestCase
8
8
  def setup
9
9
  super
10
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
10
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
11
+ end
12
+
13
+ def test_first_element_child
14
+ node = @xml.root.first_element_child
15
+ assert_equal 'employee', node.name
16
+ assert node.element?, 'node is an element'
17
+ end
18
+
19
+ def test_element_children
20
+ nodes = @xml.root.element_children
21
+ assert_equal @xml.root.first_element_child, nodes.first
22
+ assert nodes.all? { |node| node.element? }, 'all nodes are elements'
23
+ end
24
+
25
+ def test_last_element_child
26
+ nodes = @xml.root.element_children
27
+ assert_equal nodes.last, @xml.root.element_children.last
28
+ end
29
+
30
+ def test_bad_xpath
31
+ bad_xpath = '//foo['
32
+
33
+ begin
34
+ @xml.xpath(bad_xpath)
35
+ rescue Nokogiri::XML::XPath::SyntaxError => e
36
+ assert_match(bad_xpath, e.to_s)
37
+ end
38
+ end
39
+
40
+ def test_namespace_type_error
41
+ assert_raises(TypeError) do
42
+ @xml.root.namespace = Object.new
43
+ end
44
+ end
45
+
46
+ def test_remove_namespace
47
+ @xml = Nokogiri::XML('<r xmlns="v"><s /></r>')
48
+ tag = @xml.at('s')
49
+ assert tag.namespace
50
+ tag.namespace = nil
51
+ assert_nil tag.namespace
52
+ end
53
+
54
+ def test_parse_needs_doc
55
+ list = @xml.root.parse('fooooooo <hello />')
56
+ assert_equal 1, list.css('hello').length
57
+ end
58
+
59
+ def test_parse
60
+ list = @xml.root.parse('fooooooo <hello />')
61
+ assert_equal 2, list.length
62
+ end
63
+
64
+ def test_parse_with_block
65
+ called = false
66
+ list = @xml.root.parse('<hello />') { |cfg|
67
+ called = true
68
+ assert_instance_of Nokogiri::XML::ParseOptions, cfg
69
+ }
70
+ assert called, 'config block called'
71
+ assert_equal 1, list.length
72
+ end
73
+
74
+ def test_parse_with_io
75
+ list = @xml.root.parse(StringIO.new('<hello />'))
76
+ assert_equal 1, list.length
77
+ assert_equal 'hello', list.first.name
78
+ end
79
+
80
+ def test_parse_with_empty_string
81
+ list = @xml.root.parse('')
82
+ assert_equal 0, list.length
83
+ end
84
+
85
+ def test_parse_config_option
86
+ node = @xml.root
87
+ options = nil
88
+ node.parse("<item></item>") do |config|
89
+ options = config
90
+ end
91
+ assert_equal Nokogiri::XML::ParseOptions::DEFAULT_XML, options.to_i
92
+ end
93
+
94
+ # descriptive, not prescriptive.
95
+ def test_parse_invalid_html_markup_results_in_empty_nodeset
96
+ doc = Nokogiri::HTML("<html></html>")
97
+ nodeset = doc.root.parse "<div><div>a</div><snippet>b</snippet></div>"
98
+ assert_equal 1, doc.errors.length # "Tag snippet invalid"
99
+ assert_equal 1, nodeset.length
100
+ end
101
+
102
+ def test_node_context_parsing_of_malformed_html_fragment_with_recover_is_corrected
103
+ doc = HTML.parse "<html><body><div></div></body></html>"
104
+ context_node = doc.at_css "div"
105
+ nodeset = context_node.parse("<div </div>") do |options|
106
+ options.recover
107
+ end
108
+ assert_equal "<div></div>", nodeset.to_s
109
+ assert_equal 1, doc.errors.length
110
+ assert_equal 1, nodeset.length
111
+ end
112
+
113
+ def test_node_context_parsing_of_malformed_html_fragment_without_recover_is_not_corrected
114
+ doc = HTML.parse "<html><body><div></div></body></html>"
115
+ context_node = doc.at_css "div"
116
+ nodeset = context_node.parse("<div </div>") do |options|
117
+ options.strict
118
+ end
119
+ assert_equal 1, doc.errors.length
120
+ assert_equal 0, nodeset.length
121
+ end
122
+
123
+ def test_parse_error_list
124
+ error_count = @xml.errors.length
125
+ @xml.root.parse('<hello>')
126
+ assert(error_count < @xml.errors.length, "errors should have increased")
127
+ end
128
+
129
+ def test_subclass_dup
130
+ subclass = Class.new(Nokogiri::XML::Node)
131
+ node = subclass.new('foo', @xml).dup
132
+ assert_instance_of subclass, node
133
+ end
134
+
135
+ def test_gt_string_arg
136
+ node = @xml.at('employee')
137
+ nodes = (node > 'name')
138
+ assert_equal 1, nodes.length
139
+ assert_equal node, nodes.first.parent
140
+ end
141
+
142
+ def test_next_element_when_next_sibling_is_element_should_return_next_sibling
143
+ doc = Nokogiri::XML "<root><foo /><quux /></root>"
144
+ node = doc.at_css("foo")
145
+ next_element = node.next_element
146
+ assert next_element.element?
147
+ assert_equal doc.at_css("quux"), next_element
148
+ end
149
+
150
+ def test_next_element_when_there_is_no_next_sibling_should_return_nil
151
+ doc = Nokogiri::XML "<root><foo /><quux /></root>"
152
+ assert_nil doc.at_css("quux").next_element
153
+ end
154
+
155
+ def test_next_element_when_next_sibling_is_not_an_element_should_return_closest_next_element_sibling
156
+ doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
157
+ node = doc.at_css("foo")
158
+ next_element = node.next_element
159
+ assert next_element.element?
160
+ assert_equal doc.at_css("quux"), next_element
161
+ end
162
+
163
+ def test_next_element_when_next_sibling_is_not_an_element_and_no_following_element_should_return_nil
164
+ doc = Nokogiri::XML "<root><foo />bar</root>"
165
+ node = doc.at_css("foo")
166
+ next_element = node.next_element
167
+ assert_nil next_element
168
+ end
169
+
170
+ def test_previous_element_when_previous_sibling_is_element_should_return_previous_sibling
171
+ doc = Nokogiri::XML "<root><foo /><quux /></root>"
172
+ node = doc.at_css("quux")
173
+ previous_element = node.previous_element
174
+ assert previous_element.element?
175
+ assert_equal doc.at_css("foo"), previous_element
176
+ end
177
+
178
+ def test_previous_element_when_there_is_no_previous_sibling_should_return_nil
179
+ doc = Nokogiri::XML "<root><foo /><quux /></root>"
180
+ assert_nil doc.at_css("foo").previous_element
181
+ end
182
+
183
+ def test_previous_element_when_previous_sibling_is_not_an_element_should_return_closest_previous_element_sibling
184
+ doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
185
+ node = doc.at_css("quux")
186
+ previous_element = node.previous_element
187
+ assert previous_element.element?
188
+ assert_equal doc.at_css("foo"), previous_element
189
+ end
190
+
191
+ def test_previous_element_when_previous_sibling_is_not_an_element_and_no_following_element_should_return_nil
192
+ doc = Nokogiri::XML "<root>foo<bar /></root>"
193
+ node = doc.at_css("bar")
194
+ previous_element = node.previous_element
195
+ assert_nil previous_element
196
+ end
197
+
198
+ def test_element?
199
+ assert @xml.root.element?, 'is an element'
200
+ end
201
+
202
+ def test_slash_search
203
+ assert_equal 'EMP0001', (@xml/:staff/:employee/:employeeId).first.text
204
+ end
205
+
206
+ def test_append_with_document
207
+ assert_raises(ArgumentError) do
208
+ @xml.root << Nokogiri::XML::Document.new
209
+ end
210
+ end
211
+
212
+ def test_inspect_ns
213
+ xml = Nokogiri::XML(<<-eoxml) { |c| c.noblanks }
214
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
215
+ <awesome/>
216
+ </root>
217
+ eoxml
218
+ ins = xml.inspect
219
+
220
+ xml.traverse do |node|
221
+ assert_match node.class.name, ins
222
+ if node.respond_to? :attributes
223
+ node.attributes.each do |k,v|
224
+ assert_match k, ins
225
+ assert_match v, ins
226
+ end
227
+ end
228
+
229
+ if node.respond_to?(:namespace) && node.namespace
230
+ assert_match node.namespace.class.name, ins
231
+ assert_match node.namespace.href, ins
232
+ end
233
+ end
234
+ end
235
+
236
+ def test_namespace_definitions_when_some_exist
237
+ xml = Nokogiri::XML <<-eoxml
238
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
239
+ <awesome/>
240
+ </root>
241
+ eoxml
242
+ namespace_definitions = xml.root.namespace_definitions
243
+ assert_equal 2, namespace_definitions.length
244
+ end
245
+
246
+ def test_namespace_definitions_when_no_exist
247
+ xml = Nokogiri::XML <<-eoxml
248
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
249
+ <awesome/>
250
+ </root>
251
+ eoxml
252
+ namespace_definitions = xml.at_xpath('//xmlns:awesome').namespace_definitions
253
+ assert_equal 0, namespace_definitions.length
254
+ end
255
+
256
+ def test_namespace_goes_to_children
257
+ fruits = Nokogiri::XML(<<-eoxml)
258
+ <Fruit xmlns='www.fruits.org'>
259
+ </Fruit>
260
+ eoxml
261
+ apple = Nokogiri::XML::Node.new('Apple', fruits)
262
+ orange = Nokogiri::XML::Node.new('Orange', fruits)
263
+ apple << orange
264
+ fruits.root << apple
265
+ assert fruits.at('//fruit:Orange',{'fruit'=>'www.fruits.org'})
266
+ assert fruits.at('//fruit:Apple',{'fruit'=>'www.fruits.org'})
267
+ end
268
+
269
+ def test_description
270
+ assert_nil @xml.at('employee').description
271
+ end
272
+
273
+ def test_spaceship
274
+ nodes = @xml.xpath('//employee')
275
+ assert_equal(-1, (nodes.first <=> nodes.last))
276
+ list = [nodes.first, nodes.last].sort
277
+ assert_equal nodes.first, list.first
278
+ assert_equal nodes.last, list.last
279
+ end
280
+
281
+ def test_incorrect_spaceship
282
+ nodes = @xml.xpath('//employee')
283
+ assert_nil(nodes.first <=> 'asdf')
284
+ end
285
+
286
+ def test_document_compare
287
+ nodes = @xml.xpath('//employee')
288
+ assert_equal(-1, (nodes.first <=> @xml))
289
+ end
290
+
291
+ def test_different_document_compare
292
+ nodes = @xml.xpath('//employee')
293
+ doc = Nokogiri::XML('<a><b/></a>')
294
+ b = doc.at('b')
295
+ assert_nil(nodes.first <=> b)
11
296
  end
12
297
 
13
298
  def test_duplicate_node_removes_namespace
@@ -22,22 +307,38 @@ module Nokogiri
22
307
  assert_equal 2, fruits.xpath('//xmlns:Apple').length
23
308
  assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
24
309
  end
25
-
310
+
311
+ [:clone, :dup].each do |symbol|
312
+ define_method "test_#{symbol}" do
313
+ node = @xml.at('//employee')
314
+ other = node.send(symbol)
315
+ assert_equal "employee", other.name
316
+ assert_nil other.parent
317
+ end
318
+ end
319
+
320
+ def test_fragment_creates_elements
321
+ apple = @xml.fragment('<Apple/>')
322
+ apple.children.each do |child|
323
+ assert_equal Nokogiri::XML::Node::ELEMENT_NODE, child.type
324
+ assert_instance_of Nokogiri::XML::Element, child
325
+ end
326
+ end
327
+
26
328
  def test_node_added_to_root_should_get_namespace
27
329
  fruits = Nokogiri::XML(<<-eoxml)
28
330
  <Fruit xmlns='http://www.fruits.org'>
29
331
  </Fruit>
30
332
  eoxml
31
333
  apple = fruits.fragment('<Apple/>')
32
- fruits << apple
334
+ fruits.root << apple
33
335
  assert_equal 1, fruits.xpath('//xmlns:Apple').length
34
336
  end
35
337
 
36
- def test_add_child_path_following_sequential_text_nodes
338
+ def test_new_node_can_have_ancestors
37
339
  xml = Nokogiri::XML('<root>text</root>')
38
- xml.root.add_child(Nokogiri::XML::Text.new('text', xml))
39
- item = xml.root.add_child(Nokogiri::XML::Element.new('item', xml))
40
- assert_equal '/root/item', item.path
340
+ item = Nokogiri::XML::Element.new('item', xml)
341
+ assert_equal 0, item.ancestors.length
41
342
  end
42
343
 
43
344
  def test_children
@@ -60,17 +361,118 @@ module Nokogiri
60
361
  assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
61
362
  end
62
363
 
63
- def test_add_child_should_inherit_namespace
64
- doc = Nokogiri::XML(<<-eoxml)
65
- <root xmlns="http://tenderlovemaking.com/">
66
- <first>
67
- </first>
68
- </root>
69
- eoxml
70
- assert node = doc.at('//xmlns:first')
71
- child = Nokogiri::XML::Node.new('second', doc)
72
- node.add_child(child)
73
- assert doc.at('//xmlns:second')
364
+ def test_add_namespace_twice
365
+ node = @xml.at('address')
366
+ ns = node.add_namespace('foo', 'http://tenderlovemaking.com')
367
+ ns2 = node.add_namespace('foo', 'http://tenderlovemaking.com')
368
+ assert_equal ns, ns2
369
+ end
370
+
371
+ def test_add_default_ns
372
+ node = @xml.at('address')
373
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
374
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
375
+ end
376
+
377
+ def test_add_multiple_namespaces
378
+ node = @xml.at('address')
379
+
380
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
381
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
382
+
383
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
384
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
385
+ end
386
+
387
+ def test_default_namespace=
388
+ node = @xml.at('address')
389
+ node.default_namespace = 'http://tenderlovemaking.com'
390
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
391
+ end
392
+
393
+ def test_namespace=
394
+ node = @xml.at('address')
395
+ assert_nil node.namespace
396
+ definition = node.add_namespace_definition 'bar', 'http://tlm.com/'
397
+
398
+ node.namespace = definition
399
+
400
+ assert_equal definition, node.namespace
401
+
402
+ assert_equal node, @xml.at('//foo:address', {
403
+ 'foo' => 'http://tlm.com/'
404
+ })
405
+ end
406
+
407
+ def test_add_namespace_with_nil_associates_node
408
+ node = @xml.at('address')
409
+ assert_nil node.namespace
410
+ definition = node.add_namespace_definition nil, 'http://tlm.com/'
411
+ assert_equal definition, node.namespace
412
+ end
413
+
414
+ def test_add_namespace_does_not_associate_node
415
+ node = @xml.at('address')
416
+ assert_nil node.namespace
417
+ assert node.add_namespace_definition 'foo', 'http://tlm.com/'
418
+ assert_nil node.namespace
419
+ end
420
+
421
+ def test_set_namespace_from_different_doc
422
+ node = @xml.at('address')
423
+ doc = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
424
+ decl = doc.root.add_namespace_definition 'foo', 'bar'
425
+
426
+ assert_raises(ArgumentError) do
427
+ node.namespace = decl
428
+ end
429
+ end
430
+
431
+ def test_set_namespace_must_only_take_a_namespace
432
+ node = @xml.at('address')
433
+ assert_raises(TypeError) do
434
+ node.namespace = node
435
+ end
436
+ end
437
+
438
+ def test_at
439
+ node = @xml.at('address')
440
+ assert_equal node, @xml.xpath('//address').first
441
+ end
442
+
443
+ def test_at_xpath
444
+ node = @xml.at_xpath('//address')
445
+ nodes = @xml.xpath('//address')
446
+ assert_equal 5, nodes.size
447
+ assert_equal node, nodes.first
448
+ end
449
+
450
+ def test_at_css
451
+ node = @xml.at_css('address')
452
+ nodes = @xml.css('address')
453
+ assert_equal 5, nodes.size
454
+ assert_equal node, nodes.first
455
+ end
456
+
457
+ def test_percent
458
+ node = @xml % ('address')
459
+ assert_equal node, @xml.xpath('//address').first
460
+ end
461
+
462
+ def test_accept
463
+ visitor = Class.new {
464
+ attr_accessor :visited
465
+ def accept target
466
+ target.accept(self)
467
+ end
468
+
469
+ def visit node
470
+ node.children.each { |c| c.accept(self) }
471
+ @visited = true
472
+ end
473
+ }.new
474
+ visitor.accept(@xml.root)
475
+ assert visitor.visited
74
476
  end
75
477
 
76
478
  def test_write_to
@@ -95,7 +497,9 @@ module Nokogiri
95
497
  end
96
498
  io.rewind
97
499
  assert called
98
- assert_equal @xml.serialize(nil, conf.options), io.read
500
+ string = io.read
501
+ assert_equal @xml.serialize(nil, conf.options), string
502
+ assert_equal @xml.serialize(nil, conf), string
99
503
  end
100
504
 
101
505
  %w{ xml html xhtml }.each do |type|
@@ -117,6 +521,7 @@ module Nokogiri
117
521
  end
118
522
  assert called
119
523
  assert_equal @xml.serialize(nil, conf.options), string
524
+ assert_equal @xml.serialize(nil, conf), string
120
525
  end
121
526
 
122
527
  def test_hold_refence_to_subnode
@@ -152,6 +557,7 @@ module Nokogiri
152
557
  def test_new
153
558
  assert node = Nokogiri::XML::Node.new('input', @xml)
154
559
  assert_equal 1, node.node_type
560
+ assert_instance_of Nokogiri::XML::Element, node
155
561
  end
156
562
 
157
563
  def test_to_str
@@ -188,110 +594,12 @@ module Nokogiri
188
594
  assert_nil address['domestic']
189
595
  end
190
596
 
191
- def test_add_child_in_same_document
192
- child = @xml.css('employee').first
193
-
194
- assert previous_last_child = child.children.last
195
- assert new_child = child.children.first
196
-
197
- last = child.children.last
198
-
199
- child.add_child(new_child)
200
- assert_equal new_child, child.children.last
201
- assert_equal last, child.children.last
202
- end
203
-
204
- def test_add_child_from_other_document
205
- d1 = Nokogiri::XML("<root><item>1</item><item>2</item></root>")
206
- d2 = Nokogiri::XML("<root><item>3</item><item>4</item></root>")
207
-
208
- d2.at('root').search('item').each do |i|
209
- d1.at('root').add_child i
210
- end
211
-
212
- assert_equal 0, d2.search('item').size
213
- assert_equal 4, d1.search('item').size
214
- end
215
-
216
- def test_add_child
217
- xml = Nokogiri::XML(<<-eoxml)
218
- <root>
219
- <a>Hello world</a>
220
- </root>
221
- eoxml
222
- text_node = Nokogiri::XML::Text.new('hello', xml)
223
- assert_equal Nokogiri::XML::Node::TEXT_NODE, text_node.type
224
- xml.root.add_child text_node
225
- assert_match 'hello', xml.to_s
226
- end
227
-
228
- def test_chevron_works_as_add_child
229
- xml = Nokogiri::XML(<<-eoxml)
230
- <root>
231
- <a>Hello world</a>
232
- </root>
233
- eoxml
234
- text_node = Nokogiri::XML::Text.new('hello', xml)
235
- xml.root << text_node
236
- assert_match 'hello', xml.to_s
237
- end
238
-
239
597
  def test_set_content_with_symbol
240
598
  node = @xml.at('//name')
241
599
  node.content = :foo
242
600
  assert_equal 'foo', node.content
243
601
  end
244
602
 
245
- def test_add_previous_sibling
246
- xml = Nokogiri::XML(<<-eoxml)
247
- <root>
248
- <a>Hello world</a>
249
- </root>
250
- eoxml
251
- b_node = Nokogiri::XML::Node.new('a', xml)
252
- assert_equal Nokogiri::XML::Node::ELEMENT_NODE, b_node.type
253
- b_node.content = 'first'
254
- a_node = xml.xpath('//a').first
255
- a_node.add_previous_sibling(b_node)
256
- assert_equal('first', xml.xpath('//a').first.text)
257
- end
258
-
259
- def test_add_previous_sibling_merge
260
- xml = Nokogiri::XML(<<-eoxml)
261
- <root>
262
- <a>Hello world</a>
263
- </root>
264
- eoxml
265
-
266
- assert a_tag = xml.css('a').first
267
-
268
- left_space = a_tag.previous
269
- right_space = a_tag.next
270
- assert left_space.text?
271
- assert right_space.text?
272
-
273
- left_space.add_previous_sibling(right_space)
274
- assert_equal left_space, right_space
275
- end
276
-
277
- def test_add_next_sibling_merge
278
- xml = Nokogiri::XML(<<-eoxml)
279
- <root>
280
- <a>Hello world</a>
281
- </root>
282
- eoxml
283
-
284
- assert a_tag = xml.css('a').first
285
-
286
- left_space = a_tag.previous
287
- right_space = a_tag.next
288
- assert left_space.text?
289
- assert right_space.text?
290
-
291
- right_space.add_next_sibling(left_space)
292
- assert_equal left_space, right_space
293
- end
294
-
295
603
  def test_find_by_css_with_tilde_eql
296
604
  xml = Nokogiri::XML.parse(<<-eoxml)
297
605
  <root>
@@ -391,6 +699,15 @@ module Nokogiri
391
699
  assert_equal('/staff/employee[1]', node.path)
392
700
  end
393
701
 
702
+ def test_parent_xpath
703
+ assert set = @xml.search('//employee')
704
+ assert node = set.first
705
+ assert parent_set = node.search('..')
706
+ assert parent_node = parent_set.first
707
+ assert_equal '/staff', parent_node.path
708
+ assert_equal node.parent, parent_node
709
+ end
710
+
394
711
  def test_search_by_symbol
395
712
  assert set = @xml.search(:employee)
396
713
  assert 5, set.length
@@ -416,6 +733,17 @@ module Nokogiri
416
733
 
417
734
  node.content = 'hello world!'
418
735
  assert_equal('hello world!', node.content)
736
+
737
+ node.content = '& <foo> &amp;'
738
+ assert_equal('& <foo> &amp;', node.content)
739
+ assert_equal('<form>&amp; &lt;foo&gt; &amp;amp;</form>', node.to_xml)
740
+ end
741
+
742
+ def test_set_content_should_unlink_existing_content
743
+ node = @xml.at_css("employee")
744
+ children = node.children
745
+ node.content = "hello"
746
+ children.each { |child| assert_nil child.parent }
419
747
  end
420
748
 
421
749
  def test_whitespace_nodes
@@ -425,53 +753,29 @@ module Nokogiri
425
753
  assert_equal " ", children[3]
426
754
  end
427
755
 
428
- def test_replace
429
- set = @xml.search('//employee')
430
- assert 5, set.length
431
- assert 0, @xml.search('//form').length
432
-
433
- first = set[0]
434
- second = set[1]
435
-
436
- node = Nokogiri::XML::Node.new('form', @xml)
437
- first.replace(node)
438
-
439
- assert set = @xml.search('//employee')
440
- assert_equal 4, set.length
441
- assert 1, @xml.search('//form').length
442
-
443
- assert_equal set[0].to_xml, second.to_xml
444
- end
445
-
446
- def test_illegal_replace_of_node_with_doc
447
- new_node = Nokogiri::XML.parse('<foo>bar</foo>')
448
- old_node = @xml.at('//employee')
449
- assert_raises(ArgumentError){ old_node.replace new_node }
450
- end
451
-
452
756
  def test_node_equality
453
757
  doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
454
758
  doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
455
-
759
+
456
760
  address1_1 = doc1.xpath('//address').first
457
761
  address1_2 = doc1.xpath('//address').first
458
-
762
+
459
763
  address2 = doc2.xpath('//address').first
460
-
764
+
461
765
  assert_not_equal address1_1, address2 # two references to very, very similar nodes
462
766
  assert_equal address1_1, address1_2 # two references to the exact same node
463
767
  end
464
768
 
465
- def test_namespace_as_hash
769
+ def test_namespace_search_with_xpath_and_hash
466
770
  xml = Nokogiri::XML.parse(<<-eoxml)
467
- <root>
468
- <car xmlns:part="http://general-motors.com/">
469
- <part:tire>Michelin Model XGV</part:tire>
470
- </car>
471
- <bicycle xmlns:part="http://schwinn.com/">
472
- <part:tire>I'm a bicycle tire!</part:tire>
473
- </bicycle>
474
- </root>
771
+ <root>
772
+ <car xmlns:part="http://general-motors.com/">
773
+ <part:tire>Michelin Model XGV</part:tire>
774
+ </car>
775
+ <bicycle xmlns:part="http://schwinn.com/">
776
+ <part:tire>I'm a bicycle tire!</part:tire>
777
+ </bicycle>
778
+ </root>
475
779
  eoxml
476
780
 
477
781
  tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
@@ -480,68 +784,90 @@ module Nokogiri
480
784
 
481
785
  def test_namespace_search_with_css
482
786
  xml = Nokogiri::XML.parse(<<-eoxml)
483
- <root>
484
- <car xmlns:part="http://general-motors.com/">
485
- <part:tire>Michelin Model XGV</part:tire>
486
- </car>
487
- <bicycle xmlns:part="http://schwinn.com/">
488
- <part:tire>I'm a bicycle tire!</part:tire>
489
- </bicycle>
490
- </root>
787
+ <root>
788
+ <car xmlns:part="http://general-motors.com/">
789
+ <part:tire>Michelin Model XGV</part:tire>
790
+ </car>
791
+ <bicycle xmlns:part="http://schwinn.com/">
792
+ <part:tire>I'm a bicycle tire!</part:tire>
793
+ </bicycle>
794
+ </root>
491
795
  eoxml
492
796
 
493
797
  tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/')
494
798
  assert_equal 1, tires.length
495
799
  end
496
800
 
497
- def test_namespaces
801
+ def test_namespaces_should_include_all_namespace_definitions
498
802
  xml = Nokogiri::XML.parse(<<-EOF)
499
- <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
500
- <y xmlns:c='http://bazz.com/'>
501
- <a:div>hello a</a:div>
502
- <b:div>hello b</b:div>
503
- <c:div>hello c</c:div>
504
- </y>
505
- </x>
506
- EOF
507
- assert namespaces = xml.root.namespaces
508
- assert namespaces.key?('xmlns:a')
509
- assert_equal 'http://foo.com/', namespaces['xmlns:a']
510
- assert namespaces.key?('xmlns:b')
511
- assert_equal 'http://bar.com/', namespaces['xmlns:b']
512
- assert ! namespaces.key?('xmlns:c')
513
-
514
- assert namespaces = xml.namespaces
515
- assert namespaces.key?('xmlns:a')
516
- assert_equal 'http://foo.com/', namespaces['xmlns:a']
517
- assert namespaces.key?('xmlns:b')
518
- assert_equal 'http://bar.com/', namespaces['xmlns:b']
519
- assert namespaces.key?('xmlns:c')
520
- assert_equal 'http://bazz.com/', namespaces['xmlns:c']
521
-
522
- assert_equal "hello a", xml.search("//a:div", xml.namespaces).first.inner_text
523
- assert_equal "hello b", xml.search("//b:div", xml.namespaces).first.inner_text
524
- assert_equal "hello c", xml.search("//c:div", xml.namespaces).first.inner_text
803
+ <x xmlns="http://quux.com/" xmlns:a="http://foo.com/" xmlns:b="http://bar.com/">
804
+ <y xmlns:c="http://bazz.com/">
805
+ <z>hello</z>
806
+ <a xmlns:c="http://newc.com/" />
807
+ </y>
808
+ </x>
809
+ EOF
810
+
811
+ namespaces = xml.namespaces # Document#namespace
812
+ assert_equal({"xmlns" => "http://quux.com/",
813
+ "xmlns:a" => "http://foo.com/",
814
+ "xmlns:b" => "http://bar.com/"}, namespaces)
815
+
816
+ namespaces = xml.root.namespaces
817
+ assert_equal({"xmlns" => "http://quux.com/",
818
+ "xmlns:a" => "http://foo.com/",
819
+ "xmlns:b" => "http://bar.com/"}, namespaces)
820
+
821
+ namespaces = xml.at_xpath("//xmlns:y").namespaces
822
+ assert_equal({"xmlns" => "http://quux.com/",
823
+ "xmlns:a" => "http://foo.com/",
824
+ "xmlns:b" => "http://bar.com/",
825
+ "xmlns:c" => "http://bazz.com/"}, namespaces)
826
+
827
+ namespaces = xml.at_xpath("//xmlns:z").namespaces
828
+ assert_equal({"xmlns" => "http://quux.com/",
829
+ "xmlns:a" => "http://foo.com/",
830
+ "xmlns:b" => "http://bar.com/",
831
+ "xmlns:c" => "http://bazz.com/"}, namespaces)
832
+
833
+ namespaces = xml.at_xpath("//xmlns:a").namespaces
834
+ assert_equal({"xmlns" => "http://quux.com/",
835
+ "xmlns:a" => "http://foo.com/",
836
+ "xmlns:b" => "http://bar.com/",
837
+ "xmlns:c" => "http://newc.com/"}, namespaces)
525
838
  end
526
839
 
527
840
  def test_namespace
528
841
  xml = Nokogiri::XML.parse(<<-EOF)
529
- <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
530
- <y xmlns:c='http://bazz.com/'>
531
- <a:div>hello a</a:div>
532
- <b:div>hello b</b:div>
533
- <c:div>hello c</c:div>
534
- <div>hello moon</div>
535
- </y>
536
- </x>
537
- EOF
842
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
843
+ <y xmlns:c='http://bazz.com/'>
844
+ <a:div>hello a</a:div>
845
+ <b:div>hello b</b:div>
846
+ <c:div>hello c</c:div>
847
+ <div>hello moon</div>
848
+ </y>
849
+ </x>
850
+ EOF
538
851
  set = xml.search("//y/*")
539
- assert_equal "a", set[0].namespace
540
- assert_equal "b", set[1].namespace
541
- assert_equal "c", set[2].namespace
852
+ assert_equal "a", set[0].namespace.prefix
853
+ assert_equal "b", set[1].namespace.prefix
854
+ assert_equal "c", set[2].namespace.prefix
542
855
  assert_equal nil, set[3].namespace
543
856
  end
544
857
 
858
+ def test_namespace_without_an_href_on_html_node
859
+ # because microsoft word's HTML formatting does this. ick.
860
+ xml = Nokogiri::HTML.parse <<-EOF
861
+ <div><o:p>foo</o:p></div>
862
+ EOF
863
+
864
+ assert_not_nil(node = xml.at('p'))
865
+
866
+ assert_equal 1, node.namespaces.keys.size
867
+ assert node.namespaces.has_key?('xmlns:o')
868
+ assert_equal nil, node.namespaces['xmlns:o']
869
+ end
870
+
545
871
  def test_line
546
872
  xml = Nokogiri::XML(<<-eoxml)
547
873
  <root>
@@ -556,6 +882,25 @@ EOF
556
882
  assert_equal 2, node.line
557
883
  end
558
884
 
885
+ def test_xpath_results_have_document_and_are_decorated
886
+ x = Module.new do
887
+ def awesome! ; end
888
+ end
889
+ util_decorate(@xml, x)
890
+ node_set = @xml.xpath("//employee")
891
+ assert_equal @xml, node_set.document
892
+ assert node_set.respond_to?(:awesome!)
893
+ end
894
+
895
+ def test_css_results_have_document_and_are_decorated
896
+ x = Module.new do
897
+ def awesome! ; end
898
+ end
899
+ util_decorate(@xml, x)
900
+ node_set = @xml.css("employee")
901
+ assert_equal @xml, node_set.document
902
+ assert node_set.respond_to?(:awesome!)
903
+ end
559
904
  end
560
905
  end
561
906
  end