From dff02dcdc3e4d99476a82cba57aa115f21404525 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 15 Dec 2022 10:46:39 -0600 Subject: [PATCH] fix(attributes): fixed outer quotes being stripped from attribute values closes #471 --- README.md | 2 +- src/djlint/formatter/attributes.py | 18 +++++++++++++++--- src/djlint/helpers.py | 1 + tests/test_html/test_attributes.py | 15 +++++++++++++++ tests/test_html/test_tag_textarea.py | 7 +++++-- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 301ce7907..af4921011 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ Looks like this: ## 🛠️ Can I help? -Yes! +Yes! *Would you like to add a rule to the linter?* Take a look at the [linter docs](https://djlint.com/docs/linter/) and [source code](https://github.com/Riverside-Healthcare/djLint/blob/master/src/djlint/rules.yaml) diff --git a/src/djlint/formatter/attributes.py b/src/djlint/formatter/attributes.py index 0bb43e9e2..00b0e77ca 100644 --- a/src/djlint/formatter/attributes.py +++ b/src/djlint/formatter/attributes.py @@ -128,7 +128,6 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: attributes = [] - print(match, match.group(3)) # format attributes as groups for attr_grp in re.finditer( config.attribute_pattern, match.group(3).strip(), re.VERBOSE @@ -136,7 +135,21 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: attrib_name = attr_grp.group(1) is_quoted = attr_grp.group(2) and attr_grp.group(2)[0] in ["'", '"'] quote = attr_grp.group(2)[0] if is_quoted else '"' - attrib_value = attr_grp.group(2).strip("\"'") if attr_grp.group(2) else None + + attrib_value = None + + if attr_grp.group(2) and attr_grp.group(2)[0] == attr_grp.group(2)[-1]: + if attr_grp.group(2)[0] == "'": + attrib_value = attr_grp.group(2).strip("'") + + elif attr_grp.group(2)[0] == '"': + attrib_value = attr_grp.group(2).strip('"') + + else: + attrib_value = attr_grp.group(2) + else: + attrib_value = attr_grp.group(2) + standalone = attr_grp.group(3) quote_length = 1 @@ -184,7 +197,6 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: attributes.append( (attrib_name or "") + (attrib_value or "") + (standalone or "") ) - attribute_string = ("\n" + spacing).join([x for x in attributes if x]) close = match.group(4) diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py index 9b119175f..a5d91f4a2 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -122,6 +122,7 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool: ) ) + def child_of_ignored_block(config: Config, html: str, match: re.Match) -> bool: """Do not add whitespace if the tag is in a non indent block.""" return any( diff --git a/tests/test_html/test_attributes.py b/tests/test_html/test_attributes.py index 8836e05ba..19ae5e506 100644 --- a/tests/test_html/test_attributes.py +++ b/tests/test_html/test_attributes.py @@ -215,6 +215,21 @@ def test_boolean_attributes(runner: CliRunner, tmp_file: TextIO) -> None: assert output.exit_code == 0 +def test_attribute_quotes(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat( + tmp_file, + runner, + b"""""", + ) + + assert output.exit_code == 0 + + # def test_attributes(runner: CliRunner, tmp_file: TextIO) -> None: # html_in = ( diff --git a/tests/test_html/test_tag_textarea.py b/tests/test_html/test_tag_textarea.py index faebcba87..19b0b6ae1 100644 --- a/tests/test_html/test_tag_textarea.py +++ b/tests/test_html/test_tag_textarea.py @@ -69,8 +69,11 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None: ) output = reformat( - tmp_file,runner,b"""
-""") + tmp_file, + runner, + b"""
+""", + ) assert ( output.text