glebm-nokogiri 1.4.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (277) hide show
  1. data/.autotest +26 -0
  2. data/CHANGELOG.ja.rdoc +411 -0
  3. data/CHANGELOG.rdoc +397 -0
  4. data/Manifest.txt +276 -0
  5. data/README.ja.rdoc +106 -0
  6. data/README.rdoc +132 -0
  7. data/Rakefile +183 -0
  8. data/bin/nokogiri +49 -0
  9. data/deps.rip +5 -0
  10. data/ext/nokogiri/extconf.rb +97 -0
  11. data/ext/nokogiri/html_document.c +154 -0
  12. data/ext/nokogiri/html_document.h +10 -0
  13. data/ext/nokogiri/html_element_description.c +276 -0
  14. data/ext/nokogiri/html_element_description.h +10 -0
  15. data/ext/nokogiri/html_entity_lookup.c +32 -0
  16. data/ext/nokogiri/html_entity_lookup.h +8 -0
  17. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  18. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  19. data/ext/nokogiri/nokogiri.c +95 -0
  20. data/ext/nokogiri/nokogiri.h +153 -0
  21. data/ext/nokogiri/xml_attr.c +94 -0
  22. data/ext/nokogiri/xml_attr.h +9 -0
  23. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  24. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  25. data/ext/nokogiri/xml_cdata.c +56 -0
  26. data/ext/nokogiri/xml_cdata.h +9 -0
  27. data/ext/nokogiri/xml_comment.c +54 -0
  28. data/ext/nokogiri/xml_comment.h +9 -0
  29. data/ext/nokogiri/xml_document.c +464 -0
  30. data/ext/nokogiri/xml_document.h +23 -0
  31. data/ext/nokogiri/xml_document_fragment.c +48 -0
  32. data/ext/nokogiri/xml_document_fragment.h +10 -0
  33. data/ext/nokogiri/xml_dtd.c +202 -0
  34. data/ext/nokogiri/xml_dtd.h +10 -0
  35. data/ext/nokogiri/xml_element_content.c +123 -0
  36. data/ext/nokogiri/xml_element_content.h +10 -0
  37. data/ext/nokogiri/xml_element_decl.c +69 -0
  38. data/ext/nokogiri/xml_element_decl.h +9 -0
  39. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  40. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  41. data/ext/nokogiri/xml_entity_decl.c +110 -0
  42. data/ext/nokogiri/xml_entity_decl.h +10 -0
  43. data/ext/nokogiri/xml_entity_reference.c +52 -0
  44. data/ext/nokogiri/xml_entity_reference.h +9 -0
  45. data/ext/nokogiri/xml_io.c +31 -0
  46. data/ext/nokogiri/xml_io.h +11 -0
  47. data/ext/nokogiri/xml_namespace.c +84 -0
  48. data/ext/nokogiri/xml_namespace.h +13 -0
  49. data/ext/nokogiri/xml_node.c +1347 -0
  50. data/ext/nokogiri/xml_node.h +13 -0
  51. data/ext/nokogiri/xml_node_set.c +418 -0
  52. data/ext/nokogiri/xml_node_set.h +9 -0
  53. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  54. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  55. data/ext/nokogiri/xml_reader.c +665 -0
  56. data/ext/nokogiri/xml_reader.h +10 -0
  57. data/ext/nokogiri/xml_relax_ng.c +168 -0
  58. data/ext/nokogiri/xml_relax_ng.h +9 -0
  59. data/ext/nokogiri/xml_sax_parser.c +286 -0
  60. data/ext/nokogiri/xml_sax_parser.h +39 -0
  61. data/ext/nokogiri/xml_sax_parser_context.c +159 -0
  62. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  63. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  64. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  65. data/ext/nokogiri/xml_schema.c +205 -0
  66. data/ext/nokogiri/xml_schema.h +9 -0
  67. data/ext/nokogiri/xml_syntax_error.c +58 -0
  68. data/ext/nokogiri/xml_syntax_error.h +13 -0
  69. data/ext/nokogiri/xml_text.c +50 -0
  70. data/ext/nokogiri/xml_text.h +9 -0
  71. data/ext/nokogiri/xml_xpath_context.c +276 -0
  72. data/ext/nokogiri/xml_xpath_context.h +9 -0
  73. data/ext/nokogiri/xslt_stylesheet.c +142 -0
  74. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  75. data/lib/nokogiri.rb +133 -0
  76. data/lib/nokogiri/css.rb +25 -0
  77. data/lib/nokogiri/css/generated_parser.rb +669 -0
  78. data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
  79. data/lib/nokogiri/css/node.rb +99 -0
  80. data/lib/nokogiri/css/parser.rb +82 -0
  81. data/lib/nokogiri/css/parser.y +232 -0
  82. data/lib/nokogiri/css/syntax_error.rb +7 -0
  83. data/lib/nokogiri/css/tokenizer.rb +7 -0
  84. data/lib/nokogiri/css/tokenizer.rex +55 -0
  85. data/lib/nokogiri/css/xpath_visitor.rb +169 -0
  86. data/lib/nokogiri/decorators/slop.rb +33 -0
  87. data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
  88. data/lib/nokogiri/ffi/html/document.rb +28 -0
  89. data/lib/nokogiri/ffi/html/element_description.rb +81 -0
  90. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  91. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  92. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  93. data/lib/nokogiri/ffi/libxml.rb +386 -0
  94. data/lib/nokogiri/ffi/structs/common_node.rb +38 -0
  95. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  96. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  97. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  98. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  99. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  100. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  101. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
  102. data/lib/nokogiri/ffi/structs/xml_document.rb +117 -0
  103. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  104. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  105. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  106. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  107. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  108. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  109. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  110. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  111. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  112. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  113. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  114. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  115. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
  116. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  117. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  118. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  119. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +38 -0
  120. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  121. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  122. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  123. data/lib/nokogiri/ffi/weak_bucket.rb +40 -0
  124. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  125. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  126. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  127. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  128. data/lib/nokogiri/ffi/xml/document.rb +162 -0
  129. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  130. data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
  131. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  132. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  133. data/lib/nokogiri/ffi/xml/entity_decl.rb +36 -0
  134. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  135. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  136. data/lib/nokogiri/ffi/xml/node.rb +556 -0
  137. data/lib/nokogiri/ffi/xml/node_set.rb +149 -0
  138. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  139. data/lib/nokogiri/ffi/xml/reader.rb +232 -0
  140. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  141. data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
  142. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  143. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
  144. data/lib/nokogiri/ffi/xml/schema.rb +109 -0
  145. data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
  146. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  147. data/lib/nokogiri/ffi/xml/xpath.rb +9 -0
  148. data/lib/nokogiri/ffi/xml/xpath_context.rb +148 -0
  149. data/lib/nokogiri/ffi/xslt/stylesheet.rb +53 -0
  150. data/lib/nokogiri/html.rb +35 -0
  151. data/lib/nokogiri/html/builder.rb +35 -0
  152. data/lib/nokogiri/html/document.rb +90 -0
  153. data/lib/nokogiri/html/document_fragment.rb +36 -0
  154. data/lib/nokogiri/html/element_description.rb +23 -0
  155. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  156. data/lib/nokogiri/html/sax/parser.rb +48 -0
  157. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  158. data/lib/nokogiri/syntax_error.rb +4 -0
  159. data/lib/nokogiri/version.rb +37 -0
  160. data/lib/nokogiri/version_warning.rb +14 -0
  161. data/lib/nokogiri/xml.rb +67 -0
  162. data/lib/nokogiri/xml/attr.rb +14 -0
  163. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  164. data/lib/nokogiri/xml/builder.rb +418 -0
  165. data/lib/nokogiri/xml/cdata.rb +11 -0
  166. data/lib/nokogiri/xml/character_data.rb +7 -0
  167. data/lib/nokogiri/xml/document.rb +194 -0
  168. data/lib/nokogiri/xml/document_fragment.rb +77 -0
  169. data/lib/nokogiri/xml/dtd.rb +11 -0
  170. data/lib/nokogiri/xml/element_content.rb +36 -0
  171. data/lib/nokogiri/xml/element_decl.rb +13 -0
  172. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  173. data/lib/nokogiri/xml/namespace.rb +13 -0
  174. data/lib/nokogiri/xml/node.rb +793 -0
  175. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  176. data/lib/nokogiri/xml/node_set.rb +325 -0
  177. data/lib/nokogiri/xml/notation.rb +6 -0
  178. data/lib/nokogiri/xml/parse_options.rb +85 -0
  179. data/lib/nokogiri/xml/pp.rb +2 -0
  180. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  181. data/lib/nokogiri/xml/pp/node.rb +56 -0
  182. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  183. data/lib/nokogiri/xml/reader.rb +74 -0
  184. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  185. data/lib/nokogiri/xml/sax.rb +4 -0
  186. data/lib/nokogiri/xml/sax/document.rb +160 -0
  187. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  188. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  189. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  190. data/lib/nokogiri/xml/schema.rb +57 -0
  191. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  192. data/lib/nokogiri/xml/text.rb +9 -0
  193. data/lib/nokogiri/xml/xpath.rb +10 -0
  194. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  195. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  196. data/lib/nokogiri/xslt.rb +48 -0
  197. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  198. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  199. data/tasks/cross_compile.rb +158 -0
  200. data/tasks/test.rb +94 -0
  201. data/test/css/test_nthiness.rb +159 -0
  202. data/test/css/test_parser.rb +282 -0
  203. data/test/css/test_tokenizer.rb +190 -0
  204. data/test/css/test_xpath_visitor.rb +85 -0
  205. data/test/ffi/test_document.rb +35 -0
  206. data/test/files/2ch.html +108 -0
  207. data/test/files/address_book.rlx +12 -0
  208. data/test/files/address_book.xml +10 -0
  209. data/test/files/bar/bar.xsd +4 -0
  210. data/test/files/dont_hurt_em_why.xml +422 -0
  211. data/test/files/exslt.xml +8 -0
  212. data/test/files/exslt.xslt +35 -0
  213. data/test/files/foo/foo.xsd +4 -0
  214. data/test/files/po.xml +32 -0
  215. data/test/files/po.xsd +66 -0
  216. data/test/files/shift_jis.html +10 -0
  217. data/test/files/shift_jis.xml +5 -0
  218. data/test/files/snuggles.xml +3 -0
  219. data/test/files/staff.dtd +10 -0
  220. data/test/files/staff.xml +59 -0
  221. data/test/files/staff.xslt +32 -0
  222. data/test/files/tlm.html +850 -0
  223. data/test/files/valid_bar.xml +2 -0
  224. data/test/helper.rb +169 -0
  225. data/test/html/sax/test_parser.rb +74 -0
  226. data/test/html/sax/test_parser_context.rb +48 -0
  227. data/test/html/test_builder.rb +164 -0
  228. data/test/html/test_document.rb +398 -0
  229. data/test/html/test_document_encoding.rb +77 -0
  230. data/test/html/test_document_fragment.rb +182 -0
  231. data/test/html/test_element_description.rb +98 -0
  232. data/test/html/test_named_characters.rb +14 -0
  233. data/test/html/test_node.rb +181 -0
  234. data/test/html/test_node_encoding.rb +27 -0
  235. data/test/test_convert_xpath.rb +135 -0
  236. data/test/test_css_cache.rb +45 -0
  237. data/test/test_encoding_handler.rb +46 -0
  238. data/test/test_memory_leak.rb +87 -0
  239. data/test/test_nokogiri.rb +138 -0
  240. data/test/test_reader.rb +386 -0
  241. data/test/test_soap4r_sax.rb +52 -0
  242. data/test/test_xslt_transforms.rb +188 -0
  243. data/test/xml/node/test_save_options.rb +20 -0
  244. data/test/xml/node/test_subclass.rb +44 -0
  245. data/test/xml/sax/test_parser.rb +307 -0
  246. data/test/xml/sax/test_parser_context.rb +63 -0
  247. data/test/xml/sax/test_push_parser.rb +139 -0
  248. data/test/xml/test_attr.rb +38 -0
  249. data/test/xml/test_attribute_decl.rb +82 -0
  250. data/test/xml/test_builder.rb +210 -0
  251. data/test/xml/test_cdata.rb +50 -0
  252. data/test/xml/test_comment.rb +29 -0
  253. data/test/xml/test_document.rb +668 -0
  254. data/test/xml/test_document_encoding.rb +26 -0
  255. data/test/xml/test_document_fragment.rb +180 -0
  256. data/test/xml/test_dtd.rb +82 -0
  257. data/test/xml/test_dtd_encoding.rb +33 -0
  258. data/test/xml/test_element_content.rb +56 -0
  259. data/test/xml/test_element_decl.rb +73 -0
  260. data/test/xml/test_entity_decl.rb +120 -0
  261. data/test/xml/test_entity_reference.rb +21 -0
  262. data/test/xml/test_namespace.rb +68 -0
  263. data/test/xml/test_node.rb +865 -0
  264. data/test/xml/test_node_attributes.rb +34 -0
  265. data/test/xml/test_node_encoding.rb +107 -0
  266. data/test/xml/test_node_reparenting.rb +293 -0
  267. data/test/xml/test_node_set.rb +649 -0
  268. data/test/xml/test_parse_options.rb +52 -0
  269. data/test/xml/test_processing_instruction.rb +30 -0
  270. data/test/xml/test_reader_encoding.rb +126 -0
  271. data/test/xml/test_relax_ng.rb +60 -0
  272. data/test/xml/test_schema.rb +89 -0
  273. data/test/xml/test_syntax_error.rb +12 -0
  274. data/test/xml/test_text.rb +38 -0
  275. data/test/xml/test_unparented_node.rb +381 -0
  276. data/test/xml/test_xpath.rb +138 -0
  277. metadata +533 -0
