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

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: Review comments Created 4 years, 11 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 | chrome/browser/web_dev_style/css_checker_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 return '\n'.join(re.findall(r'<style>([^<]*)<\/style>', s))
41
39 def _remove_ats(s): 42 def _remove_ats(s):
40 at_reg = re.compile(r""" 43 at_reg = re.compile(r"""
41 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x 44 @(?!\d+x\b)\w+[^'"]*?{ # @at-keyword selector junk {, not @2x
42 (.*{.*?})+ # inner { curly } blocks, rules, and selector 45 (.*{.*?})+ # inner { curly } blocks, rules, and selector
43 .*?} # stuff up to the first end curly } 46 .*?} # stuff up to the first end curly }
44 """, 47 """,
45 re.DOTALL | re.VERBOSE) 48 re.DOTALL | re.VERBOSE)
46 return at_reg.sub('\\1', s) 49 return at_reg.sub('\\1', s)
47 50
48 def _remove_comments(s): 51 def _remove_comments(s):
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 'test': zero_width_lengths, 371 'test': zero_width_lengths,
369 'multiline': True, 372 'multiline': True,
370 }, 373 },
371 ] 374 ]
372 375
373 results = [] 376 results = []
374 affected_files = self.input_api.AffectedFiles(include_deletes=False, 377 affected_files = self.input_api.AffectedFiles(include_deletes=False,
375 file_filter=self.file_filter) 378 file_filter=self.file_filter)
376 files = [] 379 files = []
377 for f in affected_files: 380 for f in affected_files:
378 # Remove all /*comments*/, @at-keywords, and grit <if|include> tags; we're 381 file_contents = '\n'.join(f.NewContents())
379 # not using a real parser. TODO(dbeam): Check alpha in <if> blocks. 382 path = f.LocalPath()
380 file_contents = _remove_all('\n'.join(f.NewContents())) 383 # Handle CSS files and HTML files with inline styles.
381 files.append((f.LocalPath(), file_contents)) 384 if path.endswith('.html'):
385 file_contents = _extract_inline_style(file_contents)
382 386
383 # Only look at CSS files for now. 387 if path.endswith('.html') or path.endswith('.css'):
384 for f in filter(lambda f: f[0].endswith('.css'), files): 388 # Remove all /*comments*/, @at-keywords, and grit <if|include> tags;
389 # we're not using a real parser. TODO(dbeam): Check alpha in <if>
390 # blocks.
391 file_contents = _remove_all(file_contents)
392 files.append((path, file_contents))
393
394 for f in files:
385 file_errors = [] 395 file_errors = []
386 for check in added_or_modified_files_checks: 396 for check in added_or_modified_files_checks:
387 # If the check is multiline, it receieves the whole file and gives us 397 # 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 398 # 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. 399 # each line and the check returns something truthy if there's an issue.
390 if ('multiline' in check and check['multiline']): 400 if ('multiline' in check and check['multiline']):
391 assert not 'after' in check 401 assert not 'after' in check
392 check_errors = check['test'](f[1]) 402 check_errors = check['test'](f[1])
393 if len(check_errors) > 0: 403 if len(check_errors) > 0:
394 file_errors.append('- %s\n%s' % 404 file_errors.append('- %s\n%s' %
395 (check['desc'], '\n'.join(check_errors).rstrip())) 405 (check['desc'], '\n'.join(check_errors).rstrip()))
396 else: 406 else:
397 check_errors = [] 407 check_errors = []
398 lines = f[1].splitlines() 408 lines = f[1].splitlines()
399 for lnum, line in enumerate(lines): 409 for lnum, line in enumerate(lines):
400 if check['test'](line): 410 if check['test'](line):
401 error = ' ' + line.strip() 411 error = ' ' + line.strip()
402 if 'after' in check: 412 if 'after' in check:
403 error += check['after'](line) 413 error += check['after'](line)
404 check_errors.append(error) 414 check_errors.append(error)
405 if len(check_errors) > 0: 415 if len(check_errors) > 0:
406 file_errors.append('- %s\n%s' % 416 file_errors.append('- %s\n%s' %
407 (check['desc'], '\n'.join(check_errors))) 417 (check['desc'], '\n'.join(check_errors)))
408 if file_errors: 418 if file_errors:
409 results.append(self.output_api.PresubmitPromptWarning( 419 results.append(self.output_api.PresubmitPromptWarning(
410 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) 420 '%s:\n%s' % (f[0], '\n\n'.join(file_errors))))
411 421
412 return results 422 return results
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/web_dev_style/css_checker_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698