fdlint 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +14 -0
  3. data/README.md +68 -0
  4. data/Rakefile +92 -0
  5. data/bin/fdlint +17 -0
  6. data/lib/base_parser.rb +143 -0
  7. data/lib/cmd_runner.rb +145 -0
  8. data/lib/context.rb +31 -0
  9. data/lib/css/parser.rb +186 -0
  10. data/lib/css/reader.rb +30 -0
  11. data/lib/css/rule/check_compression_rule.rb +48 -0
  12. data/lib/css/rule/checklist.rb +45 -0
  13. data/lib/css/struct.rb +111 -0
  14. data/lib/encoding_error.rb +6 -0
  15. data/lib/file_validator.rb +38 -0
  16. data/lib/helper/code_type.rb +50 -0
  17. data/lib/helper/color_string.rb +44 -0
  18. data/lib/helper/file_reader.rb +22 -0
  19. data/lib/helper/strenc.rb +65 -0
  20. data/lib/html/parser.rb +212 -0
  21. data/lib/html/query.rb +96 -0
  22. data/lib/html/rule/check_tag_rule.rb +80 -0
  23. data/lib/html/struct.rb +291 -0
  24. data/lib/js/expr/expr.rb +66 -0
  25. data/lib/js/expr/left_hand.rb +63 -0
  26. data/lib/js/expr/operate.rb +92 -0
  27. data/lib/js/expr/primary.rb +166 -0
  28. data/lib/js/parser.rb +116 -0
  29. data/lib/js/rule/all.rb +35 -0
  30. data/lib/js/rule/checklist.rb +41 -0
  31. data/lib/js/rule/file_checker.rb +42 -0
  32. data/lib/js/rule/helper.rb +96 -0
  33. data/lib/js/rule/no_global.rb +87 -0
  34. data/lib/js/stat/if.rb +25 -0
  35. data/lib/js/stat/iter.rb +85 -0
  36. data/lib/js/stat/stat.rb +117 -0
  37. data/lib/js/stat/switch.rb +65 -0
  38. data/lib/js/stat/try.rb +28 -0
  39. data/lib/js/stat/var.rb +40 -0
  40. data/lib/js/struct.rb +248 -0
  41. data/lib/log_entry.rb +49 -0
  42. data/lib/node.rb +28 -0
  43. data/lib/parse_error.rb +13 -0
  44. data/lib/parser_visitable.rb +138 -0
  45. data/lib/position_info.rb +46 -0
  46. data/lib/printer/base_printer.rb +24 -0
  47. data/lib/printer/console_printer.rb +66 -0
  48. data/lib/printer/nocolor_printer.rb +27 -0
  49. data/lib/printer/vim_printer.rb +19 -0
  50. data/lib/rule.rb +241 -0
  51. data/lib/rule_helper.rb +14 -0
  52. data/lib/runner.rb +225 -0
  53. data/rules.d/css.rule +127 -0
  54. data/rules.d/html.dtd.rule +22 -0
  55. data/rules.d/html.prop.rule +51 -0
  56. data/rules.d/html.tag.rule +136 -0
  57. data/rules.d/js.file.rule +13 -0
  58. data/rules.d/js.jquery.rule +56 -0
  59. data/rules.d/js.mergefile.rule +71 -0
  60. data/rules.d/js.rule +84 -0
  61. data/test/all_tests.rb +84 -0
  62. data/test/cli/cli_test.rb +70 -0
  63. data/test/cli/log_level_test.rb +51 -0
  64. data/test/cli/output_format_test.rb +47 -0
  65. data/test/cli/type_test.rb +77 -0
  66. data/test/css/mac_line_end_support_test.rb +38 -0
  67. data/test/css/parser_test.rb +276 -0
  68. data/test/css/rule/check_encoding_test.rb +66 -0
  69. data/test/css/rule/check_list_rule_test.rb +167 -0
  70. data/test/css/rule/compression_test.rb +53 -0
  71. data/test/css/rule/file_name_test.rb +76 -0
  72. data/test/fixtures/css/broken.css +4 -0
  73. data/test/fixtures/css/cbu/36.css +52 -0
  74. data/test/fixtures/css/cbu/china_top.css +324 -0
  75. data/test/fixtures/css/cbu/default-merge.css +3 -0
  76. data/test/fixtures/css/cbu/default.css +13 -0
  77. data/test/fixtures/css/cbu/diy-merge.css +25 -0
  78. data/test/fixtures/css/cbu/fns-v1.css +27 -0
  79. data/test/fixtures/css/cbu/index_v0.1.css +12 -0
  80. data/test/fixtures/css/cbu/merge.css +11 -0
  81. data/test/fixtures/css/cbu/min.css +2 -0
  82. data/test/fixtures/css/cbu/my_home_admin.css +126 -0
  83. data/test/fixtures/css/cbu/nav.css +95 -0
  84. data/test/fixtures/css/cbu/pic_list.css +386 -0
  85. data/test/fixtures/css/cbu/quote-edit.css +18 -0
  86. data/test/fixtures/css/cbu/selloffer.shopwindow.css +1 -0
  87. data/test/fixtures/css/cbu/v1.css +9 -0
  88. data/test/fixtures/css/css3.css +30 -0
  89. data/test/fixtures/css/empty-min.css +0 -0
  90. data/test/fixtures/css/empty.css +0 -0
  91. data/test/fixtures/css/font-family.css +4 -0
  92. data/test/fixtures/css/gb-good.css +14 -0
  93. data/test/fixtures/css/gb_using_star.css +4 -0
  94. data/test/fixtures/css/import.css +18 -0
  95. data/test/fixtures/css/mac-line-sep-err-min.css +1 -0
  96. data/test/fixtures/css/mac-line-sep-err.css +1 -0
  97. data/test/fixtures/css/mac-line-sep-good-min.css +1 -0
  98. data/test/fixtures/css/mac-line-sep-good.css +1 -0
  99. data/test/fixtures/css/multi-encoding-in-a-file.css +0 -0
  100. data/test/fixtures/css/simple.css +1 -0
  101. data/test/fixtures/css/using_expr.css +8 -0
  102. data/test/fixtures/css/using_hack.css +21 -0
  103. data/test/fixtures/css/using_id.css +1 -0
  104. data/test/fixtures/css/using_star.css +4 -0
  105. data/test/fixtures/css/utf8_good.css +6 -0
  106. data/test/fixtures/css/utf8_good_declaring_charset.css +7 -0
  107. data/test/fixtures/css/utf8_using_star.css +5 -0
  108. data/test/fixtures/html/1-1.html +120 -0
  109. data/test/fixtures/html/1-2.html +120 -0
  110. data/test/fixtures/html/cms.html +373 -0
  111. data/test/fixtures/html/css_out_of_head.html +9 -0
  112. data/test/fixtures/html/fdev-template.html +22 -0
  113. data/test/fixtures/html/google.com.html +33 -0
  114. data/test/fixtures/html/mixed_log_levels.html +4 -0
  115. data/test/fixtures/html/mixed_types.html +13 -0
  116. data/test/fixtures/html/no_dtd.html +6 -0
  117. data/test/fixtures/html/readme.html +94 -0
  118. data/test/fixtures/html/review.board.html +163 -0
  119. data/test/fixtures/html/syntax_err.html +3 -0
  120. data/test/fixtures/html/train/detail/345/233/276/346/226/207/347/273/223/345/220/210.html +208 -0
  121. data/test/fixtures/html/train/detail/347/232/204Flash.html +212 -0
  122. data/test/fixtures/html/train/detail/347/232/204Vedio.html +212 -0
  123. data/test/fixtures/html/train/index.html +37 -0
  124. data/test/fixtures/html/train/test.html +1 -0
  125. data/test/fixtures/html/train//344/277/256/346/224/271/344/270/200/347/272/247/345/210/206/347/261/273.html +112 -0
  126. data/test/fixtures/html/train//344/277/256/346/224/271/345/255/220/345/210/206/347/261/273.html +108 -0
  127. data/test/fixtures/html/train//344/277/256/346/224/271/350/257/276/347/250/213.html +195 -0
  128. data/test/fixtures/html/train//345/215/232/345/256/242/350/256/276/347/275/256.html +142 -0
  129. data/test/fixtures/html/train//346/265/217/350/247/210/350/256/260/345/275/225.html +191 -0
  130. data/test/fixtures/html/train//346/267/273/345/212/240/344/270/200/347/272/247/345/210/206/347/261/273.html +113 -0
  131. data/test/fixtures/html/train//346/267/273/345/212/240/345/255/220/345/210/206/347/261/273.html +112 -0
  132. data/test/fixtures/html/train//346/267/273/345/212/240/350/257/276/347/250/213.html +195 -0
  133. data/test/fixtures/html/train//347/231/273/345/275/225.html +20 -0
  134. data/test/fixtures/html/train//347/256/241/347/220/206/345/210/206/347/261/273.html +210 -0
  135. data/test/fixtures/html/train//347/256/241/347/220/206/345/217/215/351/246/210.html +222 -0
  136. data/test/fixtures/html/train//347/256/241/347/220/206/350/257/276/347/250/213.html +284 -0
  137. data/test/fixtures/html/train//347/256/241/347/220/206/350/264/246/346/210/267.html +107 -0
  138. data/test/fixtures/html/train//347/275/221/344/270/212/345/237/271/350/256/255home/351/241/265.html +354 -0
  139. data/test/fixtures/html/train//347/275/221/345/225/206/345/237/271/350/256/255list/351/241/265.html +255 -0
  140. data/test/fixtures/html/train//350/256/276/347/275/256/351/246/226/351/241/265/346/216/250/350/215/220.html +168 -0
  141. data/test/fixtures/html/train//350/257/264/346/230/216.txt +3 -0
  142. data/test/fixtures/html/train//351/246/226/351/241/265/345/271/277/345/221/212/350/256/276/347/275/256.html +297 -0
  143. data/test/fixtures/html/unescaped.html +2 -0
  144. data/test/fixtures/html/view.vm +916 -0
  145. data/test/fixtures/js/jquery-1.7.js +9300 -0
  146. data/test/fixtures/js/scope-test.js +22 -0
  147. data/test/helper.rb +41 -0
  148. data/test/html/mixed_type_test.rb +35 -0
  149. data/test/html/parser/parse_comment_test.rb +47 -0
  150. data/test/html/parser/parse_dtd_test.rb +46 -0
  151. data/test/html/parser/parse_script_tag_test.rb +55 -0
  152. data/test/html/parser/parse_with_auto_close_tag_test.rb +41 -0
  153. data/test/html/parser/parse_with_diff_case_test.rb +38 -0
  154. data/test/html/parser/parse_with_emtpy_test.rb +22 -0
  155. data/test/html/parser/parse_with_multi_children_test.rb +27 -0
  156. data/test/html/parser/parse_with_multi_line_test.rb +41 -0
  157. data/test/html/parser/parse_with_prop_test.rb +88 -0
  158. data/test/html/parser/parse_with_script_tag_test.rb +26 -0
  159. data/test/html/parser/parse_with_selfclosing_test.rb +39 -0
  160. data/test/html/parser/parse_with_simple_tag_test.rb +44 -0
  161. data/test/html/parser/parse_with_simple_tree_test.rb +40 -0
  162. data/test/html/parser/parse_with_style_tag_test.rb +22 -0
  163. data/test/html/parser/parse_with_text_test.rb +45 -0
  164. data/test/html/parser_test.rb +52 -0
  165. data/test/html/query_test.rb +52 -0
  166. data/test/html/rule/check_block_level_element_test.rb +52 -0
  167. data/test/html/rule/check_button_test.rb +45 -0
  168. data/test/html/rule/check_class_count_test.rb +36 -0
  169. data/test/html/rule/check_css_in_head_test.rb +53 -0
  170. data/test/html/rule/check_dtd_test.rb +46 -0
  171. data/test/html/rule/check_form_element_name_test.rb +49 -0
  172. data/test/html/rule/check_head_contain_meta_and_title_test.rb +52 -0
  173. data/test/html/rule/check_html_template_test.rb +103 -0
  174. data/test/html/rule/check_hyperlink_with_target_test.rb +40 -0
  175. data/test/html/rule/check_hyperlink_with_title_test.rb +43 -0
  176. data/test/html/rule/check_id_n_class_downcase_test.rb +40 -0
  177. data/test/html/rule/check_img_with_alt_prop_test.rb +33 -0
  178. data/test/html/rule/check_no_import_css_test.rb +36 -0
  179. data/test/html/rule/check_prop_have_value_test.rb +32 -0
  180. data/test/html/rule/check_prop_seperator_test.rb +32 -0
  181. data/test/html/rule/check_style_prop_test.rb +30 -0
  182. data/test/html/rule/check_tag_closed_test.rb +59 -0
  183. data/test/html/rule/check_tag_downcase_test.rb +51 -0
  184. data/test/html/rule/check_unescape_char_test.rb +35 -0
  185. data/test/html/rule/check_unique_import_test.rb +56 -0
  186. data/test/html/rule_test.rb +62 -0
  187. data/test/js/expr/expr.rb +57 -0
  188. data/test/js/expr/left_hand.rb +25 -0
  189. data/test/js/expr/operate.rb +145 -0
  190. data/test/js/expr/primary.rb +89 -0
  191. data/test/js/parser_test.rb +98 -0
  192. data/test/js/rule/alert_check_test.rb +37 -0
  193. data/test/js/rule/all_test.rb +23 -0
  194. data/test/js/rule/base_test.rb +34 -0
  195. data/test/js/rule/file_checker_test.rb +131 -0
  196. data/test/js/rule/jq_check_test.rb +90 -0
  197. data/test/js/rule/nest_try_catch_test.rb +71 -0
  198. data/test/js/rule/new_object_and_new_array_test.rb +38 -0
  199. data/test/js/rule/no_eval_test.rb +34 -0
  200. data/test/js/rule/no_global_test.rb +88 -0
  201. data/test/js/rule/private_method_check_test.rb +58 -0
  202. data/test/js/rule/semicolon_test.rb +63 -0
  203. data/test/js/rule/stat_if_with_brace_test.rb +68 -0
  204. data/test/js/rule/stat_if_with_muti_else_test.rb +68 -0
  205. data/test/js/rule/use_strict_equal_test.rb +44 -0
  206. data/test/js/rule_test.rb +47 -0
  207. data/test/js/stat/if.rb +26 -0
  208. data/test/js/stat/iter.rb +115 -0
  209. data/test/js/stat/stat.rb +91 -0
  210. data/test/js/stat/switch.rb +37 -0
  211. data/test/js/stat/try.rb +32 -0
  212. data/test/js/stat/var.rb +38 -0
  213. data/test/parser_visitable_test.rb +102 -0
  214. data/test/position_info_test.rb +66 -0
  215. data/test/rule_dsl/dsl_basic_test.rb +91 -0
  216. data/test/rule_dsl/importing_test.rb +48 -0
  217. data/test/runner/log_level_test.rb +58 -0
  218. metadata +317 -0
