nokogiri-backupify 1.5.0.beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. data/.autotest +26 -0
  2. data/CHANGELOG.ja.rdoc +509 -0
  3. data/CHANGELOG.rdoc +490 -0
  4. data/Manifest.txt +274 -0
  5. data/README.ja.rdoc +106 -0
  6. data/README.rdoc +150 -0
  7. data/Rakefile +217 -0
  8. data/bin/nokogiri +54 -0
  9. data/deps.rip +5 -0
  10. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  11. data/ext/java/nokogiri/HtmlDocument.java +146 -0
  12. data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
  13. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  14. data/ext/java/nokogiri/HtmlSaxParserContext.java +256 -0
  15. data/ext/java/nokogiri/NokogiriService.java +466 -0
  16. data/ext/java/nokogiri/XmlAttr.java +183 -0
  17. data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
  18. data/ext/java/nokogiri/XmlCdata.java +89 -0
  19. data/ext/java/nokogiri/XmlComment.java +84 -0
  20. data/ext/java/nokogiri/XmlDocument.java +514 -0
  21. data/ext/java/nokogiri/XmlDocumentFragment.java +216 -0
  22. data/ext/java/nokogiri/XmlDtd.java +464 -0
  23. data/ext/java/nokogiri/XmlElement.java +221 -0
  24. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  25. data/ext/java/nokogiri/XmlElementDecl.java +147 -0
  26. data/ext/java/nokogiri/XmlEntityDecl.java +161 -0
  27. data/ext/java/nokogiri/XmlEntityReference.java +75 -0
  28. data/ext/java/nokogiri/XmlNamespace.java +127 -0
  29. data/ext/java/nokogiri/XmlNode.java +1392 -0
  30. data/ext/java/nokogiri/XmlNodeSet.java +284 -0
  31. data/ext/java/nokogiri/XmlProcessingInstruction.java +103 -0
  32. data/ext/java/nokogiri/XmlReader.java +409 -0
  33. data/ext/java/nokogiri/XmlRelaxng.java +199 -0
  34. data/ext/java/nokogiri/XmlSaxParserContext.java +353 -0
  35. data/ext/java/nokogiri/XmlSaxPushParser.java +182 -0
  36. data/ext/java/nokogiri/XmlSchema.java +175 -0
  37. data/ext/java/nokogiri/XmlSyntaxError.java +114 -0
  38. data/ext/java/nokogiri/XmlText.java +135 -0
  39. data/ext/java/nokogiri/XmlXpathContext.java +175 -0
  40. data/ext/java/nokogiri/XsltStylesheet.java +181 -0
  41. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +205 -0
  42. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
  43. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +80 -0
  44. data/ext/java/nokogiri/internals/NokogiriHandler.java +326 -0
  45. data/ext/java/nokogiri/internals/NokogiriHelpers.java +583 -0
  46. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +170 -0
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +118 -0
  48. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +73 -0
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  50. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
  51. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +120 -0
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +56 -0
  53. data/ext/java/nokogiri/internals/ParserContext.java +278 -0
  54. data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
  55. data/ext/java/nokogiri/internals/ReaderNode.java +473 -0
  56. data/ext/java/nokogiri/internals/SaveContext.java +282 -0
  57. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +68 -0
  58. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  59. data/ext/java/nokogiri/internals/XmlDomParser.java +77 -0
  60. data/ext/java/nokogiri/internals/XmlDomParserContext.java +233 -0
  61. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  62. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  63. data/ext/nokogiri/depend +358 -0
  64. data/ext/nokogiri/extconf.rb +124 -0
  65. data/ext/nokogiri/html_document.c +154 -0
  66. data/ext/nokogiri/html_document.h +10 -0
  67. data/ext/nokogiri/html_element_description.c +276 -0
  68. data/ext/nokogiri/html_element_description.h +10 -0
  69. data/ext/nokogiri/html_entity_lookup.c +32 -0
  70. data/ext/nokogiri/html_entity_lookup.h +8 -0
  71. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  72. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  73. data/ext/nokogiri/nokogiri.c +92 -0
  74. data/ext/nokogiri/nokogiri.h +160 -0
  75. data/ext/nokogiri/xml_attr.c +94 -0
  76. data/ext/nokogiri/xml_attr.h +9 -0
  77. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  78. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  79. data/ext/nokogiri/xml_cdata.c +56 -0
  80. data/ext/nokogiri/xml_cdata.h +9 -0
  81. data/ext/nokogiri/xml_comment.c +54 -0
  82. data/ext/nokogiri/xml_comment.h +9 -0
  83. data/ext/nokogiri/xml_document.c +478 -0
  84. data/ext/nokogiri/xml_document.h +23 -0
  85. data/ext/nokogiri/xml_document_fragment.c +48 -0
  86. data/ext/nokogiri/xml_document_fragment.h +10 -0
  87. data/ext/nokogiri/xml_dtd.c +202 -0
  88. data/ext/nokogiri/xml_dtd.h +10 -0
  89. data/ext/nokogiri/xml_element_content.c +123 -0
  90. data/ext/nokogiri/xml_element_content.h +10 -0
  91. data/ext/nokogiri/xml_element_decl.c +69 -0
  92. data/ext/nokogiri/xml_element_decl.h +9 -0
  93. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  94. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  95. data/ext/nokogiri/xml_entity_decl.c +110 -0
  96. data/ext/nokogiri/xml_entity_decl.h +10 -0
  97. data/ext/nokogiri/xml_entity_reference.c +52 -0
  98. data/ext/nokogiri/xml_entity_reference.h +9 -0
  99. data/ext/nokogiri/xml_io.c +31 -0
  100. data/ext/nokogiri/xml_io.h +11 -0
  101. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  102. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  103. data/ext/nokogiri/xml_namespace.c +84 -0
  104. data/ext/nokogiri/xml_namespace.h +13 -0
  105. data/ext/nokogiri/xml_node.c +1384 -0
  106. data/ext/nokogiri/xml_node.h +13 -0
  107. data/ext/nokogiri/xml_node_set.c +418 -0
  108. data/ext/nokogiri/xml_node_set.h +9 -0
  109. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  110. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  111. data/ext/nokogiri/xml_reader.c +684 -0
  112. data/ext/nokogiri/xml_reader.h +10 -0
  113. data/ext/nokogiri/xml_relax_ng.c +161 -0
  114. data/ext/nokogiri/xml_relax_ng.h +9 -0
  115. data/ext/nokogiri/xml_sax_parser.c +288 -0
  116. data/ext/nokogiri/xml_sax_parser.h +39 -0
  117. data/ext/nokogiri/xml_sax_parser_context.c +199 -0
  118. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  119. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  120. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  121. data/ext/nokogiri/xml_schema.c +205 -0
  122. data/ext/nokogiri/xml_schema.h +9 -0
  123. data/ext/nokogiri/xml_syntax_error.c +58 -0
  124. data/ext/nokogiri/xml_syntax_error.h +13 -0
  125. data/ext/nokogiri/xml_text.c +50 -0
  126. data/ext/nokogiri/xml_text.h +9 -0
  127. data/ext/nokogiri/xml_xpath_context.c +309 -0
  128. data/ext/nokogiri/xml_xpath_context.h +9 -0
  129. data/ext/nokogiri/xslt_stylesheet.c +258 -0
  130. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  131. data/lib/isorelax.jar +0 -0
  132. data/lib/jing.jar +0 -0
  133. data/lib/nekodtd.jar +0 -0
  134. data/lib/nekohtml.jar +0 -0
  135. data/lib/nokogiri.rb +143 -0
  136. data/lib/nokogiri/css.rb +23 -0
  137. data/lib/nokogiri/css/node.rb +99 -0
  138. data/lib/nokogiri/css/parser.rb +677 -0
  139. data/lib/nokogiri/css/parser.y +237 -0
  140. data/lib/nokogiri/css/parser_extras.rb +91 -0
  141. data/lib/nokogiri/css/syntax_error.rb +7 -0
  142. data/lib/nokogiri/css/tokenizer.rb +152 -0
  143. data/lib/nokogiri/css/tokenizer.rex +55 -0
  144. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  145. data/lib/nokogiri/decorators/slop.rb +35 -0
  146. data/lib/nokogiri/html.rb +36 -0
  147. data/lib/nokogiri/html/builder.rb +35 -0
  148. data/lib/nokogiri/html/document.rb +221 -0
  149. data/lib/nokogiri/html/document_fragment.rb +41 -0
  150. data/lib/nokogiri/html/element_description.rb +23 -0
  151. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  152. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  153. data/lib/nokogiri/html/sax/parser.rb +52 -0
  154. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  155. data/lib/nokogiri/syntax_error.rb +4 -0
  156. data/lib/nokogiri/version.rb +35 -0
  157. data/lib/nokogiri/xml.rb +67 -0
  158. data/lib/nokogiri/xml/attr.rb +14 -0
  159. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  160. data/lib/nokogiri/xml/builder.rb +418 -0
  161. data/lib/nokogiri/xml/cdata.rb +11 -0
  162. data/lib/nokogiri/xml/character_data.rb +7 -0
  163. data/lib/nokogiri/xml/document.rb +218 -0
  164. data/lib/nokogiri/xml/document_fragment.rb +84 -0
  165. data/lib/nokogiri/xml/dtd.rb +22 -0
  166. data/lib/nokogiri/xml/element_content.rb +36 -0
  167. data/lib/nokogiri/xml/element_decl.rb +13 -0
  168. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  169. data/lib/nokogiri/xml/namespace.rb +13 -0
  170. data/lib/nokogiri/xml/node.rb +907 -0
  171. data/lib/nokogiri/xml/node/save_options.rb +45 -0
  172. data/lib/nokogiri/xml/node_set.rb +350 -0
  173. data/lib/nokogiri/xml/notation.rb +6 -0
  174. data/lib/nokogiri/xml/parse_options.rb +85 -0
  175. data/lib/nokogiri/xml/pp.rb +2 -0
  176. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  177. data/lib/nokogiri/xml/pp/node.rb +56 -0
  178. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  179. data/lib/nokogiri/xml/reader.rb +112 -0
  180. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  181. data/lib/nokogiri/xml/sax.rb +4 -0
  182. data/lib/nokogiri/xml/sax/document.rb +164 -0
  183. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  184. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  185. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  186. data/lib/nokogiri/xml/schema.rb +57 -0
  187. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  188. data/lib/nokogiri/xml/text.rb +9 -0
  189. data/lib/nokogiri/xml/xpath.rb +10 -0
  190. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  191. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  192. data/lib/nokogiri/xslt.rb +52 -0
  193. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  194. data/lib/xercesImpl.jar +0 -0
  195. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  196. data/tasks/cross_compile.rb +177 -0
  197. data/tasks/test.rb +94 -0
  198. data/test/css/test_nthiness.rb +159 -0
  199. data/test/css/test_parser.rb +303 -0
  200. data/test/css/test_tokenizer.rb +198 -0
  201. data/test/css/test_xpath_visitor.rb +85 -0
  202. data/test/decorators/test_slop.rb +16 -0
  203. data/test/files/2ch.html +108 -0
  204. data/test/files/address_book.rlx +12 -0
  205. data/test/files/address_book.xml +10 -0
  206. data/test/files/bar/bar.xsd +4 -0
  207. data/test/files/dont_hurt_em_why.xml +422 -0
  208. data/test/files/exslt.xml +8 -0
  209. data/test/files/exslt.xslt +35 -0
  210. data/test/files/foo/foo.xsd +4 -0
  211. data/test/files/po.xml +32 -0
  212. data/test/files/po.xsd +66 -0
  213. data/test/files/shift_jis.html +10 -0
  214. data/test/files/shift_jis.xml +5 -0
  215. data/test/files/snuggles.xml +3 -0
  216. data/test/files/staff.dtd +10 -0
  217. data/test/files/staff.xml +59 -0
  218. data/test/files/staff.xslt +32 -0
  219. data/test/files/tlm.html +850 -0
  220. data/test/files/valid_bar.xml +2 -0
  221. data/test/helper.rb +171 -0
  222. data/test/html/sax/test_parser.rb +136 -0
  223. data/test/html/sax/test_parser_context.rb +48 -0
  224. data/test/html/test_builder.rb +164 -0
  225. data/test/html/test_document.rb +457 -0
  226. data/test/html/test_document_encoding.rb +123 -0
  227. data/test/html/test_document_fragment.rb +255 -0
  228. data/test/html/test_element_description.rb +100 -0
  229. data/test/html/test_named_characters.rb +14 -0
  230. data/test/html/test_node.rb +190 -0
  231. data/test/html/test_node_encoding.rb +27 -0
  232. data/test/test_convert_xpath.rb +135 -0
  233. data/test/test_css_cache.rb +45 -0
  234. data/test/test_encoding_handler.rb +46 -0
  235. data/test/test_memory_leak.rb +52 -0
  236. data/test/test_nokogiri.rb +132 -0
  237. data/test/test_reader.rb +403 -0
  238. data/test/test_soap4r_sax.rb +52 -0
  239. data/test/test_xslt_transforms.rb +189 -0
  240. data/test/xml/node/test_save_options.rb +20 -0
  241. data/test/xml/node/test_subclass.rb +44 -0
  242. data/test/xml/sax/test_parser.rb +338 -0
  243. data/test/xml/sax/test_parser_context.rb +113 -0
  244. data/test/xml/sax/test_push_parser.rb +156 -0
  245. data/test/xml/test_attr.rb +65 -0
  246. data/test/xml/test_attribute_decl.rb +86 -0
  247. data/test/xml/test_builder.rb +210 -0
  248. data/test/xml/test_cdata.rb +50 -0
  249. data/test/xml/test_comment.rb +29 -0
  250. data/test/xml/test_document.rb +675 -0
  251. data/test/xml/test_document_encoding.rb +26 -0
  252. data/test/xml/test_document_fragment.rb +192 -0
  253. data/test/xml/test_dtd.rb +107 -0
  254. data/test/xml/test_dtd_encoding.rb +33 -0
  255. data/test/xml/test_element_content.rb +56 -0
  256. data/test/xml/test_element_decl.rb +73 -0
  257. data/test/xml/test_entity_decl.rb +122 -0
  258. data/test/xml/test_entity_reference.rb +21 -0
  259. data/test/xml/test_namespace.rb +70 -0
  260. data/test/xml/test_node.rb +899 -0
  261. data/test/xml/test_node_attributes.rb +34 -0
  262. data/test/xml/test_node_encoding.rb +107 -0
  263. data/test/xml/test_node_reparenting.rb +321 -0
  264. data/test/xml/test_node_set.rb +708 -0
  265. data/test/xml/test_parse_options.rb +52 -0
  266. data/test/xml/test_processing_instruction.rb +30 -0
  267. data/test/xml/test_reader_encoding.rb +126 -0
  268. data/test/xml/test_relax_ng.rb +60 -0
  269. data/test/xml/test_schema.rb +89 -0
  270. data/test/xml/test_syntax_error.rb +12 -0
  271. data/test/xml/test_text.rb +47 -0
  272. data/test/xml/test_unparented_node.rb +381 -0
  273. data/test/xml/test_xpath.rb +237 -0
  274. data/test/xslt/test_custom_functions.rb +94 -0
  275. metadata +525 -0
