nokogiri 1.6.2.rc1-x64-mingw32

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

Potentially problematic release.


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

Files changed (263) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +26 -0
  3. data/.editorconfig +17 -0
  4. data/.gemtest +0 -0
  5. data/.travis.yml +25 -0
  6. data/CHANGELOG.ja.rdoc +857 -0
  7. data/CHANGELOG.rdoc +880 -0
  8. data/C_CODING_STYLE.rdoc +33 -0
  9. data/Gemfile +21 -0
  10. data/Manifest.txt +371 -0
  11. data/README.ja.rdoc +112 -0
  12. data/README.rdoc +180 -0
  13. data/ROADMAP.md +89 -0
  14. data/Rakefile +351 -0
  15. data/STANDARD_RESPONSES.md +47 -0
  16. data/Y_U_NO_GEMSPEC.md +155 -0
  17. data/bin/nokogiri +78 -0
  18. data/build_all +130 -0
  19. data/dependencies.yml +4 -0
  20. data/ext/nokogiri/depend +358 -0
  21. data/ext/nokogiri/extconf.rb +453 -0
  22. data/ext/nokogiri/html_document.c +170 -0
  23. data/ext/nokogiri/html_document.h +10 -0
  24. data/ext/nokogiri/html_element_description.c +279 -0
  25. data/ext/nokogiri/html_element_description.h +10 -0
  26. data/ext/nokogiri/html_entity_lookup.c +32 -0
  27. data/ext/nokogiri/html_entity_lookup.h +8 -0
  28. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  29. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  30. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  31. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  32. data/ext/nokogiri/nokogiri.c +148 -0
  33. data/ext/nokogiri/nokogiri.h +164 -0
  34. data/ext/nokogiri/xml_attr.c +94 -0
  35. data/ext/nokogiri/xml_attr.h +9 -0
  36. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  37. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  38. data/ext/nokogiri/xml_cdata.c +56 -0
  39. data/ext/nokogiri/xml_cdata.h +9 -0
  40. data/ext/nokogiri/xml_comment.c +54 -0
  41. data/ext/nokogiri/xml_comment.h +9 -0
  42. data/ext/nokogiri/xml_document.c +577 -0
  43. data/ext/nokogiri/xml_document.h +23 -0
  44. data/ext/nokogiri/xml_document_fragment.c +48 -0
  45. data/ext/nokogiri/xml_document_fragment.h +10 -0
  46. data/ext/nokogiri/xml_dtd.c +202 -0
  47. data/ext/nokogiri/xml_dtd.h +10 -0
  48. data/ext/nokogiri/xml_element_content.c +123 -0
  49. data/ext/nokogiri/xml_element_content.h +10 -0
  50. data/ext/nokogiri/xml_element_decl.c +69 -0
  51. data/ext/nokogiri/xml_element_decl.h +9 -0
  52. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  53. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  54. data/ext/nokogiri/xml_entity_decl.c +110 -0
  55. data/ext/nokogiri/xml_entity_decl.h +10 -0
  56. data/ext/nokogiri/xml_entity_reference.c +52 -0
  57. data/ext/nokogiri/xml_entity_reference.h +9 -0
  58. data/ext/nokogiri/xml_io.c +56 -0
  59. data/ext/nokogiri/xml_io.h +11 -0
  60. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  61. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  62. data/ext/nokogiri/xml_namespace.c +78 -0
  63. data/ext/nokogiri/xml_namespace.h +13 -0
  64. data/ext/nokogiri/xml_node.c +1541 -0
  65. data/ext/nokogiri/xml_node.h +13 -0
  66. data/ext/nokogiri/xml_node_set.c +467 -0
  67. data/ext/nokogiri/xml_node_set.h +14 -0
  68. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  69. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  70. data/ext/nokogiri/xml_reader.c +681 -0
  71. data/ext/nokogiri/xml_reader.h +10 -0
  72. data/ext/nokogiri/xml_relax_ng.c +161 -0
  73. data/ext/nokogiri/xml_relax_ng.h +9 -0
  74. data/ext/nokogiri/xml_sax_parser.c +312 -0
  75. data/ext/nokogiri/xml_sax_parser.h +39 -0
  76. data/ext/nokogiri/xml_sax_parser_context.c +262 -0
  77. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  78. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  79. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  80. data/ext/nokogiri/xml_schema.c +205 -0
  81. data/ext/nokogiri/xml_schema.h +9 -0
  82. data/ext/nokogiri/xml_syntax_error.c +63 -0
  83. data/ext/nokogiri/xml_syntax_error.h +13 -0
  84. data/ext/nokogiri/xml_text.c +52 -0
  85. data/ext/nokogiri/xml_text.h +9 -0
  86. data/ext/nokogiri/xml_xpath_context.c +307 -0
  87. data/ext/nokogiri/xml_xpath_context.h +10 -0
  88. data/ext/nokogiri/xslt_stylesheet.c +270 -0
  89. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  90. data/lib/nokogiri.rb +137 -0
  91. data/lib/nokogiri/2.0/nokogiri.so +0 -0
  92. data/lib/nokogiri/2.1/nokogiri.so +0 -0
  93. data/lib/nokogiri/css.rb +27 -0
  94. data/lib/nokogiri/css/node.rb +52 -0
  95. data/lib/nokogiri/css/parser.rb +715 -0
  96. data/lib/nokogiri/css/parser.y +249 -0
  97. data/lib/nokogiri/css/parser_extras.rb +91 -0
  98. data/lib/nokogiri/css/syntax_error.rb +7 -0
  99. data/lib/nokogiri/css/tokenizer.rb +152 -0
  100. data/lib/nokogiri/css/tokenizer.rex +55 -0
  101. data/lib/nokogiri/css/xpath_visitor.rb +219 -0
  102. data/lib/nokogiri/decorators/slop.rb +35 -0
  103. data/lib/nokogiri/html.rb +37 -0
  104. data/lib/nokogiri/html/builder.rb +35 -0
  105. data/lib/nokogiri/html/document.rb +333 -0
  106. data/lib/nokogiri/html/document_fragment.rb +41 -0
  107. data/lib/nokogiri/html/element_description.rb +23 -0
  108. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  109. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  110. data/lib/nokogiri/html/sax/parser.rb +52 -0
  111. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  112. data/lib/nokogiri/html/sax/push_parser.rb +16 -0
  113. data/lib/nokogiri/syntax_error.rb +4 -0
  114. data/lib/nokogiri/version.rb +106 -0
  115. data/lib/nokogiri/xml.rb +73 -0
  116. data/lib/nokogiri/xml/attr.rb +14 -0
  117. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  118. data/lib/nokogiri/xml/builder.rb +443 -0
  119. data/lib/nokogiri/xml/cdata.rb +11 -0
  120. data/lib/nokogiri/xml/character_data.rb +7 -0
  121. data/lib/nokogiri/xml/document.rb +279 -0
  122. data/lib/nokogiri/xml/document_fragment.rb +112 -0
  123. data/lib/nokogiri/xml/dtd.rb +32 -0
  124. data/lib/nokogiri/xml/element_content.rb +36 -0
  125. data/lib/nokogiri/xml/element_decl.rb +13 -0
  126. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  127. data/lib/nokogiri/xml/namespace.rb +13 -0
  128. data/lib/nokogiri/xml/node.rb +982 -0
  129. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  130. data/lib/nokogiri/xml/node_set.rb +355 -0
  131. data/lib/nokogiri/xml/notation.rb +6 -0
  132. data/lib/nokogiri/xml/parse_options.rb +98 -0
  133. data/lib/nokogiri/xml/pp.rb +2 -0
  134. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  135. data/lib/nokogiri/xml/pp/node.rb +56 -0
  136. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  137. data/lib/nokogiri/xml/reader.rb +112 -0
  138. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  139. data/lib/nokogiri/xml/sax.rb +4 -0
  140. data/lib/nokogiri/xml/sax/document.rb +171 -0
  141. data/lib/nokogiri/xml/sax/parser.rb +123 -0
  142. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  143. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  144. data/lib/nokogiri/xml/schema.rb +63 -0
  145. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  146. data/lib/nokogiri/xml/text.rb +9 -0
  147. data/lib/nokogiri/xml/xpath.rb +10 -0
  148. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  149. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  150. data/lib/nokogiri/xslt.rb +56 -0
  151. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  152. data/lib/xsd/xmlparser/nokogiri.rb +102 -0
  153. data/suppressions/README.txt +1 -0
  154. data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
  155. data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
  156. data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
  157. data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
  158. data/tasks/nokogiri.org.rb +24 -0
  159. data/tasks/test.rb +95 -0
  160. data/test/css/test_nthiness.rb +222 -0
  161. data/test/css/test_parser.rb +358 -0
  162. data/test/css/test_tokenizer.rb +198 -0
  163. data/test/css/test_xpath_visitor.rb +96 -0
  164. data/test/decorators/test_slop.rb +16 -0
  165. data/test/files/2ch.html +108 -0
  166. data/test/files/address_book.rlx +12 -0
  167. data/test/files/address_book.xml +10 -0
  168. data/test/files/atom.xml +344 -0
  169. data/test/files/bar/bar.xsd +4 -0
  170. data/test/files/bogus.xml +0 -0
  171. data/test/files/dont_hurt_em_why.xml +422 -0
  172. data/test/files/encoding.html +82 -0
  173. data/test/files/encoding.xhtml +84 -0
  174. data/test/files/exslt.xml +8 -0
  175. data/test/files/exslt.xslt +35 -0
  176. data/test/files/foo/foo.xsd +4 -0
  177. data/test/files/metacharset.html +10 -0
  178. data/test/files/noencoding.html +47 -0
  179. data/test/files/po.xml +32 -0
  180. data/test/files/po.xsd +66 -0
  181. data/test/files/saml/saml20assertion_schema.xsd +283 -0
  182. data/test/files/saml/saml20protocol_schema.xsd +302 -0
  183. data/test/files/saml/xenc_schema.xsd +146 -0
  184. data/test/files/saml/xmldsig_schema.xsd +318 -0
  185. data/test/files/shift_jis.html +10 -0
  186. data/test/files/shift_jis.xml +5 -0
  187. data/test/files/shift_jis_no_charset.html +9 -0
  188. data/test/files/snuggles.xml +3 -0
  189. data/test/files/staff.dtd +10 -0
  190. data/test/files/staff.xml +59 -0
  191. data/test/files/staff.xslt +32 -0
  192. data/test/files/test_document_url/bar.xml +2 -0
  193. data/test/files/test_document_url/document.dtd +4 -0
  194. data/test/files/test_document_url/document.xml +6 -0
  195. data/test/files/tlm.html +850 -0
  196. data/test/files/to_be_xincluded.xml +2 -0
  197. data/test/files/valid_bar.xml +2 -0
  198. data/test/files/xinclude.xml +4 -0
  199. data/test/helper.rb +164 -0
  200. data/test/html/sax/test_parser.rb +141 -0
  201. data/test/html/sax/test_parser_context.rb +46 -0
  202. data/test/html/test_builder.rb +164 -0
  203. data/test/html/test_document.rb +619 -0
  204. data/test/html/test_document_encoding.rb +148 -0
  205. data/test/html/test_document_fragment.rb +261 -0
  206. data/test/html/test_element_description.rb +105 -0
  207. data/test/html/test_named_characters.rb +14 -0
  208. data/test/html/test_node.rb +196 -0
  209. data/test/html/test_node_encoding.rb +27 -0
  210. data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +14 -0
  211. data/test/namespaces/test_namespaces_in_builder_doc.rb +75 -0
  212. data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
  213. data/test/namespaces/test_namespaces_in_created_doc.rb +75 -0
  214. data/test/namespaces/test_namespaces_in_parsed_doc.rb +66 -0
  215. data/test/test_convert_xpath.rb +135 -0
  216. data/test/test_css_cache.rb +45 -0
  217. data/test/test_encoding_handler.rb +46 -0
  218. data/test/test_memory_leak.rb +156 -0
  219. data/test/test_nokogiri.rb +138 -0
  220. data/test/test_reader.rb +558 -0
  221. data/test/test_soap4r_sax.rb +52 -0
  222. data/test/test_xslt_transforms.rb +279 -0
  223. data/test/xml/node/test_save_options.rb +28 -0
  224. data/test/xml/node/test_subclass.rb +44 -0
  225. data/test/xml/sax/test_parser.rb +382 -0
  226. data/test/xml/sax/test_parser_context.rb +115 -0
  227. data/test/xml/sax/test_push_parser.rb +157 -0
  228. data/test/xml/test_attr.rb +64 -0
  229. data/test/xml/test_attribute_decl.rb +86 -0
  230. data/test/xml/test_builder.rb +315 -0
  231. data/test/xml/test_c14n.rb +161 -0
  232. data/test/xml/test_cdata.rb +48 -0
  233. data/test/xml/test_comment.rb +29 -0
  234. data/test/xml/test_document.rb +934 -0
  235. data/test/xml/test_document_encoding.rb +28 -0
  236. data/test/xml/test_document_fragment.rb +228 -0
  237. data/test/xml/test_dtd.rb +187 -0
  238. data/test/xml/test_dtd_encoding.rb +33 -0
  239. data/test/xml/test_element_content.rb +56 -0
  240. data/test/xml/test_element_decl.rb +73 -0
  241. data/test/xml/test_entity_decl.rb +122 -0
  242. data/test/xml/test_entity_reference.rb +245 -0
  243. data/test/xml/test_namespace.rb +95 -0
  244. data/test/xml/test_node.rb +1155 -0
  245. data/test/xml/test_node_attributes.rb +113 -0
  246. data/test/xml/test_node_encoding.rb +107 -0
  247. data/test/xml/test_node_inheritance.rb +32 -0
  248. data/test/xml/test_node_reparenting.rb +374 -0
  249. data/test/xml/test_node_set.rb +755 -0
  250. data/test/xml/test_parse_options.rb +64 -0
  251. data/test/xml/test_processing_instruction.rb +30 -0
  252. data/test/xml/test_reader_encoding.rb +142 -0
  253. data/test/xml/test_relax_ng.rb +60 -0
  254. data/test/xml/test_schema.rb +129 -0
  255. data/test/xml/test_syntax_error.rb +12 -0
  256. data/test/xml/test_text.rb +45 -0
  257. data/test/xml/test_unparented_node.rb +422 -0
  258. data/test/xml/test_xinclude.rb +83 -0
  259. data/test/xml/test_xpath.rb +376 -0
  260. data/test/xslt/test_custom_functions.rb +133 -0
  261. data/test/xslt/test_exception_handling.rb +37 -0
  262. data/test_all +81 -0
  263. metadata +601 -0
