caring-nokogiri 1.4.1.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG.ja.rdoc +330 -0
  3. data/CHANGELOG.rdoc +324 -0
  4. data/Manifest.txt +269 -0
  5. data/README.ja.rdoc +105 -0
  6. data/README.rdoc +118 -0
  7. data/Rakefile +248 -0
  8. data/bin/nokogiri +49 -0
  9. data/ext/nokogiri/extconf.rb +147 -0
  10. data/ext/nokogiri/html_document.c +145 -0
  11. data/ext/nokogiri/html_document.h +10 -0
  12. data/ext/nokogiri/html_element_description.c +272 -0
  13. data/ext/nokogiri/html_element_description.h +10 -0
  14. data/ext/nokogiri/html_entity_lookup.c +32 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser_context.c +92 -0
  17. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  18. data/ext/nokogiri/nokogiri.c +95 -0
  19. data/ext/nokogiri/nokogiri.h +145 -0
  20. data/ext/nokogiri/xml_attr.c +92 -0
  21. data/ext/nokogiri/xml_attr.h +9 -0
  22. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  23. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  24. data/ext/nokogiri/xml_cdata.c +54 -0
  25. data/ext/nokogiri/xml_cdata.h +9 -0
  26. data/ext/nokogiri/xml_comment.c +52 -0
  27. data/ext/nokogiri/xml_comment.h +9 -0
  28. data/ext/nokogiri/xml_document.c +388 -0
  29. data/ext/nokogiri/xml_document.h +24 -0
  30. data/ext/nokogiri/xml_document_fragment.c +46 -0
  31. data/ext/nokogiri/xml_document_fragment.h +10 -0
  32. data/ext/nokogiri/xml_dtd.c +192 -0
  33. data/ext/nokogiri/xml_dtd.h +10 -0
  34. data/ext/nokogiri/xml_element_content.c +123 -0
  35. data/ext/nokogiri/xml_element_content.h +10 -0
  36. data/ext/nokogiri/xml_element_decl.c +69 -0
  37. data/ext/nokogiri/xml_element_decl.h +9 -0
  38. data/ext/nokogiri/xml_entity_decl.c +97 -0
  39. data/ext/nokogiri/xml_entity_decl.h +10 -0
  40. data/ext/nokogiri/xml_entity_reference.c +50 -0
  41. data/ext/nokogiri/xml_entity_reference.h +9 -0
  42. data/ext/nokogiri/xml_io.c +31 -0
  43. data/ext/nokogiri/xml_io.h +11 -0
  44. data/ext/nokogiri/xml_namespace.c +74 -0
  45. data/ext/nokogiri/xml_namespace.h +12 -0
  46. data/ext/nokogiri/xml_node.c +1060 -0
  47. data/ext/nokogiri/xml_node.h +13 -0
  48. data/ext/nokogiri/xml_node_set.c +397 -0
  49. data/ext/nokogiri/xml_node_set.h +9 -0
  50. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  51. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  52. data/ext/nokogiri/xml_reader.c +593 -0
  53. data/ext/nokogiri/xml_reader.h +10 -0
  54. data/ext/nokogiri/xml_relax_ng.c +159 -0
  55. data/ext/nokogiri/xml_relax_ng.h +9 -0
  56. data/ext/nokogiri/xml_sax_parser.c +286 -0
  57. data/ext/nokogiri/xml_sax_parser.h +43 -0
  58. data/ext/nokogiri/xml_sax_parser_context.c +155 -0
  59. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  60. data/ext/nokogiri/xml_sax_push_parser.c +91 -0
  61. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  62. data/ext/nokogiri/xml_schema.c +156 -0
  63. data/ext/nokogiri/xml_schema.h +9 -0
  64. data/ext/nokogiri/xml_syntax_error.c +261 -0
  65. data/ext/nokogiri/xml_syntax_error.h +13 -0
  66. data/ext/nokogiri/xml_text.c +48 -0
  67. data/ext/nokogiri/xml_text.h +9 -0
  68. data/ext/nokogiri/xml_xpath.c +53 -0
  69. data/ext/nokogiri/xml_xpath.h +11 -0
  70. data/ext/nokogiri/xml_xpath_context.c +239 -0
  71. data/ext/nokogiri/xml_xpath_context.h +9 -0
  72. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  73. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  74. data/lib/nokogiri.rb +116 -0
  75. data/lib/nokogiri/css.rb +25 -0
  76. data/lib/nokogiri/css/generated_parser.rb +646 -0
  77. data/lib/nokogiri/css/generated_tokenizer.rb +143 -0
  78. data/lib/nokogiri/css/node.rb +99 -0
  79. data/lib/nokogiri/css/parser.rb +82 -0
  80. data/lib/nokogiri/css/parser.y +227 -0
  81. data/lib/nokogiri/css/syntax_error.rb +7 -0
  82. data/lib/nokogiri/css/tokenizer.rb +7 -0
  83. data/lib/nokogiri/css/tokenizer.rex +54 -0
  84. data/lib/nokogiri/css/xpath_visitor.rb +162 -0
  85. data/lib/nokogiri/decorators/slop.rb +33 -0
  86. data/lib/nokogiri/ffi/html/document.rb +28 -0
  87. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  88. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  89. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  90. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  91. data/lib/nokogiri/ffi/libxml.rb +356 -0
  92. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  93. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  94. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  95. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  96. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  97. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  98. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  99. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  100. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  101. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  102. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  103. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  104. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  105. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  106. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  107. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  108. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  109. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  110. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  111. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  112. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +15 -0
  113. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  114. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  115. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  116. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  117. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  118. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  119. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  120. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  121. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  122. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  123. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  124. data/lib/nokogiri/ffi/xml/document.rb +135 -0
  125. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  126. data/lib/nokogiri/ffi/xml/dtd.rb +69 -0
  127. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  128. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  129. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  130. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  131. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  132. data/lib/nokogiri/ffi/xml/node.rb +444 -0
  133. data/lib/nokogiri/ffi/xml/node_set.rb +133 -0
  134. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  135. data/lib/nokogiri/ffi/xml/reader.rb +227 -0
  136. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  137. data/lib/nokogiri/ffi/xml/sax/parser.rb +142 -0
  138. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  139. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +39 -0
  140. data/lib/nokogiri/ffi/xml/schema.rb +92 -0
  141. data/lib/nokogiri/ffi/xml/syntax_error.rb +91 -0
  142. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  143. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  144. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  145. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  146. data/lib/nokogiri/html.rb +35 -0
  147. data/lib/nokogiri/html/builder.rb +35 -0
  148. data/lib/nokogiri/html/document.rb +88 -0
  149. data/lib/nokogiri/html/document_fragment.rb +15 -0
  150. data/lib/nokogiri/html/element_description.rb +23 -0
  151. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  152. data/lib/nokogiri/html/sax/parser.rb +48 -0
  153. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  154. data/lib/nokogiri/syntax_error.rb +4 -0
  155. data/lib/nokogiri/version.rb +33 -0
  156. data/lib/nokogiri/version_warning.rb +11 -0
  157. data/lib/nokogiri/xml.rb +67 -0
  158. data/lib/nokogiri/xml/attr.rb +14 -0
  159. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  160. data/lib/nokogiri/xml/builder.rb +405 -0
  161. data/lib/nokogiri/xml/cdata.rb +11 -0
  162. data/lib/nokogiri/xml/character_data.rb +7 -0
  163. data/lib/nokogiri/xml/document.rb +131 -0
  164. data/lib/nokogiri/xml/document_fragment.rb +73 -0
  165. data/lib/nokogiri/xml/dtd.rb +11 -0
  166. data/lib/nokogiri/xml/element_content.rb +36 -0
  167. data/lib/nokogiri/xml/element_decl.rb +13 -0
  168. data/lib/nokogiri/xml/entity_decl.rb +15 -0
  169. data/lib/nokogiri/xml/fragment_handler.rb +73 -0
  170. data/lib/nokogiri/xml/namespace.rb +13 -0
  171. data/lib/nokogiri/xml/node.rb +665 -0
  172. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  173. data/lib/nokogiri/xml/node_set.rb +307 -0
  174. data/lib/nokogiri/xml/notation.rb +6 -0
  175. data/lib/nokogiri/xml/parse_options.rb +85 -0
  176. data/lib/nokogiri/xml/pp.rb +2 -0
  177. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  178. data/lib/nokogiri/xml/pp/node.rb +56 -0
  179. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  180. data/lib/nokogiri/xml/reader.rb +74 -0
  181. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  182. data/lib/nokogiri/xml/sax.rb +4 -0
  183. data/lib/nokogiri/xml/sax/document.rb +160 -0
  184. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  185. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  186. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  187. data/lib/nokogiri/xml/schema.rb +61 -0
  188. data/lib/nokogiri/xml/syntax_error.rb +38 -0
  189. data/lib/nokogiri/xml/xpath.rb +10 -0
  190. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  191. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  192. data/lib/nokogiri/xslt.rb +48 -0
  193. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  194. data/lib/xsd/xmlparser/nokogiri.rb +71 -0
  195. data/tasks/test.rb +100 -0
  196. data/test/css/test_nthiness.rb +159 -0
  197. data/test/css/test_parser.rb +277 -0
  198. data/test/css/test_tokenizer.rb +183 -0
  199. data/test/css/test_xpath_visitor.rb +76 -0
  200. data/test/ffi/test_document.rb +35 -0
  201. data/test/files/2ch.html +108 -0
  202. data/test/files/address_book.rlx +12 -0
  203. data/test/files/address_book.xml +10 -0
  204. data/test/files/bar/bar.xsd +4 -0
  205. data/test/files/dont_hurt_em_why.xml +422 -0
  206. data/test/files/exslt.xml +8 -0
  207. data/test/files/exslt.xslt +35 -0
  208. data/test/files/foo/foo.xsd +4 -0
  209. data/test/files/po.xml +32 -0
  210. data/test/files/po.xsd +66 -0
  211. data/test/files/shift_jis.html +10 -0
  212. data/test/files/shift_jis.xml +5 -0
  213. data/test/files/snuggles.xml +3 -0
  214. data/test/files/staff.dtd +10 -0
  215. data/test/files/staff.xml +59 -0
  216. data/test/files/staff.xslt +32 -0
  217. data/test/files/tlm.html +850 -0
  218. data/test/files/valid_bar.xml +2 -0
  219. data/test/helper.rb +136 -0
  220. data/test/html/sax/test_parser.rb +64 -0
  221. data/test/html/sax/test_parser_context.rb +48 -0
  222. data/test/html/test_builder.rb +164 -0
  223. data/test/html/test_document.rb +390 -0
  224. data/test/html/test_document_encoding.rb +77 -0
  225. data/test/html/test_document_fragment.rb +142 -0
  226. data/test/html/test_element_description.rb +94 -0
  227. data/test/html/test_named_characters.rb +14 -0
  228. data/test/html/test_node.rb +228 -0
  229. data/test/html/test_node_encoding.rb +27 -0
  230. data/test/test_convert_xpath.rb +135 -0
  231. data/test/test_css_cache.rb +45 -0
  232. data/test/test_gc.rb +15 -0
  233. data/test/test_memory_leak.rb +77 -0
  234. data/test/test_nokogiri.rb +138 -0
  235. data/test/test_reader.rb +358 -0
  236. data/test/test_xslt_transforms.rb +131 -0
  237. data/test/xml/node/test_save_options.rb +20 -0
  238. data/test/xml/node/test_subclass.rb +44 -0
  239. data/test/xml/sax/test_parser.rb +307 -0
  240. data/test/xml/sax/test_parser_context.rb +56 -0
  241. data/test/xml/sax/test_push_parser.rb +106 -0
  242. data/test/xml/test_attr.rb +38 -0
  243. data/test/xml/test_attribute_decl.rb +82 -0
  244. data/test/xml/test_builder.rb +167 -0
  245. data/test/xml/test_cdata.rb +38 -0
  246. data/test/xml/test_comment.rb +29 -0
  247. data/test/xml/test_document.rb +607 -0
  248. data/test/xml/test_document_encoding.rb +26 -0
  249. data/test/xml/test_document_fragment.rb +144 -0
  250. data/test/xml/test_dtd.rb +82 -0
  251. data/test/xml/test_dtd_encoding.rb +33 -0
  252. data/test/xml/test_element_content.rb +56 -0
  253. data/test/xml/test_element_decl.rb +73 -0
  254. data/test/xml/test_entity_decl.rb +83 -0
  255. data/test/xml/test_entity_reference.rb +21 -0
  256. data/test/xml/test_namespace.rb +68 -0
  257. data/test/xml/test_node.rb +889 -0
  258. data/test/xml/test_node_attributes.rb +34 -0
  259. data/test/xml/test_node_encoding.rb +107 -0
  260. data/test/xml/test_node_set.rb +531 -0
  261. data/test/xml/test_parse_options.rb +52 -0
  262. data/test/xml/test_processing_instruction.rb +30 -0
  263. data/test/xml/test_reader_encoding.rb +126 -0
  264. data/test/xml/test_relax_ng.rb +60 -0
  265. data/test/xml/test_schema.rb +89 -0
  266. data/test/xml/test_syntax_error.rb +27 -0
  267. data/test/xml/test_text.rb +30 -0
  268. data/test/xml/test_unparented_node.rb +381 -0
  269. data/test/xml/test_xpath.rb +106 -0
  270. metadata +428 -0
