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

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

Issue 1639863004: Add support for inline CSS in HTML files to CSS Presubmit checker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
OLDNEW
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 18 matching lines...) Expand all
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)
33 s = _remove_ats(s) 33 s = _remove_ats(s)
34 s = _remove_comments(s) 34 s = _remove_comments(s)
35 s = _remove_template_expressions(s) 35 s = _remove_template_expressions(s)
36 s = _remove_mixins(s) 36 s = _remove_mixins(s)
37 return s 37 return s
38 38
39 def _extract_inline_style(s):
40 style_reg = re.compile(r"""<style>([^<]*)<\/style>""")
41 styles = style_reg.findall(s)
42 return '\n'.join(styles)
Dan Beam 2016/01/27 02:50:45 nit: to improve your code golf swing return '\n
tsergeant 2016/01/27 03:12:14 Done, one-upping by getting rid of the compile.
43
39 def _remove_ats(s): 44 def _remove_ats(s):
40 at_reg = re.compile(r""" 45 at_reg = re.compile(r"""
41 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x 46 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x
42 (.*{.*?})+ # inner { curly } blocks, rules, and selector 47 (.*{.*?})+ # inner { curly } blocks, rules, and selector
43 .*?} # stuff up to the first end curly } 48 .*?} # stuff up to the first end curly }
44 """, 49 """,
45 re.DOTALL | re.VERBOSE) 50 re.DOTALL | re.VERBOSE)
46 return at_reg.sub('\\1', s) 51 return at_reg.sub('\\1', s)
47 52
48 def _remove_comments(s): 53 def _remove_comments(s):
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 'test': zero_width_lengths, 373 'test': zero_width_lengths,
369 'multiline': True, 374 'multiline': True,
370 }, 375 },
371 ] 376 ]
372 377
373 results = [] 378 results = []
374 affected_files = self.input_api.AffectedFiles(include_deletes=False, 379 affected_files = self.input_api.AffectedFiles(include_deletes=False,
375 file_filter=self.file_filter) 380 file_filter=self.file_filter)
376 files = [] 381 files = []
377 for f in affected_files: 382 for f in affected_files:
378 # Remove all /*comments*/, @at-keywords, and grit <if|include> tags; we're 383 file_contents = '\n'.join(f.NewContents())
379 # not using a real parser. TODO(dbeam): Check alpha in <if> blocks. 384 path = f.LocalPath()
380 file_contents = _remove_all('\n'.join(f.NewContents())) 385 # Handle CSS files and HTML files with inline styles.
381 files.append((f.LocalPath(), file_contents)) 386 if path.endswith('.html'):
387 file_contents = _extract_inline_style(file_contents)
Dan Beam 2016/01/27 02:50:45 nit: \n
tsergeant 2016/01/27 03:12:14 Done.
388 if path.endswith('.html') or path.endswith('.css'):
389 # Remove all /*comments*/, @at-keywords, and grit <if|include> tags;
390 # we're not using a real parser. TODO(dbeam): Check alpha in <if>
391 # blocks.
392 file_contents = _remove_all(file_contents)
393 files.append((path, file_contents))
382 394
383 # Only look at CSS files for now. 395 for f in files:
384 for f in filter(lambda f: f[0].endswith('.css'), files):
385 file_errors = [] 396 file_errors = []
386 for check in added_or_modified_files_checks: 397 for check in added_or_modified_files_checks:
387 # If the check is multiline, it receieves the whole file and gives us 398 # If the check is multiline, it receieves the whole file and gives us
388 # back a list of things wrong. If the check isn't multiline, we pass it 399 # back a list of things wrong. If the check isn't multiline, we pass it
389 # each line and the check returns something truthy if there's an issue. 400 # each line and the check returns something truthy if there's an issue.
390 if ('multiline' in check and check['multiline']): 401 if ('multiline' in check and check['multiline']):
391 assert not 'after' in check 402 assert not 'after' in check
392 check_errors = check['test'](f[1]) 403 check_errors = check['test'](f[1])
393 if len(check_errors) > 0: 404 if len(check_errors) > 0:
394 file_errors.append('- %s\n%s' % 405 file_errors.append('- %s\n%s' %
395 (check['desc'], '\n'.join(check_errors).rstrip())) 406 (check['desc'], '\n'.join(check_errors).rstrip()))
396 else: 407 else:
397 check_errors = [] 408 check_errors = []
398 lines = f[1].splitlines() 409 lines = f[1].splitlines()
399 for lnum, line in enumerate(lines): 410 for lnum, line in enumerate(lines):
400 if check['test'](line): 411 if check['test'](line):
401 error = ' ' + line.strip() 412 error = ' ' + line.strip()
402 if 'after' in check: 413 if 'after' in check:
403 error += check['after'](line) 414 error += check['after'](line)
404 check_errors.append(error) 415 check_errors.append(error)
405 if len(check_errors) > 0: 416 if len(check_errors) > 0:
406 file_errors.append('- %s\n%s' % 417 file_errors.append('- %s\n%s' %
407 (check['desc'], '\n'.join(check_errors))) 418 (check['desc'], '\n'.join(check_errors)))
408 if file_errors: 419 if file_errors:
409 results.append(self.output_api.PresubmitPromptWarning( 420 results.append(self.output_api.PresubmitPromptWarning(
410 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) 421 '%s:\n%s' % (f[0], '\n\n'.join(file_errors))))
411 422
412 return results 423 return results
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/web_dev_style/css_checker_test.py » ('j') | chrome/browser/web_dev_style/css_checker_test.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698