1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
6 Presubmit for Chromium HTML resources. See chrome/browser/PRESUBMIT.py.
12 class HtmlChecker(object):
13 def __init__(self
, input_api
, output_api
, file_filter
=None):
14 self
.input_api
= input_api
15 self
.output_api
= output_api
16 self
.file_filter
= file_filter
18 def ClassesUseDashFormCheck(self
, line_number
, line
):
19 regex
= self
.input_api
.re
.compile("""
20 (?:^|\s) # start of line or whitespace
21 (class="[^"]*[A-Z_][^"]*") # class contains caps or '_'
23 self
.input_api
.re
.VERBOSE
)
24 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
25 "Classes should use dash-form.")
27 def DoNotCloseSingleTagsCheck(self
, line_number
, line
):
29 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
30 "Do not close single tags.")
32 def DoNotUseBrElementCheck(self
, line_number
, line
):
34 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
35 "Do not use <br>; place blocking elements (<div>) as appropriate.")
37 def DoNotUseInputTypeButtonCheck(self
, line_number
, line
):
38 regex
= self
.input_api
.re
.compile("""
39 (<input [^>]* # "<input " followed by anything but ">"
40 type="button" # type="button"
41 [^>]*>) # anything but ">" then ">"
43 self
.input_api
.re
.VERBOSE
)
44 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
45 'Use the button element instead of <input type="button">')
47 def I18nContentJavaScriptCaseCheck(self
, line_number
, line
):
48 regex
= self
.input_api
.re
.compile("""
49 (?:^|\s) # start of line or whitespace
50 i18n-content=" # i18n-content="
51 ([A-Z][^"]*|[^"]*[-_][^"]*)" # starts with caps or contains '-' or '_'
53 self
.input_api
.re
.VERBOSE
)
54 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
55 "For i18n-content use javaScriptCase.")
57 def LabelCheck(self
, line_number
, line
):
58 regex
= self
.input_api
.re
.compile("""
59 (?:^|\s) # start of line or whitespace
62 self
.input_api
.re
.VERBOSE
)
63 return regex_check
.RegexCheck(self
.input_api
.re
, line_number
, line
, regex
,
64 "Avoid 'for' attribute on <label>. Place the input within the <label>, "
65 "or use aria-labelledby for <select>.")
68 """Check for violations of the Chromium web development style guide. See
69 http://chromium.org/developers/web-development-style-guide
73 affected_files
= self
.input_api
.change
.AffectedFiles(
74 file_filter
=self
.file_filter
, include_deletes
=False)
76 for f
in affected_files
:
79 for line_number
, line
in f
.ChangedContents():
80 errors
.extend(filter(None, [
81 self
.ClassesUseDashFormCheck(line_number
, line
),
82 self
.DoNotCloseSingleTagsCheck(line_number
, line
),
83 self
.DoNotUseBrElementCheck(line_number
, line
),
84 self
.DoNotUseInputTypeButtonCheck(line_number
, line
),
85 self
.I18nContentJavaScriptCaseCheck(line_number
, line
),
86 self
.LabelCheck(line_number
, line
),
90 abs_local_path
= f
.AbsoluteLocalPath()
91 file_indicator
= 'Found HTML style issues in %s' % abs_local_path
92 prompt_msg
= file_indicator
+ '\n\n' + '\n'.join(errors
) + '\n'
93 results
.append(self
.output_api
.PresubmitPromptWarning(prompt_msg
))