caring-nokogiri 1.4.1.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG.ja.rdoc +330 -0
  3. data/CHANGELOG.rdoc +324 -0
  4. data/Manifest.txt +269 -0
  5. data/README.ja.rdoc +105 -0
  6. data/README.rdoc +118 -0
  7. data/Rakefile +248 -0
  8. data/bin/nokogiri +49 -0
  9. data/ext/nokogiri/extconf.rb +147 -0
  10. data/ext/nokogiri/html_document.c +145 -0
  11. data/ext/nokogiri/html_document.h +10 -0
  12. data/ext/nokogiri/html_element_description.c +272 -0
  13. data/ext/nokogiri/html_element_description.h +10 -0
  14. data/ext/nokogiri/html_entity_lookup.c +32 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser_context.c +92 -0
  17. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  18. data/ext/nokogiri/nokogiri.c +95 -0
  19. data/ext/nokogiri/nokogiri.h +145 -0
  20. data/ext/nokogiri/xml_attr.c +92 -0
  21. data/ext/nokogiri/xml_attr.h +9 -0
  22. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  23. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  24. data/ext/nokogiri/xml_cdata.c +54 -0
  25. data/ext/nokogiri/xml_cdata.h +9 -0
  26. data/ext/nokogiri/xml_comment.c +52 -0
  27. data/ext/nokogiri/xml_comment.h +9 -0
  28. data/ext/nokogiri/xml_document.c +388 -0
  29. data/ext/nokogiri/xml_document.h +24 -0
  30. data/ext/nokogiri/xml_document_fragment.c +46 -0
  31. data/ext/nokogiri/xml_document_fragment.h +10 -0
  32. data/ext/nokogiri/xml_dtd.c +192 -0
  33. data/ext/nokogiri/xml_dtd.h +10 -0
  34. data/ext/nokogiri/xml_element_content.c +123 -0
  35. data/ext/nokogiri/xml_element_content.h +10 -0
  36. data/ext/nokogiri/xml_element_decl.c +69 -0
  37. data/ext/nokogiri/xml_element_decl.h +9 -0
  38. data/ext/nokogiri/xml_entity_decl.c +97 -0
  39. data/ext/nokogiri/xml_entity_decl.h +10 -0
  40. data/ext/nokogiri/xml_entity_reference.c +50 -0
  41. data/ext/nokogiri/xml_entity_reference.h +9 -0
  42. data/ext/nokogiri/xml_io.c +31 -0
  43. data/ext/nokogiri/xml_io.h +11 -0
  44. data/ext/nokogiri/xml_namespace.c +74 -0
  45. data/ext/nokogiri/xml_namespace.h +12 -0
  46. data/ext/nokogiri/xml_node.c +1060 -0
  47. data/ext/nokogiri/xml_node.h +13 -0
  48. data/ext/nokogiri/xml_node_set.c +397 -0
  49. data/ext/nokogiri/xml_node_set.h +9 -0
  50. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  51. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  52. data/ext/nokogiri/xml_reader.c +593 -0
  53. data/ext/nokogiri/xml_reader.h +10 -0
  54. data/ext/nokogiri/xml_relax_ng.c +159 -0
  55. data/ext/nokogiri/xml_relax_ng.h +9 -0
  56. data/ext/nokogiri/xml_sax_parser.c +286 -0
  57. data/ext/nokogiri/xml_sax_parser.h +43 -0
  58. data/ext/nokogiri/xml_sax_parser_context.c +155 -0
  59. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  60. data/ext/nokogiri/xml_sax_push_parser.c +91 -0
  61. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  62. data/ext/nokogiri/xml_schema.c +156 -0
  63. data/ext/nokogiri/xml_schema.h +9 -0
  64. data/ext/nokogiri/xml_syntax_error.c +261 -0
  65. data/ext/nokogiri/xml_syntax_error.h +13 -0
  66. data/ext/nokogiri/xml_text.c +48 -0
  67. data/ext/nokogiri/xml_text.h +9 -0
  68. data/ext/nokogiri/xml_xpath.c +53 -0
  69. data/ext/nokogiri/xml_xpath.h +11 -0
  70. data/ext/nokogiri/xml_xpath_context.c +239 -0
  71. data/ext/nokogiri/xml_xpath_context.h +9 -0
  72. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  73. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  74. data/lib/nokogiri.rb +116 -0
  75. data/lib/nokogiri/css.rb +25 -0
  76. data/lib/nokogiri/css/generated_parser.rb +646 -0
  77. data/lib/nokogiri/css/generated_tokenizer.rb +143 -0
  78. data/lib/nokogiri/css/node.rb +99 -0
  79. data/lib/nokogiri/css/parser.rb +82 -0
  80. data/lib/nokogiri/css/parser.y +227 -0
  81. data/lib/nokogiri/css/syntax_error.rb +7 -0
  82. data/lib/nokogiri/css/tokenizer.rb +7 -0
  83. data/lib/nokogiri/css/tokenizer.rex +54 -0
  84. data/lib/nokogiri/css/xpath_visitor.rb +162 -0
  85. data/lib/nokogiri/decorators/slop.rb +33 -0
  86. data/lib/nokogiri/ffi/html/document.rb +28 -0
  87. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  88. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  89. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  90. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  91. data/lib/nokogiri/ffi/libxml.rb +356 -0
  92. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  93. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  94. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  95. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  96. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  97. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  98. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  99. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  100. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  101. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  102. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  103. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  104. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  105. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  106. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  107. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  108. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  109. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  110. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  111. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  112. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +15 -0
  113. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  114. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  115. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  116. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  117. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  118. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  119. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  120. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  121. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  122. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  123. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  124. data/lib/nokogiri/ffi/xml/document.rb +135 -0
  125. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  126. data/lib/nokogiri/ffi/xml/dtd.rb +69 -0
  127. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  128. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  129. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  130. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  131. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  132. data/lib/nokogiri/ffi/xml/node.rb +444 -0
  133. data/lib/nokogiri/ffi/xml/node_set.rb +133 -0
  134. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  135. data/lib/nokogiri/ffi/xml/reader.rb +227 -0
  136. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  137. data/lib/nokogiri/ffi/xml/sax/parser.rb +142 -0
  138. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  139. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +39 -0
  140. data/lib/nokogiri/ffi/xml/schema.rb +92 -0
  141. data/lib/nokogiri/ffi/xml/syntax_error.rb +91 -0
  142. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  143. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  144. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  145. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  146. data/lib/nokogiri/html.rb +35 -0
  147. data/lib/nokogiri/html/builder.rb +35 -0
  148. data/lib/nokogiri/html/document.rb +88 -0
  149. data/lib/nokogiri/html/document_fragment.rb +15 -0
  150. data/lib/nokogiri/html/element_description.rb +23 -0
  151. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  152. data/lib/nokogiri/html/sax/parser.rb +48 -0
  153. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  154. data/lib/nokogiri/syntax_error.rb +4 -0
  155. data/lib/nokogiri/version.rb +33 -0
  156. data/lib/nokogiri/version_warning.rb +11 -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 +405 -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 +131 -0
  164. data/lib/nokogiri/xml/document_fragment.rb +73 -0
  165. data/lib/nokogiri/xml/dtd.rb +11 -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 +15 -0
  169. data/lib/nokogiri/xml/fragment_handler.rb +73 -0
  170. data/lib/nokogiri/xml/namespace.rb +13 -0
  171. data/lib/nokogiri/xml/node.rb +665 -0
  172. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  173. data/lib/nokogiri/xml/node_set.rb +307 -0
  174. data/lib/nokogiri/xml/notation.rb +6 -0
  175. data/lib/nokogiri/xml/parse_options.rb +85 -0
  176. data/lib/nokogiri/xml/pp.rb +2 -0
  177. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  178. data/lib/nokogiri/xml/pp/node.rb +56 -0
  179. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  180. data/lib/nokogiri/xml/reader.rb +74 -0
  181. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  182. data/lib/nokogiri/xml/sax.rb +4 -0
  183. data/lib/nokogiri/xml/sax/document.rb +160 -0
  184. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  185. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  186. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  187. data/lib/nokogiri/xml/schema.rb +61 -0
  188. data/lib/nokogiri/xml/syntax_error.rb +38 -0
  189. data/lib/nokogiri/xml/xpath.rb +10 -0
  190. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  191. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  192. data/lib/nokogiri/xslt.rb +48 -0
  193. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  194. data/lib/xsd/xmlparser/nokogiri.rb +71 -0
  195. data/tasks/test.rb +100 -0
  196. data/test/css/test_nthiness.rb +159 -0
  197. data/test/css/test_parser.rb +277 -0
  198. data/test/css/test_tokenizer.rb +183 -0
  199. data/test/css/test_xpath_visitor.rb +76 -0
  200. data/test/ffi/test_document.rb +35 -0
  201. data/test/files/2ch.html +108 -0
  202. data/test/files/address_book.rlx +12 -0
  203. data/test/files/address_book.xml +10 -0
  204. data/test/files/bar/bar.xsd +4 -0
  205. data/test/files/dont_hurt_em_why.xml +422 -0
  206. data/test/files/exslt.xml +8 -0
  207. data/test/files/exslt.xslt +35 -0
  208. data/test/files/foo/foo.xsd +4 -0
  209. data/test/files/po.xml +32 -0
  210. data/test/files/po.xsd +66 -0
  211. data/test/files/shift_jis.html +10 -0
  212. data/test/files/shift_jis.xml +5 -0
  213. data/test/files/snuggles.xml +3 -0
  214. data/test/files/staff.dtd +10 -0
  215. data/test/files/staff.xml +59 -0
  216. data/test/files/staff.xslt +32 -0
  217. data/test/files/tlm.html +850 -0
  218. data/test/files/valid_bar.xml +2 -0
  219. data/test/helper.rb +136 -0
  220. data/test/html/sax/test_parser.rb +64 -0
  221. data/test/html/sax/test_parser_context.rb +48 -0
  222. data/test/html/test_builder.rb +164 -0
  223. data/test/html/test_document.rb +390 -0
  224. data/test/html/test_document_encoding.rb +77 -0
  225. data/test/html/test_document_fragment.rb +142 -0
  226. data/test/html/test_element_description.rb +94 -0
  227. data/test/html/test_named_characters.rb +14 -0
  228. data/test/html/test_node.rb +228 -0
  229. data/test/html/test_node_encoding.rb +27 -0
  230. data/test/test_convert_xpath.rb +135 -0
  231. data/test/test_css_cache.rb +45 -0
  232. data/test/test_gc.rb +15 -0
  233. data/test/test_memory_leak.rb +77 -0
  234. data/test/test_nokogiri.rb +138 -0
  235. data/test/test_reader.rb +358 -0
  236. data/test/test_xslt_transforms.rb +131 -0
  237. data/test/xml/node/test_save_options.rb +20 -0
  238. data/test/xml/node/test_subclass.rb +44 -0
  239. data/test/xml/sax/test_parser.rb +307 -0
  240. data/test/xml/sax/test_parser_context.rb +56 -0
  241. data/test/xml/sax/test_push_parser.rb +106 -0
  242. data/test/xml/test_attr.rb +38 -0
  243. data/test/xml/test_attribute_decl.rb +82 -0
  244. data/test/xml/test_builder.rb +167 -0
  245. data/test/xml/test_cdata.rb +38 -0
  246. data/test/xml/test_comment.rb +29 -0
  247. data/test/xml/test_document.rb +607 -0
  248. data/test/xml/test_document_encoding.rb +26 -0
  249. data/test/xml/test_document_fragment.rb +144 -0
  250. data/test/xml/test_dtd.rb +82 -0
  251. data/test/xml/test_dtd_encoding.rb +33 -0
  252. data/test/xml/test_element_content.rb +56 -0
  253. data/test/xml/test_element_decl.rb +73 -0
  254. data/test/xml/test_entity_decl.rb +83 -0
  255. data/test/xml/test_entity_reference.rb +21 -0
  256. data/test/xml/test_namespace.rb +68 -0
  257. data/test/xml/test_node.rb +889 -0
  258. data/test/xml/test_node_attributes.rb +34 -0
  259. data/test/xml/test_node_encoding.rb +107 -0
  260. data/test/xml/test_node_set.rb +531 -0
  261. data/test/xml/test_parse_options.rb +52 -0
  262. data/test/xml/test_processing_instruction.rb +30 -0
  263. data/test/xml/test_reader_encoding.rb +126 -0
  264. data/test/xml/test_relax_ng.rb +60 -0
  265. data/test/xml/test_schema.rb +89 -0
  266. data/test/xml/test_syntax_error.rb +27 -0
  267. data/test/xml/test_text.rb +30 -0
  268. data/test/xml/test_unparented_node.rb +381 -0
  269. data/test/xml/test_xpath.rb +106 -0
  270. metadata +428 -0