@@ -0,0 +1,122 @@
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
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
+ end
29
+
30
+ def test_create_typed_entity
31
+ entity = @xml.create_entity(
32
+ 'foo', EntityDecl::INTERNAL_GENERAL, nil, nil, nil
33
+ )
34
+ assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
35
+ assert_equal 'foo', entity.name
36
+ end
37
+
38
+ def test_new
39
+ entity = Nokogiri::XML::EntityDecl.new(
40
+ 'foo', @xml, EntityDecl::INTERNAL_GENERAL, nil, nil, nil
41
+ )
42
+ assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
43
+ assert_equal 'foo', entity.name
44
+ end
45
+
46
+ def test_create_default_args
47
+ entity = @xml.create_entity('foo')
48
+ assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
49
+ assert_equal 'foo', entity.name
50
+ end
51
+
52
+ def test_external_id
53
+ assert_nil @entity_decl.external_id
54
+ end
55
+
56
+ def test_system_id
57
+ assert_nil @entity_decl.system_id
58
+ end
59
+
60
+ def test_entity_type
61
+ assert_equal 1, @entity_decl.entity_type
62
+ end
63
+
64
+ def test_original_content
65
+ assert_equal "es", @entity_decl.original_content
66
+ if Nokogiri.jruby?
67
+ assert_nil @entities[1].original_content
68
+ else
69
+ assert_equal "", @entities[1].original_content
70
+ end
71
+ end
72
+
73
+ def test_content
74
+ assert_equal "es", @entity_decl.content
75
+ if Nokogiri.jruby?
76
+ assert_nil @entities[1].content
77
+ else
78
+ assert_equal "", @entities[1].content
79
+ end
80
+ end
81
+
82
+ def test_type
83
+ assert_equal 17, @entities.first.type
84
+ end
85
+
86
+ def test_class
87
+ assert_instance_of Nokogiri::XML::EntityDecl, @entities.first
88
+ end
89
+
90
+ def test_attributes
91
+ assert_raise NoMethodError do
92
+ @entity_decl.attributes
93
+ end
94
+ end
95
+
96
+ def test_namespace
97
+ assert_raise NoMethodError do
98
+ @entity_decl.namespace
99
+ end
100
+ end
101
+
102
+ def test_namespace_definitions
103
+ assert_raise NoMethodError do
104
+ @entity_decl.namespace_definitions
105
+ end
106
+ end
107
+
108
+ def test_line
109
+ assert_raise NoMethodError do
110
+ @entity_decl.line
111
+ end
112
+ end
113
+
114
+ def test_inspect
115
+ assert_equal(
116
+ "#<#{@entity_decl.class.name}:#{sprintf("0x%x", @entity_decl.object_id)} #{@entity_decl.to_s.inspect}>",
117
+ @entity_decl.inspect
118
+ )
119
+ end
120
+ end
121
+ end
122
+ 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,70 @@
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
+ if Nokogiri.uses_libxml?
16
+ def test_namespace_is_in_node_cache
17
+ node = @xml.root.namespace
18
+ assert @xml.instance_variable_get(:@node_cache).include?(node)
19
+ end
20
+ end
21
+
22
+ def test_built_nodes_keep_namespace_decls
23
+ doc = Document.new
24
+ e = Node.new 'element', doc
25
+ c = Node.new 'child', doc
26
+ c.default_namespace = 'woop:de:doo'
27
+
28
+ assert c.namespace, 'has a namespace'
29
+ e.add_child c
30
+ assert c.namespace, 'has a namespace'
31
+
32
+ doc.add_child e
33
+ assert c.namespace, 'has a namespace'
34
+ end
35
+
36
+ def test_inspect
37
+ ns = @xml.root.namespace
38
+ assert_equal "#<#{ns.class.name}:#{sprintf("0x%x", ns.object_id)} href=#{ns.href.inspect}>", ns.inspect
39
+ end
40
+
41
+ def test_namespace_node_prefix
42
+ namespaces = @xml.root.namespace_definitions
43
+ assert_equal [nil, 'foo'], namespaces.map { |x| x.prefix }
44
+ end
45
+
46
+ def test_namespace_node_href
47
+ namespaces = @xml.root.namespace_definitions
48
+ assert_equal [
49
+ 'http://tenderlovemaking.com/',
50
+ 'bar'
51
+ ], namespaces.map { |x| x.href }
52
+ end
53
+
54
+ def test_equality
55
+ namespaces = @xml.root.namespace_definitions
56
+ assert_equal namespaces, @xml.root.namespace_definitions
57
+ end
58
+
59
+ def test_add_definition
60
+ @xml.root.add_namespace_definition('baz', 'bar')
61
+ assert_equal 3, @xml.root.namespace_definitions.length
62
+ end
63
+
64
+ def test_add_definition_return
65
+ ns = @xml.root.add_namespace_definition('baz', 'bar')
66
+ assert_equal 'baz', ns.prefix
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,899 @@
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
+ 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)
296
+ end
297
+
298
+ def test_duplicate_node_removes_namespace
299
+ fruits = Nokogiri::XML(<<-eoxml)
300
+ <Fruit xmlns='www.fruits.org'>
301
+ <Apple></Apple>
302
+ </Fruit>
303
+ eoxml
304
+ apple = fruits.root.xpath('fruit:Apple', {'fruit'=>'www.fruits.org'})[0]
305
+ new_apple = apple.dup
306
+ fruits.root << new_apple
307
+ assert_equal 2, fruits.xpath('//xmlns:Apple').length
308
+ assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
309
+ end
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
+
328
+ def test_node_added_to_root_should_get_namespace
329
+ fruits = Nokogiri::XML(<<-eoxml)
330
+ <Fruit xmlns='http://www.fruits.org'>
331
+ </Fruit>
332
+ eoxml
333
+ apple = fruits.fragment('<Apple/>')
334
+ fruits.root << apple
335
+ assert_equal 1, fruits.xpath('//xmlns:Apple').length
336
+ end
337
+
338
+ def test_new_node_can_have_ancestors
339
+ xml = Nokogiri::XML('<root>text</root>')
340
+ item = Nokogiri::XML::Element.new('item', xml)
341
+ assert_equal 0, item.ancestors.length
342
+ end
343
+
344
+ def test_children
345
+ doc = Nokogiri::XML(<<-eoxml)
346
+ <root>#{'<a/>' * 9 }</root>
347
+ eoxml
348
+ assert_equal 9, doc.root.children.length
349
+ assert_equal 9, doc.root.children.to_a.length
350
+
351
+ doc = Nokogiri::XML(<<-eoxml)
352
+ <root>#{'<a/>' * 15 }</root>
353
+ eoxml
354
+ assert_equal 15, doc.root.children.length
355
+ assert_equal 15, doc.root.children.to_a.length
356
+ end
357
+
358
+ def test_add_namespace
359
+ node = @xml.at('address')
360
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
361
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
362
+ end
363
+
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
476
+ end
477
+
478
+ def test_write_to
479
+ io = StringIO.new
480
+ @xml.write_to io
481
+ io.rewind
482
+ assert_equal @xml.to_xml, io.read
483
+ end
484
+
485
+ def test_attribute_with_symbol
486
+ assert_equal 'Yes', @xml.css('address').first[:domestic]
487
+ end
488
+
489
+ def test_write_to_with_block
490
+ called = false
491
+ io = StringIO.new
492
+ conf = nil
493
+ @xml.write_to io do |config|
494
+ called = true
495
+ conf = config
496
+ config.format.as_html.no_empty_tags
497
+ end
498
+ io.rewind
499
+ assert called
500
+ string = io.read
501
+ assert_equal @xml.serialize(nil, conf.options), string
502
+ assert_equal @xml.serialize(nil, conf), string
503
+ end
504
+
505
+ %w{ xml html xhtml }.each do |type|
506
+ define_method(:"test_write_#{type}_to") do
507
+ io = StringIO.new
508
+ assert @xml.send(:"write_#{type}_to", io)
509
+ io.rewind
510
+ assert_match @xml.send(:"to_#{type}"), io.read
511
+ end
512
+ end
513
+
514
+ def test_serialize_with_block
515
+ called = false
516
+ conf = nil
517
+ string = @xml.serialize do |config|
518
+ called = true
519
+ conf = config
520
+ config.format.as_html.no_empty_tags
521
+ end
522
+ assert called
523
+ assert_equal @xml.serialize(nil, conf.options), string
524
+ assert_equal @xml.serialize(nil, conf), string
525
+ end
526
+
527
+ def test_hold_refence_to_subnode
528
+ doc = Nokogiri::XML(<<-eoxml)
529
+ <root>
530
+ <a>
531
+ <b />
532
+ </a>
533
+ </root>
534
+ eoxml
535
+ assert node_a = doc.css('a').first
536
+ assert node_b = node_a.css('b').first
537
+ node_a.unlink
538
+ assert_equal 'b', node_b.name
539
+ end
540
+
541
+ def test_values
542
+ assert_equal %w{ Yes Yes }, @xml.xpath('//address')[1].values
543
+ end
544
+
545
+ def test_keys
546
+ assert_equal %w{ domestic street }, @xml.xpath('//address')[1].keys
547
+ end
548
+
549
+ def test_each
550
+ attributes = []
551
+ @xml.xpath('//address')[1].each do |key, value|
552
+ attributes << [key, value]
553
+ end
554
+ assert_equal [['domestic', 'Yes'], ['street', 'Yes']], attributes
555
+ end
556
+
557
+ def test_new
558
+ assert node = Nokogiri::XML::Node.new('input', @xml)
559
+ assert_equal 1, node.node_type
560
+ assert_instance_of Nokogiri::XML::Element, node
561
+ end
562
+
563
+ def test_to_str
564
+ name = @xml.xpath('//name').first
565
+ assert_match(/Margaret/, '' + name)
566
+ assert_equal('Margaret Martin', '' + name.children.first)
567
+ end
568
+
569
+ def test_ancestors
570
+ address = @xml.xpath('//address').first
571
+ assert_equal 3, address.ancestors.length
572
+ assert_equal ['employee', 'staff', 'document'],
573
+ address.ancestors.map { |x| x.name }
574
+ end
575
+
576
+ def test_read_only?
577
+ assert entity_decl = @xml.internal_subset.children.find { |x|
578
+ x.type == Node::ENTITY_DECL
579
+ }
580
+ assert entity_decl.read_only?
581
+ end
582
+
583
+ def test_remove_attribute
584
+ address = @xml.xpath('/staff/employee/address').first
585
+ assert_equal 'Yes', address['domestic']
586
+ address.remove_attribute 'domestic'
587
+ assert_nil address['domestic']
588
+ end
589
+
590
+ def test_delete
591
+ address = @xml.xpath('/staff/employee/address').first
592
+ assert_equal 'Yes', address['domestic']
593
+ address.delete 'domestic'
594
+ assert_nil address['domestic']
595
+ end
596
+
597
+ def test_set_content_with_symbol
598
+ node = @xml.at('//name')
599
+ node.content = :foo
600
+ assert_equal 'foo', node.content
601
+ end
602
+
603
+ def test_find_by_css_with_tilde_eql
604
+ xml = Nokogiri::XML.parse(<<-eoxml)
605
+ <root>
606
+ <a>Hello world</a>
607
+ <a class='foo bar'>Bar</a>
608
+ <a class='bar foo'>Bar</a>
609
+ <a class='bar'>Bar</a>
610
+ <a class='baz bar foo'>Bar</a>
611
+ <a class='bazbarfoo'>Awesome</a>
612
+ <a class='bazbar'>Awesome</a>
613
+ </root>
614
+ eoxml
615
+ set = xml.css('a[@class~="bar"]')
616
+ assert_equal 4, set.length
617
+ assert_equal ['Bar'], set.map { |node| node.content }.uniq
618
+ end
619
+
620
+ def test_unlink
621
+ xml = Nokogiri::XML.parse(<<-eoxml)
622
+ <root>
623
+ <a class='foo bar'>Bar</a>
624
+ <a class='bar foo'>Bar</a>
625
+ <a class='bar'>Bar</a>
626
+ <a>Hello world</a>
627
+ <a class='baz bar foo'>Bar</a>
628
+ <a class='bazbarfoo'>Awesome</a>
629
+ <a class='bazbar'>Awesome</a>
630
+ </root>
631
+ eoxml
632
+ node = xml.xpath('//a')[3]
633
+ assert_equal('Hello world', node.text)
634
+ assert_match(/Hello world/, xml.to_s)
635
+ assert node.parent
636
+ assert node.document
637
+ assert node.previous_sibling
638
+ assert node.next_sibling
639
+ node.unlink
640
+ assert !node.parent
641
+ #assert !node.document
642
+ assert !node.previous_sibling
643
+ assert !node.next_sibling
644
+ assert_no_match(/Hello world/, xml.to_s)
645
+ end
646
+
647
+ def test_next_sibling
648
+ assert node = @xml.root
649
+ assert sibling = node.child.next_sibling
650
+ assert_equal('employee', sibling.name)
651
+ end
652
+
653
+ def test_previous_sibling
654
+ assert node = @xml.root
655
+ assert sibling = node.child.next_sibling
656
+ assert_equal('employee', sibling.name)
657
+ assert_equal(sibling.previous_sibling, node.child)
658
+ end
659
+
660
+ def test_name=
661
+ assert node = @xml.root
662
+ node.name = 'awesome'
663
+ assert_equal('awesome', node.name)
664
+ end
665
+
666
+ def test_child
667
+ assert node = @xml.root
668
+ assert child = node.child
669
+ assert_equal('text', child.name)
670
+ end
671
+
672
+ def test_key?
673
+ assert node = @xml.search('//address').first
674
+ assert(!node.key?('asdfasdf'))
675
+ end
676
+
677
+ def test_set_property
678
+ assert node = @xml.search('//address').first
679
+ node['foo'] = 'bar'
680
+ assert_equal('bar', node['foo'])
681
+ end
682
+
683
+ def test_attributes
684
+ assert node = @xml.search('//address').first
685
+ assert_nil(node['asdfasdfasdf'])
686
+ assert_equal('Yes', node['domestic'])
687
+
688
+ assert node = @xml.search('//address')[2]
689
+ attr = node.attributes
690
+ assert_equal 2, attr.size
691
+ assert_equal 'Yes', attr['domestic'].value
692
+ assert_equal 'Yes', attr['domestic'].to_s
693
+ assert_equal 'No', attr['street'].value
694
+ end
695
+
696
+ def test_path
697
+ assert set = @xml.search('//employee')
698
+ assert node = set.first
699
+ assert_equal('/staff/employee[1]', node.path)
700
+ end
701
+
702
+ def test_search_by_symbol
703
+ assert set = @xml.search(:employee)
704
+ assert 5, set.length
705
+
706
+ assert node = @xml.at(:employee)
707
+ assert node.text =~ /EMP0001/
708
+ end
709
+
710
+ def test_new_node
711
+ node = Nokogiri::XML::Node.new('form', @xml)
712
+ assert_equal('form', node.name)
713
+ assert(node.document)
714
+ end
715
+
716
+ def test_encode_special_chars
717
+ foo = @xml.css('employee').first.encode_special_chars('&')
718
+ assert_equal '&amp;', foo
719
+ end
720
+
721
+ def test_content
722
+ node = Nokogiri::XML::Node.new('form', @xml)
723
+ assert_equal('', node.content)
724
+
725
+ node.content = 'hello world!'
726
+ assert_equal('hello world!', node.content)
727
+
728
+ node.content = '& <foo> &amp;'
729
+ assert_equal('& <foo> &amp;', node.content)
730
+ assert_equal('<form>&amp; &lt;foo&gt; &amp;amp;</form>', node.to_xml)
731
+ end
732
+
733
+ def test_set_content_should_unlink_existing_content
734
+ node = @xml.at_css("employee")
735
+ children = node.children
736
+ node.content = "hello"
737
+ children.each { |child| assert_nil child.parent }
738
+ end
739
+
740
+ def test_whitespace_nodes
741
+ doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
742
+ children = doc.at('//root').children.collect{|j| j.to_s}
743
+ assert_equal "\n", children[1]
744
+ assert_equal " ", children[3]
745
+ end
746
+
747
+ def test_node_equality
748
+ doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
749
+ doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
750
+
751
+ address1_1 = doc1.xpath('//address').first
752
+ address1_2 = doc1.xpath('//address').first
753
+
754
+ address2 = doc2.xpath('//address').first
755
+
756
+ assert_not_equal address1_1, address2 # two references to very, very similar nodes
757
+ assert_equal address1_1, address1_2 # two references to the exact same node
758
+ end
759
+
760
+ def test_namespace_search_with_xpath_and_hash
761
+ xml = Nokogiri::XML.parse(<<-eoxml)
762
+ <root>
763
+ <car xmlns:part="http://general-motors.com/">
764
+ <part:tire>Michelin Model XGV</part:tire>
765
+ </car>
766
+ <bicycle xmlns:part="http://schwinn.com/">
767
+ <part:tire>I'm a bicycle tire!</part:tire>
768
+ </bicycle>
769
+ </root>
770
+ eoxml
771
+
772
+ tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
773
+ assert_equal 1, tires.length
774
+ end
775
+
776
+ def test_namespace_search_with_css
777
+ xml = Nokogiri::XML.parse(<<-eoxml)
778
+ <root>
779
+ <car xmlns:part="http://general-motors.com/">
780
+ <part:tire>Michelin Model XGV</part:tire>
781
+ </car>
782
+ <bicycle xmlns:part="http://schwinn.com/">
783
+ <part:tire>I'm a bicycle tire!</part:tire>
784
+ </bicycle>
785
+ </root>
786
+ eoxml
787
+
788
+ tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/')
789
+ assert_equal 1, tires.length
790
+ end
791
+
792
+ def test_namespaces_should_include_all_namespace_definitions
793
+ xml = Nokogiri::XML.parse(<<-EOF)
794
+ <x xmlns="http://quux.com/" xmlns:a="http://foo.com/" xmlns:b="http://bar.com/">
795
+ <y xmlns:c="http://bazz.com/">
796
+ <z>hello</z>
797
+ <a xmlns:c="http://newc.com/" />
798
+ </y>
799
+ </x>
800
+ EOF
801
+
802
+ namespaces = xml.namespaces # Document#namespace
803
+ assert_equal({"xmlns" => "http://quux.com/",
804
+ "xmlns:a" => "http://foo.com/",
805
+ "xmlns:b" => "http://bar.com/"}, namespaces)
806
+
807
+ namespaces = xml.root.namespaces
808
+ assert_equal({"xmlns" => "http://quux.com/",
809
+ "xmlns:a" => "http://foo.com/",
810
+ "xmlns:b" => "http://bar.com/"}, namespaces)
811
+
812
+ namespaces = xml.at_xpath("//xmlns:y").namespaces
813
+ assert_equal({"xmlns" => "http://quux.com/",
814
+ "xmlns:a" => "http://foo.com/",
815
+ "xmlns:b" => "http://bar.com/",
816
+ "xmlns:c" => "http://bazz.com/"}, namespaces)
817
+
818
+ namespaces = xml.at_xpath("//xmlns:z").namespaces
819
+ assert_equal({"xmlns" => "http://quux.com/",
820
+ "xmlns:a" => "http://foo.com/",
821
+ "xmlns:b" => "http://bar.com/",
822
+ "xmlns:c" => "http://bazz.com/"}, namespaces)
823
+
824
+ namespaces = xml.at_xpath("//xmlns:a").namespaces
825
+ assert_equal({"xmlns" => "http://quux.com/",
826
+ "xmlns:a" => "http://foo.com/",
827
+ "xmlns:b" => "http://bar.com/",
828
+ "xmlns:c" => "http://newc.com/"}, namespaces)
829
+ end
830
+
831
+ def test_namespace
832
+ xml = Nokogiri::XML.parse(<<-EOF)
833
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
834
+ <y xmlns:c='http://bazz.com/'>
835
+ <a:div>hello a</a:div>
836
+ <b:div>hello b</b:div>
837
+ <c:div>hello c</c:div>
838
+ <div>hello moon</div>
839
+ </y>
840
+ </x>
841
+ EOF
842
+ set = xml.search("//y/*")
843
+ assert_equal "a", set[0].namespace.prefix
844
+ assert_equal "b", set[1].namespace.prefix
845
+ assert_equal "c", set[2].namespace.prefix
846
+ assert_equal nil, set[3].namespace
847
+ end
848
+
849
+ if Nokogiri.uses_libxml?
850
+ def test_namespace_without_an_href_on_html_node
851
+ # because microsoft word's HTML formatting does this. ick.
852
+ xml = Nokogiri::HTML.parse <<-EOF
853
+ <div><o:p>foo</o:p></div>
854
+ EOF
855
+
856
+ assert_not_nil(node = xml.at('p'))
857
+
858
+ assert_equal 1, node.namespaces.keys.size
859
+ assert node.namespaces.has_key?('xmlns:o')
860
+ assert_equal nil, node.namespaces['xmlns:o']
861
+ end
862
+ end
863
+
864
+ def test_line
865
+ xml = Nokogiri::XML(<<-eoxml)
866
+ <root>
867
+ <a>
868
+ Hello world
869
+ </a>
870
+ </root>
871
+ eoxml
872
+
873
+ set = xml.search("//a")
874
+ node = set.first
875
+ assert_equal 2, node.line
876
+ end
877
+
878
+ def test_xpath_results_have_document_and_are_decorated
879
+ x = Module.new do
880
+ def awesome! ; end
881
+ end
882
+ util_decorate(@xml, x)
883
+ node_set = @xml.xpath("//employee")
884
+ assert_equal @xml, node_set.document
885
+ assert node_set.respond_to?(:awesome!)
886
+ end
887
+
888
+ def test_css_results_have_document_and_are_decorated
889
+ x = Module.new do
890
+ def awesome! ; end
891
+ end
892
+ util_decorate(@xml, x)
893
+ node_set = @xml.css("employee")
894
+ assert_equal @xml, node_set.document
895
+ assert node_set.respond_to?(:awesome!)
896
+ end
897
+ end
898
+ end
899
+ end