nokogiri-fitzsimmons 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. data/.autotest +26 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.ja.rdoc +666 -0
  4. data/CHANGELOG.rdoc +659 -0
  5. data/C_CODING_STYLE.rdoc +33 -0
  6. data/Manifest.txt +295 -0
  7. data/README.ja.rdoc +106 -0
  8. data/README.rdoc +178 -0
  9. data/ROADMAP.md +86 -0
  10. data/Rakefile +194 -0
  11. data/STANDARD_RESPONSES.md +47 -0
  12. data/Y_U_NO_GEMSPEC.md +155 -0
  13. data/bin/nokogiri +63 -0
  14. data/build_all +58 -0
  15. data/ext/nokogiri/depend +358 -0
  16. data/ext/nokogiri/extconf.rb +142 -0
  17. data/ext/nokogiri/html_document.c +170 -0
  18. data/ext/nokogiri/html_document.h +10 -0
  19. data/ext/nokogiri/html_element_description.c +276 -0
  20. data/ext/nokogiri/html_element_description.h +10 -0
  21. data/ext/nokogiri/html_entity_lookup.c +32 -0
  22. data/ext/nokogiri/html_entity_lookup.h +8 -0
  23. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  24. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  25. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  26. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  27. data/ext/nokogiri/nokogiri.c +133 -0
  28. data/ext/nokogiri/nokogiri.h +160 -0
  29. data/ext/nokogiri/xml_attr.c +94 -0
  30. data/ext/nokogiri/xml_attr.h +9 -0
  31. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  32. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  33. data/ext/nokogiri/xml_cdata.c +56 -0
  34. data/ext/nokogiri/xml_cdata.h +9 -0
  35. data/ext/nokogiri/xml_comment.c +54 -0
  36. data/ext/nokogiri/xml_comment.h +9 -0
  37. data/ext/nokogiri/xml_document.c +576 -0
  38. data/ext/nokogiri/xml_document.h +23 -0
  39. data/ext/nokogiri/xml_document_fragment.c +48 -0
  40. data/ext/nokogiri/xml_document_fragment.h +10 -0
  41. data/ext/nokogiri/xml_dtd.c +202 -0
  42. data/ext/nokogiri/xml_dtd.h +10 -0
  43. data/ext/nokogiri/xml_element_content.c +123 -0
  44. data/ext/nokogiri/xml_element_content.h +10 -0
  45. data/ext/nokogiri/xml_element_decl.c +69 -0
  46. data/ext/nokogiri/xml_element_decl.h +9 -0
  47. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  48. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  49. data/ext/nokogiri/xml_entity_decl.c +110 -0
  50. data/ext/nokogiri/xml_entity_decl.h +10 -0
  51. data/ext/nokogiri/xml_entity_reference.c +52 -0
  52. data/ext/nokogiri/xml_entity_reference.h +9 -0
  53. data/ext/nokogiri/xml_io.c +56 -0
  54. data/ext/nokogiri/xml_io.h +11 -0
  55. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  56. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  57. data/ext/nokogiri/xml_namespace.c +78 -0
  58. data/ext/nokogiri/xml_namespace.h +13 -0
  59. data/ext/nokogiri/xml_node.c +1480 -0
  60. data/ext/nokogiri/xml_node.h +13 -0
  61. data/ext/nokogiri/xml_node_set.c +467 -0
  62. data/ext/nokogiri/xml_node_set.h +14 -0
  63. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  64. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  65. data/ext/nokogiri/xml_reader.c +684 -0
  66. data/ext/nokogiri/xml_reader.h +10 -0
  67. data/ext/nokogiri/xml_relax_ng.c +161 -0
  68. data/ext/nokogiri/xml_relax_ng.h +9 -0
  69. data/ext/nokogiri/xml_sax_parser.c +293 -0
  70. data/ext/nokogiri/xml_sax_parser.h +39 -0
  71. data/ext/nokogiri/xml_sax_parser_context.c +222 -0
  72. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  73. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  74. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  75. data/ext/nokogiri/xml_schema.c +205 -0
  76. data/ext/nokogiri/xml_schema.h +9 -0
  77. data/ext/nokogiri/xml_syntax_error.c +58 -0
  78. data/ext/nokogiri/xml_syntax_error.h +13 -0
  79. data/ext/nokogiri/xml_text.c +52 -0
  80. data/ext/nokogiri/xml_text.h +9 -0
  81. data/ext/nokogiri/xml_xpath_context.c +319 -0
  82. data/ext/nokogiri/xml_xpath_context.h +10 -0
  83. data/ext/nokogiri/xslt_stylesheet.c +270 -0
  84. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  85. data/lib/nokogiri.rb +127 -0
  86. data/lib/nokogiri/css.rb +27 -0
  87. data/lib/nokogiri/css/node.rb +102 -0
  88. data/lib/nokogiri/css/parser.rb +720 -0
  89. data/lib/nokogiri/css/parser.y +258 -0
  90. data/lib/nokogiri/css/parser_extras.rb +91 -0
  91. data/lib/nokogiri/css/syntax_error.rb +7 -0
  92. data/lib/nokogiri/css/tokenizer.rb +152 -0
  93. data/lib/nokogiri/css/tokenizer.rex +55 -0
  94. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  95. data/lib/nokogiri/decorators/slop.rb +35 -0
  96. data/lib/nokogiri/html.rb +37 -0
  97. data/lib/nokogiri/html/builder.rb +35 -0
  98. data/lib/nokogiri/html/document.rb +254 -0
  99. data/lib/nokogiri/html/document_fragment.rb +41 -0
  100. data/lib/nokogiri/html/element_description.rb +23 -0
  101. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  102. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  103. data/lib/nokogiri/html/sax/parser.rb +52 -0
  104. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  105. data/lib/nokogiri/html/sax/push_parser.rb +16 -0
  106. data/lib/nokogiri/syntax_error.rb +4 -0
  107. data/lib/nokogiri/version.rb +88 -0
  108. data/lib/nokogiri/xml.rb +73 -0
  109. data/lib/nokogiri/xml/attr.rb +14 -0
  110. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  111. data/lib/nokogiri/xml/builder.rb +431 -0
  112. data/lib/nokogiri/xml/cdata.rb +11 -0
  113. data/lib/nokogiri/xml/character_data.rb +7 -0
  114. data/lib/nokogiri/xml/document.rb +267 -0
  115. data/lib/nokogiri/xml/document_fragment.rb +103 -0
  116. data/lib/nokogiri/xml/dtd.rb +22 -0
  117. data/lib/nokogiri/xml/element_content.rb +36 -0
  118. data/lib/nokogiri/xml/element_decl.rb +13 -0
  119. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  120. data/lib/nokogiri/xml/namespace.rb +13 -0
  121. data/lib/nokogiri/xml/node.rb +946 -0
  122. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  123. data/lib/nokogiri/xml/node_set.rb +357 -0
  124. data/lib/nokogiri/xml/notation.rb +6 -0
  125. data/lib/nokogiri/xml/parse_options.rb +98 -0
  126. data/lib/nokogiri/xml/pp.rb +2 -0
  127. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  128. data/lib/nokogiri/xml/pp/node.rb +56 -0
  129. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  130. data/lib/nokogiri/xml/reader.rb +112 -0
  131. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  132. data/lib/nokogiri/xml/sax.rb +4 -0
  133. data/lib/nokogiri/xml/sax/document.rb +164 -0
  134. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  135. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  136. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  137. data/lib/nokogiri/xml/schema.rb +63 -0
  138. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  139. data/lib/nokogiri/xml/text.rb +9 -0
  140. data/lib/nokogiri/xml/xpath.rb +10 -0
  141. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  142. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  143. data/lib/nokogiri/xslt.rb +56 -0
  144. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  145. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  146. data/tasks/cross_compile.rb +153 -0
  147. data/tasks/nokogiri.org.rb +24 -0
  148. data/tasks/test.rb +95 -0
  149. data/test/css/test_nthiness.rb +159 -0
  150. data/test/css/test_parser.rb +341 -0
  151. data/test/css/test_tokenizer.rb +198 -0
  152. data/test/css/test_xpath_visitor.rb +91 -0
  153. data/test/decorators/test_slop.rb +16 -0
  154. data/test/files/2ch.html +108 -0
  155. data/test/files/address_book.rlx +12 -0
  156. data/test/files/address_book.xml +10 -0
  157. data/test/files/bar/bar.xsd +4 -0
  158. data/test/files/dont_hurt_em_why.xml +422 -0
  159. data/test/files/encoding.html +82 -0
  160. data/test/files/encoding.xhtml +84 -0
  161. data/test/files/exslt.xml +8 -0
  162. data/test/files/exslt.xslt +35 -0
  163. data/test/files/foo/foo.xsd +4 -0
  164. data/test/files/metacharset.html +10 -0
  165. data/test/files/noencoding.html +47 -0
  166. data/test/files/po.xml +32 -0
  167. data/test/files/po.xsd +66 -0
  168. data/test/files/shift_jis.html +10 -0
  169. data/test/files/shift_jis.xml +5 -0
  170. data/test/files/snuggles.xml +3 -0
  171. data/test/files/staff.dtd +10 -0
  172. data/test/files/staff.xml +59 -0
  173. data/test/files/staff.xslt +32 -0
  174. data/test/files/tlm.html +850 -0
  175. data/test/files/to_be_xincluded.xml +2 -0
  176. data/test/files/valid_bar.xml +2 -0
  177. data/test/files/xinclude.xml +4 -0
  178. data/test/helper.rb +147 -0
  179. data/test/html/sax/test_parser.rb +138 -0
  180. data/test/html/sax/test_parser_context.rb +46 -0
  181. data/test/html/test_builder.rb +164 -0
  182. data/test/html/test_document.rb +529 -0
  183. data/test/html/test_document_encoding.rb +138 -0
  184. data/test/html/test_document_fragment.rb +254 -0
  185. data/test/html/test_element_description.rb +100 -0
  186. data/test/html/test_named_characters.rb +14 -0
  187. data/test/html/test_node.rb +188 -0
  188. data/test/html/test_node_encoding.rb +27 -0
  189. data/test/test_convert_xpath.rb +135 -0
  190. data/test/test_css_cache.rb +45 -0
  191. data/test/test_encoding_handler.rb +46 -0
  192. data/test/test_memory_leak.rb +152 -0
  193. data/test/test_nokogiri.rb +132 -0
  194. data/test/test_reader.rb +488 -0
  195. data/test/test_soap4r_sax.rb +52 -0
  196. data/test/test_xslt_transforms.rb +254 -0
  197. data/test/xml/node/test_save_options.rb +28 -0
  198. data/test/xml/node/test_subclass.rb +44 -0
  199. data/test/xml/sax/test_parser.rb +338 -0
  200. data/test/xml/sax/test_parser_context.rb +106 -0
  201. data/test/xml/sax/test_push_parser.rb +157 -0
  202. data/test/xml/test_attr.rb +64 -0
  203. data/test/xml/test_attribute_decl.rb +86 -0
  204. data/test/xml/test_builder.rb +248 -0
  205. data/test/xml/test_c14n.rb +151 -0
  206. data/test/xml/test_cdata.rb +48 -0
  207. data/test/xml/test_comment.rb +29 -0
  208. data/test/xml/test_document.rb +742 -0
  209. data/test/xml/test_document_encoding.rb +28 -0
  210. data/test/xml/test_document_fragment.rb +216 -0
  211. data/test/xml/test_dtd.rb +103 -0
  212. data/test/xml/test_dtd_encoding.rb +33 -0
  213. data/test/xml/test_element_content.rb +56 -0
  214. data/test/xml/test_element_decl.rb +73 -0
  215. data/test/xml/test_entity_decl.rb +122 -0
  216. data/test/xml/test_entity_reference.rb +235 -0
  217. data/test/xml/test_namespace.rb +75 -0
  218. data/test/xml/test_node.rb +1029 -0
  219. data/test/xml/test_node_attributes.rb +53 -0
  220. data/test/xml/test_node_encoding.rb +107 -0
  221. data/test/xml/test_node_inheritance.rb +32 -0
  222. data/test/xml/test_node_reparenting.rb +374 -0
  223. data/test/xml/test_node_set.rb +755 -0
  224. data/test/xml/test_parse_options.rb +64 -0
  225. data/test/xml/test_processing_instruction.rb +30 -0
  226. data/test/xml/test_reader_encoding.rb +142 -0
  227. data/test/xml/test_relax_ng.rb +60 -0
  228. data/test/xml/test_schema.rb +94 -0
  229. data/test/xml/test_syntax_error.rb +12 -0
  230. data/test/xml/test_text.rb +45 -0
  231. data/test/xml/test_unparented_node.rb +413 -0
  232. data/test/xml/test_xinclude.rb +83 -0
  233. data/test/xml/test_xpath.rb +295 -0
  234. data/test/xslt/test_custom_functions.rb +129 -0
  235. data/test/xslt/test_exception_handling.rb +37 -0
  236. data/test_all +84 -0
  237. metadata +534 -0
