From a3ccc1974c4dbb35043e20b714171a60ac0f77bb Mon Sep 17 00:00:00 2001 From: Ruan Bahia Date: Thu, 4 Aug 2022 23:48:38 -0300 Subject: [PATCH] fix(formatter): fix IndexError while adding indentation to attributes solves issue with boolean attributes at the start of tags. closes #290 --- src/djlint/formatter/attributes.py | 4 +- tests/test_html/test_attributes.py | 63 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/djlint/formatter/attributes.py b/src/djlint/formatter/attributes.py index eb962b2..7408151 100644 --- a/src/djlint/formatter/attributes.py +++ b/src/djlint/formatter/attributes.py @@ -56,7 +56,9 @@ def format_template_tags(config: Config, attributes: str) -> str: attributes.splitlines()[0].strip(), ) ) - )[-1] + ) + if start_test: + start_test = start_test[-1] base_indent = len(attr_name.group()) diff --git a/tests/test_html/test_attributes.py b/tests/test_html/test_attributes.py index 030df37..9f6aeac 100644 --- a/tests/test_html/test_attributes.py +++ b/tests/test_html/test_attributes.py @@ -6,6 +6,8 @@ run:: --cov-report xml:coverage.xml --cov-report term-missing pytest tests/test_html/test_attributes.py::test_long_attributes + pytest tests/test_html/test_attributes.py::test_ignored_attributes + pytest tests/test_html/test_attributes.py::test_boolean_attributes Some tests may be from prettier.io's html test suite. @@ -118,6 +120,67 @@ def test_ignored_attributes(runner: CliRunner, tmp_file: TextIO) -> None: ) +def test_boolean_attributes(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat( + tmp_file, + runner, + b"""""", + ) + + # boolean attributes after tag must be reformatted correctly + assert output.exit_code == 1 + print(output.text) + assert ( + output.text + == """ +""" + ) + + # boolean attributes after tag with long attributes must be reformatted correctly + output = reformat( + tmp_file, + runner, + b"""""", + ) + assert output.exit_code == 1 + print(output.text) + assert ( + output.text + == """ +""" + ) + + # boolean attributes after tag are accepted + output = reformat( + tmp_file, + runner, + b"""""" + ) + assert output.exit_code == 0 + # def test_attributes(runner: CliRunner, tmp_file: TextIO) -> None: # html_in = (