@@ -0,0 +1,21 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestEntityReference < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML(File.open(XML_FILE), XML_FILE)
9
+ end
10
+
11
+ def test_new
12
+ assert ref = EntityReference.new(@xml, 'ent4')
13
+ assert_instance_of EntityReference, ref
14
+ end
15
+
16
+ def test_many_references
17
+ 100.times { EntityReference.new(@xml, 'foo') }
18
+ end
19
+ end
20
+ end
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
@@ -0,0 +1,889 @@
1
+ require "helper"
2
+
3
+ require 'stringio'
4
+
5
+ module Nokogiri
6
+ module XML
7
+ class TestNode < Nokogiri::TestCase
8
+ def setup
9
+ super
10
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
11
+ end
12
+
13
+ def test_element?
14
+ assert @xml.root.element?, 'is an element'
15
+ end
16
+
17
+ def test_slash_search
18
+ assert_equal 'EMP0001', (@xml/:staff/:employee/:employeeId).first.text
19
+ end
20
+
21
+ def test_append_with_document
22
+ assert_raises(ArgumentError) do
23
+ @xml.root << Nokogiri::XML::Document.new
24
+ end
25
+ end
26
+
27
+ def test_inspect_ns
28
+ xml = Nokogiri::XML(<<-eoxml) { |c| c.noblanks }
29
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
30
+ <awesome/>
31
+ </root>
32
+ eoxml
33
+ ins = xml.inspect
34
+
35
+ xml.traverse do |node|
36
+ assert_match node.class.name, ins
37
+ if node.respond_to? :attributes
38
+ node.attributes.each do |k,v|
39
+ assert_match k, ins
40
+ assert_match v, ins
41
+ end
42
+ end
43
+
44
+ if node.respond_to?(:namespace) && node.namespace
45
+ assert_match node.namespace.class.name, ins
46
+ assert_match node.namespace.href, ins
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_namespace_nodes
52
+ xml = Nokogiri::XML <<-eoxml
53
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
54
+ <awesome/>
55
+ </root>
56
+ eoxml
57
+ awesome = xml.root
58
+ namespaces = awesome.namespace_definitions
59
+ assert_equal 2, namespaces.length
60
+ end
61
+
62
+ def test_no_definitions
63
+ xml = Nokogiri::XML <<-eoxml
64
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
65
+ <awesome/>
66
+ </root>
67
+ eoxml
68
+ awesome = xml.at('//xmlns:awesome')
69
+ namespaces = awesome.namespace_definitions
70
+ assert_equal 0, namespaces.length
71
+ end
72
+
73
+ def test_subclass_dup
74
+ subclass = Class.new(Nokogiri::XML::Node)
75
+ node = subclass.new('foo', @xml).dup
76
+ assert_instance_of subclass, node
77
+ end
78
+
79
+ def test_namespace_goes_to_children
80
+ fruits = Nokogiri::XML(<<-eoxml)
81
+ <Fruit xmlns='www.fruits.org'>
82
+ </Fruit>
83
+ eoxml
84
+ apple = Nokogiri::XML::Node.new('Apple', fruits)
85
+ orange = Nokogiri::XML::Node.new('Orange', fruits)
86
+ apple << orange
87
+ fruits.root << apple
88
+ assert fruits.at('//fruit:Orange',{'fruit'=>'www.fruits.org'})
89
+ assert fruits.at('//fruit:Apple',{'fruit'=>'www.fruits.org'})
90
+ end
91
+
92
+ def test_description
93
+ assert_nil @xml.at('employee').description
94
+ end
95
+
96
+ def test_add_namespace_add_child
97
+ doc = Nokogiri::XML::Document.new
98
+ item = Nokogiri::XML::Element.new('item', doc)
99
+ doc.root = item
100
+
101
+ entry = Nokogiri::XML::Element.new('entry', doc)
102
+ entry.add_namespace('tlm', 'http://tenderlovemaking.com')
103
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
104
+ item.add_child(entry)
105
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
106
+ end
107
+
108
+ def test_spaceship
109
+ nodes = @xml.xpath('//employee')
110
+ assert_equal(-1, (nodes.first <=> nodes.last))
111
+ list = [nodes.first, nodes.last].sort
112
+ assert_equal nodes.first, list.first
113
+ assert_equal nodes.last, list.last
114
+ end
115
+
116
+ def test_incorrect_spaceship
117
+ nodes = @xml.xpath('//employee')
118
+ assert_nil(nodes.first <=> 'asdf')
119
+ end
120
+
121
+ def test_document_compare
122
+ nodes = @xml.xpath('//employee')
123
+ assert_equal(-1, (nodes.first <=> @xml))
124
+ end
125
+
126
+ def test_different_document_compare
127
+ nodes = @xml.xpath('//employee')
128
+ doc = Nokogiri::XML('<a><b/></a>')
129
+ b = doc.at('b')
130
+ assert_nil(nodes.first <=> b)
131
+ end
132
+
133
+ def test_duplicate_node_removes_namespace
134
+ fruits = Nokogiri::XML(<<-eoxml)
135
+ <Fruit xmlns='www.fruits.org'>
136
+ <Apple></Apple>
137
+ </Fruit>
138
+ eoxml
139
+ apple = fruits.root.xpath('fruit:Apple', {'fruit'=>'www.fruits.org'})[0]
140
+ new_apple = apple.dup
141
+ fruits.root << new_apple
142
+ assert_equal 2, fruits.xpath('//xmlns:Apple').length
143
+ assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
144
+ end
145
+
146
+ [:clone, :dup].each do |symbol|
147
+ define_method "test_#{symbol}" do
148
+ node = @xml.at('//employee')
149
+ other = node.send(symbol)
150
+ assert_equal "employee", other.name
151
+ assert_nil other.parent
152
+ end
153
+ end
154
+
155
+ def test_fragment_creates_elements
156
+ apple = @xml.fragment('<Apple/>')
157
+ apple.children.each do |child|
158
+ assert_equal Nokogiri::XML::Node::ELEMENT_NODE, child.type
159
+ assert_instance_of Nokogiri::XML::Element, child
160
+ end
161
+ end
162
+
163
+ def test_node_added_to_root_should_get_namespace
164
+ fruits = Nokogiri::XML(<<-eoxml)
165
+ <Fruit xmlns='http://www.fruits.org'>
166
+ </Fruit>
167
+ eoxml
168
+ apple = fruits.fragment('<Apple/>')
169
+ fruits.root << apple
170
+ assert_equal 1, fruits.xpath('//xmlns:Apple').length
171
+ end
172
+
173
+ def test_add_child_path_following_sequential_text_nodes
174
+ xml = Nokogiri::XML('<root>text</root>')
175
+ xml.root.add_child(Nokogiri::XML::Text.new('text', xml))
176
+ item = xml.root.add_child(Nokogiri::XML::Element.new('item', xml))
177
+ assert_equal '/root/item', item.path
178
+ end
179
+
180
+ def test_new_node_can_have_ancestors
181
+ xml = Nokogiri::XML('<root>text</root>')
182
+ item = Nokogiri::XML::Element.new('item', xml)
183
+ assert_equal 0, item.ancestors.length
184
+ end
185
+
186
+ def test_children
187
+ doc = Nokogiri::XML(<<-eoxml)
188
+ <root>#{'<a/>' * 9 }</root>
189
+ eoxml
190
+ assert_equal 9, doc.root.children.length
191
+ assert_equal 9, doc.root.children.to_a.length
192
+
193
+ doc = Nokogiri::XML(<<-eoxml)
194
+ <root>#{'<a/>' * 15 }</root>
195
+ eoxml
196
+ assert_equal 15, doc.root.children.length
197
+ assert_equal 15, doc.root.children.to_a.length
198
+ end
199
+
200
+ def test_add_namespace
201
+ node = @xml.at('address')
202
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
203
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
204
+ end
205
+
206
+ def test_add_namespace_twice
207
+ node = @xml.at('address')
208
+ ns = node.add_namespace('foo', 'http://tenderlovemaking.com')
209
+ ns2 = node.add_namespace('foo', 'http://tenderlovemaking.com')
210
+ assert_equal ns, ns2
211
+ end
212
+
213
+ def test_add_default_ns
214
+ node = @xml.at('address')
215
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
216
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
217
+ end
218
+
219
+ def test_add_multiple_namespaces
220
+ node = @xml.at('address')
221
+
222
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
223
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
224
+
225
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
226
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
227
+ end
228
+
229
+ def test_default_namespace=
230
+ node = @xml.at('address')
231
+ node.default_namespace = 'http://tenderlovemaking.com'
232
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
233
+ end
234
+
235
+ def test_namespace=
236
+ node = @xml.at('address')
237
+ assert_nil node.namespace
238
+ definition = node.add_namespace_definition 'bar', 'http://tlm.com/'
239
+
240
+ node.namespace = definition
241
+
242
+ assert_equal definition, node.namespace
243
+
244
+ assert_equal node, @xml.at('//foo:address', {
245
+ 'foo' => 'http://tlm.com/'
246
+ })
247
+ end
248
+
249
+ def test_add_namespace_with_nil_associates_node
250
+ node = @xml.at('address')
251
+ assert_nil node.namespace
252
+ definition = node.add_namespace_definition nil, 'http://tlm.com/'
253
+ assert_equal definition, node.namespace
254
+ end
255
+
256
+ def test_add_namespace_does_not_associate_node
257
+ node = @xml.at('address')
258
+ assert_nil node.namespace
259
+ definition = node.add_namespace_definition 'foo', 'http://tlm.com/'
260
+ assert_nil node.namespace
261
+ end
262
+
263
+ def test_set_namespace_from_different_doc
264
+ node = @xml.at('address')
265
+ doc = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
266
+ decl = doc.root.add_namespace_definition 'foo', 'bar'
267
+
268
+ assert_raises(ArgumentError) do
269
+ node.namespace = decl
270
+ end
271
+ end
272
+
273
+ def test_set_namespace_must_only_take_a_namespace
274
+ node = @xml.at('address')
275
+ assert_raises(TypeError) do
276
+ node.namespace = node
277
+ end
278
+ end
279
+
280
+ def test_at
281
+ node = @xml.at('address')
282
+ assert_equal node, @xml.xpath('//address').first
283
+ end
284
+
285
+ def test_at_xpath
286
+ node = @xml.at_xpath('//address')
287
+ nodes = @xml.xpath('//address')
288
+ assert_equal 5, nodes.size
289
+ assert_equal node, nodes.first
290
+ end
291
+
292
+ def test_at_css
293
+ node = @xml.at_css('address')
294
+ nodes = @xml.css('address')
295
+ assert_equal 5, nodes.size
296
+ assert_equal node, nodes.first
297
+ end
298
+
299
+ def test_percent
300
+ node = @xml % ('address')
301
+ assert_equal node, @xml.xpath('//address').first
302
+ end
303
+
304
+ def test_accept
305
+ visitor = Class.new {
306
+ attr_accessor :visited
307
+ def accept target
308
+ target.accept(self)
309
+ end
310
+
311
+ def visit node
312
+ node.children.each { |c| c.accept(self) }
313
+ @visited = true
314
+ end
315
+ }.new
316
+ visitor.accept(@xml.root)
317
+ assert visitor.visited
318
+ end
319
+
320
+ def test_replace_with_default_namespaces
321
+ fruits = Nokogiri::XML(<<-eoxml)
322
+ <fruit xmlns="http://fruits.org">
323
+ <apple />
324
+ </fruit>
325
+ eoxml
326
+
327
+ apple = fruits.css('apple').first
328
+
329
+ orange = Nokogiri::XML::Node.new('orange', fruits)
330
+ apple.replace(orange)
331
+
332
+ assert_equal orange, fruits.css('orange').first
333
+ end
334
+
335
+ def test_add_child_should_inherit_namespace
336
+ doc = Nokogiri::XML(<<-eoxml)
337
+ <root xmlns="http://tenderlovemaking.com/">
338
+ <first>
339
+ </first>
340
+ </root>
341
+ eoxml
342
+ assert node = doc.at('//xmlns:first')
343
+ child = Nokogiri::XML::Node.new('second', doc)
344
+ node.add_child(child)
345
+ assert doc.at('//xmlns:second')
346
+ end
347
+
348
+ def test_add_child_should_not_inherit_namespace_if_it_has_one
349
+ doc = Nokogiri::XML(<<-eoxml)
350
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="http://flavorjon.es/">
351
+ <first>
352
+ </first>
353
+ </root>
354
+ eoxml
355
+ assert node = doc.at('//xmlns:first')
356
+ child = Nokogiri::XML::Node.new('second', doc)
357
+
358
+ ns = doc.root.namespace_definitions.detect { |x| x.prefix == "foo" }
359
+ child.namespace = ns
360
+
361
+ node.add_child(child)
362
+ assert doc.at('//foo:second', "foo" => "http://flavorjon.es/")
363
+ end
364
+
365
+ def test_write_to
366
+ io = StringIO.new
367
+ @xml.write_to io
368
+ io.rewind
369
+ assert_equal @xml.to_xml, io.read
370
+ end
371
+
372
+ def test_attribute_with_symbol
373
+ assert_equal 'Yes', @xml.css('address').first[:domestic]
374
+ end
375
+
376
+ def test_write_to_with_block
377
+ called = false
378
+ io = StringIO.new
379
+ conf = nil
380
+ @xml.write_to io do |config|
381
+ called = true
382
+ conf = config
383
+ config.format.as_html.no_empty_tags
384
+ end
385
+ io.rewind
386
+ assert called
387
+ assert_equal @xml.serialize(nil, conf.options), io.read
388
+ end
389
+
390
+ %w{ xml html xhtml }.each do |type|
391
+ define_method(:"test_write_#{type}_to") do
392
+ io = StringIO.new
393
+ assert @xml.send(:"write_#{type}_to", io)
394
+ io.rewind
395
+ assert_match @xml.send(:"to_#{type}"), io.read
396
+ end
397
+ end
398
+
399
+ def test_serialize_with_block
400
+ called = false
401
+ conf = nil
402
+ string = @xml.serialize do |config|
403
+ called = true
404
+ conf = config
405
+ config.format.as_html.no_empty_tags
406
+ end
407
+ assert called
408
+ assert_equal @xml.serialize(nil, conf.options), string
409
+ end
410
+
411
+ def test_hold_refence_to_subnode
412
+ doc = Nokogiri::XML(<<-eoxml)
413
+ <root>
414
+ <a>
415
+ <b />
416
+ </a>
417
+ </root>
418
+ eoxml
419
+ assert node_a = doc.css('a').first
420
+ assert node_b = node_a.css('b').first
421
+ node_a.unlink
422
+ assert_equal 'b', node_b.name
423
+ end
424
+
425
+ def test_values
426
+ assert_equal %w{ Yes Yes }, @xml.xpath('//address')[1].values
427
+ end
428
+
429
+ def test_keys
430
+ assert_equal %w{ domestic street }, @xml.xpath('//address')[1].keys
431
+ end
432
+
433
+ def test_each
434
+ attributes = []
435
+ @xml.xpath('//address')[1].each do |key, value|
436
+ attributes << [key, value]
437
+ end
438
+ assert_equal [['domestic', 'Yes'], ['street', 'Yes']], attributes
439
+ end
440
+
441
+ def test_new
442
+ assert node = Nokogiri::XML::Node.new('input', @xml)
443
+ assert_equal 1, node.node_type
444
+ assert_instance_of Nokogiri::XML::Element, node
445
+ end
446
+
447
+ def test_to_str
448
+ name = @xml.xpath('//name').first
449
+ assert_match(/Margaret/, '' + name)
450
+ assert_equal('Margaret Martin', '' + name.children.first)
451
+ end
452
+
453
+ def test_ancestors
454
+ address = @xml.xpath('//address').first
455
+ assert_equal 3, address.ancestors.length
456
+ assert_equal ['employee', 'staff', 'document'],
457
+ address.ancestors.map { |x| x.name }
458
+ end
459
+
460
+ def test_read_only?
461
+ assert entity_decl = @xml.internal_subset.children.find { |x|
462
+ x.type == Node::ENTITY_DECL
463
+ }
464
+ assert entity_decl.read_only?
465
+ end
466
+
467
+ def test_remove_attribute
468
+ address = @xml.xpath('/staff/employee/address').first
469
+ assert_equal 'Yes', address['domestic']
470
+ address.remove_attribute 'domestic'
471
+ assert_nil address['domestic']
472
+ end
473
+
474
+ def test_delete
475
+ address = @xml.xpath('/staff/employee/address').first
476
+ assert_equal 'Yes', address['domestic']
477
+ address.delete 'domestic'
478
+ assert_nil address['domestic']
479
+ end
480
+
481
+ def test_add_child_in_same_document
482
+ child = @xml.css('employee').first
483
+
484
+ assert previous_last_child = child.children.last
485
+ assert new_child = child.children.first
486
+
487
+ last = child.children.last
488
+
489
+ child.add_child(new_child)
490
+ assert_equal new_child, child.children.last
491
+ assert_equal last, child.children.last
492
+ end
493
+
494
+ def test_add_child_from_other_document
495
+ d1 = Nokogiri::XML("<root><item>1</item><item>2</item></root>")
496
+ d2 = Nokogiri::XML("<root><item>3</item><item>4</item></root>")
497
+
498
+ d2.at('root').search('item').each do |i|
499
+ d1.at('root').add_child i
500
+ end
501
+
502
+ assert_equal 0, d2.search('item').size
503
+ assert_equal 4, d1.search('item').size
504
+ end
505
+
506
+ def test_add_child
507
+ xml = Nokogiri::XML(<<-eoxml)
508
+ <root>
509
+ <a>Hello world</a>
510
+ </root>
511
+ eoxml
512
+ text_node = Nokogiri::XML::Text.new('hello', xml)
513
+ assert_equal Nokogiri::XML::Node::TEXT_NODE, text_node.type
514
+ xml.root.add_child text_node
515
+ assert_match 'hello', xml.to_s
516
+ end
517
+
518
+ def test_chevron_works_as_add_child
519
+ xml = Nokogiri::XML(<<-eoxml)
520
+ <root>
521
+ <a>Hello world</a>
522
+ </root>
523
+ eoxml
524
+ text_node = Nokogiri::XML::Text.new('hello', xml)
525
+ xml.root << text_node
526
+ assert_match 'hello', xml.to_s
527
+ end
528
+
529
+ def test_set_content_with_symbol
530
+ node = @xml.at('//name')
531
+ node.content = :foo
532
+ assert_equal 'foo', node.content
533
+ end
534
+
535
+ def test_add_previous_sibling
536
+ xml = Nokogiri::XML(<<-eoxml)
537
+ <root>
538
+ <a>Hello world</a>
539
+ </root>
540
+ eoxml
541
+ b_node = Nokogiri::XML::Node.new('a', xml)
542
+ assert_equal Nokogiri::XML::Node::ELEMENT_NODE, b_node.type
543
+ b_node.content = 'first'
544
+ a_node = xml.xpath('//a').first
545
+ a_node.add_previous_sibling(b_node)
546
+ assert_equal('first', xml.xpath('//a').first.text)
547
+ end
548
+
549
+ def test_add_previous_sibling_merge
550
+ xml = Nokogiri::XML(<<-eoxml)
551
+ <root>
552
+ <a>Hello world</a>
553
+ </root>
554
+ eoxml
555
+
556
+ assert a_tag = xml.css('a').first
557
+
558
+ left_space = a_tag.previous
559
+ right_space = a_tag.next
560
+ assert left_space.text?
561
+ assert right_space.text?
562
+
563
+ left_space.add_previous_sibling(right_space)
564
+ assert_equal left_space, right_space
565
+ end
566
+
567
+ def test_add_next_sibling_merge
568
+ xml = Nokogiri::XML(<<-eoxml)
569
+ <root>
570
+ <a>Hello world</a>
571
+ </root>
572
+ eoxml
573
+
574
+ assert a_tag = xml.css('a').first
575
+
576
+ left_space = a_tag.previous
577
+ right_space = a_tag.next
578
+ assert left_space.text?
579
+ assert right_space.text?
580
+
581
+ right_space.add_next_sibling(left_space)
582
+ assert_equal left_space, right_space
583
+ end
584
+
585
+ def test_find_by_css_with_tilde_eql
586
+ xml = Nokogiri::XML.parse(<<-eoxml)
587
+ <root>
588
+ <a>Hello world</a>
589
+ <a class='foo bar'>Bar</a>
590
+ <a class='bar foo'>Bar</a>
591
+ <a class='bar'>Bar</a>
592
+ <a class='baz bar foo'>Bar</a>
593
+ <a class='bazbarfoo'>Awesome</a>
594
+ <a class='bazbar'>Awesome</a>
595
+ </root>
596
+ eoxml
597
+ set = xml.css('a[@class~="bar"]')
598
+ assert_equal 4, set.length
599
+ assert_equal ['Bar'], set.map { |node| node.content }.uniq
600
+ end
601
+
602
+ def test_unlink
603
+ xml = Nokogiri::XML.parse(<<-eoxml)
604
+ <root>
605
+ <a class='foo bar'>Bar</a>
606
+ <a class='bar foo'>Bar</a>
607
+ <a class='bar'>Bar</a>
608
+ <a>Hello world</a>
609
+ <a class='baz bar foo'>Bar</a>
610
+ <a class='bazbarfoo'>Awesome</a>
611
+ <a class='bazbar'>Awesome</a>
612
+ </root>
613
+ eoxml
614
+ node = xml.xpath('//a')[3]
615
+ assert_equal('Hello world', node.text)
616
+ assert_match(/Hello world/, xml.to_s)
617
+ assert node.parent
618
+ assert node.document
619
+ assert node.previous_sibling
620
+ assert node.next_sibling
621
+ node.unlink
622
+ assert !node.parent
623
+ #assert !node.document
624
+ assert !node.previous_sibling
625
+ assert !node.next_sibling
626
+ assert_no_match(/Hello world/, xml.to_s)
627
+ end
628
+
629
+ def test_next_sibling
630
+ assert node = @xml.root
631
+ assert sibling = node.child.next_sibling
632
+ assert_equal('employee', sibling.name)
633
+ end
634
+
635
+ def test_previous_sibling
636
+ assert node = @xml.root
637
+ assert sibling = node.child.next_sibling
638
+ assert_equal('employee', sibling.name)
639
+ assert_equal(sibling.previous_sibling, node.child)
640
+ end
641
+
642
+ def test_name=
643
+ assert node = @xml.root
644
+ node.name = 'awesome'
645
+ assert_equal('awesome', node.name)
646
+ end
647
+
648
+ def test_child
649
+ assert node = @xml.root
650
+ assert child = node.child
651
+ assert_equal('text', child.name)
652
+ end
653
+
654
+ def test_key?
655
+ assert node = @xml.search('//address').first
656
+ assert(!node.key?('asdfasdf'))
657
+ end
658
+
659
+ def test_set_property
660
+ assert node = @xml.search('//address').first
661
+ node['foo'] = 'bar'
662
+ assert_equal('bar', node['foo'])
663
+ end
664
+
665
+ def test_attributes
666
+ assert node = @xml.search('//address').first
667
+ assert_nil(node['asdfasdfasdf'])
668
+ assert_equal('Yes', node['domestic'])
669
+
670
+ assert node = @xml.search('//address')[2]
671
+ attr = node.attributes
672
+ assert_equal 2, attr.size
673
+ assert_equal 'Yes', attr['domestic'].value
674
+ assert_equal 'Yes', attr['domestic'].to_s
675
+ assert_equal 'No', attr['street'].value
676
+ end
677
+
678
+ def test_path
679
+ assert set = @xml.search('//employee')
680
+ assert node = set.first
681
+ assert_equal('/staff/employee[1]', node.path)
682
+ end
683
+
684
+ def test_search_by_symbol
685
+ assert set = @xml.search(:employee)
686
+ assert 5, set.length
687
+
688
+ assert node = @xml.at(:employee)
689
+ assert node.text =~ /EMP0001/
690
+ end
691
+
692
+ def test_new_node
693
+ node = Nokogiri::XML::Node.new('form', @xml)
694
+ assert_equal('form', node.name)
695
+ assert(node.document)
696
+ end
697
+
698
+ def test_encode_special_chars
699
+ foo = @xml.css('employee').first.encode_special_chars('&')
700
+ assert_equal '&amp;', foo
701
+ end
702
+
703
+ def test_content
704
+ node = Nokogiri::XML::Node.new('form', @xml)
705
+ assert_equal('', node.content)
706
+
707
+ node.content = 'hello world!'
708
+ assert_equal('hello world!', node.content)
709
+ end
710
+
711
+ def test_whitespace_nodes
712
+ doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
713
+ children = doc.at('//root').children.collect{|j| j.to_s}
714
+ assert_equal "\n", children[1]
715
+ assert_equal " ", children[3]
716
+ end
717
+
718
+ def test_replace
719
+ set = @xml.search('//employee')
720
+ assert 5, set.length
721
+ assert 0, @xml.search('//form').length
722
+
723
+ first = set[0]
724
+ second = set[1]
725
+
726
+ node = Nokogiri::XML::Node.new('form', @xml)
727
+ first.replace(node)
728
+
729
+ assert set = @xml.search('//employee')
730
+ assert_equal 4, set.length
731
+ assert 1, @xml.search('//form').length
732
+
733
+ assert_equal set[0].to_xml, second.to_xml
734
+ end
735
+
736
+ def test_illegal_replace_of_node_with_doc
737
+ new_node = Nokogiri::XML.parse('<foo>bar</foo>')
738
+ old_node = @xml.at('//employee')
739
+ assert_raises(ArgumentError){ old_node.replace new_node }
740
+ end
741
+
742
+ def test_node_equality
743
+ doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
744
+ doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
745
+
746
+ address1_1 = doc1.xpath('//address').first
747
+ address1_2 = doc1.xpath('//address').first
748
+
749
+ address2 = doc2.xpath('//address').first
750
+
751
+ assert_not_equal address1_1, address2 # two references to very, very similar nodes
752
+ assert_equal address1_1, address1_2 # two references to the exact same node
753
+ end
754
+
755
+ def test_namespace_as_hash
756
+ xml = Nokogiri::XML.parse(<<-eoxml)
757
+ <root>
758
+ <car xmlns:part="http://general-motors.com/">
759
+ <part:tire>Michelin Model XGV</part:tire>
760
+ </car>
761
+ <bicycle xmlns:part="http://schwinn.com/">
762
+ <part:tire>I'm a bicycle tire!</part:tire>
763
+ </bicycle>
764
+ </root>
765
+ eoxml
766
+
767
+ tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
768
+ assert_equal 1, tires.length
769
+ end
770
+
771
+ def test_namespace_search_with_css
772
+ xml = Nokogiri::XML.parse(<<-eoxml)
773
+ <root>
774
+ <car xmlns:part="http://general-motors.com/">
775
+ <part:tire>Michelin Model XGV</part:tire>
776
+ </car>
777
+ <bicycle xmlns:part="http://schwinn.com/">
778
+ <part:tire>I'm a bicycle tire!</part:tire>
779
+ </bicycle>
780
+ </root>
781
+ eoxml
782
+
783
+ tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/')
784
+ assert_equal 1, tires.length
785
+ end
786
+
787
+ def test_namespaces
788
+ xml = Nokogiri::XML.parse(<<-EOF)
789
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
790
+ <y xmlns:c='http://bazz.com/'>
791
+ <a:div>hello a</a:div>
792
+ <b:div>hello b</b:div>
793
+ <c:div>hello c</c:div>
794
+ </y>
795
+ </x>
796
+ EOF
797
+ assert namespaces = xml.root.namespaces
798
+ assert namespaces.key?('xmlns:a')
799
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
800
+ assert namespaces.key?('xmlns:b')
801
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
802
+ assert ! namespaces.key?('xmlns:c')
803
+
804
+ assert namespaces = xml.namespaces
805
+ assert namespaces.key?('xmlns:a')
806
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
807
+ assert namespaces.key?('xmlns:b')
808
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
809
+
810
+ assert_equal "hello a", xml.search("//a:div", xml.namespaces).first.inner_text
811
+ assert_equal "hello b", xml.search("//b:div", xml.namespaces).first.inner_text
812
+ end
813
+
814
+ def test_namespace
815
+ xml = Nokogiri::XML.parse(<<-EOF)
816
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
817
+ <y xmlns:c='http://bazz.com/'>
818
+ <a:div>hello a</a:div>
819
+ <b:div>hello b</b:div>
820
+ <c:div>hello c</c:div>
821
+ <div>hello moon</div>
822
+ </y>
823
+ </x>
824
+ EOF
825
+ set = xml.search("//y/*")
826
+ assert_equal "a", set[0].namespace.prefix
827
+ assert_equal "b", set[1].namespace.prefix
828
+ assert_equal "c", set[2].namespace.prefix
829
+ assert_equal nil, set[3].namespace
830
+ end
831
+
832
+ def test_namespace_without_an_href_on_html_node
833
+ # because microsoft word's HTML formatting does this. ick.
834
+ xml = Nokogiri::HTML.parse <<-EOF
835
+ <div><o:p>foo</o:p></div>
836
+ EOF
837
+
838
+ assert_not_nil(node = xml.at('p'))
839
+
840
+ assert_equal 1, node.namespaces.keys.size
841
+ assert node.namespaces.has_key?('xmlns:o')
842
+ assert_equal nil, node.namespaces['xmlns:o']
843
+ end
844
+
845
+ def test_line
846
+ xml = Nokogiri::XML(<<-eoxml)
847
+ <root>
848
+ <a>
849
+ Hello world
850
+ </a>
851
+ </root>
852
+ eoxml
853
+
854
+ set = xml.search("//a")
855
+ node = set.first
856
+ assert_equal 2, node.line
857
+ end
858
+
859
+ def test_unlink_then_reparent
860
+ # see http://github.com/tenderlove/nokogiri/issues#issue/22
861
+ 10.times do
862
+ STDOUT.putc "."
863
+ STDOUT.flush
864
+ begin
865
+ doc = Nokogiri::XML <<-EOHTML
866
+ <root>
867
+ <a>
868
+ <b/>
869
+ <c/>
870
+ </a>
871
+ </root>
872
+ EOHTML
873
+
874
+ root = doc.at("root")
875
+ a = root.at("a")
876
+ b = a.at("b")
877
+ c = a.at("c")
878
+ a.add_next_sibling(b.unlink)
879
+ c.unlink
880
+ end
881
+
882
+ GC.start
883
+ end
884
+
885
+ end
886
+
887
+ end
888
+ end
889
+ end