diff --git a/README.md b/README.md
index 7cf124ee3..af4921011 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
-
@@ -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 ff390bcb3..00b0e77ca 100644 --- a/src/djlint/formatter/attributes.py +++ b/src/djlint/formatter/attributes.py @@ -4,7 +4,7 @@ import regex as re -from ..helpers import inside_ignored_block +from ..helpers import child_of_ignored_block from ..settings import Config @@ -115,7 +115,7 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: """Spread long attributes over multiple lines.""" # check that we are not inside an ignored block if ( - inside_ignored_block(config, html, match) + child_of_ignored_block(config, html, match) or len(match.group(3).strip()) < config.max_attribute_length ): return match.group() @@ -132,11 +132,24 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: for attr_grp in re.finditer( config.attribute_pattern, match.group(3).strip(), re.VERBOSE ): - 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 2d9de3414..a5d91f4a2 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -123,6 +123,28 @@ 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( + ignored_match.start(0) < match.start() and match.end(0) <= ignored_match.end() + for ignored_match in list( + re.finditer( + re.compile( + config.ignored_blocks, + re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE, + ), + html, + ) + ) + + list( + re.finditer( + re.compile(config.ignored_inline_blocks, re.IGNORECASE | re.VERBOSE), + html, + ) + ) + ) + + def overlaps_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/src/djlint/settings.py b/src/djlint/settings.py index be456f3c7..92e133a00 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -116,7 +116,7 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict: pyproject_file = find_pyproject(src) if pyproject_file: - content = tomllib.load(pyproject_file.open("rb")) + content = tomllib.loads(pyproject_file.read_text(encoding="utf8")) try: return {**djlint_content, **content["tool"]["djlint"]} # type: ignore except KeyError: 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 f0586543d..19b0b6ae1 100644 --- a/tests/test_html/test_tag_textarea.py +++ b/tests/test_html/test_tag_textarea.py @@ -68,6 +68,26 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None: """ ) + output = reformat( + tmp_file, + runner, + b"""
+""", + ) + + assert ( + output.text + == """