superfeedr-nokogiri 1.4.0.20091116183308

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 +314 -0
  4. data/Manifest.txt +269 -0
  5. data/README.ja.rdoc +105 -0
  6. data/README.rdoc +118 -0
  7. data/Rakefile +244 -0
  8. data/bin/nokogiri +49 -0
  9. data/ext/nokogiri/extconf.rb +145 -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 +89 -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 +114 -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 +142 -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 +69 -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 +71 -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 +132 -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 +134 -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 +131 -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 +138 -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 +430 -0
@@ -0,0 +1,38 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestCDATA < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
9
+ end
10
+
11
+ def test_cdata_node
12
+ name = @xml.xpath('//employee[2]/name').first
13
+ assert cdata = name.children[1]
14
+ assert cdata.cdata?
15
+ assert_equal '#cdata-section', cdata.name
16
+ end
17
+
18
+ def test_new
19
+ node = CDATA.new(@xml, "foo")
20
+ assert_equal "foo", node.content
21
+
22
+ node = CDATA.new(@xml.root, "foo")
23
+ assert_equal "foo", node.content
24
+ end
25
+
26
+ def test_new_with_nil
27
+ node = CDATA.new(@xml, nil)
28
+ assert_equal nil, node.content
29
+ end
30
+
31
+ def test_lots_of_new_cdata
32
+ 100.times {
33
+ node = CDATA.new(@xml, "asdfasdf")
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestComment < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
9
+ end
10
+
11
+ def test_new
12
+ comment = Nokogiri::XML::Comment.new(@xml, 'hello world')
13
+ assert_equal('<!--hello world-->', comment.to_s)
14
+ end
15
+
16
+ def test_comment?
17
+ comment = Nokogiri::XML::Comment.new(@xml, 'hello world')
18
+ assert(comment.comment?)
19
+ assert(!@xml.root.comment?)
20
+ end
21
+
22
+ def test_many_comments
23
+ 100.times {
24
+ Nokogiri::XML::Comment.new(@xml, 'hello world')
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,607 @@
1
+ require "helper"
2
+
3
+ require 'uri'
4
+
5
+ module Nokogiri
6
+ module XML
7
+ class TestDocument < Nokogiri::TestCase
8
+ def setup
9
+ super
10
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
11
+ end
12
+
13
+ def test_subclass_initialize_modify # testing a segv
14
+ Class.new(Nokogiri::XML::Document) {
15
+ def initialize
16
+ super
17
+ body_node = Nokogiri::XML::Node.new "body", self
18
+ body_node.content = "stuff"
19
+ self.root = body_node
20
+ end
21
+ }.new
22
+ end
23
+
24
+ def test_create_text_node
25
+ txt = @xml.create_text_node 'foo'
26
+ assert_instance_of Nokogiri::XML::Text, txt
27
+ assert_equal 'foo', txt.text
28
+ assert_equal @xml, txt.document
29
+ end
30
+
31
+ def test_create_element
32
+ elm = @xml.create_element('foo')
33
+ assert_instance_of Nokogiri::XML::Element, elm
34
+ assert_equal 'foo', elm.name
35
+ assert_equal @xml, elm.document
36
+ end
37
+
38
+ def test_pp
39
+ out = StringIO.new('')
40
+ assert_nothing_raised do
41
+ ::PP.pp @xml, out
42
+ end
43
+ end
44
+
45
+ def test_create_internal_subset_on_existing_subset
46
+ assert_not_nil @xml.internal_subset
47
+ assert_raises(RuntimeError) do
48
+ @xml.create_internal_subset('staff', nil, 'staff.dtd')
49
+ end
50
+ end
51
+
52
+ def test_create_internal_subset
53
+ xml = Nokogiri::XML('<root />')
54
+ assert_nil xml.internal_subset
55
+
56
+ xml.create_internal_subset('name', nil, 'staff.dtd')
57
+ ss = xml.internal_subset
58
+ assert_equal 'name', ss.name
59
+ assert_nil ss.external_id
60
+ assert_equal 'staff.dtd', ss.system_id
61
+ end
62
+
63
+ def test_external_subset
64
+ assert_nil @xml.external_subset
65
+ Dir.chdir(ASSETS_DIR) do
66
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
67
+ cfg.dtdload
68
+ }
69
+ end
70
+ assert @xml.external_subset
71
+ end
72
+
73
+ def test_create_external_subset_fails_with_existing_subset
74
+ assert_nil @xml.external_subset
75
+ Dir.chdir(ASSETS_DIR) do
76
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
77
+ cfg.dtdload
78
+ }
79
+ end
80
+ assert @xml.external_subset
81
+
82
+ assert_raises(RuntimeError) do
83
+ @xml.create_external_subset('staff', nil, 'staff.dtd')
84
+ end
85
+ end
86
+
87
+ def test_create_external_subset
88
+ dtd = @xml.create_external_subset('staff', nil, 'staff.dtd')
89
+ assert_nil dtd.external_id
90
+ assert_equal 'staff.dtd', dtd.system_id
91
+ assert_equal 'staff', dtd.name
92
+ assert_equal dtd, @xml.external_subset
93
+ end
94
+
95
+ def test_version
96
+ assert_equal '1.0', @xml.version
97
+ end
98
+
99
+ def test_attributes
100
+ assert_raise NoMethodError do
101
+ @xml.attributes
102
+ end
103
+ end
104
+
105
+ def test_namespace
106
+ assert_raise NoMethodError do
107
+ @xml.namespace
108
+ end
109
+ end
110
+
111
+ def test_namespace_definitions
112
+ assert_raise NoMethodError do
113
+ @xml.namespace_definitions
114
+ end
115
+ end
116
+
117
+ def test_line
118
+ assert_raise NoMethodError do
119
+ @xml.line
120
+ end
121
+ end
122
+
123
+ def test_empty_node_converted_to_html_is_not_self_closing
124
+ doc = Nokogiri::XML('<a></a>')
125
+ assert_equal "<a></a>", doc.inner_html
126
+ end
127
+
128
+ def test_fragment
129
+ fragment = @xml.fragment
130
+ assert_equal 0, fragment.children.length
131
+ end
132
+
133
+ def test_add_child_with_fragment
134
+ doc = Nokogiri::XML::Document.new
135
+ fragment = doc.fragment('<hello />')
136
+ doc.add_child fragment
137
+ assert_equal '/hello', doc.at('//hello').path
138
+ assert_equal 'hello', doc.root.name
139
+ end
140
+
141
+ def test_add_child_with_multiple_roots
142
+ assert_raises(RuntimeError) do
143
+ @xml << Node.new('foo', @xml)
144
+ end
145
+ end
146
+
147
+ def test_move_root_to_document_with_no_root
148
+ sender = Nokogiri::XML('<root>foo</root>')
149
+ newdoc = Nokogiri::XML::Document.new
150
+ newdoc.root = sender.root
151
+ end
152
+
153
+ def test_move_root_with_existing_root_gets_gcd
154
+ doc = Nokogiri::XML('<root>test</root>')
155
+ doc2 = Nokogiri::XML("<root>#{'x' * 5000000}</root>")
156
+ doc2.root = doc.root
157
+ end
158
+
159
+ def test_validate
160
+ assert_equal 44, @xml.validate.length
161
+ end
162
+
163
+ def test_validate_no_internal_subset
164
+ doc = Nokogiri::XML('<test/>')
165
+ assert_nil doc.validate
166
+ end
167
+
168
+ def test_clone
169
+ assert @xml.clone
170
+ end
171
+
172
+ def test_document_should_not_have_default_ns
173
+ doc = Nokogiri::XML::Document.new
174
+
175
+ assert_raises NoMethodError do
176
+ doc.default_namespace = 'http://innernet.com/'
177
+ end
178
+
179
+ assert_raises NoMethodError do
180
+ doc.add_namespace_definition('foo', 'bar')
181
+ end
182
+ end
183
+
184
+ def test_parse_handles_nil_gracefully
185
+ assert_nothing_raised do
186
+ @doc = Nokogiri::XML::Document.parse(nil)
187
+ end
188
+ assert_instance_of Nokogiri::XML::Document, @doc
189
+ end
190
+
191
+ def test_parse_takes_block
192
+ options = nil
193
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
194
+ options = cfg
195
+ end
196
+ assert options
197
+ end
198
+
199
+ def test_parse_yields_parse_options
200
+ options = nil
201
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
202
+ options = cfg
203
+ options.nonet.nowarning.dtdattr
204
+ end
205
+ assert options.nonet?
206
+ assert options.nowarning?
207
+ assert options.dtdattr?
208
+ end
209
+
210
+ def test_XML_takes_block
211
+ options = nil
212
+ Nokogiri::XML(File.read(XML_FILE), XML_FILE) do |cfg|
213
+ options = cfg
214
+ options.nonet.nowarning.dtdattr
215
+ end
216
+ assert options.nonet?
217
+ assert options.nowarning?
218
+ assert options.dtdattr?
219
+ end
220
+
221
+ def test_subclass
222
+ klass = Class.new(Nokogiri::XML::Document)
223
+ doc = klass.new
224
+ assert_instance_of klass, doc
225
+ end
226
+
227
+ def test_subclass_initialize
228
+ klass = Class.new(Nokogiri::XML::Document) do
229
+ attr_accessor :initialized_with
230
+
231
+ def initialize(*args)
232
+ @initialized_with = args
233
+ end
234
+ end
235
+ doc = klass.new("1.0", 1)
236
+ assert_equal ["1.0", 1], doc.initialized_with
237
+ end
238
+
239
+ def test_subclass_dup
240
+ klass = Class.new(Nokogiri::XML::Document)
241
+ doc = klass.new.dup
242
+ assert_instance_of klass, doc
243
+ end
244
+
245
+ def test_subclass_parse
246
+ klass = Class.new(Nokogiri::XML::Document)
247
+ doc = klass.parse(File.read(XML_FILE))
248
+ assert_equal @xml.to_s, doc.to_s
249
+ assert_instance_of klass, doc
250
+ end
251
+
252
+ def test_document_parse_method
253
+ xml = Nokogiri::XML::Document.parse(File.read(XML_FILE))
254
+ assert_equal @xml.to_s, xml.to_s
255
+ end
256
+
257
+ def test_encoding=
258
+ @xml.encoding = 'UTF-8'
259
+ assert_match 'UTF-8', @xml.to_xml
260
+
261
+ @xml.encoding = 'EUC-JP'
262
+ assert_match 'EUC-JP', @xml.to_xml
263
+ end
264
+
265
+ def test_namespace_should_not_exist
266
+ assert_raises(NoMethodError) {
267
+ @xml.namespace
268
+ }
269
+ end
270
+
271
+ def test_non_existant_function
272
+ # WTF. I don't know why this is different between MRI and ffi.
273
+ # They should be the same... Either way, raising an exception
274
+ # is the correct thing to do.
275
+ exception = RuntimeError
276
+
277
+ if Nokogiri::VERSION_INFO['libxml']['platform'] == 'jruby'
278
+ exception = Nokogiri::XML::XPath::SyntaxError
279
+ end
280
+
281
+ assert_raises(exception) {
282
+ @xml.xpath('//name[foo()]')
283
+ }
284
+ end
285
+
286
+ def test_ancestors
287
+ assert_equal 0, @xml.ancestors.length
288
+ end
289
+
290
+ def test_root_node_parent_is_document
291
+ parent = @xml.root.parent
292
+ assert_equal @xml, parent
293
+ assert_instance_of Nokogiri::XML::Document, parent
294
+ end
295
+
296
+ def test_xmlns_is_automatically_registered
297
+ doc = Nokogiri::XML(<<-eoxml)
298
+ <root xmlns="http://tenderlovemaking.com/">
299
+ <foo>
300
+ bar
301
+ </foo>
302
+ </root>
303
+ eoxml
304
+ assert_equal 1, doc.css('xmlns|foo').length
305
+ assert_equal 1, doc.css('foo').length
306
+ assert_equal 0, doc.css('|foo').length
307
+ assert_equal 1, doc.xpath('//xmlns:foo').length
308
+ assert_equal 1, doc.search('xmlns|foo').length
309
+ assert_equal 1, doc.search('//xmlns:foo').length
310
+ assert doc.at('xmlns|foo')
311
+ assert doc.at('//xmlns:foo')
312
+ assert doc.at('foo')
313
+ end
314
+
315
+ def test_xmlns_is_registered_for_nodesets
316
+ doc = Nokogiri::XML(<<-eoxml)
317
+ <root xmlns="http://tenderlovemaking.com/">
318
+ <foo>
319
+ <bar>
320
+ baz
321
+ </bar>
322
+ </foo>
323
+ </root>
324
+ eoxml
325
+ assert_equal 1, doc.css('xmlns|foo').css('xmlns|bar').length
326
+ assert_equal 1, doc.css('foo').css('bar').length
327
+ assert_equal 1, doc.xpath('//xmlns:foo').xpath('./xmlns:bar').length
328
+ assert_equal 1, doc.search('xmlns|foo').search('xmlns|bar').length
329
+ assert_equal 1, doc.search('//xmlns:foo').search('./xmlns:bar').length
330
+ end
331
+
332
+ def test_to_xml_with_indent
333
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
334
+ doc = Nokogiri::XML(doc.to_xml(:indent => 5))
335
+
336
+ assert_indent 5, doc
337
+ end
338
+
339
+ def test_write_xml_to_with_indent
340
+ io = StringIO.new
341
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
342
+ doc.write_xml_to io, :indent => 5
343
+ io.rewind
344
+ doc = Nokogiri::XML(io.read)
345
+ assert_indent 5, doc
346
+ end
347
+
348
+ # wtf... osx's libxml sucks.
349
+ unless Nokogiri::LIBXML_VERSION =~ /^2\.6\./
350
+ def test_encoding
351
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
352
+ assert_equal 'UTF-8', xml.encoding
353
+ end
354
+ end
355
+
356
+ def test_document_has_errors
357
+ doc = Nokogiri::XML(<<-eoxml)
358
+ <foo><bar></foo>
359
+ eoxml
360
+ assert doc.errors.length > 0
361
+ doc.errors.each do |error|
362
+ assert_match error.message, error.inspect
363
+ assert_match error.message, error.to_s
364
+ end
365
+ end
366
+
367
+ def test_strict_document_throws_syntax_error
368
+ assert_raises(Nokogiri::XML::SyntaxError) {
369
+ Nokogiri::XML('<foo><bar></foo>', nil, nil, 0)
370
+ }
371
+
372
+ assert_raises(Nokogiri::XML::SyntaxError) {
373
+ Nokogiri::XML('<foo><bar></foo>') { |cfg|
374
+ cfg.strict
375
+ }
376
+ }
377
+
378
+ assert_raises(Nokogiri::XML::SyntaxError) {
379
+ Nokogiri::XML(StringIO.new('<foo><bar></foo>')) { |cfg|
380
+ cfg.strict
381
+ }
382
+ }
383
+ end
384
+
385
+ def test_XML_function
386
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
387
+ assert xml.xml?
388
+ end
389
+
390
+ def test_url
391
+ assert @xml.url
392
+ assert_equal XML_FILE, URI.unescape(@xml.url).sub('file:///', '')
393
+ end
394
+
395
+ def test_document_parent
396
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
397
+ assert_raises(NoMethodError) {
398
+ xml.parent
399
+ }
400
+ end
401
+
402
+ def test_document_name
403
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
404
+ assert_equal 'document', xml.name
405
+ end
406
+
407
+ def test_parse_can_take_io
408
+ xml = nil
409
+ File.open(XML_FILE, 'rb') { |f|
410
+ xml = Nokogiri::XML(f)
411
+ }
412
+ assert xml.xml?
413
+ set = xml.search('//employee')
414
+ assert set.length > 0
415
+ end
416
+
417
+ def test_search_on_empty_documents
418
+ doc = Nokogiri::XML::Document.new
419
+ ns = doc.search('//foo')
420
+ assert_equal 0, ns.length
421
+
422
+ ns = doc.css('foo')
423
+ assert_equal 0, ns.length
424
+
425
+ ns = doc.xpath('//foo')
426
+ assert_equal 0, ns.length
427
+ end
428
+
429
+ def test_bad_xpath_raises_syntax_error
430
+ assert_raises(XML::XPath::SyntaxError) {
431
+ @xml.xpath('\\')
432
+ }
433
+ end
434
+
435
+ def test_find_with_namespace
436
+ doc = Nokogiri::XML.parse(<<-eoxml)
437
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
438
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
439
+ </x>
440
+ eoxml
441
+
442
+ ctx = Nokogiri::XML::XPathContext.new(doc)
443
+ ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
444
+ set = ctx.evaluate('//tenderlove:foo').node_set
445
+ assert_equal 1, set.length
446
+ assert_equal 'foo', set.first.name
447
+
448
+ # It looks like only the URI is important:
449
+ ctx = Nokogiri::XML::XPathContext.new(doc)
450
+ ctx.register_ns 'america', 'http://tenderlovemaking.com/'
451
+ set = ctx.evaluate('//america:foo').node_set
452
+ assert_equal 1, set.length
453
+ assert_equal 'foo', set.first.name
454
+
455
+ # Its so important that a missing slash will cause it to return nothing
456
+ ctx = Nokogiri::XML::XPathContext.new(doc)
457
+ ctx.register_ns 'america', 'http://tenderlovemaking.com'
458
+ set = ctx.evaluate('//america:foo').node_set
459
+ assert_equal 0, set.length
460
+ end
461
+
462
+ def test_xml?
463
+ assert @xml.xml?
464
+ end
465
+
466
+ def test_document
467
+ assert @xml.document
468
+ end
469
+
470
+ def test_singleton_methods
471
+ assert node_set = @xml.search('//name')
472
+ assert node_set.length > 0
473
+ node = node_set.first
474
+ def node.test
475
+ 'test'
476
+ end
477
+ assert node_set = @xml.search('//name')
478
+ assert_equal 'test', node_set.first.test
479
+ end
480
+
481
+ def test_multiple_search
482
+ assert node_set = @xml.search('//employee', '//name')
483
+ employees = @xml.search('//employee')
484
+ names = @xml.search('//name')
485
+ assert_equal(employees.length + names.length, node_set.length)
486
+ end
487
+
488
+ def test_node_set_index
489
+ assert node_set = @xml.search('//employee')
490
+
491
+ assert_equal(5, node_set.length)
492
+ assert node_set[4]
493
+ assert_nil node_set[5]
494
+ end
495
+
496
+ def test_search
497
+ assert node_set = @xml.search('//employee')
498
+
499
+ assert_equal(5, node_set.length)
500
+
501
+ node_set.each do |node|
502
+ assert_equal('employee', node.name)
503
+ end
504
+ end
505
+
506
+ def test_dump
507
+ assert @xml.serialize
508
+ assert @xml.to_xml
509
+ end
510
+
511
+ def test_dup
512
+ dup = @xml.dup
513
+ assert_instance_of Nokogiri::XML::Document, dup
514
+ assert dup.xml?, 'duplicate should be xml'
515
+ end
516
+
517
+ def test_subset_is_decorated
518
+ x = Module.new do
519
+ def awesome!
520
+ end
521
+ end
522
+ util_decorate(@xml, x)
523
+
524
+ assert @xml.respond_to?(:awesome!)
525
+ assert node_set = @xml.search('//staff')
526
+ assert node_set.respond_to?(:awesome!)
527
+ assert subset = node_set.search('.//employee')
528
+ assert subset.respond_to?(:awesome!)
529
+ assert sub_subset = node_set.search('.//name')
530
+ assert sub_subset.respond_to?(:awesome!)
531
+ end
532
+
533
+ def test_decorator_is_applied
534
+ x = Module.new do
535
+ def awesome!
536
+ end
537
+ end
538
+ util_decorate(@xml, x)
539
+
540
+ assert @xml.respond_to?(:awesome!)
541
+ assert node_set = @xml.search('//employee')
542
+ assert node_set.respond_to?(:awesome!)
543
+ node_set.each do |node|
544
+ assert node.respond_to?(:awesome!), node.class
545
+ end
546
+ assert @xml.root.respond_to?(:awesome!)
547
+ end
548
+
549
+ def test_new
550
+ doc = nil
551
+ assert_nothing_raised {
552
+ doc = Nokogiri::XML::Document.new
553
+ }
554
+ assert doc
555
+ assert doc.xml?
556
+ assert_nil doc.root
557
+ end
558
+
559
+ def test_set_root
560
+ doc = nil
561
+ assert_nothing_raised {
562
+ doc = Nokogiri::XML::Document.new
563
+ }
564
+ assert doc
565
+ assert doc.xml?
566
+ assert_nil doc.root
567
+ node = Nokogiri::XML::Node.new("b", doc) { |n|
568
+ n.content = 'hello world'
569
+ }
570
+ assert_equal('hello world', node.content)
571
+ doc.root = node
572
+ assert_equal(node, doc.root)
573
+ end
574
+
575
+ def test_remove_namespaces
576
+ doc = Nokogiri::XML <<-EOX
577
+ <root xmlns:a="http://a.flavorjon.es/" xmlns:b="http://b.flavorjon.es/">
578
+ <a:foo>hello from a</a:foo>
579
+ <b:foo>hello from b</b:foo>
580
+ <container xmlns:c="http://c.flavorjon.es/">
581
+ <c:foo>hello from c</c:foo>
582
+ </container>
583
+ </root>
584
+ EOX
585
+
586
+ # assert on setup
587
+ assert_equal 0, doc.xpath("//foo").length
588
+ assert_equal 1, doc.xpath("//a:foo").length
589
+ assert_equal 1, doc.xpath("//a:foo").length
590
+ assert_equal 1, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
591
+
592
+ doc.remove_namespaces!
593
+
594
+ assert_equal 3, doc.xpath("//foo").length
595
+ assert_equal 0, doc.xpath("//a:foo").length
596
+ assert_equal 0, doc.xpath("//a:foo").length
597
+ assert_equal 0, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
598
+ end
599
+
600
+ def util_decorate(document, x)
601
+ document.decorators(XML::Node) << x
602
+ document.decorators(XML::NodeSet) << x
603
+ document.decorate!
604
+ end
605
+ end
606
+ end
607
+ end