Skip to content

Commit

Permalink
Merge pull request #487 from Riverside-Healthcare/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering authored Dec 15, 2022
2 parents d2a7ddf + 5890161 commit 2e67f9d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a href="https://www.djlint.com"><img src="https://raw.githubusercontent.com/Riverside-Healthcare/djLint/master/docs/src/static/img/icon.png" alt="djLint Logo" width="270"></a>
<br>
</h1>
<h3 align="center">🙏 Passed 100k downloads! Thank you! 🙏</h3>
<h3 align="center">🏗️ Maintainers needed, please reach out on discord or email!</h3>
<h4 align="center">The missing formatter and linter for HTML templates.</h4>

<p align="center">
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 17 additions & 5 deletions src/djlint/formatter/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions src/djlint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_html/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""<button id="test"
name="test"
type="submit"
one="isTrue ? 'True' : 'False'"
two="'Test' ."
three="'Test'"></button>""",
)

assert output.exit_code == 0


# def test_attributes(runner: CliRunner, tmp_file: TextIO) -> None:

# html_in = (
Expand Down
20 changes: 20 additions & 0 deletions tests/test_html/test_tag_textarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None:
"""
)

output = reformat(
tmp_file,
runner,
b"""<div><textarea type="textarea" id="messageContent" name="adContent" maxlength="300" class="form-control class_two" rows="10">{{ adContent|default }}</textarea></div>
""",
)

assert (
output.text
== """<div>
<textarea type="textarea"
id="messageContent"
name="adContent"
maxlength="300"
class="form-control class_two"
rows="10">{{ adContent|default }}</textarea>
</div>
"""
)


def test_a_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
Expand Down

0 comments on commit 2e67f9d

Please sign in to comment.