linter: Make html singleton tags use 2 space indentation.

This commit is contained in:
Aditya Bansal 2018-04-03 13:37:23 +05:30 committed by Tim Abbott
parent 7eacf2aa9a
commit 2fe012ffff
31 changed files with 154 additions and 112 deletions

View File

@ -114,19 +114,25 @@ def pretty_print_html(html, num_spaces=4):
offsets[start_line] = info['offset']
line = lines[token.line - 1]
adjustment = len(line)-len(line.lstrip()) + 1
if adjustment == token.col:
if adjustment == token.col and token.kind != 'html_singleton_end':
offsets[end_line] = (info['offset'] +
info['adjustment'] -
adjustment +
info['extra_indent'] -
info['extra_indent_prev'])
elif (start_line + info['line_span'] - 1 == end_line and
info['line_span'] > 2 and token.kind != 'html_singleton_end'):
(info['line_span'] > 2 or
(info['line_span'] == 2 and
token.kind == 'html_singleton_end'))):
offsets[end_line] = (1 + info['extra_indent'] +
(info['depth'] + 1) * num_spaces) - adjustment
if token.kind == 'html_singleton_end':
# We would like singleton tags to have 2 space
# indentation in case they span over multiple lines.
offsets[end_line] -= 2
elif token.line != info['line']:
offsets[end_line] = info['offset']
if token.tag != 'pre' and token.kind != 'html_singleton_end' and token.tag != 'script':
if token.tag != 'pre' and token.tag != 'script':
for line_num in range(start_line + 1, end_line):
# Be careful not to override offsets that happened
# deeper in the HTML within our block.
@ -140,6 +146,10 @@ def pretty_print_html(html, num_spaces=4):
extra_indent = info['extra_indent']
adjustment = len(line)-len(line.lstrip()) + 1
offset = (1 + extra_indent + new_depth * num_spaces) - adjustment
if token.kind == 'html_singleton_end':
# We would like singleton tags to have 2 space
# indentation in case they span over multiple lines.
offset -= 2
offsets[line_num] = offset
elif (token.kind in ('handlebars_end', 'django_end') and
info['indenting'] and

View File

@ -389,6 +389,37 @@ GOOD_HTML14 = """
{{/if}}
</div>
"""
BAD_HTML15 = """
<div>
<img alt=":thumbs_up:"
class="emoji"
src="/path/to/png"
title=":thumbs_up:"/>
<img alt=":thumbs_up:"
class="emoji"
src="/path/to/png"
title=":thumbs_up:"/>
<img alt=":thumbs_up:"
title=":thumbs_up:"/>
</div>
"""
GOOD_HTML15 = """
<div>
<img alt=":thumbs_up:"
class="emoji"
src="/path/to/png"
title=":thumbs_up:"/>
<img alt=":thumbs_up:"
class="emoji"
src="/path/to/png"
title=":thumbs_up:"/>
<img alt=":thumbs_up:"
title=":thumbs_up:"/>
</div>
"""
class TestPrettyPrinter(unittest.TestCase):
def compare(self, a: str, b: str) -> None:
self.assertEqual(a.split('\n'), b.split('\n'))
@ -410,3 +441,4 @@ class TestPrettyPrinter(unittest.TestCase):
self.compare(pretty_print_html(BAD_HTML12), GOOD_HTML12)
self.compare(pretty_print_html(BAD_HTML13), GOOD_HTML13)
self.compare(pretty_print_html(BAD_HTML14), GOOD_HTML14)
self.compare(pretty_print_html(BAD_HTML15), GOOD_HTML15)