@@ -0,0 +1,398 @@
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_exceptions_remove_newlines
12
+ errors = @html.errors
13
+ assert errors.length > 0, 'has errors'
14
+ errors.each do |error|
15
+ assert_equal(error.to_s.chomp, error.to_s)
16
+ end
17
+ end
18
+
19
+ def test_fragment
20
+ fragment = @html.fragment
21
+ assert_equal 0, fragment.children.length
22
+ end
23
+
24
+ def test_document_takes_config_block
25
+ options = nil
26
+ Nokogiri::HTML(File.read(HTML_FILE), HTML_FILE) do |cfg|
27
+ options = cfg
28
+ options.nonet.nowarning.dtdattr
29
+ end
30
+ assert options.nonet?
31
+ assert options.nowarning?
32
+ assert options.dtdattr?
33
+ end
34
+
35
+ def test_parse_takes_config_block
36
+ options = nil
37
+ Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE) do |cfg|
38
+ options = cfg
39
+ options.nonet.nowarning.dtdattr
40
+ end
41
+ assert options.nonet?
42
+ assert options.nowarning?
43
+ assert options.dtdattr?
44
+ end
45
+
46
+ def test_subclass
47
+ klass = Class.new(Nokogiri::HTML::Document)
48
+ doc = klass.new
49
+ assert_instance_of klass, doc
50
+ end
51
+
52
+ def test_subclass_initialize
53
+ klass = Class.new(Nokogiri::HTML::Document) do
54
+ attr_accessor :initialized_with
55
+
56
+ def initialize(*args)
57
+ @initialized_with = args
58
+ end
59
+ end
60
+ doc = klass.new("uri", "external_id", 1)
61
+ assert_equal ["uri", "external_id", 1], doc.initialized_with
62
+ end
63
+
64
+ def test_subclass_dup
65
+ klass = Class.new(Nokogiri::HTML::Document)
66
+ doc = klass.new.dup
67
+ assert_instance_of klass, doc
68
+ end
69
+
70
+ def test_subclass_parse
71
+ klass = Class.new(Nokogiri::HTML::Document)
72
+ doc = klass.parse(File.read(HTML_FILE))
73
+ assert_equal @html.to_s, doc.to_s
74
+ assert_instance_of klass, doc
75
+ end
76
+
77
+ def test_document_parse_method
78
+ html = Nokogiri::HTML::Document.parse(File.read(HTML_FILE))
79
+ assert_equal @html.to_s, html.to_s
80
+ end
81
+
82
+ ###
83
+ # Nokogiri::HTML returns an empty Document when given a blank string GH#11
84
+ def test_empty_string_returns_empty_doc
85
+ doc = Nokogiri::HTML('')
86
+ assert_instance_of Nokogiri::HTML::Document, doc
87
+ assert_nil doc.root
88
+ end
89
+
90
+ unless %w[2 6] === LIBXML_VERSION.split('.')[0..1]
91
+ # FIXME: this is a hack around broken libxml versions
92
+ def test_to_xhtml_with_indent
93
+ doc = Nokogiri::HTML('<html><body><a>foo</a></body></html>')
94
+ doc = Nokogiri::HTML(doc.to_xhtml(:indent => 2))
95
+ assert_indent 2, doc
96
+ end
97
+
98
+ def test_write_to_xhtml_with_indent
99
+ io = StringIO.new
100
+ doc = Nokogiri::HTML('<html><body><a>foo</a></body></html>')
101
+ doc.write_xhtml_to io, :indent => 5
102
+ io.rewind
103
+ doc = Nokogiri::HTML(io.read)
104
+ assert_indent 5, doc
105
+ end
106
+ end
107
+
108
+ def test_swap_should_not_exist
109
+ assert_raises(NoMethodError) {
110
+ @html.swap
111
+ }
112
+ end
113
+
114
+ def test_namespace_should_not_exist
115
+ assert_raises(NoMethodError) {
116
+ @html.namespace
117
+ }
118
+ end
119
+
120
+ def test_meta_encoding
121
+ assert_equal 'UTF-8', @html.meta_encoding
122
+ end
123
+
124
+ def test_meta_encoding=
125
+ @html.meta_encoding = 'EUC-JP'
126
+ assert_equal 'EUC-JP', @html.meta_encoding
127
+ end
128
+
129
+ def test_meta_encoding_without_head
130
+ html = Nokogiri::HTML('<html><body>foo</body></html>')
131
+ assert_nil html.meta_encoding
132
+
133
+ html.meta_encoding = 'EUC-JP'
134
+ assert_nil html.meta_encoding
135
+ end
136
+
137
+ def test_root_node_parent_is_document
138
+ parent = @html.root.parent
139
+ assert_equal @html, parent
140
+ assert_instance_of Nokogiri::HTML::Document, parent
141
+ end
142
+
143
+ def test_parse_handles_nil_gracefully
144
+ assert_nothing_raised do
145
+ @doc = Nokogiri::HTML::Document.parse(nil)
146
+ end
147
+ assert_instance_of Nokogiri::HTML::Document, @doc
148
+ end
149
+
150
+ def test_parse_empty_document
151
+ doc = Nokogiri::HTML("\n")
152
+ assert_equal 0, doc.css('a').length
153
+ assert_equal 0, doc.xpath('//a').length
154
+ assert_equal 0, doc.search('//a').length
155
+ end
156
+
157
+ def test_HTML_function
158
+ html = Nokogiri::HTML(File.read(HTML_FILE))
159
+ assert html.html?
160
+ end
161
+
162
+ def test_parse_io
163
+ assert doc = File.open(HTML_FILE, 'rb') { |f|
164
+ Document.read_io(f, nil, 'UTF-8',
165
+ XML::ParseOptions::NOERROR | XML::ParseOptions::NOWARNING
166
+ )
167
+ }
168
+ end
169
+
170
+ def test_to_xhtml
171
+ assert_match 'XHTML', @html.to_xhtml
172
+ assert_match 'XHTML', @html.to_xhtml(:encoding => 'UTF-8')
173
+ assert_match 'UTF-8', @html.to_xhtml(:encoding => 'UTF-8')
174
+ end
175
+
176
+ def test_no_xml_header
177
+ html = Nokogiri::HTML(<<-eohtml)
178
+ <html>
179
+ </html>
180
+ eohtml
181
+ assert html.to_html.length > 0, 'html length is too short'
182
+ assert_no_match(/^<\?xml/, html.to_html)
183
+ end
184
+
185
+ def test_document_has_error
186
+ html = Nokogiri::HTML(<<-eohtml)
187
+ <html>
188
+ <body>
189
+ <div awesome="asdf>
190
+ <p>inside div tag</p>
191
+ </div>
192
+ <p>outside div tag</p>
193
+ </body>
194
+ </html>
195
+ eohtml
196
+ assert html.errors.length > 0
197
+ end
198
+
199
+ def test_relative_css
200
+ html = Nokogiri::HTML(<<-eohtml)
201
+ <html>
202
+ <body>
203
+ <div>
204
+ <p>inside div tag</p>
205
+ </div>
206
+ <p>outside div tag</p>
207
+ </body>
208
+ </html>
209
+ eohtml
210
+ set = html.search('div').search('p')
211
+ assert_equal(1, set.length)
212
+ assert_equal('inside div tag', set.first.inner_text)
213
+ end
214
+
215
+ def test_multi_css
216
+ html = Nokogiri::HTML(<<-eohtml)
217
+ <html>
218
+ <body>
219
+ <div>
220
+ <p>p tag</p>
221
+ <a>a tag</a>
222
+ </div>
223
+ </body>
224
+ </html>
225
+ eohtml
226
+ set = html.css('p, a')
227
+ assert_equal(2, set.length)
228
+ assert_equal ['a tag', 'p tag'].sort, set.map { |x| x.content }.sort
229
+ end
230
+
231
+ def test_inner_text
232
+ html = Nokogiri::HTML(<<-eohtml)
233
+ <html>
234
+ <body>
235
+ <div>
236
+ <p>
237
+ Hello world!
238
+ </p>
239
+ </div>
240
+ </body>
241
+ </html>
242
+ eohtml
243
+ node = html.xpath('//div').first
244
+ assert_equal('Hello world!', node.inner_text.strip)
245
+ end
246
+
247
+ def test_find_by_xpath
248
+ found = @html.xpath('//div/a')
249
+ assert_equal 3, found.length
250
+ end
251
+
252
+ def test_find_by_css
253
+ found = @html.css('div > a')
254
+ assert_equal 3, found.length
255
+ end
256
+
257
+ def test_find_by_css_with_square_brackets
258
+ found = @html.css("div[@id='header'] > h1")
259
+ found = @html.css("div[@id='header'] h1") # this blows up on commit 6fa0f6d329d9dbf1cc21c0ac72f7e627bb4c05fc
260
+ assert_equal 1, found.length
261
+ end
262
+
263
+ def test_find_with_function
264
+ found = @html.css("div:awesome() h1", Class.new {
265
+ def awesome divs
266
+ [divs.first]
267
+ end
268
+ }.new)
269
+ end
270
+
271
+ def test_dup_shallow
272
+ found = @html.search('//div/a').first
273
+ dup = found.dup(0)
274
+ assert dup
275
+ assert_equal '', dup.content
276
+ end
277
+
278
+ def test_search_can_handle_xpath_and_css
279
+ found = @html.search('//div/a', 'div > p')
280
+ length = @html.xpath('//div/a').length +
281
+ @html.css('div > p').length
282
+ assert_equal length, found.length
283
+ end
284
+
285
+ def test_dup_document
286
+ assert dup = @html.dup
287
+ assert_not_equal dup, @html
288
+ assert @html.html?
289
+ assert_instance_of Nokogiri::HTML::Document, dup
290
+ assert dup.html?, 'duplicate should be html'
291
+ assert_equal @html.to_s, dup.to_s
292
+ end
293
+
294
+ def test_dup_document_shallow
295
+ assert dup = @html.dup(0)
296
+ assert_not_equal dup, @html
297
+ end
298
+
299
+ def test_dup
300
+ found = @html.search('//div/a').first
301
+ dup = found.dup
302
+ assert dup
303
+ assert_equal found.content, dup.content
304
+ assert_equal found.document, dup.document
305
+ end
306
+
307
+ def test_inner_html
308
+ html = Nokogiri::HTML(<<-eohtml)
309
+ <html>
310
+ <body>
311
+ <div>
312
+ <p>
313
+ Hello world!
314
+ </p>
315
+ </div>
316
+ </body>
317
+ </html>
318
+ eohtml
319
+ node = html.xpath('//div').first
320
+ assert_equal('<p>Helloworld!</p>', node.inner_html.gsub(/\s/, ''))
321
+ end
322
+
323
+ def test_round_trip
324
+ doc = Nokogiri::HTML(@html.inner_html)
325
+ assert_equal @html.root.to_html, doc.root.to_html
326
+ end
327
+
328
+ def test_fragment_contains_text_node
329
+ fragment = Nokogiri::HTML.fragment('fooo')
330
+ assert_equal 1, fragment.children.length
331
+ assert_equal 'fooo', fragment.inner_text
332
+ end
333
+
334
+ def test_fragment_includes_two_tags
335
+ assert_equal 2, Nokogiri::HTML.fragment("<br/><hr/>").children.length
336
+ end
337
+
338
+ def test_relative_css_finder
339
+ doc = Nokogiri::HTML(<<-eohtml)
340
+ <html>
341
+ <body>
342
+ <div class="red">
343
+ <p>
344
+ inside red
345
+ </p>
346
+ </div>
347
+ <div class="green">
348
+ <p>
349
+ inside green
350
+ </p>
351
+ </div>
352
+ </body>
353
+ </html>
354
+ eohtml
355
+ red_divs = doc.css('div.red')
356
+ assert_equal 1, red_divs.length
357
+ p_tags = red_divs.first.css('p')
358
+ assert_equal 1, p_tags.length
359
+ assert_equal 'inside red', p_tags.first.text.strip
360
+ end
361
+
362
+ def test_find_classes
363
+ doc = Nokogiri::HTML(<<-eohtml)
364
+ <html>
365
+ <body>
366
+ <p class="red">RED</p>
367
+ <p class="awesome red">RED</p>
368
+ <p class="notred">GREEN</p>
369
+ <p class="green notred">GREEN</p>
370
+ </body>
371
+ </html>
372
+ eohtml
373
+ list = doc.css('.red')
374
+ assert_equal 2, list.length
375
+ assert_equal %w{ RED RED }, list.map { |x| x.text }
376
+ end
377
+
378
+ def test_parse_can_take_io
379
+ html = nil
380
+ File.open(HTML_FILE, 'rb') { |f|
381
+ html = Nokogiri::HTML(f)
382
+ }
383
+ assert html.html?
384
+ end
385
+
386
+ def test_html?
387
+ assert !@html.xml?
388
+ assert @html.html?
389
+ end
390
+
391
+ def test_serialize
392
+ assert @html.serialize
393
+ assert @html.to_html
394
+ end
395
+ end
396
+ end
397
+ end
398
+
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "helper"
3
+
4
+ module Nokogiri
5
+ module HTML
6
+ if RUBY_VERSION =~ /^1\.9/
7
+ class TestDocumentEncoding < Nokogiri::TestCase
8
+ def test_encoding
9
+ doc = Nokogiri::HTML File.open(SHIFT_JIS_HTML, 'rb')
10
+
11
+ hello = "こんにちは"
12
+
13
+ assert_match doc.encoding, doc.to_html
14
+ assert_match hello.encode('Shift_JIS'), doc.to_html
15
+ assert_equal 'Shift_JIS', doc.to_html.encoding.name
16
+
17
+ assert_match hello, doc.to_html(:encoding => 'UTF-8')
18
+ assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8')
19
+ assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8').encoding.name
20
+ end
21
+
22
+ def test_default_to_encoding_from_string
23
+ bad_charset = <<-eohtml
24
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
25
+ <html>
26
+ <head>
27
+ <meta http-equiv="Content-Type" content="text/html; charset=charset=UTF-8">
28
+ </head>
29
+ <body>
30
+ <a href="http://tenderlovemaking.com/">blah!</a>
31
+ </body>
32
+ </html>
33
+ eohtml
34
+ doc = Nokogiri::HTML(bad_charset)
35
+ assert_equal bad_charset.encoding.name, doc.encoding
36
+
37
+ doc = Nokogiri.parse(bad_charset)
38
+ assert_equal bad_charset.encoding.name, doc.encoding
39
+ end
40
+
41
+ def test_encoding_non_utf8
42
+ orig = '日本語が上手です'
43
+ bin = Encoding::ASCII_8BIT
44
+ [Encoding::Shift_JIS, Encoding::EUC_JP].each do |enc|
45
+ html = <<-eohtml.encode(enc)
46
+ <html>
47
+ <meta http-equiv="Content-Type" content="text/html; charset=#{enc.name}">
48
+ <title xml:lang="ja">#{orig}</title></html>
49
+ eohtml
50
+ text = Nokogiri::HTML.parse(html).at('title').inner_text
51
+ assert_equal(
52
+ orig.encode(enc).force_encoding(bin),
53
+ text.encode(enc).force_encoding(bin)
54
+ )
55
+ end
56
+ end
57
+
58
+ def test_encoding_with_a_bad_name
59
+ bad_charset = <<-eohtml
60
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
61
+ <html>
62
+ <head>
63
+ <meta http-equiv="Content-Type" content="text/html; charset=charset=UTF-8">
64
+ </head>
65
+ <body>
66
+ <a href="http://tenderlovemaking.com/">blah!</a>
67
+ </body>
68
+ </html>
69
+ eohtml
70
+ doc = Nokogiri::HTML(bad_charset, nil, 'askldjfhalsdfjhlkasdfjh')
71
+ assert_equal ['http://tenderlovemaking.com/'],
72
+ doc.css('a').map { |a| a['href'] }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end