Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/web_dev_style/css_checker.py

Issue 1091063002: Get sublime's syntax highlighting working in css_checker.py (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 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 17 matching lines...) Expand all
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 return _remove_grit(_remove_ats(_remove_comments(s))) 32 return _remove_grit(_remove_ats(_remove_comments(s)))
33 33
34 def _remove_ats(s): 34 def _remove_ats(s):
35 at_reg = re.compile(r""" 35 at_reg = re.compile(r"""
36 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x 36 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x
37 (.*{.*?})+ # inner { curly } blocks, rules, and selector 37 (.*{.*?})+ # inner { curly } blocks, rules, and selector
38 .*?} # stuff up to the first end curly }""", 38 .*?} # stuff up to the first end curly }
39 """,
39 re.DOTALL | re.VERBOSE) 40 re.DOTALL | re.VERBOSE)
40 return at_reg.sub('\\1', s) 41 return at_reg.sub('\\1', s)
41 42
42 def _remove_comments(s): 43 def _remove_comments(s):
43 return re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', s) 44 return re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', s)
44 45
45 def _remove_grit(s): 46 def _remove_grit(s):
46 grit_reg = re.compile(r""" 47 grit_reg = re.compile(r"""
47 <if[^>]+>.*?<\s*/\s*if[^>]*>| # <if> contents </if> 48 <if[^>]+>.*?<\s*/\s*if[^>]*>| # <if> contents </if>
48 <include[^>]+> # <include>""", 49 <include[^>]+> # <include>
50 """,
49 re.DOTALL | re.VERBOSE) 51 re.DOTALL | re.VERBOSE)
50 return re.sub(grit_reg, '', s) 52 return re.sub(grit_reg, '', s)
51 53
52 def _rgb_from_hex(s): 54 def _rgb_from_hex(s):
53 if len(s) == 3: 55 if len(s) == 3:
54 r, g, b = s[0] + s[0], s[1] + s[1], s[2] + s[2] 56 r, g, b = s[0] + s[0], s[1] + s[1], s[2] + s[2]
55 else: 57 else:
56 r, g, b = s[0:2], s[2:4], s[4:6] 58 r, g, b = s[0:2], s[2:4], s[4:6]
57 return int(r, base=16), int(g, base=16), int(b, base=16) 59 return int(r, base=16), int(g, base=16), int(b, base=16)
58 60
59 def _strip_prefix(s): 61 def _strip_prefix(s):
60 return re.sub(r'^-(?:o|ms|moz|khtml|webkit)-', '', s) 62 return re.sub(r'^-(?:o|ms|moz|khtml|webkit)-', '', s)
61 63
62 def alphabetize_props(contents): 64 def alphabetize_props(contents):
63 errors = [] 65 errors = []
64 for rule in re.finditer(r'{(.*?)}', contents, re.DOTALL): 66 for rule in re.finditer(r'{(.*?)}', contents, re.DOTALL):
65 semis = map(lambda t: t.strip(), rule.group(1).split(';'))[:-1] 67 semis = map(lambda t: t.strip(), rule.group(1).split(';'))[:-1]
66 rules = filter(lambda r: ': ' in r, semis) 68 rules = filter(lambda r: ': ' in r, semis)
67 props = map(lambda r: r[0:r.find(':')], rules) 69 props = map(lambda r: r[0:r.find(':')], rules)
68 if props != sorted(props): 70 if props != sorted(props):
69 errors.append(' %s;\n' % (';\n '.join(rules))) 71 errors.append(' %s;\n' % (';\n '.join(rules)))
70 return errors 72 return errors
71 73
72 def braces_have_space_before_and_nothing_after(line): 74 def braces_have_space_before_and_nothing_after(line):
73 brace_space_reg = re.compile(r""" 75 brace_space_reg = re.compile(r"""
74 (?:^|\S){| # selector{ or selector\n{ or 76 (?:^|\S){| # selector{ or selector\n{ or
75 {\s*\S+\s* # selector { with stuff after it 77 {\s*\S+\s* # selector { with stuff after it
76 $ # must be at the end of a line""", 78 $ # must be at the end of a line
79 """,
77 re.VERBOSE) 80 re.VERBOSE)
78 return brace_space_reg.search(line) 81 return brace_space_reg.search(line)
79 82
80 def classes_use_dashes(line): 83 def classes_use_dashes(line):
81 # Intentionally dumbed down version of CSS 2.1 grammar for class without 84 # Intentionally dumbed down version of CSS 2.1 grammar for class without
82 # non-ASCII, escape chars, or whitespace. 85 # non-ASCII, escape chars, or whitespace.
83 class_reg = re.compile(r""" 86 class_reg = re.compile(r"""
84 \.(-?[\w-]+).* # ., then maybe -, then alpha numeric and - 87 \.(-?[\w-]+).* # ., then maybe -, then alpha numeric and -
85 [,{]\s*$ # selectors should end with a , or {""", 88 [,{]\s*$ # selectors should end with a , or {
89 """,
86 re.VERBOSE) 90 re.VERBOSE)
87 m = class_reg.search(line) 91 m = class_reg.search(line)
88 if not m: 92 if not m:
89 return False 93 return False
90 class_name = m.group(1) 94 class_name = m.group(1)
91 return class_name.lower() != class_name or '_' in class_name 95 return class_name.lower() != class_name or '_' in class_name
92 96
93 def close_brace_on_new_line(line): 97 def close_brace_on_new_line(line):
94 # Ignore single frames in a @keyframe, i.e. 0% { margin: 50px; } 98 # Ignore single frames in a @keyframe, i.e. 0% { margin: 50px; }
95 frame_reg = re.compile(r""" 99 frame_reg = re.compile(r"""
96 \s*(from|to|\d+%)\s*{ # 50% { 100 \s*(from|to|\d+%)\s*{ # 50% {
97 \s*[\w-]+: # rule: 101 \s*[\w-]+: # rule:
98 (\s*[\w\(\), -]+)+\s*; # value; 102 (\s*[\w\(\), -]+)+\s*; # value;
99 \s*}\s* # }""", 103 \s*}\s* # }
104 """,
100 re.VERBOSE) 105 re.VERBOSE)
101 return ('}' in line and re.search(r'[^ }]', line) and 106 return ('}' in line and re.search(r'[^ }]', line) and
102 not frame_reg.match(line)) 107 not frame_reg.match(line))
103 108
104 def colons_have_space_after(line): 109 def colons_have_space_after(line):
105 colon_space_reg = re.compile(r""" 110 colon_space_reg = re.compile(r"""
106 (?<!data) # ignore data URIs 111 (?<!data) # ignore data URIs
107 :(?!//) # ignore url(http://), etc. 112 :(?!//) # ignore url(http://), etc.
108 \S[^;]+;\s* # only catch one-line rules for now""", 113 \S[^;]+;\s* # only catch one-line rules for now
114 """,
109 re.VERBOSE) 115 re.VERBOSE)
110 return colon_space_reg.search(line) 116 return colon_space_reg.search(line)
111 117
112 def favor_single_quotes(line): 118 def favor_single_quotes(line):
113 return '"' in line 119 return '"' in line
114 120
115 # Shared between hex_could_be_shorter and rgb_if_not_gray. 121 # Shared between hex_could_be_shorter and rgb_if_not_gray.
116 hex_reg = re.compile(r""" 122 hex_reg = re.compile(r"""
117 \#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}) # pound followed by 3 or 6 hex digits 123 \#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}) # pound followed by 3 or 6 hex digits
118 (?=[^\w-]|$) # no more alphanum chars or at EOL 124 (?=[^\w-]|$) # no more alphanum chars or at EOL
119 (?!.*(?:{.*|,\s*)$) # not in a selector""", 125 (?!.*(?:{.*|,\s*)$) # not in a selector
126 """,
120 re.VERBOSE) 127 re.VERBOSE)
121 128
122 def hex_could_be_shorter(line): 129 def hex_could_be_shorter(line):
123 m = hex_reg.search(line) 130 m = hex_reg.search(line)
124 return (m and _is_gray(m.group(1)) and _collapseable_hex(m.group(1))) 131 return (m and _is_gray(m.group(1)) and _collapseable_hex(m.group(1)))
125 132
126 def rgb_if_not_gray(line): 133 def rgb_if_not_gray(line):
127 m = hex_reg.search(line) 134 m = hex_reg.search(line)
128 return (m and not _is_gray(m.group(1))) 135 return (m and not _is_gray(m.group(1)))
129 136
130 small_seconds_reg = re.compile(r""" 137 small_seconds_reg = re.compile(r"""
131 (?:^|[^\w-]) # start of a line or a non-alphanumeric char 138 (?:^|[^\w-]) # start of a line or a non-alphanumeric char
132 (0?\.[0-9]+)s # 1.0s 139 (0?\.[0-9]+)s # 1.0s
133 (?!-?[\w-]) # no following - or alphanumeric chars""", 140 (?!-?[\w-]) # no following - or alphanumeric chars
141 """,
134 re.VERBOSE) 142 re.VERBOSE)
135 143
136 def milliseconds_for_small_times(line): 144 def milliseconds_for_small_times(line):
137 return small_seconds_reg.search(line) 145 return small_seconds_reg.search(line)
138 146
139 def suggest_ms_from_s(line): 147 def suggest_ms_from_s(line):
140 ms = int(float(small_seconds_reg.search(line).group(1)) * 1000) 148 ms = int(float(small_seconds_reg.search(line).group(1)) * 1000)
141 return ' (replace with %dms)' % ms 149 return ' (replace with %dms)' % ms
142 150
143 def no_data_uris_in_source_files(line): 151 def no_data_uris_in_source_files(line):
144 return re.search(r'\(\s*\s*data:', line) 152 return re.search(r'\(\s*\s*data:', line)
145 153
146 def no_quotes_in_url(line): 154 def no_quotes_in_url(line):
147 return re.search('url\s*\(\s*["\']', line, re.IGNORECASE) 155 return re.search('url\s*\(\s*["\']', line, re.IGNORECASE)
148 156
149 def one_rule_per_line(line): 157 def one_rule_per_line(line):
150 one_rule_reg = re.compile(r""" 158 one_rule_reg = re.compile(r"""
151 [\w-](?<!data): # a rule: but no data URIs 159 [\w-](?<!data): # a rule: but no data URIs
152 (?!//)[^;]+; # value; ignoring colons in protocols:// 160 (?!//)[^;]+; # value; ignoring colons in protocols://
153 \s*[^ }]\s* # any non-space after the end colon""", 161 \s*[^ }]\s* # any non-space after the end colon
162 """,
154 re.VERBOSE) 163 re.VERBOSE)
155 return one_rule_reg.search(line) 164 return one_rule_reg.search(line)
156 165
157 def pseudo_elements_double_colon(contents): 166 def pseudo_elements_double_colon(contents):
158 pseudo_elements = ['after', 167 pseudo_elements = ['after',
159 'before', 168 'before',
160 'calendar-picker-indicator', 169 'calendar-picker-indicator',
161 'color-swatch', 170 'color-swatch',
162 'color-swatch-wrapper', 171 'color-swatch-wrapper',
163 'date-and-time-container', 172 'date-and-time-container',
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 'textfield-decoration-container', 218 'textfield-decoration-container',
210 'validation-bubble', 219 'validation-bubble',
211 'validation-bubble-arrow', 220 'validation-bubble-arrow',
212 'validation-bubble-arrow-clipper', 221 'validation-bubble-arrow-clipper',
213 'validation-bubble-heading', 222 'validation-bubble-heading',
214 'validation-bubble-message', 223 'validation-bubble-message',
215 'validation-bubble-text-block'] 224 'validation-bubble-text-block']
216 pseudo_reg = re.compile(r""" 225 pseudo_reg = re.compile(r"""
217 (?<!:): # a single colon, i.e. :after but not ::after 226 (?<!:): # a single colon, i.e. :after but not ::after
218 ([a-zA-Z-]+) # a pseudo element, class, or function 227 ([a-zA-Z-]+) # a pseudo element, class, or function
219 (?=[^{}]+?{) # make sure a selector, not inside { rules }""", 228 (?=[^{}]+?{) # make sure a selector, not inside { rules }
229 """,
220 re.MULTILINE | re.VERBOSE) 230 re.MULTILINE | re.VERBOSE)
221 errors = [] 231 errors = []
222 for p in re.finditer(pseudo_reg, contents): 232 for p in re.finditer(pseudo_reg, contents):
223 pseudo = p.group(1).strip().splitlines()[0] 233 pseudo = p.group(1).strip().splitlines()[0]
224 if _strip_prefix(pseudo.lower()) in pseudo_elements: 234 if _strip_prefix(pseudo.lower()) in pseudo_elements:
225 errors.append(' :%s (should be ::%s)' % (pseudo, pseudo)) 235 errors.append(' :%s (should be ::%s)' % (pseudo, pseudo))
226 return errors 236 return errors
227 237
228 def one_selector_per_line(contents): 238 def one_selector_per_line(contents):
229 any_reg = re.compile(r""" 239 any_reg = re.compile(r"""
230 :(?:-webkit-)?any\(.*?\) # :-webkit-any(a, b, i) selector""", 240 :(?:-webkit-)?any\(.*?\) # :-webkit-any(a, b, i) selector
241 """,
231 re.DOTALL | re.VERBOSE) 242 re.DOTALL | re.VERBOSE)
232 multi_sels_reg = re.compile(r""" 243 multi_sels_reg = re.compile(r"""
233 (?:}\s*)? # ignore 0% { blah: blah; }, from @keyframes 244 (?:}\s*)? # ignore 0% { blah: blah; }, from @keyframes
234 ([^,]+,(?=[^{}]+?{) # selector junk {, not in a { rule } 245 ([^,]+,(?=[^{}]+?{) # selector junk {, not in a { rule }
235 .*[,{])\s*$ # has to end with , or {""", 246 .*[,{])\s*$ # has to end with , or {
247 """,
236 re.MULTILINE | re.VERBOSE) 248 re.MULTILINE | re.VERBOSE)
237 errors = [] 249 errors = []
238 for b in re.finditer(multi_sels_reg, re.sub(any_reg, '', contents)): 250 for b in re.finditer(multi_sels_reg, re.sub(any_reg, '', contents)):
239 errors.append(' ' + b.group(1).strip().splitlines()[-1:][0]) 251 errors.append(' ' + b.group(1).strip().splitlines()[-1:][0])
240 return errors 252 return errors
241 253
242 def suggest_rgb_from_hex(line): 254 def suggest_rgb_from_hex(line):
243 suggestions = ['rgb(%d, %d, %d)' % _rgb_from_hex(h.group(1)) 255 suggestions = ['rgb(%d, %d, %d)' % _rgb_from_hex(h.group(1))
244 for h in re.finditer(hex_reg, line)] 256 for h in re.finditer(hex_reg, line)]
245 return ' (replace with %s)' % ', '.join(suggestions) 257 return ' (replace with %s)' % ', '.join(suggestions)
246 258
247 def suggest_short_hex(line): 259 def suggest_short_hex(line):
248 h = hex_reg.search(line).group(1) 260 h = hex_reg.search(line).group(1)
249 return ' (replace with #%s)' % (h[0] + h[2] + h[4]) 261 return ' (replace with #%s)' % (h[0] + h[2] + h[4])
250 262
251 webkit_before_or_after_reg = re.compile(r'-webkit-(\w+-)(after|before):') 263 webkit_before_or_after_reg = re.compile(r'-webkit-(\w+-)(after|before):')
252 264
253 def suggest_top_or_bottom(line): 265 def suggest_top_or_bottom(line):
254 prop, pos = webkit_before_or_after_reg.search(line).groups() 266 prop, pos = webkit_before_or_after_reg.search(line).groups()
255 top_or_bottom = 'top' if pos == 'before' else 'bottom' 267 top_or_bottom = 'top' if pos == 'before' else 'bottom'
256 return ' (replace with %s)' % (prop + top_or_bottom) 268 return ' (replace with %s)' % (prop + top_or_bottom)
257 269
258 def webkit_before_or_after(line): 270 def webkit_before_or_after(line):
259 return webkit_before_or_after_reg.search(line) 271 return webkit_before_or_after_reg.search(line)
260 272
261 def zero_width_lengths(contents): 273 def zero_width_lengths(contents):
262 hsl_reg = re.compile(r""" 274 hsl_reg = re.compile(r"""
263 hsl\([^\)]* # hsl(<maybe stuff> 275 hsl\([^\)]* # hsl(maybestuff
264 (?:[, ]|(?<=\()) # a comma or space not followed by a ( 276 (?:[, ]|(?<=\()) # a comma or space not followed by a (
265 (?:0?\.?)?0% # some equivalent to 0%""", 277 (?:0?\.?)?0% # some equivalent to 0%
278 """,
266 re.VERBOSE) 279 re.VERBOSE)
267 zeros_reg = re.compile(r""" 280 zeros_reg = re.compile(r"""
268 ^.*(?:^|[^0-9.]) # start/non-number 281 ^.*(?:^|[^0-9.]) # start/non-number
269 (?:\.0|0(?:\.0? # .0, 0, or 0.0 282 (?:\.0|0(?:\.0? # .0, 0, or 0.0
270 |px|em|%|in|cm|mm|pc|pt|ex)) # a length unit 283 |px|em|%|in|cm|mm|pc|pt|ex)) # a length unit
271 (?:\D|$) # non-number/end 284 (?:\D|$) # non-number/end
272 (?=[^{}]+?}).*$ # only { rules }""", 285 (?=[^{}]+?}).*$ # only { rules }
286 """,
273 re.MULTILINE | re.VERBOSE) 287 re.MULTILINE | re.VERBOSE)
274 errors = [] 288 errors = []
275 for z in re.finditer(zeros_reg, contents): 289 for z in re.finditer(zeros_reg, contents):
276 first_line = z.group(0).strip().splitlines()[0] 290 first_line = z.group(0).strip().splitlines()[0]
277 if not hsl_reg.search(first_line): 291 if not hsl_reg.search(first_line):
278 errors.append(' ' + first_line) 292 errors.append(' ' + first_line)
279 return errors 293 return errors
280 294
281 # NOTE: Currently multi-line checks don't support 'after'. Instead, add 295 # NOTE: Currently multi-line checks don't support 'after'. Instead, add
282 # suggestions while parsing the file so another pass isn't necessary. 296 # suggestions while parsing the file so another pass isn't necessary.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) 396 '%s:\n%s' % (f[0], '\n\n'.join(file_errors))))
383 397
384 if results: 398 if results:
385 # Add your name if you're here often mucking around in the code. 399 # Add your name if you're here often mucking around in the code.
386 authors = ['dbeam@chromium.org'] 400 authors = ['dbeam@chromium.org']
387 results.append(self.output_api.PresubmitNotifyResult( 401 results.append(self.output_api.PresubmitNotifyResult(
388 'Was the CSS checker useful? Send feedback or hate mail to %s.' % 402 'Was the CSS checker useful? Send feedback or hate mail to %s.' %
389 ', '.join(authors))) 403 ', '.join(authors)))
390 404
391 return results 405 return results
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698