Commit 02602da5 authored by dschuyler's avatar dschuyler Committed by Commit bot

[Presubmit] check for single quotes in html properties

This CL will cause presubmit to warn programmers when using single
quotes in HTML property names.

BUG=622814

Review-Url: https://codereview.chromium.org/2091903002
Cr-Commit-Position: refs/heads/master@{#402605}
parent 9d585f6c
......@@ -44,6 +44,17 @@ class HtmlChecker(object):
return regex_check.RegexCheck(self.input_api.re, line_number, line, regex,
'Use the button element instead of <input type="button">')
def DoNotUseSingleQuotesCheck(self, line_number, line):
regex = self.input_api.re.compile("""
<\S+ # The tag name.
(?:\s+\S+\$?="[^"]*"|\s+\S+)* # Correctly quoted or non-value props.
\s+(\S+\$?='[^']*') # Find incorrectly quoted (foo='bar').
[^>]*> # To the end of the tag.
""",
self.input_api.re.MULTILINE | self.input_api.re.VERBOSE)
return regex_check.RegexCheck(self.input_api.re, line_number, line, regex,
'Use double quotes rather than single quotes in HTML properties')
def I18nContentJavaScriptCaseCheck(self, line_number, line):
regex = self.input_api.re.compile("""
(?:^|\s) # start of line or whitespace
......
......@@ -56,6 +56,28 @@ class HtmlCheckerTest(SuperMoxTestBase):
for line in lines:
self.ShouldPassCheck(line, self.checker.ClassesUseDashFormCheck)
def testSingleQuoteCheckFails(self):
lines = [
""" <a href='classBar'> """,
"""<a foo$="bar" href$='classBar'>""",
"""<a foo="bar" less="more" href='classBar' kittens="cats">""",
"""<a cats href='classBar' dogs>""",
"""<a cats\n href='classBat\nclassBaz'\n dogs>""",
]
for line in lines:
self.ShouldFailCheck(line, self.checker.DoNotUseSingleQuotesCheck)
def testSingleQuoteCheckPasses(self):
lines = [
"""<b id="super-valid">SO VALID!</b>""",
"""<a text$="i ain't got invalid quotes">i don't</a>""",
"""<span>[[i18n('blah')]]</span> """,
"""<a cats href="classBar" dogs>""",
"""<a cats\n href="classBar"\n dogs>""",
]
for line in lines:
self.ShouldPassCheck(line, self.checker.DoNotUseSingleQuotesCheck)
def testDoNotCloseSingleTagsCheckFails(self):
lines = [
"<input/>",
......
......@@ -6,5 +6,10 @@
def GetHighlight(line, error):
"""Returns the substring of |line| that is highlighted in |error|."""
error_lines = error.split('\n')
# TODO(dschuyler): Splitting the error on \n will prevent index(line)
# from finding the line. As a workaround, return the whole, unfiltered
# line.
if line not in error_lines:
return line
highlight = error_lines[error_lines.index(line) + 1]
return ''.join(ch1 for (ch1, ch2) in zip(line, highlight) if ch2 == '^')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment