nokogiri-maglev- 1.5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. data/.autotest +26 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.ja.rdoc +544 -0
  4. data/CHANGELOG.rdoc +532 -0
  5. data/Manifest.txt +283 -0
  6. data/README.ja.rdoc +106 -0
  7. data/README.rdoc +174 -0
  8. data/Rakefile +171 -0
  9. data/bin/nokogiri +53 -0
  10. data/ext/nokogiri/depend +358 -0
  11. data/ext/nokogiri/extconf.rb +124 -0
  12. data/ext/nokogiri/html_document.c +154 -0
  13. data/ext/nokogiri/html_document.h +10 -0
  14. data/ext/nokogiri/html_element_description.c +276 -0
  15. data/ext/nokogiri/html_element_description.h +10 -0
  16. data/ext/nokogiri/html_entity_lookup.c +32 -0
  17. data/ext/nokogiri/html_entity_lookup.h +8 -0
  18. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  19. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  20. data/ext/nokogiri/nokogiri.c +115 -0
  21. data/ext/nokogiri/nokogiri.h +160 -0
  22. data/ext/nokogiri/st.c +576 -0
  23. data/ext/nokogiri/xml_attr.c +94 -0
  24. data/ext/nokogiri/xml_attr.h +9 -0
  25. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  26. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  27. data/ext/nokogiri/xml_cdata.c +56 -0
  28. data/ext/nokogiri/xml_cdata.h +9 -0
  29. data/ext/nokogiri/xml_comment.c +54 -0
  30. data/ext/nokogiri/xml_comment.h +9 -0
  31. data/ext/nokogiri/xml_document.c +478 -0
  32. data/ext/nokogiri/xml_document.h +23 -0
  33. data/ext/nokogiri/xml_document_fragment.c +48 -0
  34. data/ext/nokogiri/xml_document_fragment.h +10 -0
  35. data/ext/nokogiri/xml_dtd.c +202 -0
  36. data/ext/nokogiri/xml_dtd.h +10 -0
  37. data/ext/nokogiri/xml_element_content.c +123 -0
  38. data/ext/nokogiri/xml_element_content.h +10 -0
  39. data/ext/nokogiri/xml_element_decl.c +69 -0
  40. data/ext/nokogiri/xml_element_decl.h +9 -0
  41. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  42. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  43. data/ext/nokogiri/xml_entity_decl.c +110 -0
  44. data/ext/nokogiri/xml_entity_decl.h +10 -0
  45. data/ext/nokogiri/xml_entity_reference.c +52 -0
  46. data/ext/nokogiri/xml_entity_reference.h +9 -0
  47. data/ext/nokogiri/xml_io.c +56 -0
  48. data/ext/nokogiri/xml_io.h +11 -0
  49. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  50. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  51. data/ext/nokogiri/xml_namespace.c +84 -0
  52. data/ext/nokogiri/xml_namespace.h +13 -0
  53. data/ext/nokogiri/xml_node.c +1397 -0
  54. data/ext/nokogiri/xml_node.h +13 -0
  55. data/ext/nokogiri/xml_node_set.c +418 -0
  56. data/ext/nokogiri/xml_node_set.h +9 -0
  57. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  58. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  59. data/ext/nokogiri/xml_reader.c +684 -0
  60. data/ext/nokogiri/xml_reader.h +10 -0
  61. data/ext/nokogiri/xml_relax_ng.c +162 -0
  62. data/ext/nokogiri/xml_relax_ng.h +9 -0
  63. data/ext/nokogiri/xml_sax_parser.c +293 -0
  64. data/ext/nokogiri/xml_sax_parser.h +39 -0
  65. data/ext/nokogiri/xml_sax_parser_context.c +199 -0
  66. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  67. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  68. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  69. data/ext/nokogiri/xml_schema.c +205 -0
  70. data/ext/nokogiri/xml_schema.h +9 -0
  71. data/ext/nokogiri/xml_syntax_error.c +58 -0
  72. data/ext/nokogiri/xml_syntax_error.h +13 -0
  73. data/ext/nokogiri/xml_text.c +50 -0
  74. data/ext/nokogiri/xml_text.h +9 -0
  75. data/ext/nokogiri/xml_xpath_context.c +315 -0
  76. data/ext/nokogiri/xml_xpath_context.h +9 -0
  77. data/ext/nokogiri/xslt_stylesheet.c +265 -0
  78. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  79. data/lib/nokogiri.rb +127 -0
  80. data/lib/nokogiri/css.rb +27 -0
  81. data/lib/nokogiri/css/node.rb +99 -0
  82. data/lib/nokogiri/css/parser.rb +677 -0
  83. data/lib/nokogiri/css/parser.y +237 -0
  84. data/lib/nokogiri/css/parser_extras.rb +91 -0
  85. data/lib/nokogiri/css/syntax_error.rb +7 -0
  86. data/lib/nokogiri/css/tokenizer.rb +152 -0
  87. data/lib/nokogiri/css/tokenizer.rex +55 -0
  88. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  89. data/lib/nokogiri/decorators/slop.rb +35 -0
  90. data/lib/nokogiri/html.rb +36 -0
  91. data/lib/nokogiri/html/builder.rb +35 -0
  92. data/lib/nokogiri/html/document.rb +213 -0
  93. data/lib/nokogiri/html/document_fragment.rb +41 -0
  94. data/lib/nokogiri/html/element_description.rb +23 -0
  95. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  96. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  97. data/lib/nokogiri/html/sax/parser.rb +52 -0
  98. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  99. data/lib/nokogiri/syntax_error.rb +4 -0
  100. data/lib/nokogiri/version.rb +88 -0
  101. data/lib/nokogiri/xml.rb +67 -0
  102. data/lib/nokogiri/xml/attr.rb +14 -0
  103. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  104. data/lib/nokogiri/xml/builder.rb +426 -0
  105. data/lib/nokogiri/xml/cdata.rb +11 -0
  106. data/lib/nokogiri/xml/character_data.rb +7 -0
  107. data/lib/nokogiri/xml/document.rb +234 -0
  108. data/lib/nokogiri/xml/document_fragment.rb +98 -0
  109. data/lib/nokogiri/xml/dtd.rb +22 -0
  110. data/lib/nokogiri/xml/element_content.rb +36 -0
  111. data/lib/nokogiri/xml/element_decl.rb +13 -0
  112. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  113. data/lib/nokogiri/xml/namespace.rb +13 -0
  114. data/lib/nokogiri/xml/node.rb +915 -0
  115. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  116. data/lib/nokogiri/xml/node_set.rb +357 -0
  117. data/lib/nokogiri/xml/notation.rb +6 -0
  118. data/lib/nokogiri/xml/parse_options.rb +93 -0
  119. data/lib/nokogiri/xml/pp.rb +2 -0
  120. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  121. data/lib/nokogiri/xml/pp/node.rb +56 -0
  122. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  123. data/lib/nokogiri/xml/reader.rb +112 -0
  124. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  125. data/lib/nokogiri/xml/sax.rb +4 -0
  126. data/lib/nokogiri/xml/sax/document.rb +164 -0
  127. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  128. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  129. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  130. data/lib/nokogiri/xml/schema.rb +63 -0
  131. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  132. data/lib/nokogiri/xml/text.rb +9 -0
  133. data/lib/nokogiri/xml/xpath.rb +10 -0
  134. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  135. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  136. data/lib/nokogiri/xslt.rb +52 -0
  137. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  138. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  139. data/nokogiri_help_responses.md +40 -0
  140. data/tasks/cross_compile.rb +152 -0
  141. data/tasks/nokogiri.org.rb +18 -0
  142. data/tasks/test.rb +94 -0
  143. data/test/css/test_nthiness.rb +159 -0
  144. data/test/css/test_parser.rb +303 -0
  145. data/test/css/test_tokenizer.rb +198 -0
  146. data/test/css/test_xpath_visitor.rb +85 -0
  147. data/test/decorators/test_slop.rb +16 -0
  148. data/test/files/2ch.html +108 -0
  149. data/test/files/address_book.rlx +12 -0
  150. data/test/files/address_book.xml +10 -0
  151. data/test/files/bar/bar.xsd +4 -0
  152. data/test/files/dont_hurt_em_why.xml +422 -0
  153. data/test/files/encoding.html +82 -0
  154. data/test/files/encoding.xhtml +84 -0
  155. data/test/files/exslt.xml +8 -0
  156. data/test/files/exslt.xslt +35 -0
  157. data/test/files/foo/foo.xsd +4 -0
  158. data/test/files/metacharset.html +10 -0
  159. data/test/files/noencoding.html +47 -0
  160. data/test/files/po.xml +32 -0
  161. data/test/files/po.xsd +66 -0
  162. data/test/files/shift_jis.html +10 -0
  163. data/test/files/shift_jis.xml +5 -0
  164. data/test/files/snuggles.xml +3 -0
  165. data/test/files/staff.dtd +10 -0
  166. data/test/files/staff.xml +59 -0
  167. data/test/files/staff.xslt +32 -0
  168. data/test/files/tlm.html +850 -0
  169. data/test/files/valid_bar.xml +2 -0
  170. data/test/helper.rb +173 -0
  171. data/test/html/sax/test_parser.rb +139 -0
  172. data/test/html/sax/test_parser_context.rb +48 -0
  173. data/test/html/test_builder.rb +165 -0
  174. data/test/html/test_document.rb +472 -0
  175. data/test/html/test_document_encoding.rb +138 -0
  176. data/test/html/test_document_fragment.rb +255 -0
  177. data/test/html/test_element_description.rb +101 -0
  178. data/test/html/test_named_characters.rb +14 -0
  179. data/test/html/test_node.rb +193 -0
  180. data/test/html/test_node_encoding.rb +27 -0
  181. data/test/test_convert_xpath.rb +135 -0
  182. data/test/test_css_cache.rb +45 -0
  183. data/test/test_encoding_handler.rb +46 -0
  184. data/test/test_memory_leak.rb +72 -0
  185. data/test/test_nokogiri.rb +133 -0
  186. data/test/test_reader.rb +425 -0
  187. data/test/test_soap4r_sax.rb +52 -0
  188. data/test/test_xslt_transforms.rb +193 -0
  189. data/test/xml/node/test_save_options.rb +28 -0
  190. data/test/xml/node/test_subclass.rb +44 -0
  191. data/test/xml/sax/test_parser.rb +338 -0
  192. data/test/xml/sax/test_parser_context.rb +113 -0
  193. data/test/xml/sax/test_push_parser.rb +156 -0
  194. data/test/xml/test_attr.rb +65 -0
  195. data/test/xml/test_attribute_decl.rb +86 -0
  196. data/test/xml/test_builder.rb +227 -0
  197. data/test/xml/test_cdata.rb +50 -0
  198. data/test/xml/test_comment.rb +29 -0
  199. data/test/xml/test_document.rb +697 -0
  200. data/test/xml/test_document_encoding.rb +26 -0
  201. data/test/xml/test_document_fragment.rb +192 -0
  202. data/test/xml/test_dtd.rb +107 -0
  203. data/test/xml/test_dtd_encoding.rb +33 -0
  204. data/test/xml/test_element_content.rb +56 -0
  205. data/test/xml/test_element_decl.rb +73 -0
  206. data/test/xml/test_entity_decl.rb +122 -0
  207. data/test/xml/test_entity_reference.rb +21 -0
  208. data/test/xml/test_namespace.rb +70 -0
  209. data/test/xml/test_node.rb +917 -0
  210. data/test/xml/test_node_attributes.rb +34 -0
  211. data/test/xml/test_node_encoding.rb +107 -0
  212. data/test/xml/test_node_reparenting.rb +334 -0
  213. data/test/xml/test_node_set.rb +742 -0
  214. data/test/xml/test_parse_options.rb +52 -0
  215. data/test/xml/test_processing_instruction.rb +30 -0
  216. data/test/xml/test_reader_encoding.rb +126 -0
  217. data/test/xml/test_relax_ng.rb +60 -0
  218. data/test/xml/test_schema.rb +94 -0
  219. data/test/xml/test_syntax_error.rb +12 -0
  220. data/test/xml/test_text.rb +47 -0
  221. data/test/xml/test_unparented_node.rb +381 -0
  222. data/test/xml/test_xpath.rb +237 -0
  223. data/test/xslt/test_custom_functions.rb +94 -0
  224. data/test/xslt/test_exception_handling.rb +37 -0
  225. metadata +548 -0