@@ -0,0 +1,152 @@
1
+ require "helper"
2
+
3
+ class TestMemoryLeak < Nokogiri::TestCase
4
+ def setup
5
+ super
6
+ @str = <<EOF
7
+ <!DOCTYPE HTML>
8
+ <html>
9
+ <body>
10
+ <br />
11
+ </body>
12
+ </html>
13
+ EOF
14
+ end
15
+
16
+ if ENV['NOKOGIRI_GC'] # turning these off by default for now
17
+ def test_dont_hurt_em_why
18
+ content = File.open("#{File.dirname(__FILE__)}/files/dont_hurt_em_why.xml").read
19
+ ndoc = Nokogiri::XML(content)
20
+ 2.times do
21
+ ndoc.search('status text').first.inner_text
22
+ ndoc.search('user name').first.inner_text
23
+ GC.start
24
+ end
25
+ end
26
+
27
+ class BadIO
28
+ def read(*args)
29
+ raise 'hell'
30
+ end
31
+
32
+ def write(*args)
33
+ raise 'chickens'
34
+ end
35
+ end
36
+
37
+ def test_for_mem_leak_on_io_callbacks
38
+ io = File.open SNUGGLES_FILE
39
+ Nokogiri::XML.parse(io)
40
+
41
+ loop do
42
+ Nokogiri::XML.parse(BadIO.new) rescue nil
43
+ doc.write BadIO.new rescue nil
44
+ end
45
+ end
46
+
47
+ def test_for_memory_leak
48
+ begin
49
+ # we don't use Dike in any tests, but requiring it has side effects
50
+ # that can create memory leaks, and that's what we're testing for.
51
+ require 'rubygems'
52
+ require 'dike' # do not remove!
53
+
54
+ count_start = count_object_space_documents
55
+ xml_data = <<-EOS
56
+ <test>
57
+ <items>
58
+ <item>abc</item>
59
+ <item>1234</item>
60
+ <item>Zzz</item>
61
+ <items>
62
+ </test>
63
+ EOS
64
+ 20.times do
65
+ doc = Nokogiri::XML(xml_data)
66
+ doc.xpath("//item")
67
+ end
68
+ 2.times { GC.start }
69
+ count_end = count_object_space_documents
70
+ assert((count_end - count_start) <= 2, "memory leak detected")
71
+ rescue LoadError
72
+ puts "\ndike is not installed, skipping memory leak test"
73
+ end
74
+ end
75
+
76
+ def test_node_set_namespace_mem_leak
77
+ xml = Nokogiri::XML "<foo></foo>"
78
+ ctx = Nokogiri::XML::XPathContext.new(xml)
79
+ loop do
80
+ ctx.evaluate("//namespace::*")
81
+ end
82
+ end
83
+
84
+ def test_leak_on_node_replace
85
+ loop do
86
+ doc = Nokogiri.XML("<root><foo /></root>")
87
+ n = Nokogiri::XML::CDATA.new(doc, "bar")
88
+ pivot = doc.root.children[0]
89
+ pivot.replace(n)
90
+ end
91
+ end
92
+
93
+ def test_sax_parser_context
94
+ io = StringIO.new(@str)
95
+
96
+ loop do
97
+ Nokogiri::XML::SAX::ParserContext.new(@str)
98
+ Nokogiri::XML::SAX::ParserContext.new(io)
99
+ io.rewind
100
+
101
+ Nokogiri::HTML::SAX::ParserContext.new(@str)
102
+ Nokogiri::HTML::SAX::ParserContext.new(io)
103
+ io.rewind
104
+ end
105
+ end
106
+
107
+ class JumpingSaxHandler < Nokogiri::XML::SAX::Document
108
+ def initialize(jumptag)
109
+ @jumptag = jumptag
110
+ super()
111
+ end
112
+
113
+ def start_element(name, attrs = [])
114
+ throw @jumptag
115
+ end
116
+ end
117
+
118
+ def test_jumping_sax_handler
119
+ doc = JumpingSaxHandler.new(:foo)
120
+
121
+ loop do
122
+ catch(:foo) do
123
+ Nokogiri::HTML::SAX::Parser.new(doc).parse(@str)
124
+ end
125
+ end
126
+ end
127
+
128
+ def test_in_context_parser_leak
129
+ loop do
130
+ doc = Nokogiri::XML::Document.new
131
+ fragment1 = Nokogiri::XML::DocumentFragment.new(doc, '<foo/>')
132
+ node = fragment1.children[0]
133
+ node.parse('<bar></bar>')
134
+ end
135
+ end
136
+
137
+ def test_leak_on_xpath_string_function
138
+ doc = Nokogiri::XML(@str)
139
+ loop do
140
+ doc.xpath('name(//node())')
141
+ end
142
+ end
143
+ end # if NOKOGIRI_GC
144
+
145
+ private
146
+
147
+ def count_object_space_documents
148
+ count = 0
149
+ ObjectSpace.each_object {|j| count += 1 if j.is_a?(Nokogiri::XML::Document) }
150
+ count
151
+ end
152
+ end
@@ -0,0 +1,132 @@
1
+ require "helper"
2
+
3
+ class TestNokogiri < Nokogiri::TestCase
4
+ def test_versions
5
+ version_match = /\d+\.\d+\.\d+/
6
+ assert_match version_match, Nokogiri::VERSION
7
+
8
+ assert_equal Nokogiri::VERSION_INFO['ruby']['version'], ::RUBY_VERSION
9
+ assert_equal Nokogiri::VERSION_INFO['ruby']['platform'], ::RUBY_PLATFORM
10
+
11
+ if Nokogiri.uses_libxml?
12
+ assert_match version_match, Nokogiri::LIBXML_VERSION
13
+ assert_equal 'extension', Nokogiri::VERSION_INFO['libxml']['binding']
14
+
15
+ assert_match version_match, Nokogiri::VERSION_INFO['libxml']['compiled']
16
+ assert_equal Nokogiri::LIBXML_VERSION, Nokogiri::VERSION_INFO['libxml']['compiled']
17
+
18
+ assert_match version_match, Nokogiri::VERSION_INFO['libxml']['loaded']
19
+ Nokogiri::LIBXML_PARSER_VERSION =~ /(\d)(\d{2})(\d{2})/
20
+ major = $1.to_i
21
+ minor = $2.to_i
22
+ bug = $3.to_i
23
+ assert_equal "#{major}.#{minor}.#{bug}", Nokogiri::VERSION_INFO['libxml']['loaded']
24
+ end
25
+ end
26
+
27
+ def test_libxml_iconv
28
+ assert Nokogiri.const_defined?(:LIBXML_ICONV_ENABLED) if Nokogiri.uses_libxml?
29
+ end
30
+
31
+ def test_parse_with_io
32
+ doc = Nokogiri.parse(
33
+ StringIO.new("<html><head><title></title><body></body></html>")
34
+ )
35
+ assert_instance_of Nokogiri::HTML::Document, doc
36
+ end
37
+
38
+ def test_xml?
39
+ doc = Nokogiri.parse(File.read(XML_FILE))
40
+ assert doc.xml?
41
+ assert !doc.html?
42
+ end
43
+
44
+ def test_html?
45
+ doc = Nokogiri.parse(File.read(HTML_FILE))
46
+ assert !doc.xml?
47
+ assert doc.html?
48
+ end
49
+
50
+ def test_nokogiri_method_with_html
51
+ doc1 = Nokogiri(File.read(HTML_FILE))
52
+ doc2 = Nokogiri.parse(File.read(HTML_FILE))
53
+ assert_equal doc1.serialize, doc2.serialize
54
+ end
55
+
56
+ def test_nokogiri_method_with_block
57
+ doc = Nokogiri { b "bold tag" }
58
+ assert_equal('<b>bold tag</b>', doc.to_html.chomp)
59
+ end
60
+
61
+ def test_make_with_html
62
+ doc = Nokogiri.make("<b>bold tag</b>")
63
+ assert_equal('<b>bold tag</b>', doc.to_html.chomp)
64
+ end
65
+
66
+ def test_make_with_block
67
+ doc = Nokogiri.make { b "bold tag" }
68
+ assert_equal('<b>bold tag</b>', doc.to_html.chomp)
69
+ end
70
+
71
+ SLOP_HTML = <<-END
72
+ <html>
73
+ <body>
74
+ <ul>
75
+ <li class='red'>one</li>
76
+ <li class='blue'>two</li>
77
+ </ul>
78
+ <div>
79
+ one
80
+ <div>div two</div>
81
+ </div>
82
+ </body>
83
+ </html>
84
+ END
85
+
86
+ def test_slop_css
87
+ doc = Nokogiri::Slop(<<-eohtml)
88
+ <html>
89
+ <body>
90
+ <div>
91
+ one
92
+ <div class='foo'>
93
+ div two
94
+ <div class='foo'>
95
+ div three
96
+ </div>
97
+ </div>
98
+ </div>
99
+ </body>
100
+ </html>
101
+ eohtml
102
+ assert_equal "div", doc.html.body.div.div('.foo').name
103
+ end
104
+
105
+ def test_slop
106
+ doc = Nokogiri::Slop(SLOP_HTML)
107
+
108
+ assert_equal "one", doc.html.body.ul.li.first.text
109
+ assert_equal "two", doc.html.body.ul.li(".blue").text
110
+ assert_equal "div two", doc.html.body.div.div.text
111
+
112
+ assert_equal "two", doc.html.body.ul.li(:css => ".blue").text
113
+
114
+ assert_equal "two", doc.html.body.ul.li(:xpath => "position()=2").text
115
+ assert_equal "one", doc.html.body.ul.li(:xpath => ["contains(text(),'o')"]).first.text
116
+ assert_equal "two", doc.html.body.ul.li(:xpath => ["contains(text(),'o')","contains(text(),'t')"]).text
117
+
118
+ assert_raise(NoMethodError) { doc.nonexistent }
119
+ end
120
+
121
+ def test_slop_decorator
122
+ doc = Nokogiri(SLOP_HTML)
123
+ assert !doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
124
+
125
+ doc.slop!
126
+ assert doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
127
+
128
+ doc.slop!
129
+ assert_equal 1, doc.decorators(Nokogiri::XML::Node).select { |d| d == Nokogiri::Decorators::Slop }.size
130
+ end
131
+
132
+ end
@@ -0,0 +1,488 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "helper"
3
+
4
+ class TestReader < Nokogiri::TestCase
5
+ def test_from_io_sets_io_as_source
6
+ io = File.open SNUGGLES_FILE
7
+ reader = Nokogiri::XML::Reader.from_io(io)
8
+ assert_equal io, reader.source
9
+ end
10
+
11
+ def test_empty_element?
12
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
13
+ <xml><city>Paris</city><state/></xml>
14
+ eoxml
15
+
16
+ results = reader.map do |node|
17
+ if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE
18
+ node.empty_element?
19
+ end
20
+ end
21
+ assert_equal [false, false, nil, nil, true, nil], results
22
+ end
23
+
24
+ def test_self_closing?
25
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
26
+ <xml><city>Paris</city><state/></xml>
27
+ eoxml
28
+
29
+ results = reader.map do |node|
30
+ if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE
31
+ node.self_closing?
32
+ end
33
+ end
34
+ assert_equal [false, false, nil, nil, true, nil], results
35
+ end
36
+
37
+ def test_reader_takes_block
38
+ options = nil
39
+ Nokogiri::XML::Reader(File.read(XML_FILE), XML_FILE) do |cfg|
40
+ options = cfg
41
+ options.nonet.nowarning.dtdattr
42
+ end
43
+ assert options.nonet?
44
+ assert options.nowarning?
45
+ assert options.dtdattr?
46
+ end
47
+
48
+ def test_nil_raises
49
+ assert_raises(ArgumentError) {
50
+ Nokogiri::XML::Reader.from_memory(nil)
51
+ }
52
+ assert_raises(ArgumentError) {
53
+ Nokogiri::XML::Reader.from_io(nil)
54
+ }
55
+ end
56
+
57
+ def test_from_io
58
+ io = File.open SNUGGLES_FILE
59
+ reader = Nokogiri::XML::Reader.from_io(io)
60
+ assert_equal false, reader.default?
61
+ assert_equal [false, false, false, false, false, false, false],
62
+ reader.map { |x| x.default? }
63
+ end
64
+
65
+ def test_io
66
+ io = File.open SNUGGLES_FILE
67
+ reader = Nokogiri::XML::Reader(io)
68
+ assert_equal false, reader.default?
69
+ assert_equal [false, false, false, false, false, false, false],
70
+ reader.map { |x| x.default? }
71
+ end
72
+
73
+ def test_string_io
74
+ io = StringIO.new(<<-eoxml)
75
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
76
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
77
+ </x>
78
+ eoxml
79
+ reader = Nokogiri::XML::Reader(io)
80
+ assert_equal false, reader.default?
81
+ assert_equal [false, false, false, false, false, false, false],
82
+ reader.map { |x| x.default? }
83
+ end
84
+
85
+ class ReallyBadIO
86
+ def read(size)
87
+ 'a' * size ** 10
88
+ end
89
+ end
90
+
91
+ class ReallyBadIO4Java
92
+ def read(size=1)
93
+ 'a' * size ** 10
94
+ end
95
+ end
96
+
97
+ def test_io_that_reads_too_much
98
+ if Nokogiri.jruby?
99
+ io = ReallyBadIO4Java.new
100
+ Nokogiri::XML::Reader(io)
101
+ else
102
+ io = ReallyBadIO.new
103
+ Nokogiri::XML::Reader(io)
104
+ end
105
+ end
106
+
107
+ def test_in_memory
108
+ assert Nokogiri::XML::Reader(<<-eoxml)
109
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
110
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
111
+ </x>
112
+ eoxml
113
+ end
114
+
115
+ def test_reader_holds_on_to_string
116
+ xml = <<-eoxml
117
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
118
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
119
+ </x>
120
+ eoxml
121
+ reader = Nokogiri::XML::Reader(xml)
122
+ assert_equal xml, reader.source
123
+ end
124
+
125
+ def test_default?
126
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
127
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
128
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
129
+ </x>
130
+ eoxml
131
+ assert_equal false, reader.default?
132
+ assert_equal [false, false, false, false, false, false, false],
133
+ reader.map { |x| x.default? }
134
+ end
135
+
136
+ def test_value?
137
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
138
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
139
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
140
+ </x>
141
+ eoxml
142
+ assert_equal false, reader.value?
143
+ assert_equal [false, true, false, true, false, true, false],
144
+ reader.map { |x| x.value? }
145
+ end
146
+
147
+ def test_read_error_document
148
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
149
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
150
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
151
+ <foo>
152
+ </x>
153
+ eoxml
154
+ assert_raises(Nokogiri::XML::SyntaxError) do
155
+ reader.each { |node| }
156
+ end
157
+ assert 1, reader.errors.length
158
+ end
159
+
160
+ def test_attributes?
161
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
162
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
163
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
164
+ </x>
165
+ eoxml
166
+ assert_equal false, reader.attributes?
167
+ assert_equal [true, false, true, false, true, false, true],
168
+ reader.map { |x| x.attributes? }
169
+ end
170
+
171
+ def test_attributes
172
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
173
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'
174
+ xmlns='http://mothership.connection.com/'
175
+ >
176
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
177
+ </x>
178
+ eoxml
179
+ assert_equal({}, reader.attributes)
180
+ assert_equal [{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
181
+ 'xmlns'=>'http://mothership.connection.com/'},
182
+ {}, {"awesome"=>"true"}, {}, {"awesome"=>"true"}, {},
183
+ {'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
184
+ 'xmlns'=>'http://mothership.connection.com/'}],
185
+ reader.map { |x| x.attributes }
186
+ end
187
+
188
+ def test_attribute_roundtrip
189
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
190
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'
191
+ xmlns='http://mothership.connection.com/'
192
+ >
193
+ <tenderlove:foo awesome='true' size='giant'>snuggles!</tenderlove:foo>
194
+ </x>
195
+ eoxml
196
+ reader.each do |node|
197
+ node.attributes.each do |key, value|
198
+ assert_equal value, node.attribute(key)
199
+ end
200
+ end
201
+ end
202
+
203
+ def test_attribute_at
204
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
205
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
206
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
207
+ </x>
208
+ eoxml
209
+ assert_nil reader.attribute_at(nil)
210
+ assert_nil reader.attribute_at(0)
211
+ assert_equal ['http://tenderlovemaking.com/', nil, 'true', nil, 'true', nil, 'http://tenderlovemaking.com/'],
212
+ reader.map { |x| x.attribute_at(0) }
213
+ end
214
+
215
+ def test_attribute
216
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
217
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
218
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
219
+ </x>
220
+ eoxml
221
+ assert_nil reader.attribute(nil)
222
+ assert_nil reader.attribute('awesome')
223
+ assert_equal [nil, nil, 'true', nil, 'true', nil, nil],
224
+ reader.map { |x| x.attribute('awesome') }
225
+ end
226
+
227
+ def test_attribute_length
228
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
229
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
230
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
231
+ </x>
232
+ eoxml
233
+ assert_equal 0, reader.attribute_count
234
+ assert_equal [1, 0, 1, 0, 0, 0, 0], reader.map { |x| x.attribute_count }
235
+ end
236
+
237
+ def test_depth
238
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
239
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
240
+ <tenderlove:foo>snuggles!</tenderlove:foo>
241
+ </x>
242
+ eoxml
243
+ assert_equal 0, reader.depth
244
+ assert_equal [0, 1, 1, 2, 1, 1, 0], reader.map { |x| x.depth }
245
+ end
246
+
247
+ def test_encoding
248
+ string = <<-eoxml
249
+ <awesome>
250
+ <p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
251
+ <p xml:lang="ja">日本語が上手です</p>
252
+ </awesome>
253
+ eoxml
254
+ reader = Nokogiri::XML::Reader.from_memory(string, nil, 'UTF-8')
255
+ assert_equal ['UTF-8'], reader.map { |x| x.encoding }.uniq
256
+ end
257
+
258
+ def test_xml_version
259
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
260
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
261
+ <tenderlove:foo>snuggles!</tenderlove:foo>
262
+ </x>
263
+ eoxml
264
+ assert_nil reader.xml_version
265
+ assert_equal ['1.0'], reader.map { |x| x.xml_version }.uniq
266
+ end
267
+
268
+ def test_lang
269
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
270
+ <awesome>
271
+ <p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
272
+ <p xml:lang="ja">日本語が上手です</p>
273
+ </awesome>
274
+ eoxml
275
+ assert_nil reader.lang
276
+ assert_equal [nil, nil, "en", "en", "en", nil, "ja", "ja", "ja", nil, nil],
277
+ reader.map { |x| x.lang }
278
+ end
279
+
280
+ def test_value
281
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
282
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
283
+ <tenderlove:foo>snuggles!</tenderlove:foo>
284
+ </x>
285
+ eoxml
286
+ assert_nil reader.value
287
+ assert_equal [nil, "\n ", nil, "snuggles!", nil, "\n ", nil],
288
+ reader.map { |x| x.value }
289
+ end
290
+
291
+ def test_prefix
292
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
293
+ <x xmlns:edi='http://ecommerce.example.org/schema'>
294
+ <edi:foo>hello</edi:foo>
295
+ </x>
296
+ eoxml
297
+ assert_nil reader.prefix
298
+ assert_equal [nil, nil, "edi", nil, "edi", nil, nil],
299
+ reader.map { |n| n.prefix }
300
+ end
301
+
302
+ def test_node_type
303
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
304
+ <x>
305
+ <y>hello</y>
306
+ </x>
307
+ eoxml
308
+ assert_equal 0, reader.node_type
309
+ assert_equal [1, 14, 1, 3, 15, 14, 15], reader.map { |n| n.node_type }
310
+ end
311
+
312
+ def test_inner_xml
313
+ str = "<x><y>hello</y></x>"
314
+ reader = Nokogiri::XML::Reader.from_memory(str)
315
+
316
+ reader.read
317
+
318
+ assert_equal "<y>hello</y>", reader.inner_xml
319
+ end
320
+
321
+ def test_outer_xml
322
+ str = "<x><y>hello</y></x>"
323
+ reader = Nokogiri::XML::Reader.from_memory(str)
324
+
325
+ reader.read
326
+
327
+ assert_equal str, reader.outer_xml
328
+ end
329
+
330
+ def test_state
331
+ reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
332
+ assert reader.state
333
+ end
334
+
335
+ def test_ns_uri
336
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
337
+ <x xmlns:edi='http://ecommerce.example.org/schema'>
338
+ <edi:foo>hello</edi:foo>
339
+ </x>
340
+ eoxml
341
+ assert_nil reader.namespace_uri
342
+ assert_equal([nil,
343
+ nil,
344
+ "http://ecommerce.example.org/schema",
345
+ nil,
346
+ "http://ecommerce.example.org/schema",
347
+ nil,
348
+ nil],
349
+ reader.map { |n| n.namespace_uri })
350
+ end
351
+
352
+ def test_local_name
353
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
354
+ <x xmlns:edi='http://ecommerce.example.org/schema'>
355
+ <edi:foo>hello</edi:foo>
356
+ </x>
357
+ eoxml
358
+ assert_nil reader.local_name
359
+ assert_equal(["x", "#text", "foo", "#text", "foo", "#text", "x"],
360
+ reader.map { |n| n.local_name })
361
+ end
362
+
363
+ def test_name
364
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
365
+ <x xmlns:edi='http://ecommerce.example.org/schema'>
366
+ <edi:foo>hello</edi:foo>
367
+ </x>
368
+ eoxml
369
+ assert_nil reader.name
370
+ assert_equal(["x", "#text", "edi:foo", "#text", "edi:foo", "#text", "x"],
371
+ reader.map { |n| n.name })
372
+ end
373
+
374
+ def test_base_uri
375
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
376
+ <x xml:base="http://base.example.org/base/">
377
+ <link href="link"/>
378
+ <other xml:base="http://other.example.org/"/>
379
+ <relative xml:base="relative">
380
+ <link href="stuff" />
381
+ </relative>
382
+ </x>
383
+ eoxml
384
+
385
+ assert_nil reader.base_uri
386
+ assert_equal(["http://base.example.org/base/",
387
+ "http://base.example.org/base/",
388
+ "http://base.example.org/base/",
389
+ "http://base.example.org/base/",
390
+ "http://other.example.org/",
391
+ "http://base.example.org/base/",
392
+ "http://base.example.org/base/relative",
393
+ "http://base.example.org/base/relative",
394
+ "http://base.example.org/base/relative",
395
+ "http://base.example.org/base/relative",
396
+ "http://base.example.org/base/relative",
397
+ "http://base.example.org/base/",
398
+ "http://base.example.org/base/"],
399
+ reader.map {|n| n.base_uri })
400
+ end
401
+
402
+ def test_read_from_memory
403
+ called = false
404
+ reader = Nokogiri::XML::Reader.from_memory('<foo>bar</foo>')
405
+ reader.each do |node|
406
+ called = true
407
+ assert node
408
+ end
409
+ assert called
410
+ end
411
+
412
+ def test_large_document_smoke_test
413
+ # simply run on a large document to verify that there no GC issues
414
+ xml = []
415
+ xml << "<elements>"
416
+ 10000.times { |j| xml << "<element id=\"#{j}\"/>" }
417
+ xml << "</elements>"
418
+ xml = xml.join("\n")
419
+
420
+ Nokogiri::XML::Reader.from_memory(xml).each do |e|
421
+ e.attributes
422
+ end
423
+ end
424
+
425
+ def test_correct_outer_xml_inclusion
426
+ xml = Nokogiri::XML::Reader.from_io(StringIO.new(<<-eoxml))
427
+ <root-element>
428
+ <children>
429
+ <child n="1">
430
+ <field>child-1</field>
431
+ </child>
432
+ <child n="2">
433
+ <field>child-2</field>
434
+ </child>
435
+ <child n="3">
436
+ <field>child-3</field>
437
+ </child>
438
+ </children>
439
+ </root-element>
440
+ eoxml
441
+
442
+ nodelengths = []
443
+ has_child2 = []
444
+
445
+ xml.each do |node|
446
+ if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT and node.name == "child"
447
+ nodelengths << node.outer_xml.length
448
+ has_child2 << !!(node.outer_xml =~ /child-2/)
449
+ end
450
+ end
451
+
452
+ assert_equal(nodelengths[0], nodelengths[1])
453
+ assert(has_child2[1])
454
+ assert(!has_child2[0])
455
+ end
456
+
457
+ def test_correct_inner_xml_inclusion
458
+ xml = Nokogiri::XML::Reader.from_io(StringIO.new(<<-eoxml))
459
+ <root-element>
460
+ <children>
461
+ <child n="1">
462
+ <field>child-1</field>
463
+ </child>
464
+ <child n="2">
465
+ <field>child-2</field>
466
+ </child>
467
+ <child n="3">
468
+ <field>child-3</field>
469
+ </child>
470
+ </children>
471
+ </root-element>
472
+ eoxml
473
+
474
+ nodelengths = []
475
+ has_child2 = []
476
+
477
+ xml.each do |node|
478
+ if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT and node.name == "child"
479
+ nodelengths << node.inner_xml.length
480
+ has_child2 << !!(node.inner_xml =~ /child-2/)
481
+ end
482
+ end
483
+
484
+ assert_equal(nodelengths[0], nodelengths[1])
485
+ assert(has_child2[1])
486
+ assert(!has_child2[0])
487
+ end
488
+ end