OLD | NEW |
1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Presubmit script for Chromium WebUI resources. | 5 """Presubmit script for Chromium WebUI resources. |
6 | 6 |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
8 for more details about the presubmit API built into depot_tools, and see | 8 for more details about the presubmit API built into depot_tools, and see |
9 http://www.chromium.org/developers/web-development-style-guide for the rules | 9 http://www.chromium.org/developers/web-development-style-guide for the rules |
10 we're checking against here. | 10 we're checking against here. |
(...skipping 11 matching lines...) Expand all Loading... |
22 # We use this a lot, so make a nick name variable. | 22 # We use this a lot, so make a nick name variable. |
23 re = self.input_api.re | 23 re = self.input_api.re |
24 | 24 |
25 def _collapseable_hex(s): | 25 def _collapseable_hex(s): |
26 return (len(s) == 6 and s[0] == s[1] and s[2] == s[3] and s[4] == s[5]) | 26 return (len(s) == 6 and s[0] == s[1] and s[2] == s[3] and s[4] == s[5]) |
27 | 27 |
28 def _is_gray(s): | 28 def _is_gray(s): |
29 return s[0] == s[1] == s[2] if len(s) == 3 else s[0:2] == s[2:4] == s[4:6] | 29 return s[0] == s[1] == s[2] if len(s) == 3 else s[0:2] == s[2:4] == s[4:6] |
30 | 30 |
31 def _remove_all(s): | 31 def _remove_all(s): |
32 s = _remove_grit(s) | 32 s = _remove_grit(s) # Must be done first. |
33 s = _remove_ats(s) | 33 s = _remove_ats(s) |
34 s = _remove_comments(s) | 34 s = _remove_comments(s) |
| 35 s = _remove_mixins_and_valid_vars(s) |
35 s = _remove_template_expressions(s) | 36 s = _remove_template_expressions(s) |
36 s = _remove_mixins(s) | |
37 return s | 37 return s |
38 | 38 |
39 def _extract_inline_style(s): | 39 def _extract_inline_style(s): |
40 return '\n'.join(re.findall(r'<style\b[^>]*>([^<]*)<\/style>', s)) | 40 return '\n'.join(re.findall(r'<style\b[^>]*>([^<]*)<\/style>', s)) |
41 | 41 |
42 def _remove_ats(s): | 42 def _remove_ats(s): |
43 at_reg = re.compile(r""" | 43 return re.sub(r""" |
44 @(?!apply)(?!\d+x\b) # @at-keyword, not (apply|2x) | 44 @(?!apply)(?!\d+x\b) # @at-keyword, not (apply|2x) |
45 \w+[^'"]*?{ # selector junk { | 45 \w+[^'"]*?{ # selector junk { |
46 (.*{.*?})+ # inner { curly } blocks, rules, and selector | 46 (.*{.*?})+ # inner { curly } blocks, rules, and selector |
47 .*?} # stuff up to the first end curly } | 47 .*?} # stuff up to the first end curly } |
48 """, | 48 """, r'\1', s, flags=re.DOTALL | re.VERBOSE) |
49 re.DOTALL | re.VERBOSE) | |
50 return at_reg.sub('\\1', s) | |
51 | 49 |
52 def _remove_comments(s): | 50 def _remove_comments(s): |
53 return re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', s) | 51 return re.sub(r'/\*.*?\*/', '', s, flags=re.DOTALL) |
54 | 52 |
55 def _remove_mixins(s): | 53 def _remove_grit(s): |
56 return re.sub(re.compile(r'--[\d\w-]+: {.*?};', re.DOTALL), '', s) | 54 return re.sub(r""" |
| 55 <if[^>]+>.*?<\s*/\s*if[^>]*>| # <if> contents </if> |
| 56 <include[^>]+> # <include> |
| 57 """, '', s, flags=re.DOTALL | re.VERBOSE) |
| 58 |
| 59 mixin_shim_reg = r'[\w-]+_-_[\w-]+' |
| 60 |
| 61 def _remove_mixins_and_valid_vars(s): |
| 62 valid_vars = r'--(?!' + mixin_shim_reg + r')[\w-]+:\s*' |
| 63 mixin_or_value = r'({.*?}|[^;}]+);?\s*' |
| 64 return re.sub(valid_vars + mixin_or_value, '', s, flags=re.DOTALL) |
57 | 65 |
58 def _remove_template_expressions(s): | 66 def _remove_template_expressions(s): |
59 return re.sub(re.compile(r'\$i18n(Raw)?{[^}]*}', re.DOTALL), '', s) | 67 return re.sub(r'\$i18n(Raw)?{[^}]*}', '', s, flags=re.DOTALL) |
60 | |
61 def _remove_grit(s): | |
62 grit_reg = re.compile(r""" | |
63 <if[^>]+>.*?<\s*/\s*if[^>]*>| # <if> contents </if> | |
64 <include[^>]+> # <include> | |
65 """, | |
66 re.DOTALL | re.VERBOSE) | |
67 return re.sub(grit_reg, '', s) | |
68 | 68 |
69 def _rgb_from_hex(s): | 69 def _rgb_from_hex(s): |
70 if len(s) == 3: | 70 if len(s) == 3: |
71 r, g, b = s[0] + s[0], s[1] + s[1], s[2] + s[2] | 71 r, g, b = s[0] + s[0], s[1] + s[1], s[2] + s[2] |
72 else: | 72 else: |
73 r, g, b = s[0:2], s[2:4], s[4:6] | 73 r, g, b = s[0:2], s[2:4], s[4:6] |
74 return int(r, base=16), int(g, base=16), int(b, base=16) | 74 return int(r, base=16), int(g, base=16), int(b, base=16) |
75 | 75 |
76 def _strip_prefix(s): | 76 def _strip_prefix(s): |
77 return re.sub(r'^-(?:o|ms|moz|khtml|webkit)-', '', s) | 77 return re.sub(r'^-(?:o|ms|moz|khtml|webkit)-', '', s) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 return small_seconds_reg.search(line) | 163 return small_seconds_reg.search(line) |
164 | 164 |
165 def suggest_ms_from_s(line): | 165 def suggest_ms_from_s(line): |
166 ms = int(float(small_seconds_reg.search(line).group(1)) * 1000) | 166 ms = int(float(small_seconds_reg.search(line).group(1)) * 1000) |
167 return ' (replace with %dms)' % ms | 167 return ' (replace with %dms)' % ms |
168 | 168 |
169 def no_data_uris_in_source_files(line): | 169 def no_data_uris_in_source_files(line): |
170 return re.search(r'\(\s*\s*data:', line) | 170 return re.search(r'\(\s*\s*data:', line) |
171 | 171 |
172 def no_mixin_shims(line): | 172 def no_mixin_shims(line): |
173 return re.search('\-\-[\w\-]+_\-_[\w\-]+\s*:', line) | 173 return re.search(r'--' + mixin_shim_reg + r'\s*:', line) |
174 | 174 |
175 def no_quotes_in_url(line): | 175 def no_quotes_in_url(line): |
176 return re.search('url\s*\(\s*["\']', line, re.IGNORECASE) | 176 return re.search('url\s*\(\s*["\']', line, re.IGNORECASE) |
177 | 177 |
178 def one_rule_per_line(line): | 178 def one_rule_per_line(line): |
179 one_rule_reg = re.compile(r""" | 179 one_rule_reg = re.compile(r""" |
180 [\w-](?<!data): # a rule: but no data URIs | 180 [\w-](?<!data): # a rule: but no data URIs |
181 (?!//)[^;]+; # value; ignoring colons in protocols:// and }; | 181 (?!//)[^;]+; # value; ignoring colons in protocols:// and }; |
182 \s*[^ }]\s* # any non-space after the end colon | 182 \s*[^ }]\s* # any non-space after the end colon |
183 """, | 183 """, |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 error += check['after'](line) | 421 error += check['after'](line) |
422 check_errors.append(error) | 422 check_errors.append(error) |
423 if len(check_errors) > 0: | 423 if len(check_errors) > 0: |
424 file_errors.append('- %s\n%s' % | 424 file_errors.append('- %s\n%s' % |
425 (check['desc'], '\n'.join(check_errors))) | 425 (check['desc'], '\n'.join(check_errors))) |
426 if file_errors: | 426 if file_errors: |
427 results.append(self.output_api.PresubmitPromptWarning( | 427 results.append(self.output_api.PresubmitPromptWarning( |
428 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) | 428 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) |
429 | 429 |
430 return results | 430 return results |
OLD | NEW |