@@ -0,0 +1,159 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module CSS
5
+ class TestNthiness < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ doc = <<EOF
9
+ <html>
10
+ <table>
11
+ <tr><td>row1 </td></tr>
12
+ <tr><td>row2 </td></tr>
13
+ <tr><td>row3 </td></tr>
14
+ <tr><td>row4 </td></tr>
15
+ <tr><td>row5 </td></tr>
16
+ <tr><td>row6 </td></tr>
17
+ <tr><td>row7 </td></tr>
18
+ <tr><td>row8 </td></tr>
19
+ <tr><td>row9 </td></tr>
20
+ <tr><td>row10 </td></tr>
21
+ <tr><td>row11 </td></tr>
22
+ <tr><td>row12 </td></tr>
23
+ <tr><td>row13 </td></tr>
24
+ <tr><td>row14 </td></tr>
25
+ </table>
26
+ <div>
27
+ <b>bold1 </b>
28
+ <i>italic1 </i>
29
+ <b>bold2 </b>
30
+ <i>italic2 </i>
31
+ <p>para1 </p>
32
+ <b>bold3 </b>
33
+ </div>
34
+ <div>
35
+ <p>para2 </p>
36
+ <p>para3 </p>
37
+ </div>
38
+ <div>
39
+ <p>para4 </p>
40
+ </div>
41
+ <p class='empty'></p>
42
+ <p class='not-empty'><b></b></p>
43
+ </html>
44
+ EOF
45
+ @parser = Nokogiri.HTML doc
46
+ end
47
+
48
+
49
+ def test_even
50
+ assert_result_rows [2,4,6,8,10,12,14], @parser.search("table/tr:nth(even)")
51
+ end
52
+
53
+ def test_odd
54
+ assert_result_rows [1,3,5,7,9,11,13], @parser.search("table/tr:nth(odd)")
55
+ end
56
+
57
+ def test_2n
58
+ assert_equal @parser.search("table/tr:nth(even)").inner_text, @parser.search("table/tr:nth(2n)").inner_text
59
+ end
60
+
61
+ def test_2np1
62
+ assert_equal @parser.search("table/tr:nth(odd)").inner_text, @parser.search("table/tr:nth(2n+1)").inner_text
63
+ end
64
+
65
+ def test_4np3
66
+ assert_result_rows [3,7,11], @parser.search("table/tr:nth(4n+3)")
67
+ end
68
+
69
+ def test_3np4
70
+ assert_result_rows [4,7,10,13], @parser.search("table/tr:nth(3n+4)")
71
+ end
72
+
73
+ def test_mnp3
74
+ assert_result_rows [1,2,3], @parser.search("table/tr:nth(-n+3)")
75
+ end
76
+
77
+ def test_np3
78
+ assert_result_rows [3,4,5,6,7,8,9,10,11,12,13,14], @parser.search("table/tr:nth(n+3)")
79
+ end
80
+
81
+ def test_first
82
+ assert_result_rows [1], @parser.search("table/tr:first")
83
+ assert_result_rows [1], @parser.search("table/tr:first()")
84
+ end
85
+
86
+ def test_last
87
+ assert_result_rows [14], @parser.search("table/tr:last")
88
+ assert_result_rows [14], @parser.search("table/tr:last()")
89
+ end
90
+
91
+ def test_first_child
92
+ assert_result_rows [1], @parser.search("div/b:first-child"), "bold"
93
+ assert_result_rows [1], @parser.search("table/tr:first-child")
94
+ end
95
+
96
+ def test_last_child
97
+ assert_result_rows [3], @parser.search("div/b:last-child"), "bold"
98
+ assert_result_rows [14], @parser.search("table/tr:last-child")
99
+ end
100
+
101
+ def test_first_of_type
102
+ assert_result_rows [1], @parser.search("table/tr:first-of-type")
103
+ assert_result_rows [1], @parser.search("div/b:first-of-type"), "bold"
104
+ end
105
+
106
+ def test_last_of_type
107
+ assert_result_rows [14], @parser.search("table/tr:last-of-type")
108
+ assert_result_rows [3], @parser.search("div/b:last-of-type"), "bold"
109
+ end
110
+
111
+ def test_only_of_type
112
+ assert_result_rows [1,4], @parser.search("div/p:only-of-type"), "para"
113
+ end
114
+
115
+ def test_only_child
116
+ assert_result_rows [4], @parser.search("div/p:only-child"), "para"
117
+ end
118
+
119
+ def test_empty
120
+ result = @parser.search("p:empty")
121
+ assert_equal 1, result.size, "unexpected number of rows returned: '#{result.inner_text}'"
122
+ assert_equal 'empty', result.first['class']
123
+ end
124
+
125
+ def test_parent
126
+ result = @parser.search("p:parent")
127
+ assert_equal 5, result.size
128
+ 0.upto(3) do |j|
129
+ assert_equal "para#{j+1} ", result[j].inner_text
130
+ end
131
+ assert_equal "not-empty", result[4]['class']
132
+ end
133
+
134
+ def test_siblings
135
+ doc = <<-EOF
136
+ <html><body><div>
137
+ <p id="1">p1 </p>
138
+ <p id="2">p2 </p>
139
+ <p id="3">p3 </p>
140
+ <p id="4">p4 </p>
141
+ <p id="5">p5 </p>
142
+ EOF
143
+ parser = Nokogiri.HTML doc
144
+ assert_equal 2, parser.search("#3 ~ p").size
145
+ assert_equal "p4 p5 ", parser.search("#3 ~ p").inner_text
146
+ assert_equal 0, parser.search("#5 ~ p").size
147
+
148
+ assert_equal 1, parser.search("#3 + p").size
149
+ assert_equal "p4 ", parser.search("#3 + p").inner_text
150
+ assert_equal 0, parser.search("#5 + p").size
151
+ end
152
+
153
+ def assert_result_rows intarray, result, word="row"
154
+ assert_equal intarray.size, result.size, "unexpected number of rows returned: '#{result.inner_text}'"
155
+ assert_equal intarray.map{|j| "#{word}#{j}"}.join(' '), result.inner_text.strip, result.inner_text
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,277 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module CSS
5
+ class TestParser < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @parser = Nokogiri::CSS::Parser.new
9
+ end
10
+
11
+ def test_extra_single_quote
12
+ assert_raises(CSS::SyntaxError) { @parser.parse("'") }
13
+ end
14
+
15
+ def test_syntax_error_raised
16
+ assert_raises(CSS::SyntaxError) { @parser.parse("a[x=]") }
17
+ end
18
+
19
+ def test_find_by_type
20
+ ast = @parser.parse("a:nth-child(2)").first
21
+ matches = ast.find_by_type(
22
+ [:CONDITIONAL_SELECTOR,
23
+ [:ELEMENT_NAME],
24
+ [:PSEUDO_CLASS,
25
+ [:FUNCTION]
26
+ ]
27
+ ]
28
+ )
29
+ assert_equal(1, matches.length)
30
+ assert_equal(ast, matches.first)
31
+ end
32
+
33
+ def test_to_type
34
+ ast = @parser.parse("a:nth-child(2)").first
35
+ assert_equal(
36
+ [:CONDITIONAL_SELECTOR,
37
+ [:ELEMENT_NAME],
38
+ [:PSEUDO_CLASS,
39
+ [:FUNCTION]
40
+ ]
41
+ ], ast.to_type
42
+ )
43
+ end
44
+
45
+ def test_to_a
46
+ asts = @parser.parse("a:nth-child(2)")
47
+ assert_equal(
48
+ [:CONDITIONAL_SELECTOR,
49
+ [:ELEMENT_NAME, ["a"]],
50
+ [:PSEUDO_CLASS,
51
+ [:FUNCTION, ["nth-child("], ["2"]]
52
+ ]
53
+ ], asts.first.to_a
54
+ )
55
+ end
56
+
57
+ def test_dashmatch
58
+ assert_xpath "//a[@class = 'bar' or starts-with(@class, concat('bar', '-'))]",
59
+ @parser.parse("a[@class|='bar']")
60
+ assert_xpath "//a[@class = 'bar' or starts-with(@class, concat('bar', '-'))]",
61
+ @parser.parse("a[@class |= 'bar']")
62
+ end
63
+
64
+ def test_includes
65
+ assert_xpath "//a[contains(concat(\" \", @class, \" \"),concat(\" \", 'bar', \" \"))]",
66
+ @parser.parse("a[@class~='bar']")
67
+ assert_xpath "//a[contains(concat(\" \", @class, \" \"),concat(\" \", 'bar', \" \"))]",
68
+ @parser.parse("a[@class ~= 'bar']")
69
+ end
70
+
71
+ def test_function_with_arguments
72
+ assert_xpath "//*[position() = 2 and self::a]",
73
+ @parser.parse("a[2]")
74
+ assert_xpath "//*[position() = 2 and self::a]",
75
+ @parser.parse("a:nth-child(2)")
76
+ end
77
+
78
+ def test_carrot
79
+ assert_xpath "//a[starts-with(@id, 'Boing')]",
80
+ @parser.parse("a[id^='Boing']")
81
+ assert_xpath "//a[starts-with(@id, 'Boing')]",
82
+ @parser.parse("a[id ^= 'Boing']")
83
+ end
84
+
85
+ def test_suffix_match
86
+ assert_xpath "//a[substring(@id, string-length(@id) - string-length('Boing') + 1, string-length('Boing')) = 'Boing']",
87
+ @parser.parse("a[id$='Boing']")
88
+ assert_xpath "//a[substring(@id, string-length(@id) - string-length('Boing') + 1, string-length('Boing')) = 'Boing']",
89
+ @parser.parse("a[id $= 'Boing']")
90
+ end
91
+
92
+ def test_attributes_with_at
93
+ ## This is non standard CSS
94
+ assert_xpath "//a[@id = 'Boing']",
95
+ @parser.parse("a[@id='Boing']")
96
+ assert_xpath "//a[@id = 'Boing']",
97
+ @parser.parse("a[@id = 'Boing']")
98
+ end
99
+
100
+ def test_attributes_with_at_and_stuff
101
+ ## This is non standard CSS
102
+ assert_xpath "//a[@id = 'Boing']//div",
103
+ @parser.parse("a[@id='Boing'] div")
104
+ end
105
+
106
+ def test_not_equal
107
+ ## This is non standard CSS
108
+ assert_xpath "//a[child::text() != 'Boing']",
109
+ @parser.parse("a[text()!='Boing']")
110
+ assert_xpath "//a[child::text() != 'Boing']",
111
+ @parser.parse("a[text() != 'Boing']")
112
+ end
113
+
114
+ def test_function
115
+ ## This is non standard CSS
116
+ assert_xpath "//a[child::text()]",
117
+ @parser.parse("a[text()]")
118
+
119
+ ## This is non standard CSS
120
+ assert_xpath "//child::text()",
121
+ @parser.parse("text()")
122
+
123
+ ## This is non standard CSS
124
+ assert_xpath "//a[contains(child::text(), 'Boing')]",
125
+ @parser.parse("a[text()*='Boing']")
126
+ assert_xpath "//a[contains(child::text(), 'Boing')]",
127
+ @parser.parse("a[text() *= 'Boing']")
128
+
129
+ ## This is non standard CSS
130
+ assert_xpath "//script//comment()",
131
+ @parser.parse("script comment()")
132
+ end
133
+
134
+ def test_nonstandard_nth_selectors
135
+ ## These are non standard CSS
136
+ assert_xpath '//a[position() = 99]', @parser.parse('a:eq(99)')
137
+ assert_xpath '//a[position() = 1]', @parser.parse('a:first') # no parens
138
+ assert_xpath '//a[position() = last()]', @parser.parse('a:last') # no parens
139
+ assert_xpath '//a[position() = 99]', @parser.parse('a:nth(99)')
140
+ assert_xpath '//a[position() = 1]', @parser.parse('a:first()')
141
+ assert_xpath '//a[position() = last()]', @parser.parse('a:last()')
142
+ assert_xpath '//a[node()]', @parser.parse('a:parent')
143
+ end
144
+
145
+ def test_standard_nth_selectors
146
+ assert_xpath '//a[position() = 99]', @parser.parse('a:nth-of-type(99)')
147
+ assert_xpath '//a[position() = 1]', @parser.parse('a:first-of-type()')
148
+ assert_xpath '//a[position() = last()]', @parser.parse('a:last-of-type()')
149
+ assert_xpath '//a[position() = 1]', @parser.parse('a:first-of-type') # no parens
150
+ assert_xpath '//a[position() = last()]', @parser.parse('a:last-of-type') # no parens
151
+ assert_xpath '//a[position() = last() - 99]', @parser.parse('a:nth-last-of-type(99)')
152
+ assert_xpath '//a[position() = last() - 99]', @parser.parse('a:nth-last-of-type(99)')
153
+ end
154
+
155
+ def test_nth_child_selectors
156
+ assert_xpath '//*[position() = 1 and self::a]', @parser.parse('a:first-child')
157
+ assert_xpath '//*[position() = last() and self::a]', @parser.parse('a:last-child')
158
+ assert_xpath '//*[position() = 99 and self::a]', @parser.parse('a:nth-child(99)')
159
+ assert_xpath '//*[position() = last() - 99 and self::a]', @parser.parse('a:nth-last-child(99)')
160
+ end
161
+
162
+ def test_miscellaneous_selectors
163
+ assert_xpath '//*[last() = 1 and self::a]',
164
+ @parser.parse('a:only-child')
165
+ assert_xpath '//a[last() = 1]', @parser.parse('a:only-of-type')
166
+ assert_xpath '//a[not(node())]', @parser.parse('a:empty')
167
+ end
168
+
169
+ def test_nth_a_n_plus_b
170
+ assert_xpath '//a[(position() mod 2) = 0]', @parser.parse('a:nth-of-type(2n)')
171
+ assert_xpath '//a[(position() >= 1) and (((position()-1) mod 2) = 0)]', @parser.parse('a:nth-of-type(2n+1)')
172
+ assert_xpath '//a[(position() mod 2) = 0]', @parser.parse('a:nth-of-type(even)')
173
+ assert_xpath '//a[(position() >= 1) and (((position()-1) mod 2) = 0)]', @parser.parse('a:nth-of-type(odd)')
174
+ assert_xpath '//a[(position() >= 3) and (((position()-3) mod 4) = 0)]', @parser.parse('a:nth-of-type(4n+3)')
175
+ assert_xpath '//a[(position() <= 3) and (((position()-3) mod 1) = 0)]', @parser.parse('a:nth-of-type(-1n+3)')
176
+ assert_xpath '//a[(position() <= 3) and (((position()-3) mod 1) = 0)]', @parser.parse('a:nth-of-type(-n+3)')
177
+ assert_xpath '//a[(position() >= 3) and (((position()-3) mod 1) = 0)]', @parser.parse('a:nth-of-type(1n+3)')
178
+ assert_xpath '//a[(position() >= 3) and (((position()-3) mod 1) = 0)]', @parser.parse('a:nth-of-type(n+3)')
179
+ end
180
+
181
+ def test_preceding_selector
182
+ assert_xpath "//F[preceding-sibling::E]",
183
+ @parser.parse("E ~ F")
184
+ end
185
+
186
+ def test_direct_preceding_selector
187
+ assert_xpath "//E/following-sibling::*[1]/self::F",
188
+ @parser.parse("E + F")
189
+ end
190
+
191
+ def test_attribute
192
+ assert_xpath "//h1[@a = 'Tender Lovemaking']",
193
+ @parser.parse("h1[a='Tender Lovemaking']")
194
+ end
195
+
196
+ def test_id
197
+ assert_xpath "//*[@id = 'foo']", @parser.parse('#foo')
198
+ end
199
+
200
+ def test_pseudo_class_no_ident
201
+ assert_xpath "//*[link(.)]", @parser.parse(':link')
202
+ end
203
+
204
+ def test_pseudo_class
205
+ assert_xpath "//a[link(.)]", @parser.parse('a:link')
206
+ assert_xpath "//a[visited(.)]", @parser.parse('a:visited')
207
+ assert_xpath "//a[hover(.)]", @parser.parse('a:hover')
208
+ assert_xpath "//a[active(.)]", @parser.parse('a:active')
209
+ assert_xpath "//a[active(.) and contains(concat(' ', @class, ' '), ' foo ')]",
210
+ @parser.parse('a:active.foo')
211
+ end
212
+
213
+ def test_star
214
+ assert_xpath "//*", @parser.parse('*')
215
+ assert_xpath "//*[contains(concat(' ', @class, ' '), ' pastoral ')]",
216
+ @parser.parse('*.pastoral')
217
+ end
218
+
219
+ def test_class
220
+ assert_xpath "//*[contains(concat(' ', @class, ' '), ' a ') and contains(concat(' ', @class, ' '), ' b ')]",
221
+ @parser.parse('.a.b')
222
+ assert_xpath "//*[contains(concat(' ', @class, ' '), ' awesome ')]",
223
+ @parser.parse('.awesome')
224
+ assert_xpath "//foo[contains(concat(' ', @class, ' '), ' awesome ')]",
225
+ @parser.parse('foo.awesome')
226
+ assert_xpath "//foo//*[contains(concat(' ', @class, ' '), ' awesome ')]",
227
+ @parser.parse('foo .awesome')
228
+ end
229
+
230
+ def test_not_so_simple_not
231
+ assert_xpath "//*[@id = 'p' and not(contains(concat(' ', @class, ' '), ' a '))]",
232
+ @parser.parse('#p:not(.a)')
233
+ assert_xpath "//p[contains(concat(' ', @class, ' '), ' a ') and not(contains(concat(' ', @class, ' '), ' b '))]",
234
+ @parser.parse('p.a:not(.b)')
235
+ assert_xpath "//p[@a = 'foo' and not(contains(concat(' ', @class, ' '), ' b '))]",
236
+ @parser.parse("p[a='foo']:not(.b)")
237
+ end
238
+
239
+ def test_ident
240
+ assert_xpath '//x', @parser.parse('x')
241
+ end
242
+
243
+ def test_parse_space
244
+ assert_xpath '//x//y', @parser.parse('x y')
245
+ end
246
+
247
+ def test_parse_descendant
248
+ assert_xpath '//x/y', @parser.parse('x > y')
249
+ end
250
+
251
+ def test_parse_slash
252
+ ## This is non standard CSS
253
+ assert_xpath '//x/y', @parser.parse('x/y')
254
+ end
255
+
256
+ def test_parse_doubleslash
257
+ ## This is non standard CSS
258
+ assert_xpath '//x//y', @parser.parse('x//y')
259
+ end
260
+
261
+ def test_multi_path
262
+ assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y, y > z')
263
+ assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y,y > z')
264
+ ###
265
+ # TODO: should we make this work?
266
+ # assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y | y > z')
267
+ end
268
+
269
+ def assert_xpath expecteds, asts
270
+ expecteds = [expecteds].flatten
271
+ expecteds.zip(asts).each do |expected, actual|
272
+ assert_equal expected, actual.to_xpath
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end
@@ -0,0 +1,183 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "helper"
4
+
5
+ module Nokogiri
6
+ module CSS
7
+ class TestTokenizer < Nokogiri::TestCase
8
+ def setup
9
+ super
10
+ @scanner = Nokogiri::CSS::Tokenizer.new
11
+ end
12
+
13
+ def test_unicode
14
+ @scanner.scan("a日本語")
15
+ assert_tokens([[:IDENT, 'a日本語']], @scanner)
16
+ end
17
+
18
+ def test_tokenize_bad_single_quote
19
+ @scanner.scan("'")
20
+ assert_tokens([["'", "'"]], @scanner)
21
+ end
22
+
23
+ def test_not_equal
24
+ @scanner.scan("h1[a!='Tender Lovemaking']")
25
+ assert_tokens([ [:IDENT, 'h1'],
26
+ [:LSQUARE, '['],
27
+ [:IDENT, 'a'],
28
+ [:NOT_EQUAL, '!='],
29
+ [:STRING, "'Tender Lovemaking'"],
30
+ [:RSQUARE, ']'],
31
+ ], @scanner)
32
+ end
33
+
34
+ def test_negation
35
+ @scanner.scan("p:not(.a)")
36
+ assert_tokens([ [:IDENT, 'p'],
37
+ [:NOT, ':not('],
38
+ ['.', '.'],
39
+ [:IDENT, 'a'],
40
+ [:RPAREN, ')'],
41
+ ], @scanner)
42
+ end
43
+
44
+ def test_function
45
+ @scanner.scan("script comment()")
46
+ assert_tokens([ [:IDENT, 'script'],
47
+ [:S, ' '],
48
+ [:FUNCTION, 'comment('],
49
+ [:RPAREN, ')'],
50
+ ], @scanner)
51
+ end
52
+
53
+ def test_preceding_selector
54
+ @scanner.scan("E ~ F")
55
+ assert_tokens([ [:IDENT, 'E'],
56
+ [:TILDE, ' ~ '],
57
+ [:IDENT, 'F'],
58
+ ], @scanner)
59
+ end
60
+
61
+ def test_scan_attribute_string
62
+ @scanner.scan("h1[a='Tender Lovemaking']")
63
+ assert_tokens([ [:IDENT, 'h1'],
64
+ [:LSQUARE, '['],
65
+ [:IDENT, 'a'],
66
+ [:EQUAL, '='],
67
+ [:STRING, "'Tender Lovemaking'"],
68
+ [:RSQUARE, ']'],
69
+ ], @scanner)
70
+ @scanner.scan('h1[a="Tender Lovemaking"]')
71
+ assert_tokens([ [:IDENT, 'h1'],
72
+ [:LSQUARE, '['],
73
+ [:IDENT, 'a'],
74
+ [:EQUAL, '='],
75
+ [:STRING, '"Tender Lovemaking"'],
76
+ [:RSQUARE, ']'],
77
+ ], @scanner)
78
+ end
79
+
80
+ def test_scan_id
81
+ @scanner.scan('#foo')
82
+ assert_tokens([ [:HASH, '#foo'] ], @scanner)
83
+ end
84
+
85
+ def test_scan_pseudo
86
+ @scanner.scan('a:visited')
87
+ assert_tokens([ [:IDENT, 'a'],
88
+ [':', ':'],
89
+ [:IDENT, 'visited']
90
+ ], @scanner)
91
+ end
92
+
93
+ def test_scan_star
94
+ @scanner.scan('*')
95
+ assert_tokens([ ['*', '*'], ], @scanner)
96
+ end
97
+
98
+ def test_scan_class
99
+ @scanner.scan('x.awesome')
100
+ assert_tokens([ [:IDENT, 'x'],
101
+ ['.', '.'],
102
+ [:IDENT, 'awesome'],
103
+ ], @scanner)
104
+ end
105
+
106
+ def test_scan_greater
107
+ @scanner.scan('x > y')
108
+ assert_tokens([ [:IDENT, 'x'],
109
+ [:GREATER, ' > '],
110
+ [:IDENT, 'y']
111
+ ], @scanner)
112
+ end
113
+
114
+ def test_scan_slash
115
+ @scanner.scan('x/y')
116
+ assert_tokens([ [:IDENT, 'x'],
117
+ [:SLASH, '/'],
118
+ [:IDENT, 'y']
119
+ ], @scanner)
120
+ end
121
+
122
+ def test_scan_doubleslash
123
+ @scanner.scan('x//y')
124
+ assert_tokens([ [:IDENT, 'x'],
125
+ [:DOUBLESLASH, '//'],
126
+ [:IDENT, 'y']
127
+ ], @scanner)
128
+ end
129
+
130
+ def test_scan_function_selector
131
+ @scanner.scan('x:eq(0)')
132
+ assert_tokens([ [:IDENT, 'x'],
133
+ [':', ':'],
134
+ [:FUNCTION, 'eq('],
135
+ [:NUMBER, "0"],
136
+ [:RPAREN, ')'],
137
+ ], @scanner)
138
+ end
139
+
140
+ def test_scan_an_plus_b
141
+ @scanner.scan('x:nth-child(5n+3)')
142
+ assert_tokens([ [:IDENT, 'x'],
143
+ [':', ':'],
144
+ [:FUNCTION, 'nth-child('],
145
+ [:NUMBER, '5'],
146
+ [:IDENT, 'n'],
147
+ [:PLUS, '+'],
148
+ [:NUMBER, '3'],
149
+ [:RPAREN, ')'],
150
+ ], @scanner)
151
+
152
+ @scanner.scan('x:nth-child(-1n+3)')
153
+ assert_tokens([ [:IDENT, 'x'],
154
+ [':', ':'],
155
+ [:FUNCTION, 'nth-child('],
156
+ [:NUMBER, '-1'],
157
+ [:IDENT, 'n'],
158
+ [:PLUS, '+'],
159
+ [:NUMBER, '3'],
160
+ [:RPAREN, ')'],
161
+ ], @scanner)
162
+
163
+ @scanner.scan('x:nth-child(-n+3)')
164
+ assert_tokens([ [:IDENT, 'x'],
165
+ [':', ':'],
166
+ [:FUNCTION, 'nth-child('],
167
+ [:IDENT, '-n'],
168
+ [:PLUS, '+'],
169
+ [:NUMBER, '3'],
170
+ [:RPAREN, ')'],
171
+ ], @scanner)
172
+ end
173
+
174
+ def assert_tokens(tokens, scanner)
175
+ toks = []
176
+ while tok = @scanner.next_token
177
+ toks << tok
178
+ end
179
+ assert_equal(tokens, toks)
180
+ end
181
+ end
182
+ end
183
+ end