@@ -0,0 +1,50 @@
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_nothing_raised do
33
+ 100.times { CDATA.new(@xml, "asdfasdf") }
34
+ end
35
+ end
36
+
37
+ def test_content=
38
+ node = CDATA.new(@xml, 'foo')
39
+ assert_equal('foo', node.content)
40
+
41
+ node.content = '& <foo> &amp;'
42
+ assert_equal('& <foo> &amp;', node.content)
43
+ assert_equal('<![CDATA[& <foo> &amp;]]>', node.to_xml)
44
+
45
+ node.content = 'foo ]]> bar'
46
+ assert_equal('foo ]]> bar', node.content)
47
+ end
48
+ end
49
+ end
50
+ 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,697 @@
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_root_set_to_nil
20
+ @xml.root = nil
21
+ assert_equal nil, @xml.root
22
+ end
23
+
24
+ def test_collect_namespaces
25
+ doc = Nokogiri::XML(<<-eoxml)
26
+ <xml>
27
+ <foo xmlns='hello'>
28
+ <bar xmlns:foo='world' />
29
+ </foo>
30
+ </xml>
31
+ eoxml
32
+ assert_equal({"xmlns"=>"hello", "xmlns:foo"=>"world"},
33
+ doc.collect_namespaces)
34
+ end
35
+
36
+ def test_subclass_initialize_modify # testing a segv
37
+ Class.new(Nokogiri::XML::Document) {
38
+ def initialize
39
+ super
40
+ body_node = Nokogiri::XML::Node.new "body", self
41
+ body_node.content = "stuff"
42
+ self.root = body_node
43
+ end
44
+ }.new
45
+ end
46
+
47
+ def test_create_text_node
48
+ txt = @xml.create_text_node 'foo'
49
+ assert_instance_of Nokogiri::XML::Text, txt
50
+ assert_equal 'foo', txt.text
51
+ assert_equal @xml, txt.document
52
+ end
53
+
54
+ def test_create_element
55
+ elm = @xml.create_element('foo')
56
+ assert_instance_of Nokogiri::XML::Element, elm
57
+ assert_equal 'foo', elm.name
58
+ assert_equal @xml, elm.document
59
+ end
60
+
61
+ def test_create_element_with_attributes
62
+ elm = @xml.create_element('foo',:a => "1")
63
+ assert_instance_of Nokogiri::XML::Element, elm
64
+ assert_instance_of Nokogiri::XML::Attr, elm.attributes["a"]
65
+ assert_equal "1", elm["a"]
66
+ end
67
+
68
+ def test_create_element_with_namespace
69
+ elm = @xml.create_element('foo',:'xmlns:foo' => 'http://tenderlovemaking.com')
70
+ assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:foo']
71
+ end
72
+
73
+ def test_create_element_with_content
74
+ elm = @xml.create_element('foo',"needs more xml/violence")
75
+ assert_equal "needs more xml/violence", elm.content
76
+ end
77
+
78
+ def test_create_cdata
79
+ cdata = @xml.create_cdata("abc")
80
+ assert_instance_of Nokogiri::XML::CDATA, cdata
81
+ assert_equal "abc", cdata.content
82
+ end
83
+
84
+ def test_pp
85
+ out = StringIO.new('')
86
+ assert_nothing_raised do
87
+ ::PP.pp @xml, out
88
+ end
89
+ end
90
+
91
+ def test_create_internal_subset_on_existing_subset
92
+ assert_not_nil @xml.internal_subset
93
+ assert_raises(RuntimeError) do
94
+ @xml.create_internal_subset('staff', nil, 'staff.dtd')
95
+ end
96
+ end
97
+
98
+ def test_create_internal_subset
99
+ xml = Nokogiri::XML('<root />')
100
+ assert_nil xml.internal_subset
101
+
102
+ xml.create_internal_subset('name', nil, 'staff.dtd')
103
+ ss = xml.internal_subset
104
+ assert_equal 'name', ss.name
105
+ assert_nil ss.external_id
106
+ assert_equal 'staff.dtd', ss.system_id
107
+ end
108
+
109
+ def test_external_subset
110
+ assert_nil @xml.external_subset
111
+ Dir.chdir(ASSETS_DIR) do
112
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
113
+ cfg.dtdload
114
+ }
115
+ end
116
+ assert @xml.external_subset
117
+ end
118
+
119
+ def test_create_external_subset_fails_with_existing_subset
120
+ assert_nil @xml.external_subset
121
+ Dir.chdir(ASSETS_DIR) do
122
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
123
+ cfg.dtdload
124
+ }
125
+ end
126
+ assert @xml.external_subset
127
+
128
+ assert_raises(RuntimeError) do
129
+ @xml.create_external_subset('staff', nil, 'staff.dtd')
130
+ end
131
+ end
132
+
133
+ def test_create_external_subset
134
+ dtd = @xml.create_external_subset('staff', nil, 'staff.dtd')
135
+ assert_nil dtd.external_id
136
+ assert_equal 'staff.dtd', dtd.system_id
137
+ assert_equal 'staff', dtd.name
138
+ assert_equal dtd, @xml.external_subset
139
+ end
140
+
141
+ def test_version
142
+ assert_equal '1.0', @xml.version
143
+ end
144
+
145
+ def test_add_namespace
146
+ assert_raise NoMethodError do
147
+ @xml.add_namespace('foo', 'bar')
148
+ end
149
+ end
150
+
151
+ def test_attributes
152
+ assert_raise NoMethodError do
153
+ @xml.attributes
154
+ end
155
+ end
156
+
157
+ def test_namespace
158
+ assert_raise NoMethodError do
159
+ @xml.namespace
160
+ end
161
+ end
162
+
163
+ def test_namespace_definitions
164
+ assert_raise NoMethodError do
165
+ @xml.namespace_definitions
166
+ end
167
+ end
168
+
169
+ def test_line
170
+ assert_raise NoMethodError do
171
+ @xml.line
172
+ end
173
+ end
174
+
175
+ def test_empty_node_converted_to_html_is_not_self_closing
176
+ doc = Nokogiri::XML('<a></a>')
177
+ assert_equal "<a></a>", doc.inner_html
178
+ end
179
+
180
+ def test_fragment
181
+ fragment = @xml.fragment
182
+ assert_equal 0, fragment.children.length
183
+ end
184
+
185
+ def test_add_child_fragment_with_single_node
186
+ doc = Nokogiri::XML::Document.new
187
+ fragment = doc.fragment('<hello />')
188
+ doc.add_child fragment
189
+ assert_equal '/hello', doc.at('//hello').path
190
+ assert_equal 'hello', doc.root.name
191
+ end
192
+
193
+ def test_add_child_fragment_with_multiple_nodes
194
+ doc = Nokogiri::XML::Document.new
195
+ fragment = doc.fragment('<hello /><goodbye />')
196
+ assert_raises(RuntimeError) do
197
+ doc.add_child fragment
198
+ end
199
+ end
200
+
201
+ def test_add_child_with_multiple_roots
202
+ assert_raises(RuntimeError) do
203
+ @xml << Node.new('foo', @xml)
204
+ end
205
+ end
206
+
207
+ def test_move_root_to_document_with_no_root
208
+ sender = Nokogiri::XML('<root>foo</root>')
209
+ newdoc = Nokogiri::XML::Document.new
210
+ newdoc.root = sender.root
211
+ end
212
+
213
+ def test_move_root_with_existing_root_gets_gcd
214
+ doc = Nokogiri::XML('<root>test</root>')
215
+ doc2 = Nokogiri::XML("<root>#{'x' * 5000000}</root>")
216
+ doc2.root = doc.root
217
+ end
218
+
219
+ def test_validate
220
+ if Nokogiri.uses_libxml?
221
+ assert_equal 44, @xml.validate.length
222
+ else
223
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) {|cfg| cfg.dtdvalid}
224
+ assert_equal 37, xml.validate.length
225
+ end
226
+ end
227
+
228
+ def test_validate_no_internal_subset
229
+ doc = Nokogiri::XML('<test/>')
230
+ assert_nil doc.validate
231
+ end
232
+
233
+ def test_clone
234
+ assert @xml.clone
235
+ end
236
+
237
+ def test_document_should_not_have_default_ns
238
+ doc = Nokogiri::XML::Document.new
239
+
240
+ assert_raises NoMethodError do
241
+ doc.default_namespace = 'http://innernet.com/'
242
+ end
243
+
244
+ assert_raises NoMethodError do
245
+ doc.add_namespace_definition('foo', 'bar')
246
+ end
247
+ end
248
+
249
+ def test_parse_handles_nil_gracefully
250
+ assert_nothing_raised do
251
+ @doc = Nokogiri::XML::Document.parse(nil)
252
+ end
253
+ assert_instance_of Nokogiri::XML::Document, @doc
254
+ end
255
+
256
+ def test_parse_takes_block
257
+ options = nil
258
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
259
+ options = cfg
260
+ end
261
+ assert options
262
+ end
263
+
264
+ def test_parse_yields_parse_options
265
+ options = nil
266
+ Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
267
+ options = cfg
268
+ options.nonet.nowarning.dtdattr
269
+ end
270
+ assert options.nonet?
271
+ assert options.nowarning?
272
+ assert options.dtdattr?
273
+ end
274
+
275
+ def test_XML_takes_block
276
+ options = nil
277
+ Nokogiri::XML(File.read(XML_FILE), XML_FILE) do |cfg|
278
+ options = cfg
279
+ options.nonet.nowarning.dtdattr
280
+ end
281
+ assert options.nonet?
282
+ assert options.nowarning?
283
+ assert options.dtdattr?
284
+ end
285
+
286
+ def test_subclass
287
+ klass = Class.new(Nokogiri::XML::Document)
288
+ doc = klass.new
289
+ assert_instance_of klass, doc
290
+ end
291
+
292
+ def test_subclass_initialize
293
+ klass = Class.new(Nokogiri::XML::Document) do
294
+ attr_accessor :initialized_with
295
+
296
+ def initialize(*args)
297
+ @initialized_with = args
298
+ end
299
+ end
300
+ doc = klass.new("1.0", 1)
301
+ assert_equal ["1.0", 1], doc.initialized_with
302
+ end
303
+
304
+ def test_subclass_dup
305
+ klass = Class.new(Nokogiri::XML::Document)
306
+ doc = klass.new.dup
307
+ assert_instance_of klass, doc
308
+ end
309
+
310
+ def test_subclass_parse
311
+ klass = Class.new(Nokogiri::XML::Document)
312
+ doc = klass.parse(File.read(XML_FILE))
313
+ assert_equal @xml.to_s, doc.to_s
314
+ assert_instance_of klass, doc
315
+ end
316
+
317
+ def test_document_parse_method
318
+ xml = Nokogiri::XML::Document.parse(File.read(XML_FILE))
319
+ assert_equal @xml.to_s, xml.to_s
320
+ end
321
+
322
+ def test_z_encoding=
323
+ @xml.encoding = 'UTF-8'
324
+ assert_match 'UTF-8', @xml.to_xml
325
+
326
+ @xml.encoding = 'EUC-JP'
327
+ assert_match 'EUC-JP', @xml.to_xml
328
+ end
329
+
330
+ def test_namespace_should_not_exist
331
+ assert_raises(NoMethodError) {
332
+ @xml.namespace
333
+ }
334
+ end
335
+
336
+ def test_non_existant_function
337
+ # WTF. I don't know why this is different between MRI and Jruby
338
+ # They should be the same... Either way, raising an exception
339
+ # is the correct thing to do.
340
+ exception = RuntimeError
341
+
342
+ if !Nokogiri.uses_libxml? || (Nokogiri.uses_libxml? && Nokogiri::VERSION_INFO['libxml']['platform'] == 'jruby')
343
+ exception = Nokogiri::XML::XPath::SyntaxError
344
+ end
345
+
346
+ assert_raises(exception) {
347
+ @xml.xpath('//name[foo()]')
348
+ }
349
+ end
350
+
351
+ def test_ancestors
352
+ assert_equal 0, @xml.ancestors.length
353
+ end
354
+
355
+ def test_root_node_parent_is_document
356
+ parent = @xml.root.parent
357
+ assert_equal @xml, parent
358
+ assert_instance_of Nokogiri::XML::Document, parent
359
+ end
360
+
361
+ def test_xmlns_is_automatically_registered
362
+ doc = Nokogiri::XML(<<-eoxml)
363
+ <root xmlns="http://tenderlovemaking.com/">
364
+ <foo>
365
+ bar
366
+ </foo>
367
+ </root>
368
+ eoxml
369
+ assert_equal 1, doc.css('xmlns|foo').length
370
+ assert_equal 1, doc.css('foo').length
371
+ assert_equal 0, doc.css('|foo').length
372
+ assert_equal 1, doc.xpath('//xmlns:foo').length
373
+ assert_equal 1, doc.search('xmlns|foo').length
374
+ assert_equal 1, doc.search('//xmlns:foo').length
375
+ assert doc.at('xmlns|foo')
376
+ assert doc.at('//xmlns:foo')
377
+ assert doc.at('foo')
378
+ end
379
+
380
+ def test_xmlns_is_registered_for_nodesets
381
+ doc = Nokogiri::XML(<<-eoxml)
382
+ <root xmlns="http://tenderlovemaking.com/">
383
+ <foo>
384
+ <bar>
385
+ baz
386
+ </bar>
387
+ </foo>
388
+ </root>
389
+ eoxml
390
+ assert_equal 1, doc.css('xmlns|foo').css('xmlns|bar').length
391
+ assert_equal 1, doc.css('foo').css('bar').length
392
+ assert_equal 1, doc.xpath('//xmlns:foo').xpath('./xmlns:bar').length
393
+ assert_equal 1, doc.search('xmlns|foo').search('xmlns|bar').length
394
+ assert_equal 1, doc.search('//xmlns:foo').search('./xmlns:bar').length
395
+ end
396
+
397
+ def test_to_xml_with_indent
398
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
399
+ doc = Nokogiri::XML(doc.to_xml(:indent => 5))
400
+
401
+ assert_indent 5, doc
402
+ end
403
+
404
+ def test_write_xml_to_with_indent
405
+ io = StringIO.new
406
+ doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
407
+ doc.write_xml_to io, :indent => 5
408
+ io.rewind
409
+ doc = Nokogiri::XML(io.read)
410
+ assert_indent 5, doc
411
+ end
412
+
413
+ # wtf... osx's libxml sucks.
414
+ unless !Nokogiri.uses_libxml? || Nokogiri::LIBXML_VERSION =~ /^2\.6\./
415
+ def test_z_encoding
416
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
417
+ assert_equal 'UTF-8', xml.encoding
418
+ end
419
+ end
420
+
421
+ def test_document_has_errors
422
+ doc = Nokogiri::XML(<<-eoxml)
423
+ <foo><bar></foo>
424
+ eoxml
425
+ assert doc.errors.length > 0
426
+ doc.errors.each do |error|
427
+ assert_match error.message, error.inspect
428
+ assert_match error.message, error.to_s
429
+ end
430
+ end
431
+
432
+ def test_strict_document_throws_syntax_error
433
+ assert_raises(Nokogiri::XML::SyntaxError) {
434
+ Nokogiri::XML('<foo><bar></foo>', nil, nil, 0)
435
+ }
436
+
437
+ assert_raises(Nokogiri::XML::SyntaxError) {
438
+ Nokogiri::XML('<foo><bar></foo>') { |cfg|
439
+ cfg.strict
440
+ }
441
+ }
442
+
443
+ assert_raises(Nokogiri::XML::SyntaxError) {
444
+ Nokogiri::XML(StringIO.new('<foo><bar></foo>')) { |cfg|
445
+ cfg.strict
446
+ }
447
+ }
448
+ end
449
+
450
+ def test_XML_function
451
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
452
+ assert xml.xml?
453
+ end
454
+
455
+ def test_url
456
+ assert @xml.url
457
+ assert_equal XML_FILE, URI.unescape(@xml.url).sub('file:///', '')
458
+ end
459
+
460
+ def test_document_parent
461
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
462
+ assert_raises(NoMethodError) {
463
+ xml.parent
464
+ }
465
+ end
466
+
467
+ def test_document_name
468
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
469
+ assert_equal 'document', xml.name
470
+ end
471
+
472
+ def test_parse_can_take_io
473
+ xml = nil
474
+ File.open(XML_FILE, 'rb') { |f|
475
+ xml = Nokogiri::XML(f)
476
+ }
477
+ assert xml.xml?
478
+ set = xml.search('//employee')
479
+ assert set.length > 0
480
+ end
481
+
482
+ def test_search_on_empty_documents
483
+ doc = Nokogiri::XML::Document.new
484
+ ns = doc.search('//foo')
485
+ assert_equal 0, ns.length
486
+
487
+ ns = doc.css('foo')
488
+ assert_equal 0, ns.length
489
+
490
+ ns = doc.xpath('//foo')
491
+ assert_equal 0, ns.length
492
+ end
493
+
494
+ def test_bad_xpath_raises_syntax_error
495
+ assert_raises(XML::XPath::SyntaxError) {
496
+ @xml.xpath('\\')
497
+ }
498
+ end
499
+
500
+ def test_find_with_namespace
501
+ doc = Nokogiri::XML.parse(<<-eoxml)
502
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
503
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
504
+ </x>
505
+ eoxml
506
+
507
+ ctx = Nokogiri::XML::XPathContext.new(doc)
508
+ ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
509
+ set = ctx.evaluate('//tenderlove:foo')
510
+ assert_equal 1, set.length
511
+ assert_equal 'foo', set.first.name
512
+
513
+ # It looks like only the URI is important:
514
+ ctx = Nokogiri::XML::XPathContext.new(doc)
515
+ ctx.register_ns 'america', 'http://tenderlovemaking.com/'
516
+ set = ctx.evaluate('//america:foo')
517
+ assert_equal 1, set.length
518
+ assert_equal 'foo', set.first.name
519
+
520
+ # Its so important that a missing slash will cause it to return nothing
521
+ ctx = Nokogiri::XML::XPathContext.new(doc)
522
+ ctx.register_ns 'america', 'http://tenderlovemaking.com'
523
+ set = ctx.evaluate('//america:foo')
524
+ assert_equal 0, set.length
525
+ end
526
+
527
+ def test_xml?
528
+ assert @xml.xml?
529
+ end
530
+
531
+ def test_document
532
+ assert @xml.document
533
+ end
534
+
535
+ def test_singleton_methods
536
+ assert node_set = @xml.search('//name')
537
+ assert node_set.length > 0
538
+ node = node_set.first
539
+ def node.test
540
+ 'test'
541
+ end
542
+ assert node_set = @xml.search('//name')
543
+ assert_equal 'test', node_set.first.test
544
+ end
545
+
546
+ def test_multiple_search
547
+ assert node_set = @xml.search('//employee', '//name')
548
+ employees = @xml.search('//employee')
549
+ names = @xml.search('//name')
550
+ assert_equal(employees.length + names.length, node_set.length)
551
+ end
552
+
553
+ def test_node_set_index
554
+ assert node_set = @xml.search('//employee')
555
+
556
+ assert_equal(5, node_set.length)
557
+ assert node_set[4]
558
+ assert_nil node_set[5]
559
+ end
560
+
561
+ def test_search
562
+ assert node_set = @xml.search('//employee')
563
+
564
+ assert_equal(5, node_set.length)
565
+
566
+ node_set.each do |node|
567
+ assert_equal('employee', node.name)
568
+ end
569
+ end
570
+
571
+ def test_dump
572
+ assert @xml.serialize
573
+ assert @xml.to_xml
574
+ end
575
+
576
+ def test_dup
577
+ dup = @xml.dup
578
+ assert_instance_of Nokogiri::XML::Document, dup
579
+ assert dup.xml?, 'duplicate should be xml'
580
+ end
581
+
582
+ def test_new
583
+ doc = nil
584
+ assert_nothing_raised {
585
+ doc = Nokogiri::XML::Document.new
586
+ }
587
+ assert doc
588
+ assert doc.xml?
589
+ assert_nil doc.root
590
+ end
591
+
592
+ def test_set_root
593
+ doc = nil
594
+ assert_nothing_raised {
595
+ doc = Nokogiri::XML::Document.new
596
+ }
597
+ assert doc
598
+ assert doc.xml?
599
+ assert_nil doc.root
600
+ node = Nokogiri::XML::Node.new("b", doc) { |n|
601
+ n.content = 'hello world'
602
+ }
603
+ assert_equal('hello world', node.content)
604
+ doc.root = node
605
+ assert_equal(node, doc.root)
606
+ end
607
+
608
+ def test_remove_namespaces
609
+ doc = Nokogiri::XML <<-EOX
610
+ <root xmlns:a="http://a.flavorjon.es/" xmlns:b="http://b.flavorjon.es/">
611
+ <a:foo>hello from a</a:foo>
612
+ <b:foo>hello from b</b:foo>
613
+ <container xmlns:c="http://c.flavorjon.es/">
614
+ <c:foo c:attr='attr-value'>hello from c</c:foo>
615
+ </container>
616
+ </root>
617
+ EOX
618
+
619
+ namespaces = doc.root.namespaces
620
+
621
+ # assert on setup
622
+ assert_equal 2, doc.root.namespaces.length
623
+ assert_equal 3, doc.at_xpath("//container").namespaces.length
624
+ assert_equal 0, doc.xpath("//foo").length
625
+ assert_equal 1, doc.xpath("//a:foo").length
626
+ assert_equal 1, doc.xpath("//a:foo").length
627
+ assert_equal 1, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
628
+ assert_match %r{foo c:attr}, doc.to_xml
629
+
630
+ doc.remove_namespaces!
631
+
632
+ assert_equal 0, doc.root.namespaces.length
633
+ assert_equal 0, doc.at_xpath("//container").namespaces.length
634
+ assert_equal 3, doc.xpath("//foo").length
635
+ assert_equal 0, doc.xpath("//a:foo", namespaces).length
636
+ assert_equal 0, doc.xpath("//a:foo", namespaces).length
637
+ assert_equal 0, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
638
+ assert_match %r{foo attr}, doc.to_xml
639
+ end
640
+
641
+ def test_subset_is_decorated
642
+ x = Module.new do
643
+ def awesome!
644
+ end
645
+ end
646
+ util_decorate(@xml, x)
647
+
648
+ assert @xml.respond_to?(:awesome!)
649
+ assert node_set = @xml.search('//staff')
650
+ assert node_set.respond_to?(:awesome!)
651
+ assert subset = node_set.search('.//employee')
652
+ assert subset.respond_to?(:awesome!)
653
+ assert sub_subset = node_set.search('.//name')
654
+ assert sub_subset.respond_to?(:awesome!)
655
+ end
656
+
657
+ def test_decorator_is_applied
658
+ x = Module.new do
659
+ def awesome!
660
+ end
661
+ end
662
+ util_decorate(@xml, x)
663
+
664
+ assert @xml.respond_to?(:awesome!)
665
+ assert node_set = @xml.search('//employee')
666
+ assert node_set.respond_to?(:awesome!)
667
+ node_set.each do |node|
668
+ assert node.respond_to?(:awesome!), node.class
669
+ end
670
+ assert @xml.root.respond_to?(:awesome!)
671
+ assert @xml.children.respond_to?(:awesome!)
672
+ end
673
+
674
+ def test_java_integration
675
+ if Nokogiri.jruby?
676
+ require 'java'
677
+ factory = javax.xml.parsers.DocumentBuilderFactory.newInstance
678
+ builder = factory.newDocumentBuilder
679
+ document = builder.newDocument
680
+ root = document.createElement("foo")
681
+ document.appendChild(root)
682
+ noko_doc = Nokogiri::XML::Document.wrap(document)
683
+ assert_equal 'foo', noko_doc.root.name
684
+
685
+ noko_doc = Nokogiri::XML(<<eoxml)
686
+ <foo xmlns='hello'>
687
+ <bar xmlns:foo='world' />
688
+ </foo>
689
+ eoxml
690
+ dom = noko_doc.to_java
691
+ assert dom.kind_of? org.w3c.dom.Document
692
+ assert_equal 'foo', dom.getDocumentElement().getTagName()
693
+ end
694
+ end
695
+ end
696
+ end
697
+ end