glebm-nokogiri 1.4.2.1

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