@@ -0,0 +1,161 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestC14N < Nokogiri::TestCase
6
+ # http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
7
+ def test_3_1
8
+ doc = Nokogiri.XML <<-eoxml
9
+ <?xml version="1.0"?>
10
+
11
+ <?xml-stylesheet href="doc.xsl"
12
+ type="text/xsl" ?>
13
+
14
+ <!DOCTYPE doc SYSTEM "doc.dtd">
15
+
16
+ <doc>Hello, world!<!-- Comment 1 --></doc>
17
+
18
+ <?pi-without-data ?>
19
+
20
+ <!-- Comment 2 -->
21
+
22
+ <!-- Comment 3 -->
23
+ eoxml
24
+
25
+ c14n = doc.canonicalize
26
+ assert_no_match(/version=/, c14n)
27
+ assert_match(/Hello, world/, c14n)
28
+ assert_no_match(/Comment/, c14n)
29
+ c14n = doc.canonicalize(nil, nil, true)
30
+ assert_match(/Comment/, c14n)
31
+ c14n = doc.canonicalize(nil, nil, false)
32
+ assert_no_match(/Comment/, c14n)
33
+ end
34
+
35
+ def test_exclude_block_params
36
+ xml = '<a><b></b></a>'
37
+ doc = Nokogiri.XML xml
38
+
39
+ list = []
40
+ doc.canonicalize do |node, parent|
41
+ list << [node, parent]
42
+ true
43
+ end
44
+ if Nokogiri.jruby?
45
+ assert_equal(
46
+ ['a', 'document', 'document', nil, 'b', 'a'],
47
+ list.flatten.map { |x| x ? x.name : x }
48
+ )
49
+ else
50
+ assert_equal(
51
+ ['a', 'document', 'document', nil, 'b', 'a', 'a', 'document'],
52
+ list.flatten.map { |x| x ? x.name : x }
53
+ )
54
+ end
55
+ end
56
+
57
+ def test_exclude_block_true
58
+ xml = '<a><b></b></a>'
59
+ doc = Nokogiri.XML xml
60
+
61
+ c14n = doc.canonicalize do |node, parent|
62
+ true
63
+ end
64
+ assert_equal xml, c14n
65
+ end
66
+
67
+ def test_exclude_block_false
68
+ xml = '<a><b></b></a>'
69
+ doc = Nokogiri.XML xml
70
+
71
+ c14n = doc.canonicalize do |node, parent|
72
+ false
73
+ end
74
+ assert_equal '', c14n
75
+ end
76
+
77
+ def test_exclude_block_nil
78
+ xml = '<a><b></b></a>'
79
+ doc = Nokogiri.XML xml
80
+
81
+ c14n = doc.canonicalize do |node, parent|
82
+ nil
83
+ end
84
+ assert_equal '', c14n
85
+ end
86
+
87
+ def test_exclude_block_object
88
+ xml = '<a><b></b></a>'
89
+ doc = Nokogiri.XML xml
90
+
91
+ c14n = doc.canonicalize do |node, parent|
92
+ Object.new
93
+ end
94
+ assert_equal xml, c14n
95
+ end
96
+
97
+ def test_c14n_node
98
+ xml = '<a><b><c></c></b></a>'
99
+ doc = Nokogiri.XML xml
100
+ c14n = doc.at_xpath('//b').canonicalize
101
+ assert_equal '<b><c></c></b>', c14n
102
+ end
103
+
104
+ def test_c14n_modes
105
+ # http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping
106
+
107
+ doc1 = Nokogiri.XML <<-eoxml
108
+ <n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org">
109
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
110
+ <n3:stuff xmlns:n3="ftp://example.org"/>
111
+ </n1:elem2>
112
+ </n0:local>
113
+ eoxml
114
+ doc2 = Nokogiri.XML <<-eoxml
115
+ <n2:pdu xmlns:n1="http://example.com"
116
+ xmlns:n2="http://foo.example"
117
+ xml:lang="fr"
118
+ xml:space="retain">
119
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
120
+ <n3:stuff xmlns:n3="ftp://example.org"/>
121
+ </n1:elem2>
122
+ </n2:pdu>
123
+ eoxml
124
+
125
+ c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
126
+ assert_equal '<n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en">
127
+ <n3:stuff></n3:stuff>
128
+ </n1:elem2>', c14n
129
+ c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
130
+ assert_equal '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en" xml:space="retain">
131
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
132
+ </n1:elem2>', c14n
133
+
134
+ excl_c14n = '<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
135
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
136
+ </n1:elem2>'
137
+ c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
138
+ assert_equal excl_c14n, c14n
139
+ c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
140
+ assert_equal excl_c14n, c14n
141
+
142
+ c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2'])
143
+ assert_equal '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">
144
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
145
+ </n1:elem2>', c14n
146
+
147
+ end
148
+
149
+ def test_wrong_params
150
+ xml = '<a><b></b></a>'
151
+ doc = Nokogiri.XML xml
152
+
153
+ assert_raise(TypeError){ doc.canonicalize :wrong_type }
154
+ assert_raise(TypeError){ doc.canonicalize nil, :wrong_type }
155
+ doc.canonicalize nil, nil, :wrong_type
156
+ end
157
+
158
+
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,48 @@
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
+ assert 100.times { CDATA.new(@xml, "asdfasdf") }
33
+ end
34
+
35
+ def test_content=
36
+ node = CDATA.new(@xml, 'foo')
37
+ assert_equal('foo', node.content)
38
+
39
+ node.content = '& <foo> &amp;'
40
+ assert_equal('& <foo> &amp;', node.content)
41
+ assert_equal('<![CDATA[& <foo> &amp;]]>', node.to_xml)
42
+
43
+ node.content = 'foo ]]> bar'
44
+ assert_equal('foo ]]> bar', node.content)
45
+ end
46
+ end
47
+ end
48
+ 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,934 @@
1
+ require "helper"
2
+
3
+ require 'uri'
4
+
5
+ module Nokogiri
6
+ module XML
7
+ class TestDocument < Nokogiri::TestCase
8
+ URI = if URI.const_defined?(:DEFAULT_PARSER)
9
+ ::URI::DEFAULT_PARSER
10
+ else
11
+ ::URI
12
+ end
13
+
14
+ def setup
15
+ super
16
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
17
+ end
18
+
19
+ def test_dtd_with_empty_internal_subset
20
+ doc = Nokogiri::XML <<-eoxml
21
+ <?xml version="1.0"?>
22
+ <!DOCTYPE people >
23
+ <people>
24
+ </people>
25
+ eoxml
26
+ assert doc.root
27
+ end
28
+
29
+ # issue #1005
30
+ def test_strict_parsing_empty_doc_should_raise_exception
31
+ assert_raises(SyntaxError) do
32
+ Nokogiri::XML(StringIO.new('')) { |c| c.strict }
33
+ end
34
+ end
35
+
36
+ # issue #838
37
+ def test_document_with_invalid_prolog
38
+ doc = Nokogiri::XML '<? ?>'
39
+ assert_empty doc.content
40
+ end
41
+
42
+ # issue #837
43
+ def test_document_with_refentity
44
+ doc = Nokogiri::XML '&amp;'
45
+ assert_equal '', doc.content
46
+ end
47
+
48
+ # issue #835
49
+ def test_manually_adding_reference_entities
50
+ d = Nokogiri::XML::Document.new
51
+ root = Nokogiri::XML::Element.new('bar', d)
52
+ txt = Nokogiri::XML::Text.new('foo', d)
53
+ ent = Nokogiri::XML::EntityReference.new(d, '#8217')
54
+ root << txt
55
+ root << ent
56
+ d << root
57
+ assert_match /&#8217;/, d.to_html
58
+ end
59
+
60
+ def test_document_with_initial_space
61
+ doc = Nokogiri::XML(" <?xml version='1.0' encoding='utf-8' ?><first \>")
62
+ assert_equal 2, doc.children.size
63
+ end
64
+
65
+ def test_root_set_to_nil
66
+ @xml.root = nil
67
+ assert_equal nil, @xml.root
68
+ end
69
+
70
+ def test_million_laugh_attach
71
+ doc = Nokogiri::XML '<?xml version="1.0"?>
72
+ <!DOCTYPE lolz [
73
+ <!ENTITY lol "lol">
74
+ <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
75
+ <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
76
+ <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
77
+ <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
78
+ <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
79
+ <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
80
+ <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
81
+ <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
82
+ ]>
83
+ <lolz>&lol9;</lolz>'
84
+ assert_not_nil doc
85
+ end
86
+
87
+ def test_million_laugh_attach_2
88
+ doc = Nokogiri::XML '<?xml version="1.0" encoding="UTF-8"?>
89
+ <!DOCTYPE member [
90
+ <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
91
+ <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
92
+ <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
93
+ <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
94
+ <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
95
+ <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
96
+ <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
97
+ ]>
98
+ <member>
99
+ &a;
100
+ </member>'
101
+ assert_not_nil doc
102
+ end
103
+
104
+ def test_ignore_unknown_namespace
105
+ doc = Nokogiri::XML(<<-eoxml)
106
+ <xml>
107
+ <unknown:foo xmlns='http://hello.com/' />
108
+ <bar />
109
+ </xml>
110
+ eoxml
111
+ if Nokogiri.jruby?
112
+ refute doc.xpath('//foo').first.namespace # assert that the namespace is nil
113
+ end
114
+ refute_empty doc.xpath('//bar'), "bar wasn't found in the document" # bar should be part of the doc
115
+ end
116
+
117
+ def test_collect_namespaces
118
+ doc = Nokogiri::XML(<<-eoxml)
119
+ <xml>
120
+ <foo xmlns='hello'>
121
+ <bar xmlns:foo='world' />
122
+ </foo>
123
+ </xml>
124
+ eoxml
125
+ assert_equal({"xmlns"=>"hello", "xmlns:foo"=>"world"},
126
+ doc.collect_namespaces)
127
+ end
128
+
129
+ def test_subclass_initialize_modify # testing a segv
130
+ Class.new(Nokogiri::XML::Document) {
131
+ def initialize
132
+ super
133
+ body_node = Nokogiri::XML::Node.new "body", self
134
+ body_node.content = "stuff"
135
+ self.root = body_node
136
+ end
137
+ }.new
138
+ end
139
+
140
+ def test_create_text_node
141
+ txt = @xml.create_text_node 'foo'
142
+ assert_instance_of Nokogiri::XML::Text, txt
143
+ assert_equal 'foo', txt.text
144
+ assert_equal @xml, txt.document
145
+ end
146
+
147
+ def test_create_text_node_with_block
148
+ @xml.create_text_node 'foo' do |txt|
149
+ assert_instance_of Nokogiri::XML::Text, txt
150
+ assert_equal 'foo', txt.text
151
+ assert_equal @xml, txt.document
152
+ end
153
+ end
154
+
155
+ def test_create_element
156
+ elm = @xml.create_element('foo')
157
+ assert_instance_of Nokogiri::XML::Element, elm
158
+ assert_equal 'foo', elm.name
159
+ assert_equal @xml, elm.document
160
+ end
161
+
162
+ def test_create_element_with_block
163
+ @xml.create_element('foo') do |elm|
164
+ assert_instance_of Nokogiri::XML::Element, elm
165
+ assert_equal 'foo', elm.name
166
+ assert_equal @xml, elm.document
167
+ end
168
+ end
169
+
170
+ def test_create_element_with_attributes
171
+ elm = @xml.create_element('foo',:a => "1")
172
+ assert_instance_of Nokogiri::XML::Element, elm
173
+ assert_instance_of Nokogiri::XML::Attr, elm.attributes["a"]
174
+ assert_equal "1", elm["a"]
175
+ end
176
+
177
+ def test_create_element_with_namespace
178
+ elm = @xml.create_element('foo',:'xmlns:foo' => 'http://tenderlovemaking.com')
179
+ assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:foo']
180
+ end
181
+
182
+ def test_create_element_with_hyphenated_namespace
183
+ elm = @xml.create_element('foo',:'xmlns:SOAP-ENC' => 'http://tenderlovemaking.com')
184
+ assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:SOAP-ENC']
185
+ end
186
+
187
+ def test_create_element_with_content
188
+ elm = @xml.create_element('foo',"needs more xml/violence")
189
+ assert_equal "needs more xml/violence", elm.content
190
+ end
191
+
192
+ def test_create_cdata
193
+ cdata = @xml.create_cdata("abc")
194
+ assert_instance_of Nokogiri::XML::CDATA, cdata
195
+ assert_equal "abc", cdata.content
196
+ end
197
+
198
+ def test_create_cdata_with_block
199
+ @xml.create_cdata("abc") do |cdata|
200
+ assert_instance_of Nokogiri::XML::CDATA, cdata
201
+ assert_equal "abc", cdata.content
202
+ end
203
+ end
204
+
205
+ def test_create_comment
206
+ comment = @xml.create_comment("abc")
207
+ assert_instance_of Nokogiri::XML::Comment, comment
208
+ assert_equal "abc", comment.content
209
+ end
210
+
211
+ def test_create_comment_with_block
212
+ @xml.create_comment("abc") do |comment|
213
+ assert_instance_of Nokogiri::XML::Comment, comment
214
+ assert_equal "abc", comment.content
215
+ end
216
+ end
217
+
218
+ def test_pp
219
+ out = StringIO.new('')
220
+ ::PP.pp @xml, out
221
+ assert_operator out.string.length, :>, 0
222
+ end
223
+
224
+ def test_create_internal_subset_on_existing_subset
225
+ assert_not_nil @xml.internal_subset
226
+ assert_raises(RuntimeError) do
227
+ @xml.create_internal_subset('staff', nil, 'staff.dtd')
228
+ end
229
+ end
230
+
231
+ def test_create_internal_subset
232
+ xml = Nokogiri::XML('<root />')
233
+ assert_nil xml.internal_subset
234
+
235
+ xml.create_internal_subset('name', nil, 'staff.dtd')
236
+ ss = xml.internal_subset
237
+ assert_equal 'name', ss.name
238
+ assert_nil ss.external_id
239
+ assert_equal 'staff.dtd', ss.system_id
240
+ end
241
+
242
+ def test_external_subset
243
+ assert_nil @xml.external_subset
244
+ Dir.chdir(ASSETS_DIR) do
245
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
246
+ cfg.dtdload
247
+ }
248
+ end
249
+ assert @xml.external_subset
250
+ end
251
+
252
+ def test_create_external_subset_fails_with_existing_subset
253
+ assert_nil @xml.external_subset
254
+ Dir.chdir(ASSETS_DIR) do
255
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
256
+ cfg.dtdload
257
+ }
258
+ end
259
+ assert @xml.external_subset
260
+
261
+ assert_raises(RuntimeError) do
262
+ @xml.create_external_subset('staff', nil, 'staff.dtd')
263
+ end
264
+ end
265
+
266
+ def test_create_external_subset
267
+ dtd = @xml.create_external_subset('staff', nil, 'staff.dtd')
268
+ assert_nil dtd.external_id
269
+ assert_equal 'staff.dtd', dtd.system_id
270
+ assert_equal 'staff', dtd.name
271
+ assert_equal dtd, @xml.external_subset
272
+ end
273
+
274
+ def test_version
275
+ assert_equal '1.0', @xml.version
276
+ end
277
+
278
+ def test_add_namespace
279
+ assert_raise NoMethodError do
280
+ @xml.add_namespace('foo', 'bar')
281
+ end
282
+ end
283
+
284
+ def test_attributes
285
+ assert_raise NoMethodError do
286
+ @xml.attributes
287
+ end
288
+ end
289
+
290
+ def test_namespace
291
+ assert_raise NoMethodError do
292
+ @xml.namespace
293
+ end
294
+ end
295
+
296
+ def test_namespace_definitions
297
+ assert_raise NoMethodError do
298
+ @xml.namespace_definitions
299
+ end
300
+ end
301
+
302
+ def test_line
303
+ assert_raise NoMethodError do
304
+ @xml.line
305
+ end
306
+ end
307
+
308
+ def test_empty_node_converted_to_html_is_not_self_closing
309
+ doc = Nokogiri::XML('<a></a>')
310
+ assert_equal "<a></a>", doc.inner_html
311
+ end
312
+
313
+ def test_fragment
314
+ fragment = @xml.fragment
315
+ assert_equal 0, fragment.children.length
316
+ end
317
+
318
+ def test_add_child_fragment_with_single_node
319
+ doc = Nokogiri::XML::Document.new
320
+ fragment = doc.fragment('<hello />')
321
+ doc.add_child fragment
322
+ assert_equal '/hello', doc.at('//hello').path
323
+ assert_equal 'hello', doc.root.name
324
+ end
325
+
326
+ def test_add_child_fragment_with_multiple_nodes
327
+ doc = Nokogiri::XML::Document.new
328
+ fragment = doc.fragment('<hello /><goodbye />')
329
+ assert_raises(RuntimeError) do
330
+ doc.add_child fragment
331
+ end
332
+ end
333
+
334
+ def test_add_child_with_multiple_roots
335
+ assert_raises(RuntimeError) do
336
+ @xml << Node.new('foo', @xml)
337
+ end
338
+ end
339
+
340
+ def test_add_child_with_string
341
+ doc = Nokogiri::XML::Document.new
342
+ doc.add_child "<div>quack!</div>"
343
+ assert_equal 1, doc.root.children.length
344
+ assert_equal "quack!", doc.root.children.first.content
345
+ end
346
+
347
+ def test_prepend
348
+ doc = Nokogiri::XML('<root>')
349
+
350
+ node_set = doc.root.prepend_child '<branch/>'
351
+ assert_equal %w[branch], node_set.map(&:name)
352
+
353
+ branch = doc.at('//branch')
354
+
355
+ leaves = %w[leaf1 leaf2 leaf3]
356
+ leaves.each { |name|
357
+ branch.prepend_child('<%s/>' % name)
358
+ }
359
+ assert_equal leaves.length, branch.children.length
360
+ assert_equal leaves.reverse, branch.children.map(&:name)
361
+ end
362
+
363
+ def test_prepend_child_fragment_with_single_node
364
+ doc = Nokogiri::XML::Document.new
365
+ fragment = doc.fragment('<hello />')
366
+ doc.prepend_child fragment
367
+ assert_equal '/hello', doc.at('//hello').path
368
+ assert_equal 'hello', doc.root.name
369
+ end
370
+
371
+ def test_prepend_child_fragment_with_multiple_nodes
372
+ doc = Nokogiri::XML::Document.new
373
+ fragment = doc.fragment('<hello /><goodbye />')
374
+ assert_raises(RuntimeError) do
375
+ doc.prepend_child fragment
376
+ end
377
+ end
378
+
379
+ def test_prepend_child_fragment_with_multiple_nodes
380
+ doc = Nokogiri::XML::Document.new
381
+ fragment = doc.fragment('<hello /><goodbye />')
382
+ assert_raises(RuntimeError) do
383
+ doc.prepend_child fragment
384
+ end
385
+ end
386
+
387
+ def test_prepend_child_with_multiple_roots
388
+ assert_raises(RuntimeError) do
389
+ @xml.prepend_child Node.new('foo', @xml)
390
+ end
391
+ end
392
+
393
+ def test_prepend_child_with_string
394
+ doc = Nokogiri::XML::Document.new
395
+ doc.prepend_child "<div>quack!</div>"
396
+ assert_equal 1, doc.root.children.length
397
+ assert_equal "quack!", doc.root.children.first.content
398
+ end
399
+
400
+ def test_move_root_to_document_with_no_root
401
+ sender = Nokogiri::XML('<root>foo</root>')
402
+ newdoc = Nokogiri::XML::Document.new
403
+ newdoc.root = sender.root
404
+ end
405
+
406
+ def test_move_root_with_existing_root_gets_gcd
407
+ doc = Nokogiri::XML('<root>test</root>')
408
+ doc2 = Nokogiri::XML("<root>#{'x' * 5000000}</root>")
409
+ doc2.root = doc.root
410
+ end
411
+
412
+ def test_validate
413
+ if Nokogiri.uses_libxml?
414
+ assert_equal 44, @xml.validate.length
415
+ else
416
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) {|cfg| cfg.dtdvalid}
417
+ assert_equal 40, xml.validate.length
418
+ end
419
+ end
420
+
421
+ def test_validate_no_internal_subset
422
+ doc = Nokogiri::XML('<test/>')
423
+ assert_nil doc.validate
424
+ end
425
+
426
+ def test_clone
427
+ assert @xml.clone
428
+ end
429
+
430
+ def test_document_should_not_have_default_ns
431
+ doc = Nokogiri::XML::Document.new
432
+
433
+ assert_raises NoMethodError do
434
+ doc.default_namespace = 'http://innernet.com/'
435
+ end
436
+
437
+ assert_raises NoMethodError do
438
+ doc.add_namespace_definition('foo', 'bar')
439
+ end
440
+ end
441
+
442
+ def test_parse_handles_nil_gracefully
443
+ @doc = Nokogiri::XML::Document.parse(nil)
444
+ assert_instance_of Nokogiri::XML::Document, @doc
445
+ end
446
+
447
+ def test_parse_takes_block
448
+ options = nil
449
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
450
+ options = cfg
451
+ end
452
+ assert options
453
+ end
454
+
455
+ def test_parse_yields_parse_options
456
+ options = nil
457
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
458
+ options = cfg
459
+ options.nonet.nowarning.dtdattr
460
+ end
461
+ assert options.nonet?
462
+ assert options.nowarning?
463
+ assert options.dtdattr?
464
+ end
465
+
466
+ def test_XML_takes_block
467
+ options = nil
468
+ Nokogiri::XML(File.read(XML_FILE), XML_FILE) do |cfg|
469
+ options = cfg
470
+ options.nonet.nowarning.dtdattr
471
+ end
472
+ assert options.nonet?
473
+ assert options.nowarning?
474
+ assert options.dtdattr?
475
+ end
476
+
477
+ def test_subclass
478
+ klass = Class.new(Nokogiri::XML::Document)
479
+ doc = klass.new
480
+ assert_instance_of klass, doc
481
+ end
482
+
483
+ def test_subclass_initialize
484
+ klass = Class.new(Nokogiri::XML::Document) do
485
+ attr_accessor :initialized_with
486
+
487
+ def initialize(*args)
488
+ @initialized_with = args
489
+ end
490
+ end
491
+ doc = klass.new("1.0", 1)
492
+ assert_equal ["1.0", 1], doc.initialized_with
493
+ end
494
+
495
+ def test_subclass_dup
496
+ klass = Class.new(Nokogiri::XML::Document)
497
+ doc = klass.new.dup
498
+ assert_instance_of klass, doc
499
+ end
500
+
501
+ def test_subclass_parse
502
+ klass = Class.new(Nokogiri::XML::Document)
503
+ doc = klass.parse(File.read(XML_FILE))
504
+ # lame hack uses root to avoid comparing DOCTYPE tags which can appear out of order.
505
+ # I should really finish lorax and use that here.
506
+ assert_equal @xml.root.to_s, doc.root.to_s
507
+ assert_instance_of klass, doc
508
+ end
509
+
510
+ def test_document_parse_method
511
+ xml = Nokogiri::XML::Document.parse(File.read(XML_FILE))
512
+ # lame hack uses root to avoid comparing DOCTYPE tags which can appear out of order.
513
+ # I should really finish lorax and use that here.
514
+ assert_equal @xml.root.to_s, xml.root.to_s
515
+ end
516
+
517
+ def test_encoding=
518
+ @xml.encoding = 'UTF-8'
519
+ assert_match 'UTF-8', @xml.to_xml
520
+
521
+ @xml.encoding = 'EUC-JP'
522
+ assert_match 'EUC-JP', @xml.to_xml
523
+ end
524
+
525
+ def test_namespace_should_not_exist
526
+ assert_raises(NoMethodError) {
527
+ @xml.namespace
528
+ }
529
+ end
530
+
531
+ def test_non_existant_function
532
+ # WTF. I don't know why this is different between MRI and Jruby
533
+ # They should be the same... Either way, raising an exception
534
+ # is the correct thing to do.
535
+ exception = RuntimeError
536
+
537
+ if !Nokogiri.uses_libxml? || (Nokogiri.uses_libxml? && Nokogiri::VERSION_INFO['libxml']['platform'] == 'jruby')
538
+ exception = Nokogiri::XML::XPath::SyntaxError
539
+ end
540
+
541
+ assert_raises(exception) {
542
+ @xml.xpath('//name[foo()]')
543
+ }
544
+ end
545
+
546
+ def test_xpath_syntax_error
547
+ assert_raises(Nokogiri::XML::XPath::SyntaxError) do
548
+ @xml.xpath('\\')
549
+ end
550
+ end
551
+
552
+ def test_ancestors
553
+ assert_equal 0, @xml.ancestors.length
554
+ end
555
+
556
+ def test_root_node_parent_is_document
557
+ parent = @xml.root.parent
558
+ assert_equal @xml, parent
559
+ assert_instance_of Nokogiri::XML::Document, parent
560
+ end
561
+
562
+ def test_xmlns_is_automatically_registered
563
+ doc = Nokogiri::XML(<<-eoxml)
564
+ <root xmlns="http://tenderlovemaking.com/">
565
+ <foo>
566
+ bar
567
+ </foo>
568
+ </root>
569
+ eoxml
570
+ assert_equal 1, doc.css('xmlns|foo').length
571
+ assert_equal 1, doc.css('foo').length
572
+ assert_equal 0, doc.css('|foo').length
573
+ assert_equal 1, doc.xpath('//xmlns:foo').length
574
+ assert_equal 1, doc.search('xmlns|foo').length
575
+ assert_equal 1, doc.search('//xmlns:foo').length
576
+ assert doc.at('xmlns|foo')
577
+ assert doc.at('//xmlns:foo')
578
+ assert doc.at('foo')
579
+ end
580
+
581
+ def test_xmlns_is_registered_for_nodesets
582
+ doc = Nokogiri::XML(<<-eoxml)
583
+ <root xmlns="http://tenderlovemaking.com/">
584
+ <foo>
585
+ <bar>
586
+ baz
587
+ </bar>
588
+ </foo>
589
+ </root>
590
+ eoxml
591
+ assert_equal 1, doc.css('xmlns|foo').css('xmlns|bar').length
592
+ assert_equal 1, doc.css('foo').css('bar').length
593
+ assert_equal 1, doc.xpath('//xmlns:foo').xpath('./xmlns:bar').length
594
+ assert_equal 1, doc.search('xmlns|foo').search('xmlns|bar').length
595
+ assert_equal 1, doc.search('//xmlns:foo').search('./xmlns:bar').length
596
+ end
597
+
598
+ def test_to_xml_with_indent
599
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
600
+ doc = Nokogiri::XML(doc.to_xml(:indent => 5))
601
+
602
+ assert_indent 5, doc
603
+ end
604
+
605
+ def test_write_xml_to_with_indent
606
+ io = StringIO.new
607
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
608
+ doc.write_xml_to io, :indent => 5
609
+ io.rewind
610
+ doc = Nokogiri::XML(io.read)
611
+ assert_indent 5, doc
612
+ end
613
+
614
+ # wtf... osx's libxml sucks.
615
+ unless !Nokogiri.uses_libxml? || Nokogiri::LIBXML_VERSION =~ /^2\.6\./
616
+ def test_encoding
617
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
618
+ assert_equal 'UTF-8', xml.encoding
619
+ end
620
+ end
621
+
622
+ def test_memory_explosion_on_invalid_xml
623
+ doc = Nokogiri::XML("<<<")
624
+ refute_nil doc
625
+ refute_empty doc.errors
626
+ end
627
+
628
+ def test_document_has_errors
629
+ doc = Nokogiri::XML(<<-eoxml)
630
+ <foo><bar></foo>
631
+ eoxml
632
+ assert doc.errors.length > 0
633
+ doc.errors.each do |error|
634
+ assert_match error.message, error.inspect
635
+ assert_match error.message, error.to_s
636
+ end
637
+ end
638
+
639
+ def test_strict_document_throws_syntax_error
640
+ assert_raises(Nokogiri::XML::SyntaxError) {
641
+ Nokogiri::XML('<foo><bar></foo>', nil, nil, 0)
642
+ }
643
+
644
+ assert_raises(Nokogiri::XML::SyntaxError) {
645
+ Nokogiri::XML('<foo><bar></foo>') { |cfg|
646
+ cfg.strict
647
+ }
648
+ }
649
+
650
+ assert_raises(Nokogiri::XML::SyntaxError) {
651
+ Nokogiri::XML(StringIO.new('<foo><bar></foo>')) { |cfg|
652
+ cfg.strict
653
+ }
654
+ }
655
+ end
656
+
657
+ def test_XML_function
658
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
659
+ assert xml.xml?
660
+ end
661
+
662
+ def test_url
663
+ assert @xml.url
664
+ assert_equal XML_FILE, URI.unescape(@xml.url).sub('file:///', '')
665
+ end
666
+
667
+ def test_document_parent
668
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
669
+ assert_raises(NoMethodError) {
670
+ xml.parent
671
+ }
672
+ end
673
+
674
+ def test_document_name
675
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
676
+ assert_equal 'document', xml.name
677
+ end
678
+
679
+ def test_parse_can_take_io
680
+ xml = nil
681
+ File.open(XML_FILE, 'rb') { |f|
682
+ xml = Nokogiri::XML(f)
683
+ }
684
+ assert xml.xml?
685
+ set = xml.search('//employee')
686
+ assert set.length > 0
687
+ end
688
+
689
+ def test_parsing_empty_io
690
+ doc = Nokogiri::XML.parse(StringIO.new(''))
691
+ refute_nil doc
692
+ end
693
+
694
+ def test_search_on_empty_documents
695
+ doc = Nokogiri::XML::Document.new
696
+ ns = doc.search('//foo')
697
+ assert_equal 0, ns.length
698
+
699
+ ns = doc.css('foo')
700
+ assert_equal 0, ns.length
701
+
702
+ ns = doc.xpath('//foo')
703
+ assert_equal 0, ns.length
704
+ end
705
+
706
+ def test_bad_xpath_raises_syntax_error
707
+ assert_raises(XML::XPath::SyntaxError) {
708
+ @xml.xpath('\\')
709
+ }
710
+ end
711
+
712
+ def test_find_with_namespace
713
+ doc = Nokogiri::XML.parse(<<-eoxml)
714
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
715
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
716
+ </x>
717
+ eoxml
718
+
719
+ ctx = Nokogiri::XML::XPathContext.new(doc)
720
+ ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
721
+ set = ctx.evaluate('//tenderlove:foo')
722
+ assert_equal 1, set.length
723
+ assert_equal 'foo', set.first.name
724
+
725
+ # It looks like only the URI is important:
726
+ ctx = Nokogiri::XML::XPathContext.new(doc)
727
+ ctx.register_ns 'america', 'http://tenderlovemaking.com/'
728
+ set = ctx.evaluate('//america:foo')
729
+ assert_equal 1, set.length
730
+ assert_equal 'foo', set.first.name
731
+
732
+ # Its so important that a missing slash will cause it to return nothing
733
+ ctx = Nokogiri::XML::XPathContext.new(doc)
734
+ ctx.register_ns 'america', 'http://tenderlovemaking.com'
735
+ set = ctx.evaluate('//america:foo')
736
+ assert_equal 0, set.length
737
+ end
738
+
739
+ def test_xml?
740
+ assert @xml.xml?
741
+ end
742
+
743
+ def test_document
744
+ assert @xml.document
745
+ end
746
+
747
+ def test_singleton_methods
748
+ assert node_set = @xml.search('//name')
749
+ assert node_set.length > 0
750
+ node = node_set.first
751
+ def node.test
752
+ 'test'
753
+ end
754
+ assert node_set = @xml.search('//name')
755
+ assert_equal 'test', node_set.first.test
756
+ end
757
+
758
+ def test_multiple_search
759
+ assert node_set = @xml.search('//employee', '//name')
760
+ employees = @xml.search('//employee')
761
+ names = @xml.search('//name')
762
+ assert_equal(employees.length + names.length, node_set.length)
763
+ end
764
+
765
+ def test_node_set_index
766
+ assert node_set = @xml.search('//employee')
767
+
768
+ assert_equal(5, node_set.length)
769
+ assert node_set[4]
770
+ assert_nil node_set[5]
771
+ end
772
+
773
+ def test_search
774
+ assert node_set = @xml.search('//employee')
775
+
776
+ assert_equal(5, node_set.length)
777
+
778
+ node_set.each do |node|
779
+ assert_equal('employee', node.name)
780
+ end
781
+ end
782
+
783
+ def test_dump
784
+ assert @xml.serialize
785
+ assert @xml.to_xml
786
+ end
787
+
788
+ def test_dup
789
+ dup = @xml.dup
790
+ assert_instance_of Nokogiri::XML::Document, dup
791
+ assert dup.xml?, 'duplicate should be xml'
792
+ end
793
+
794
+ def test_new
795
+ doc = nil
796
+ doc = Nokogiri::XML::Document.new
797
+ assert doc
798
+ assert doc.xml?
799
+ assert_nil doc.root
800
+ end
801
+
802
+ def test_set_root
803
+ doc = nil
804
+ doc = Nokogiri::XML::Document.new
805
+ assert doc
806
+ assert doc.xml?
807
+ assert_nil doc.root
808
+ node = Nokogiri::XML::Node.new("b", doc) { |n|
809
+ n.content = 'hello world'
810
+ }
811
+ assert_equal('hello world', node.content)
812
+ doc.root = node
813
+ assert_equal(node, doc.root)
814
+ end
815
+
816
+ def test_remove_namespaces
817
+ doc = Nokogiri::XML <<-EOX
818
+ <root xmlns:a="http://a.flavorjon.es/" xmlns:b="http://b.flavorjon.es/">
819
+ <a:foo>hello from a</a:foo>
820
+ <b:foo>hello from b</b:foo>
821
+ <container xmlns:c="http://c.flavorjon.es/">
822
+ <c:foo c:attr='attr-value'>hello from c</c:foo>
823
+ </container>
824
+ </root>
825
+ EOX
826
+
827
+ namespaces = doc.root.namespaces
828
+
829
+ # assert on setup
830
+ assert_equal 2, doc.root.namespaces.length
831
+ assert_equal 3, doc.at_xpath("//container").namespaces.length
832
+ assert_equal 0, doc.xpath("//foo").length
833
+ assert_equal 1, doc.xpath("//a:foo").length
834
+ assert_equal 1, doc.xpath("//a:foo").length
835
+ assert_equal 1, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
836
+ assert_match %r{foo c:attr}, doc.to_xml
837
+
838
+ doc.remove_namespaces!
839
+
840
+ assert_equal 0, doc.root.namespaces.length
841
+ assert_equal 0, doc.at_xpath("//container").namespaces.length
842
+ assert_equal 3, doc.xpath("//foo").length
843
+ assert_equal 0, doc.xpath("//a:foo", namespaces).length
844
+ assert_equal 0, doc.xpath("//a:foo", namespaces).length
845
+ assert_equal 0, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
846
+ assert_match %r{foo attr}, doc.to_xml
847
+ end
848
+
849
+ # issue #785
850
+ def test_attribute_decoration
851
+ decorator = Module.new do
852
+ def test_method
853
+ end
854
+ end
855
+
856
+ util_decorate(@xml, decorator)
857
+
858
+ assert @xml.search('//@street').first.respond_to?(:test_method)
859
+ end
860
+
861
+ def test_subset_is_decorated
862
+ x = Module.new do
863
+ def awesome!
864
+ end
865
+ end
866
+ util_decorate(@xml, x)
867
+
868
+ assert @xml.respond_to?(:awesome!)
869
+ assert node_set = @xml.search('//staff')
870
+ assert node_set.respond_to?(:awesome!)
871
+ assert subset = node_set.search('.//employee')
872
+ assert subset.respond_to?(:awesome!)
873
+ assert sub_subset = node_set.search('.//name')
874
+ assert sub_subset.respond_to?(:awesome!)
875
+ end
876
+
877
+ def test_decorator_is_applied
878
+ x = Module.new do
879
+ def awesome!
880
+ end
881
+ end
882
+ util_decorate(@xml, x)
883
+
884
+ assert @xml.respond_to?(:awesome!)
885
+ assert node_set = @xml.search('//employee')
886
+ assert node_set.respond_to?(:awesome!)
887
+ node_set.each do |node|
888
+ assert node.respond_to?(:awesome!), node.class
889
+ end
890
+ assert @xml.root.respond_to?(:awesome!)
891
+ assert @xml.children.respond_to?(:awesome!)
892
+ end
893
+
894
+ if Nokogiri.jruby?
895
+ def wrap_java_document
896
+ require 'java'
897
+ factory = javax.xml.parsers.DocumentBuilderFactory.newInstance
898
+ builder = factory.newDocumentBuilder
899
+ document = builder.newDocument
900
+ root = document.createElement("foo")
901
+ document.appendChild(root)
902
+ Nokogiri::XML::Document.wrap(document)
903
+ end
904
+ end
905
+
906
+ def test_java_integration
907
+ skip("Ruby doesn't have the wrap method") unless Nokogiri.jruby?
908
+ noko_doc = wrap_java_document
909
+ assert_equal 'foo', noko_doc.root.name
910
+
911
+ noko_doc = Nokogiri::XML(<<eoxml)
912
+ <foo xmlns='hello'>
913
+ <bar xmlns:foo='world' />
914
+ </foo>
915
+ eoxml
916
+ dom = noko_doc.to_java
917
+ assert dom.kind_of? org.w3c.dom.Document
918
+ assert_equal 'foo', dom.getDocumentElement().getTagName()
919
+ end
920
+
921
+ def test_add_child
922
+ skip("Ruby doesn't have the wrap method") unless Nokogiri.jruby?
923
+ doc = wrap_java_document
924
+ doc.root.add_child "<bar />"
925
+ end
926
+
927
+ def test_can_be_closed
928
+ f = File.open XML_FILE
929
+ Nokogiri::XML f
930
+ f.close
931
+ end
932
+ end
933
+ end
934
+ end