@@ -0,0 +1,276 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../helper'
4
+
5
+ require 'css/parser'
6
+
7
+
8
+ module XRayTest
9
+ module CSS
10
+ class ParserTest < Test::Unit::TestCase
11
+ ParseError = XRay::ParseError
12
+ include XRay::CSS
13
+
14
+ def test_parse_stylesheet_empty
15
+ css = " \n "
16
+ sheet = parse_css css
17
+ assert_equal 0, sheet.statements.size
18
+ end
19
+
20
+ def test_parse_directive
21
+ css = '
22
+ @import "subs.css";
23
+ @import "print-main.css" print;
24
+ @-moz-border-radius print {
25
+ body { font-size: 10pt }
26
+ }
27
+ h1 { color: blue }
28
+
29
+ @import url(http://style.china.alibaba.com/css/fdevlib2/grid/grid-min.css);
30
+ '
31
+ sheet = parse_css css
32
+
33
+ at_rules = sheet.at_rules
34
+ assert_equal 4, at_rules.size
35
+
36
+ assert_equal 'import', at_rules[1].keyword.text
37
+ assert_equal '"print-main.css" print', at_rules[1].expression.text
38
+ assert_equal '-moz-border-radius', at_rules[2].keyword.text
39
+ assert_equal 'url(http://style.china.alibaba.com/css/fdevlib2/grid/grid-min.css)',
40
+ at_rules[3].expression.text
41
+ end
42
+
43
+ def test_parse_ruleset
44
+ css = '
45
+ a { font-size: 12px }
46
+ { color: #fff } /* no selector */
47
+ '
48
+
49
+ sheet = parse_css css
50
+ rs = sheet.rulesets
51
+ assert_equal 2, rs.size
52
+
53
+ assert_equal 'a', rs[0].selector.text
54
+ assert_equal nil, rs[1].selector
55
+ end
56
+
57
+ def test_parse_selector
58
+ css = '
59
+ div, #header .mypart, .div ul li {
60
+ font-size: 12px;
61
+ }
62
+ ul, body a, .part ul:first {
63
+ background: #f00;
64
+ }
65
+
66
+ a[name="helloworld"], a {
67
+ color: #fff;
68
+ }
69
+ '
70
+
71
+ sheet = parse_css css
72
+ rs = sheet.rulesets
73
+ assert_equal 3, rs.length
74
+
75
+ assert_equal 'div, #header .mypart, .div ul li', rs[0].selector.text
76
+ assert_equal 3, rs[0].selector.simple_selectors.length
77
+
78
+ assert_equal 'ul, body a, .part ul:first', rs[1].selector.text
79
+
80
+ assert_equal 'a[name="helloworld"]', rs[2].selector.simple_selectors[0].text
81
+ end
82
+
83
+ def test_parse_declarations
84
+ css = '
85
+ body {
86
+ ;;
87
+ color: #333;
88
+ ;
89
+ font-size: 12px
90
+ }
91
+
92
+ a:hover {
93
+ color: #ff7300
94
+ }
95
+
96
+ #content {
97
+ font-size: 12px;
98
+ width: 952px;
99
+ background: #ffffff url(img/bg.png) no-repeat left top;
100
+ }
101
+ '
102
+
103
+ sheet = parse_css css
104
+ rs = sheet.rulesets
105
+ assert_equal 3, rs.size
106
+
107
+ decs = rs[0].declarations
108
+ assert_equal 2, decs.size
109
+ assert_equal 'color', decs[0].property.text
110
+ assert_equal '#333', decs[0].value.text
111
+ assert_equal 'font-size', decs[1].property.text
112
+ assert_equal '12px', decs[1].value.text
113
+
114
+ assert_equal 1, rs[1].declarations.size
115
+
116
+ decs = rs[2].declarations
117
+ assert_equal 3, decs.size
118
+ assert_equal 'font-size', decs[0].property.text
119
+ assert_equal '12px', decs[0].value.text
120
+ assert_equal 'width', decs[1].property.text
121
+ assert_equal '952px', decs[1].value.text
122
+ assert_equal '#ffffff url(img/bg.png) no-repeat left top', decs[2].value.text
123
+ end
124
+
125
+ def test_parse_property
126
+ css = %q[
127
+ body {
128
+ color: #f00;
129
+ _color: #0ff;
130
+ *font-size: 12px;
131
+ background: '';
132
+ -moz-border-radius: ''
133
+ }
134
+ ]
135
+
136
+ sheet = parse_css css
137
+ decs = sheet.rulesets[0].declarations
138
+ assert_equal 'color', decs[0].property.text
139
+ assert_equal '_color', decs[1].property.text
140
+ assert_equal '*font-size', decs[2].property.text
141
+ assert_equal 'background', decs[3].property.text
142
+ assert_equal '-moz-border-radius', decs[4].property.text
143
+ end
144
+
145
+ def test_parse_value
146
+ css = %q[
147
+ div ul>li:first {
148
+ content: '{123}hello"';
149
+ background: url("http://alibaba.com/{123}456");
150
+ font-family: "Lucida Sans", Arial, "\6587\6CC9\9A7F\5FAE\7C73\9ED1";
151
+ font-size: ' ';
152
+ content: ''
153
+ }
154
+ ]
155
+
156
+ sheet = parse_css css
157
+ rs = sheet.rulesets[0]
158
+ decs = rs.declarations
159
+
160
+ assert_equal 'div ul>li:first', rs.selector.text
161
+ assert_equal %q/'{123}hello"'/, decs[0].value.text
162
+ assert_equal 'url("http://alibaba.com/{123}456")', decs[1].value.text
163
+ assert_equal '"Lucida Sans", Arial, "\6587\6CC9\9A7F\5FAE\7C73\9ED1"',
164
+ decs[2].value.text
165
+ assert_equal "' '", decs[3].value.text
166
+ assert_equal "''", decs[4].value.text
167
+ end
168
+
169
+ def test_parse_stylesheet_broken_01
170
+ css = 'body {'
171
+
172
+ assert_raise(ParseError) {
173
+ parse_css css
174
+ }
175
+
176
+ begin
177
+ parse_css css
178
+ rescue ParseError => e
179
+ pos = e.position
180
+ assert_equal 1, pos.row
181
+ assert_equal 7, pos.column
182
+ end
183
+ end
184
+
185
+ def test_parse_comment
186
+ css = '
187
+ /**
188
+ * this is comment
189
+ */
190
+ body {
191
+ font-size: 12px;
192
+ /**/
193
+ /**这个是中文注释*/
194
+ }
195
+ /*this is comment 2*/
196
+
197
+ a { font-size: 14px; }
198
+ '
199
+
200
+ parser = create_parser css
201
+ parser.parse_stylesheet
202
+
203
+ comments = parser.comments
204
+ assert_equal 4, comments.size
205
+ assert_equal '/**这个是中文注释*/', comments[2].text
206
+ end
207
+
208
+ def test_parse_stylesheet_css3
209
+ css = <<END
210
+ @charset: "utf-8";
211
+
212
+ /** animation **/
213
+ @-webkit-keyframes greenPulse {
214
+ from { background-color: #749a02; -webkit-box-shadow: 0 0 9px #333; }
215
+ 50% { background-color: #91bd09; -webkit-box-shadow: 0 0 18px #91bd09; }
216
+ to { background-color: #749a02; -webkit-box-shadow: 0 0 9px #333; }
217
+ }
218
+
219
+ .green.button {
220
+ -webkit-animation-name: greenPulse;
221
+ -webkit-animation-duration: 2s;
222
+ -webkit-animation-iteration-count: infinite;
223
+ }
224
+
225
+ /** media query import **/
226
+ @import url(color.css) screen and (color);
227
+
228
+ /** media query **/
229
+ @media all and (min-width:500px) {
230
+ .big {
231
+ font-size:120%;
232
+ }
233
+ }
234
+
235
+ @media screen and (color), projection and (color) {
236
+ .big {
237
+ font-size:120%;
238
+ }
239
+ }
240
+ END
241
+ sheet = parse_css css
242
+ assert_equal 6, sheet.statements.size
243
+ rule = sheet.at_rules[1]
244
+
245
+ assert_equal '-webkit-keyframes', rule.keyword.text
246
+ rs = rule.block.rulesets
247
+ assert_equal 3, rs.size
248
+
249
+ assert_equal '50%', rs[1].selector.text
250
+ assert_equal '-webkit-box-shadow', rs[1].declarations[1].property.text
251
+ assert_equal '0 0 18px #91bd09', rs[1].declarations[1].value.text
252
+
253
+ rule = sheet.at_rules.last
254
+ assert_equal 'screen and (color), projection and (color)', rule.expression.text
255
+
256
+ rs = rule.block.rulesets[0]
257
+ assert_equal '.big', rs.selector.text
258
+ assert_equal 'font-size: 120%', rs.declarations[0].text
259
+ end
260
+
261
+ private
262
+
263
+ def create_parser(css)
264
+ Parser.new css, XRayTest::Logger.new
265
+ end
266
+
267
+ def parse_css(css, name = 'stylesheet')
268
+ parser = create_parser css
269
+ parser.send "parse_#{name}"
270
+ end
271
+
272
+
273
+
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../helper'
4
+
5
+ require 'node'
6
+ require 'css/struct'
7
+ require 'css/rule/checklist'
8
+
9
+ module XRayTest
10
+ module CSS
11
+ module Rule
12
+
13
+ class CheckEncodingTest < Test::Unit::TestCase
14
+
15
+ def setup
16
+ @runner = XRay::Runner.new :encoding => 'gb2312'
17
+ @expect_err = XRay::LogEntry.new("File can't be read as gb2312 charset", :fatal)
18
+ end
19
+
20
+ def test_check_utf8_file_well_written
21
+ check_file "#{FIXTURE_PATH}/css/utf8_good.css" do |results|
22
+ assert !has_encoding_error?(results), "如果没有指定css文件的编码,utf-8也是合法的"
23
+ end
24
+ end
25
+
26
+ def test_check_utf8_file_with_errors
27
+ check_file "#{FIXTURE_PATH}/css/utf8_using_star.css" do |results|
28
+ assert !has_encoding_error?(results), "如果没有指定css文件的编码,utf-8也是合法的"
29
+ end
30
+ end
31
+
32
+ def test_check_utf8_file_with_charset_declaraction
33
+ check_file "#{FIXTURE_PATH}/css/utf8_good_declaring_charset.css" do |results|
34
+ assert !has_encoding_error?(results), "带有@charset声明的文件应该可以被正确读取"
35
+ end
36
+ end
37
+
38
+ def test_check_gb2312_file_well_written
39
+ check_file "#{FIXTURE_PATH}/css/gb-good.css" do |results|
40
+ assert !has_encoding_error?(results), "GB2312的文件应该可以被正常读取"
41
+ end
42
+ end
43
+
44
+ def test_check_gb2312_file_with_errors
45
+ check_file "#{FIXTURE_PATH}/css/gb_using_star.css" do |results|
46
+ assert !has_encoding_error?(results), "GB2312的文件应该可以被正常读取"
47
+ assert results.any? { |r| r.message.include? '禁止使用星号选择符' }
48
+ end
49
+ end
50
+
51
+ private
52
+ def check_file( file )
53
+ results = @runner.check_css_file file
54
+ yield results
55
+ end
56
+
57
+ def has_encoding_error?( results )
58
+ results.include? @expect_err
59
+ end
60
+
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,167 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../helper'
4
+
5
+ require 'node'
6
+ require 'css/struct'
7
+ require 'css/rule/checklist'
8
+
9
+ module XRayTest
10
+ module CSS
11
+ module Rule
12
+
13
+ class CheckListRuleTest < Test::Unit::TestCase
14
+
15
+ include XRay::CSS, XRay::CSS::Rule, XRay::Rule, XRay::Context
16
+
17
+ Node = XRay::Node
18
+ XRay::Rule.import_all
19
+
20
+ def setup
21
+ self.scope = :page
22
+ end
23
+
24
+ # selector
25
+
26
+ def test_check_selector_with_id
27
+
28
+ selector = Node.new '#mydiv a'
29
+ message, level = check_selector_with_id selector
30
+
31
+ assert_equal :error, level
32
+
33
+ selector = Node.new 'ul #mydiv dt'
34
+ ret = check_selector_with_id selector
35
+ assert_not_nil ret
36
+
37
+ selector = Node.new '.guide-part ul li'
38
+ ret = check_selector_with_id selector
39
+ assert_nil ret
40
+ end
41
+
42
+ def test_check_selector_with_global_tag
43
+ selector = Node.new 'a'
44
+ message, level = check_selector_with_global_tag selector
45
+
46
+ assert_equal :error, level
47
+
48
+ selector = Node.new 'body'
49
+ message, level = check_selector_with_global_tag selector
50
+
51
+ assert_equal :error, level
52
+ end
53
+
54
+ def test_check_selector_level
55
+ selector = Node.new '.mypart .mysubpart ul li a'
56
+ message, level = check_selector_level selector
57
+
58
+ assert_equal :error, level
59
+
60
+ selector = Node.new '.mypart ul li a'
61
+ ret = check_selector_level selector
62
+ assert_nil ret
63
+
64
+ selector = Node.new 'html>div.mypart ul li a'
65
+ message, level = check_selector_level selector
66
+ assert_equal :error, level
67
+
68
+ selector = Node.new 'div.mypart ul li a'
69
+ ret = check_selector_level selector
70
+ assert_nil ret
71
+ end
72
+
73
+ def test_check_selector_with_star
74
+ selector = Node.new '* html'
75
+ message, level = check_selector_with_star selector
76
+
77
+ assert_equal :error, level
78
+ end
79
+
80
+ def test_check_declarations_sequence
81
+
82
+ end
83
+
84
+ # declaration
85
+
86
+ def test_check_good_declaration_font
87
+ sel = Node.new 'font'
88
+ expr = Node.new 'Arial'
89
+ dec = Declaration.new(sel, expr)
90
+
91
+ message, level = check_declaration_font dec
92
+ assert_equal nil, level
93
+ end
94
+
95
+ def test_check_declaration_font
96
+ sel = Node.new 'font'
97
+ expr = Node.new '宋体'
98
+ dec = Declaration.new(sel, expr)
99
+
100
+ message, level = check_declaration_font dec
101
+ assert_equal :error, level
102
+ end
103
+
104
+ def test_check_redefine_a_hover_color
105
+ selector = Node.new 'a:hover'
106
+
107
+ prop = Node.new 'color'
108
+ expr = Node.new '#f00'
109
+ dec = Declaration.new(prop, expr)
110
+
111
+ ruleset = RuleSet.new(selector, [dec])
112
+
113
+ message, level = check_ruleset_redefine_a_hover_color ruleset
114
+ assert_equal :error, level
115
+ end
116
+
117
+ def test_check_redefine_lib_css
118
+ selector = Node.new '.fd-hide'
119
+ message, level = check_selector_redefine_lib_css selector
120
+ assert_equal :error, level
121
+
122
+ @scope = :lib
123
+ ret = check_selector_redefine_lib_css selector
124
+ assert_nil ret
125
+ end
126
+
127
+ def test_check_property_hack
128
+ props = %w(_background +font-size *color)
129
+ props.each do |prop|
130
+ prop = Node.new prop
131
+ message, level = check_property_hack prop
132
+ assert_equal :error, level
133
+ end
134
+ end
135
+
136
+ def test_check_property_slash_hack
137
+ prop = Node.new 'd\isplay'
138
+ message, level = check_property_hack prop
139
+ assert_equal :error, level
140
+ end
141
+
142
+ def test_check_selector_using_hack
143
+ selector = Node.new 'html*'
144
+ message, level = check_selector_using_hack selector
145
+ assert_equal :error, level
146
+ end
147
+
148
+ def test_check_value_use_css_expression
149
+ expr = Node.new 'expression(onfocus=this.blur())'
150
+ message, level = check_value_use_css_expression expr
151
+ assert_equal :error, level
152
+ end
153
+
154
+ def test_check_value_use_hack
155
+ exprs = %w(9px\0 #000\9)
156
+ exprs.each do |expr|
157
+ expr = Node.new expr
158
+ message, level = check_value_use_hack expr
159
+ assert_equal :error, level
160
+ end
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require_relative '../../helper'
3
+
4
+ require 'file_validator'
5
+ require 'css/rule/check_compression_rule'
6
+
7
+ module XRayTest
8
+ module CSS
9
+ module Rule
10
+
11
+ class CompressionTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+ @validator = XRay::FileValidator.new( :encoding => 'gb2312' )
15
+ @validator.add_validator XRay::CSS::Rule::CompressionChecker.new
16
+ end
17
+
18
+ def test_source_file_with_min
19
+ file = "#{FIXTURE_PATH}/css/empty.css"
20
+ results = @validator.check file
21
+
22
+ assert results.empty?, "同目录下有min文件,测试应通过"
23
+ end
24
+
25
+ def test_source_file_without_min
26
+ file = "#{FIXTURE_PATH}/css/import.css"
27
+ results = @validator.check file
28
+
29
+ expect_err = XRay::LogEntry.new('发布上线的文件需要压缩,命名规则如a.js->a-min.js,且两者在同一目录下', :error)
30
+ assert_equal [expect_err], results, "同目录下没有对应的min文件,测试不应通过"
31
+ end
32
+
33
+ def test_merge_file
34
+ file = 'merge.css'
35
+ results = @validator.check file
36
+
37
+ assert results.empty?, "merge文件不需要被压缩"
38
+ end
39
+
40
+ def test_min_file
41
+ file = 'test-min.css'
42
+ results = @validator.check file
43
+
44
+ assert results.empty?, "本身已经被压缩,不需要再检查"
45
+ end
46
+
47
+
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../helper'
4
+
5
+ require 'node'
6
+ require 'css/struct'
7
+ require 'css/rule/checklist'
8
+
9
+ module XRayTest
10
+ module CSS
11
+ module Rule
12
+
13
+ class CheckFileNameTest < Test::Unit::TestCase
14
+
15
+ include XRay::Rule
16
+
17
+ def setup
18
+ end
19
+
20
+ def test_file_name_with_ad
21
+ check 'css/ad.css' do |results|
22
+ expect_err = ["路径和文件名中不应该出现ad", :error]
23
+ assert_equal [expect_err], results, "文件名中不应出现ad"
24
+ end
25
+ end
26
+
27
+ def test_path_name_with_ad
28
+ check "/css/adver/test.css" do |results|
29
+ expect_err = ["路径和文件名中不应该出现ad", :error]
30
+ assert_equal [expect_err], results, "文件路径中不应出现ad"
31
+ end
32
+ end
33
+
34
+ def test_file_name_without_ad
35
+ check 'not-exsiting-file.css' do |results|
36
+ assert results.empty?, "文件名中不应出现ad,包括路径"
37
+ end
38
+ end
39
+
40
+ def test_file_name_with_underscore
41
+ check 'not_exsiting_file.css' do |results|
42
+ expect_err = ["文件名中单词的分隔符应该使用中横线“-”", :error]
43
+ assert_equal [expect_err], results, "文件名中单词的分隔符应该使用中横线“-”"
44
+ end
45
+ end
46
+
47
+ def test_file_name_with_upcase
48
+ check 'someFile.css' do |results|
49
+ expect_err = ["文件夹和文件命名必须用小写字母", :error]
50
+ assert_equal [expect_err], results, "文件夹和文件命名必须用小写字母"
51
+ end
52
+ end
53
+
54
+ def test_dir_name_with_minus
55
+ check 'test-post-offer/main.css' do |results|
56
+ expect_err = ["文件夹只有需要版本区分时才可用中横线分隔,如fdev-v3", :error]
57
+ assert_equal [expect_err], results, "文件夹只有需要版本区分时才可用中横线分隔"
58
+ end
59
+ end
60
+
61
+ def test_dir_name_with_version_minus
62
+ check 'test-2/main.css' do |results|
63
+ assert results.empty?, "目录名字中带有版本号时可以用中横线"
64
+ end
65
+ end
66
+
67
+ protected
68
+ def check(file, &block)
69
+ yield check_css_file(file)
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,4 @@
1
+ body {
2
+
3
+
4
+
@@ -0,0 +1,52 @@
1
+ /**
2
+ * ����:���Ϸ���
3
+ *
4
+ * @author qijun.weiqj
5
+ */
6
+
7
+ /**
8
+ * company-name
9
+ */
10
+ .skin-topbanner {
11
+ background: #fddde2 url(http://img.china.alibaba.com/images/app/winport/layout/diybackground/style/temp36/topbaner.jpg);
12
+ }
13
+ .wp-company-name .chinaname, .wp-company-name .chinaname:hover {
14
+ color: #990033;
15
+ }
16
+ .wp-company-name .enname, .wp-company-name .enname:hover {
17
+ color: #a23a5c;
18
+ }
19
+
20
+ /**
21
+ * top-nav
22
+ */
23
+ .wp-top-nav {
24
+ border-top: 1px solid #fff;
25
+ border-bottom: 1px solid #fff;
26
+ background: #d51d5d url(http://img.china.alibaba.com/images/app/winport/layout/diybackground/style/temp36/header_menu.gif) repeat-x left top;
27
+ }
28
+ .wp-top-nav li a,
29
+ .wp-top-nav li a:hover {
30
+ color: #fff;
31
+ }
32
+ .wp-top-nav li.selected a,
33
+ .wp-top-nav li.selected a:hover {
34
+ background: url(http://img.china.alibaba.com/images/app/winport/layout/diybackground/style/temp36/header_menu_check.png) no-repeat left top;
35
+ _background: none;
36
+ _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://img.china.alibaba.com/images/app/winport/layout/diybackground/style/temp36/header_menu_check.png,sizingMethod=scale);
37
+ color: #c20606;
38
+ }
39
+
40
+
41
+ /**
42
+ * mod
43
+ */
44
+ .mod .m-body {
45
+ border: 1px solid #ffe4e4;
46
+ }
47
+ .mod .m-header {
48
+ background: #333 url(http://img.china.alibaba.com/images/app/winport/layout/diybackground/style/temp36/body_cont_title.gif) repeat-x left top;
49
+ color: #fff;
50
+ }
51
+
52
+