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,472 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module HTML
5
+ class TestDocument < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @html = Nokogiri::HTML.parse(File.read(HTML_FILE))
9
+ end
10
+
11
+ def test_nil_css
12
+ # Behavior is undefined but shouldn't break
13
+ assert @html.css(nil)
14
+ assert @html.xpath(nil)
15
+ end
16
+
17
+ def test_exceptions_remove_newlines
18
+ errors = @html.errors
19
+ assert errors.length > 0, 'has errors'
20
+ errors.each do |error|
21
+ assert_equal(error.to_s.chomp, error.to_s)
22
+ end
23
+ end
24
+
25
+ def test_fragment
26
+ fragment = @html.fragment
27
+ assert_equal 0, fragment.children.length
28
+ end
29
+
30
+ def test_document_takes_config_block
31
+ options = nil
32
+ Nokogiri::HTML(File.read(HTML_FILE), HTML_FILE) do |cfg|
33
+ options = cfg
34
+ options.nonet.nowarning.dtdattr
35
+ end
36
+ assert options.nonet?
37
+ assert options.nowarning?
38
+ assert options.dtdattr?
39
+ end
40
+
41
+ def test_parse_takes_config_block
42
+ options = nil
43
+ Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE) do |cfg|
44
+ options = cfg
45
+ options.nonet.nowarning.dtdattr
46
+ end
47
+ assert options.nonet?
48
+ assert options.nowarning?
49
+ assert options.dtdattr?
50
+ end
51
+
52
+ def test_subclass
53
+ klass = Class.new(Nokogiri::HTML::Document)
54
+ doc = klass.new
55
+ assert_instance_of klass, doc
56
+ end
57
+
58
+ def test_subclass_initialize
59
+ klass = Class.new(Nokogiri::HTML::Document) do
60
+ attr_accessor :initialized_with
61
+
62
+ def initialize(*args)
63
+ @initialized_with = args
64
+ end
65
+ end
66
+ doc = klass.new("uri", "external_id", 1)
67
+ assert_equal ["uri", "external_id", 1], doc.initialized_with
68
+ end
69
+
70
+ def test_subclass_dup
71
+ klass = Class.new(Nokogiri::HTML::Document)
72
+ doc = klass.new.dup
73
+ assert_instance_of klass, doc
74
+ end
75
+
76
+ def test_subclass_parse
77
+ klass = Class.new(Nokogiri::HTML::Document)
78
+ doc = klass.parse(File.read(HTML_FILE))
79
+ assert_equal @html.to_s, doc.to_s
80
+ assert_instance_of klass, doc
81
+ end
82
+
83
+ def test_document_parse_method
84
+ html = Nokogiri::HTML::Document.parse(File.read(HTML_FILE))
85
+ assert_equal @html.to_s, html.to_s
86
+ end
87
+
88
+ ###
89
+ # Nokogiri::HTML returns an empty Document when given a blank string GH#11
90
+ def test_empty_string_returns_empty_doc
91
+ doc = Nokogiri::HTML('')
92
+ assert_instance_of Nokogiri::HTML::Document, doc
93
+ assert_nil doc.root
94
+ end
95
+
96
+ unless Nokogiri.uses_libxml? && %w[2 6] === LIBXML_VERSION.split('.')[0..1]
97
+ # FIXME: this is a hack around broken libxml versions
98
+ def test_to_xhtml_with_indent
99
+ doc = Nokogiri::HTML('<html><body><a>foo</a></body></html>')
100
+ doc = Nokogiri::HTML(doc.to_xhtml(:indent => 2))
101
+ assert_indent 2, doc
102
+ end
103
+
104
+ def test_write_to_xhtml_with_indent
105
+ io = StringIO.new
106
+ doc = Nokogiri::HTML('<html><body><a>foo</a></body></html>')
107
+ doc.write_xhtml_to io, :indent => 5
108
+ io.rewind
109
+ doc = Nokogiri::HTML(io.read)
110
+ assert_indent 5, doc
111
+ end
112
+ end
113
+
114
+ def test_swap_should_not_exist
115
+ assert_raises(NoMethodError) {
116
+ @html.swap
117
+ }
118
+ end
119
+
120
+ def test_namespace_should_not_exist
121
+ assert_raises(NoMethodError) {
122
+ @html.namespace
123
+ }
124
+ end
125
+
126
+ def test_meta_encoding
127
+ assert_equal 'UTF-8', @html.meta_encoding
128
+
129
+ html = Nokogiri::HTML(<<-eohtml)
130
+ <html>
131
+ <head>
132
+ <meta http-equiv="X-Content-Type" content="text/html; charset=Shift_JIS">
133
+ </head>
134
+ <body>
135
+ foo
136
+ </body>
137
+ </html>
138
+ eohtml
139
+ assert_nil html.meta_encoding
140
+ end
141
+
142
+ def test_meta_encoding=
143
+ @html.meta_encoding = 'EUC-JP'
144
+ assert_equal 'EUC-JP', @html.meta_encoding
145
+ end
146
+
147
+ def test_title
148
+ assert_equal 'Tender Lovemaking ', @html.title
149
+ doc = Nokogiri::HTML('<html><body>foo</body></html>')
150
+ assert_nil doc.title
151
+ end
152
+
153
+ def test_title=()
154
+ doc = Nokogiri::HTML(<<eohtml)
155
+ <html>
156
+ <head>
157
+ <title>old</title>
158
+ </head>
159
+ <body>
160
+ foo
161
+ </body>
162
+ </html>
163
+ eohtml
164
+ doc.title = 'new'
165
+ assert_equal 'new', doc.title
166
+
167
+ doc = Nokogiri::HTML(<<eohtml)
168
+ <html>
169
+ <head>
170
+ </head>
171
+ <body>
172
+ foo
173
+ </body>
174
+ </html>
175
+ eohtml
176
+ doc.title = 'new'
177
+ assert_equal 'new', doc.title
178
+
179
+ doc = Nokogiri::HTML(<<eohtml)
180
+ <html>
181
+ <body>
182
+ foo
183
+ </body>
184
+ </html>
185
+ eohtml
186
+ doc.title = 'new'
187
+ if Nokogiri.uses_libxml?
188
+ assert_nil doc.title
189
+ else
190
+ assert_equal 'new', doc.title
191
+ end
192
+ end
193
+
194
+ def test_meta_encoding_without_head
195
+ html = Nokogiri::HTML('<html><body>foo</body></html>')
196
+ assert_nil html.meta_encoding
197
+
198
+ html.meta_encoding = 'EUC-JP'
199
+ assert_nil html.meta_encoding
200
+ end
201
+
202
+ def test_root_node_parent_is_document
203
+ parent = @html.root.parent
204
+ assert_equal @html, parent
205
+ assert_instance_of Nokogiri::HTML::Document, parent
206
+ end
207
+
208
+ def test_parse_handles_nil_gracefully
209
+ assert_nothing_raised do
210
+ @doc = Nokogiri::HTML::Document.parse(nil)
211
+ end
212
+ assert_instance_of Nokogiri::HTML::Document, @doc
213
+ end
214
+
215
+ def test_parse_empty_document
216
+ doc = Nokogiri::HTML("\n")
217
+ assert_equal 0, doc.css('a').length
218
+ assert_equal 0, doc.xpath('//a').length
219
+ assert_equal 0, doc.search('//a').length
220
+ end
221
+
222
+ def test_HTML_function
223
+ html = Nokogiri::HTML(File.read(HTML_FILE))
224
+ assert html.html?
225
+ end
226
+
227
+ def test_parse_io
228
+ assert File.open(HTML_FILE, 'rb') { |f|
229
+ Document.read_io(f, nil, 'UTF-8',
230
+ XML::ParseOptions::NOERROR | XML::ParseOptions::NOWARNING
231
+ )
232
+ }
233
+ end
234
+
235
+ def test_parse_temp_file
236
+ temp_html_file = Tempfile.new("TEMP_HTML_FILE")
237
+ File.open(HTML_FILE, 'rb') { |f| temp_html_file.write f.read }
238
+ temp_html_file.close
239
+ temp_html_file.open
240
+ assert_equal Nokogiri::HTML.parse(File.read(HTML_FILE)).xpath('//div/a').length,
241
+ Nokogiri::HTML.parse(temp_html_file).xpath('//div/a').length
242
+ end
243
+
244
+ def test_to_xhtml
245
+ assert_match 'XHTML', @html.to_xhtml
246
+ assert_match 'XHTML', @html.to_xhtml(:encoding => 'UTF-8')
247
+ assert_match 'UTF-8', @html.to_xhtml(:encoding => 'UTF-8')
248
+ end
249
+
250
+ def test_no_xml_header
251
+ html = Nokogiri::HTML(<<-eohtml)
252
+ <html>
253
+ </html>
254
+ eohtml
255
+ assert html.to_html.length > 0, 'html length is too short'
256
+ assert_no_match(/^<\?xml/, html.to_html)
257
+ end
258
+
259
+ def test_document_has_error
260
+ html = Nokogiri::HTML(<<-eohtml)
261
+ <html>
262
+ <body>
263
+ <div awesome="asdf>
264
+ <p>inside div tag</p>
265
+ </div>
266
+ <p>outside div tag</p>
267
+ </body>
268
+ </html>
269
+ eohtml
270
+ assert html.errors.length > 0
271
+ end
272
+
273
+ def test_relative_css
274
+ html = Nokogiri::HTML(<<-eohtml)
275
+ <html>
276
+ <body>
277
+ <div>
278
+ <p>inside div tag</p>
279
+ </div>
280
+ <p>outside div tag</p>
281
+ </body>
282
+ </html>
283
+ eohtml
284
+ set = html.search('div').search('p')
285
+ assert_equal(1, set.length)
286
+ assert_equal('inside div tag', set.first.inner_text)
287
+ end
288
+
289
+ def test_multi_css
290
+ html = Nokogiri::HTML(<<-eohtml)
291
+ <html>
292
+ <body>
293
+ <div>
294
+ <p>p tag</p>
295
+ <a>a tag</a>
296
+ </div>
297
+ </body>
298
+ </html>
299
+ eohtml
300
+ set = html.css('p, a')
301
+ assert_equal(2, set.length)
302
+ assert_equal ['a tag', 'p tag'].sort, set.map { |x| x.content }.sort
303
+ end
304
+
305
+ def test_inner_text
306
+ html = Nokogiri::HTML(<<-eohtml)
307
+ <html>
308
+ <body>
309
+ <div>
310
+ <p>
311
+ Hello world!
312
+ </p>
313
+ </div>
314
+ </body>
315
+ </html>
316
+ eohtml
317
+ node = html.xpath('//div').first
318
+ assert_equal('Hello world!', node.inner_text.strip)
319
+ end
320
+
321
+ def test_find_by_xpath
322
+ found = @html.xpath('//div/a')
323
+ assert_equal 3, found.length
324
+ end
325
+
326
+ def test_find_by_css
327
+ found = @html.css('div > a')
328
+ assert_equal 3, found.length
329
+ end
330
+
331
+ def test_find_by_css_with_square_brackets
332
+ found = @html.css("div[@id='header'] > h1")
333
+ found = @html.css("div[@id='header'] h1") # this blows up on commit 6fa0f6d329d9dbf1cc21c0ac72f7e627bb4c05fc
334
+ assert_equal 1, found.length
335
+ end
336
+
337
+ def test_find_with_function
338
+ assert @html.css("div:awesome() h1", Class.new {
339
+ def awesome divs
340
+ [divs.first]
341
+ end
342
+ }.new)
343
+ end
344
+
345
+ def test_dup_shallow
346
+ found = @html.search('//div/a').first
347
+ dup = found.dup(0)
348
+ assert dup
349
+ assert_equal '', dup.content
350
+ end
351
+
352
+ def test_search_can_handle_xpath_and_css
353
+ found = @html.search('//div/a', 'div > p')
354
+ length = @html.xpath('//div/a').length +
355
+ @html.css('div > p').length
356
+ assert_equal length, found.length
357
+ end
358
+
359
+ def test_dup_document
360
+ assert dup = @html.dup
361
+ assert_not_equal dup, @html
362
+ assert @html.html?
363
+ assert_instance_of Nokogiri::HTML::Document, dup
364
+ assert dup.html?, 'duplicate should be html'
365
+ assert_equal @html.to_s, dup.to_s
366
+ end
367
+
368
+ def test_dup_document_shallow
369
+ assert dup = @html.dup(0)
370
+ assert_not_equal dup, @html
371
+ end
372
+
373
+ def test_dup
374
+ found = @html.search('//div/a').first
375
+ dup = found.dup
376
+ assert dup
377
+ assert_equal found.content, dup.content
378
+ assert_equal found.document, dup.document
379
+ end
380
+
381
+ def test_inner_html
382
+ html = Nokogiri::HTML(<<-eohtml)
383
+ <html>
384
+ <body>
385
+ <div>
386
+ <p>
387
+ Hello world!
388
+ </p>
389
+ </div>
390
+ </body>
391
+ </html>
392
+ eohtml
393
+ node = html.xpath('//div').first
394
+ assert_equal('<p>Helloworld!</p>', node.inner_html.gsub(/\s/, ''))
395
+ end
396
+
397
+ def test_round_trip
398
+ doc = Nokogiri::HTML(@html.inner_html)
399
+ assert_equal @html.root.to_html, doc.root.to_html
400
+ end
401
+
402
+ def test_fragment_contains_text_node
403
+ fragment = Nokogiri::HTML.fragment('fooo')
404
+ assert_equal 1, fragment.children.length
405
+ assert_equal 'fooo', fragment.inner_text
406
+ end
407
+
408
+ def test_fragment_includes_two_tags
409
+ assert_equal 2, Nokogiri::HTML.fragment("<br/><hr/>").children.length
410
+ end
411
+
412
+ def test_relative_css_finder
413
+ doc = Nokogiri::HTML(<<-eohtml)
414
+ <html>
415
+ <body>
416
+ <div class="red">
417
+ <p>
418
+ inside red
419
+ </p>
420
+ </div>
421
+ <div class="green">
422
+ <p>
423
+ inside green
424
+ </p>
425
+ </div>
426
+ </body>
427
+ </html>
428
+ eohtml
429
+ red_divs = doc.css('div.red')
430
+ assert_equal 1, red_divs.length
431
+ p_tags = red_divs.first.css('p')
432
+ assert_equal 1, p_tags.length
433
+ assert_equal 'inside red', p_tags.first.text.strip
434
+ end
435
+
436
+ def test_find_classes
437
+ doc = Nokogiri::HTML(<<-eohtml)
438
+ <html>
439
+ <body>
440
+ <p class="red">RED</p>
441
+ <p class="awesome red">RED</p>
442
+ <p class="notred">GREEN</p>
443
+ <p class="green notred">GREEN</p>
444
+ </body>
445
+ </html>
446
+ eohtml
447
+ list = doc.css('.red')
448
+ assert_equal 2, list.length
449
+ assert_equal %w{ RED RED }, list.map { |x| x.text }
450
+ end
451
+
452
+ def test_parse_can_take_io
453
+ html = nil
454
+ File.open(HTML_FILE, 'rb') { |f|
455
+ html = Nokogiri::HTML(f)
456
+ }
457
+ assert html.html?
458
+ end
459
+
460
+ def test_html?
461
+ assert !@html.xml?
462
+ assert @html.html?
463
+ end
464
+
465
+ def test_serialize
466
+ assert @html.serialize
467
+ assert @html.to_html
468
+ end
469
+ end
470
+ end
471
+ end
472
+