linter: Fix bug resulting in <pre> being indented cyclically.

This commit is contained in:
Aditya Bansal 2017-06-13 10:44:48 +05:30 committed by showell
parent a77aa79da9
commit d2f0e17454
3 changed files with 43 additions and 5 deletions

View File

@ -150,7 +150,7 @@ def check_html_templates(templates, all_dups):
'templates/zerver/markdown_help.html',
'templates/zerver/navbar.html',
'templates/zerver/register.html',
'templates/zerver/test_emails.html',
'zerver/webhooks/deskdotcom/doc.html',
'zerver/webhooks/librato/doc.html',
'zerver/webhooks/splunk/doc.html',

View File

@ -30,7 +30,8 @@ def pretty_print_html(html, num_spaces=4):
line=-1,
token_kind='html_start',
tag='html',
extra_indent=0) # type: Dict[str, Any]
extra_indent=0,
ignore_lines=[]) # type: Dict[str, Any]
stack.append(info)
# Our main job is to figure out offsets that we use to nudge lines
@ -79,7 +80,8 @@ def pretty_print_html(html, num_spaces=4):
extra_indent_prev=extra_indent,
adjustment=adjustment,
indenting=True,
adjust_offset_until=token.line
adjust_offset_until=token.line,
ignore_lines=[]
)
if token.kind in ('handlebars_start', 'django_start'):
info.update(dict(depth=new_depth - 1, indenting=False))
@ -91,7 +93,8 @@ def pretty_print_html(html, num_spaces=4):
line=token.line,
tag=token.tag,
token_kind=token.kind,
extra_indent=stack[-1]['extra_indent']
extra_indent=stack[-1]['extra_indent'],
ignore_lines=[]
)
stack.append(info)
elif token.kind in ('html_end', 'handlebars_end',
@ -107,6 +110,8 @@ def pretty_print_html(html, num_spaces=4):
if token.tag == 'pre':
offsets[start_line] = 0
offsets[end_line] = 0
stack[-1]['ignore_lines'].append(start_line)
stack[-1]['ignore_lines'].append(end_line)
else:
offsets[start_line] = info['offset']
line = lines[token.line - 1]
@ -139,7 +144,8 @@ def pretty_print_html(html, num_spaces=4):
offsets[line_num] = offset
elif (token.kind in ('handlebars_end', 'django_end') and
info['indenting'] and
line_num < info['adjust_offset_until']):
line_num < info['adjust_offset_until'] and
line_num not in info['ignore_lines']):
offsets[line_num] += num_spaces
elif token.tag != 'pre':
for line_num in range(start_line + 1, end_line):
@ -149,6 +155,7 @@ def pretty_print_html(html, num_spaces=4):
for line_num in range(start_line + 1, end_line):
if line_num not in offsets:
offsets[line_num] = 0
stack[-1]['ignore_lines'].append(line_num)
# Now that we have all of our offsets calculated, we can just
# join all our lines together, fixing up offsets as needed.

View File

@ -362,6 +362,36 @@ GOOD_HTML13 = """
<div>{{this.count}}</div>
</div>
"""
BAD_HTML14 = """
<div>
{{#if this.code}}
<pre>Here goes some cool code.</pre>
{{else}}
<div>
content of first div
<div>
content of second div.
</div>
</div>
{{/if}}
</div>
"""
GOOD_HTML14 = """
<div>
{{#if this.code}}
<pre>Here goes some cool code.</pre>
{{else}}
<div>
content of first div
<div>
content of second div.
</div>
</div>
{{/if}}
</div>
"""
class TestPrettyPrinter(unittest.TestCase):
def compare(self, a, b):
# type: (str, str) -> None
@ -384,3 +414,4 @@ class TestPrettyPrinter(unittest.TestCase):
self.compare(pretty_print_html(BAD_HTML11), GOOD_HTML11)
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)