mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-12 23:33:10 +00:00
added fix for #152
This commit is contained in:
parent
c1857efb8d
commit
a10ac08686
14 changed files with 389 additions and 276 deletions
|
|
@ -20,10 +20,10 @@ This pattern is not recommended:
|
|||
<div class="class1{% if condition -%} class2{%- endif %}">content</div>
|
||||
^ space here
|
||||
|
||||
Spaceless Conditional Attributes
|
||||
--------------------------------
|
||||
``format_attribute_template_tags`` and Spaceless Conditional Attributes
|
||||
-----------------------------------------------------------------------
|
||||
|
||||
Conditional attributes should use spaceless tags, for example ``{% if a -%}`` in nunjuck and jinja, to remove spaces inside the.
|
||||
If ``format_attribute_template_tags`` option is enabled, conditional attributes should use spaceless tags, for example ``{% if a -%}`` in nunjuck and jinja, or ``{% spaceless %}{% if a %}{% endspaceless %}`` in django, to remove spaces inside the.
|
||||
|
||||
djLint will format long attributes onto multiple lines, and the whitespace saved inside of attributes could break your code.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
Changelog
|
||||
=========
|
||||
|
||||
next release
|
||||
------------
|
||||
- Added config option ``format_attribute_template_tags`` as opt-in for template tag formatting inside of attributes
|
||||
|
||||
0.6.6
|
||||
-----
|
||||
- Big fixes
|
||||
|
|
|
|||
|
|
@ -160,3 +160,24 @@ Usage:
|
|||
.. code:: ini
|
||||
|
||||
use_gitignore = True
|
||||
|
||||
format_attribute_template_tags
|
||||
------------------------------
|
||||
|
||||
Formatter will attempt to format template syntax inside of tag attributes. Disabled by default.
|
||||
|
||||
Usage:
|
||||
|
||||
.. code:: ini
|
||||
|
||||
format_attribute_template_tags=true
|
||||
|
||||
For example, with this option enabled, the following html will be acceptable:
|
||||
|
||||
.. code:: html
|
||||
|
||||
<input class="{% if this %}
|
||||
then something neat
|
||||
{% else %}
|
||||
that is long stuff asdf and more even
|
||||
{% endif %}"/>
|
||||
|
|
|
|||
|
|
@ -124,7 +124,9 @@ def format_template_tags(config: Config, attributes: str) -> str:
|
|||
)
|
||||
|
||||
base_indent_space = base_indent * " "
|
||||
indented += f"\n{leading_space}{base_indent_space}{tmp}"
|
||||
|
||||
if tmp.strip() != "":
|
||||
indented += f"\n{leading_space}{base_indent_space}{tmp}"
|
||||
|
||||
end_text = re.findall(re.compile(r"[\"']$", re.M), line.strip())
|
||||
|
||||
|
|
@ -260,7 +262,8 @@ def format_attributes(config: Config, match: re.match) -> str:
|
|||
attributes = f"{leading_space}{tag}{attributes}{close}"
|
||||
|
||||
# format template tags
|
||||
attributes = format_template_tags(config, attributes)
|
||||
if config.format_attribute_template_tags:
|
||||
attributes = format_template_tags(config, attributes)
|
||||
|
||||
# format styles
|
||||
func = partial(format_style)
|
||||
|
|
|
|||
|
|
@ -180,6 +180,10 @@ class Config:
|
|||
build_custom_blocks(djlint_settings.get("custom_blocks")) or ""
|
||||
)
|
||||
|
||||
self.format_attribute_template_tags = djlint_settings.get(
|
||||
"format_attribute_template_tags", False
|
||||
)
|
||||
|
||||
# ignore is based on input and also profile
|
||||
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
|
||||
|
||||
|
|
|
|||
77
tests/config_format_attribute_template_tags/html-one.html
Normal file
77
tests/config_format_attribute_template_tags/html-one.html
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<!-- list of good and complex attribute patterns -->
|
||||
<input class="{% if this %}
|
||||
then something neat
|
||||
{% else %}
|
||||
that is long stuff asdf and more even
|
||||
{% endif %}"/>
|
||||
<img data-src="{% if report.imgs.exists %}
|
||||
{{ report.imgs.first.get_absolute_url|size:"96x96" }}
|
||||
{% else %}
|
||||
{% static '/img/report_thumb_placeholder_400x300.png' %}
|
||||
{% endif %}"
|
||||
src="{% static '/img/loader.gif' %}"
|
||||
alt="report image"/>
|
||||
<a class="asdf
|
||||
{% if favorite == "yes" %}
|
||||
favorite
|
||||
{% endif %}
|
||||
has-tooltip-arrow has-tooltip-right"
|
||||
data-tooltip="{% if favorite == "yes" %}
|
||||
Remove from Favorites
|
||||
{% else %}
|
||||
Add to Favorites
|
||||
{% endif %}"
|
||||
fav-type="report"
|
||||
object-id="{{ report.report_id }}">
|
||||
<span class="icon has-text-grey is-large ">
|
||||
<i class="fas fa-lg fa-star"></i>
|
||||
</span>
|
||||
</a>
|
||||
<div class="media-content"
|
||||
{% ifchanged comment.stream_id %}
|
||||
comments-msg
|
||||
{% else %}
|
||||
comments-newMsgReply
|
||||
{% endifchanged %}>
|
||||
</div>
|
||||
<a class="piwik_download"
|
||||
href="{% static activity_version.get_win_document_with_images_file_path %}?{% now "jSFYHi" %}">
|
||||
</a>
|
||||
<span {% if a %}
|
||||
required
|
||||
{% endif %}
|
||||
title="{% if eev.status == eev.STATUS_CURRENT %}
|
||||
{% trans 'A' %}
|
||||
{% elif eev.status == eev.STATUS_APPROVED %}
|
||||
{% trans 'B' %}
|
||||
{% elif eev.status == eev.STATUS_EXPIRED %}
|
||||
{% trans 'C' %}
|
||||
{% endif %}"
|
||||
class="asdf
|
||||
{% if a %}
|
||||
b
|
||||
{% endif %}
|
||||
asdf"
|
||||
{% if a %}
|
||||
checked
|
||||
{% endif %}>
|
||||
</span>
|
||||
{% block body %}
|
||||
<form action="{% if gpg -%}asdf something pretty long. can't beat this length{%- endif %}"
|
||||
method="POST">
|
||||
</form>
|
||||
{% endblock body %}
|
||||
<ul class="menu-list
|
||||
{% for child in entry.children %}
|
||||
{% if child.url== page.url %}
|
||||
is-active
|
||||
{% else %}
|
||||
not-active
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
is-collapsible">
|
||||
</ul>
|
||||
<div {% if true %}
|
||||
class="test"
|
||||
{% endif %}
|
||||
{% include "django/forms/widgets/attrs.html" %}></div>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[tool]
|
||||
[tool.djlint]
|
||||
format_attribute_template_tags=true
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Generator, TextIO
|
||||
|
||||
import pytest
|
||||
|
|
@ -32,7 +33,9 @@ def write_to_file(the_file: str, the_text: bytes) -> None:
|
|||
open_file.write(the_text)
|
||||
|
||||
|
||||
def reformat(the_file: TextIO, runner: CliRunner, the_text: bytes) -> dict:
|
||||
def reformat(the_file: TextIO, runner: CliRunner, the_text: bytes) -> SimpleNamespace:
|
||||
write_to_file(the_file.name, the_text)
|
||||
result = runner.invoke(djlint, [the_file.name, "--reformat"])
|
||||
return {"text": Path(the_file.name).read_text(), "exit_code": result.exit_code}
|
||||
return SimpleNamespace(
|
||||
**{"text": Path(the_file.name).read_text(), "exit_code": result.exit_code}
|
||||
)
|
||||
|
|
|
|||
171
tests/test_config_format_attribute_template_tags.py
Normal file
171
tests/test_config_format_attribute_template_tags.py
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
"""Djlint tests specific to pyproject.toml > format_attribute_template_tags configuration.
|
||||
|
||||
run::
|
||||
|
||||
pytest tests/test_config_format_attribute_template_tags.py --cov=src/djlint --cov-branch \
|
||||
--cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
for a single test, run::
|
||||
|
||||
pytest tests/test_config_format_attribute_template_tags.py::test_attribute_include --cov=src/djlint \
|
||||
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
"""
|
||||
# pylint: disable=C0116
|
||||
|
||||
from typing import TextIO
|
||||
|
||||
from click.testing import CliRunner
|
||||
|
||||
from src.djlint import main as djlint
|
||||
|
||||
from .conftest import reformat
|
||||
|
||||
|
||||
def test_with_config(runner: CliRunner) -> None:
|
||||
result = runner.invoke(
|
||||
djlint, ["tests/config_format_attribute_template_tags", "--check"]
|
||||
)
|
||||
print(result.output)
|
||||
assert """0 files would be updated.""" in result.output
|
||||
assert result.exit_code == 0
|
||||
|
||||
|
||||
def test_without_config(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b'<input class="{% if this %}then something neat{% else %}that is long stuff asdf and more even{% endif %}"/>\n',
|
||||
)
|
||||
|
||||
assert output.exit_code == 0
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<img data-src="{% if report.imgs.exists %}{{ report.imgs.first.get_absolute_url|size:"96x96"}}{% else %}{% static '/img/report_thumb_placeholder_400x300.png' %}{% endif %}" src="{% static '/img/loader.gif' %}" alt="report image"/>""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== r"""<img data-src="{% if report.imgs.exists %}{{ report.imgs.first.get_absolute_url|size:"96x96" }}{% else %}{% static '/img/report_thumb_placeholder_400x300.png' %}{% endif %}"
|
||||
src="{% static '/img/loader.gif' %}"
|
||||
alt="report image"/>
|
||||
"""
|
||||
)
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<a class="asdf {% if favorite == "yes" %}favorite{% endif %} has-tooltip-arrow has-tooltip-right" data-tooltip="{% if favorite == "yes" %}Remove from Favorites {% else %}Add to Favorites{% endif %}" fav-type="report" object-id="{{ report.report_id }}"><span class="icon has-text-grey is-large "><i class="fas fa-lg fa-star"></i></span></a>""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== r"""<a class="asdf {% if favorite == "yes" %}favorite{% endif %} has-tooltip-arrow has-tooltip-right"
|
||||
data-tooltip="{% if favorite == "yes" %}Remove from Favorites {% else %}Add to Favorites{% endif %}"
|
||||
fav-type="report"
|
||||
object-id="{{ report.report_id }}">
|
||||
<span class="icon has-text-grey is-large ">
|
||||
<i class="fas fa-lg fa-star"></i>
|
||||
</span>
|
||||
</a>
|
||||
"""
|
||||
)
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div class="media-content" {% ifchanged comment.stream_id %} comments-msg {% else %} comments-newMsgReply {% endifchanged %}>""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== r"""<div class="media-content"
|
||||
{% ifchanged comment.stream_id %} comments-msg {% else %} comments-newMsgReply {% endifchanged %}>
|
||||
"""
|
||||
)
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<a class="piwik_download" href="{% static activity_version.get_win_document_with_images_file_path %}?{% now "jSFYHi" %}">""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== """<a class="piwik_download"
|
||||
href="{% static activity_version.get_win_document_with_images_file_path %}?{% now "jSFYHi" %}">
|
||||
"""
|
||||
)
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<span {%if a%}required{%endif%}title="{% if eev.status == eev.STATUS_CURRENT %} {% trans 'A' %} {% elif eev.status == eev.STATUS_APPROVED %} {% trans 'B' %} {% elif eev.status == eev.STATUS_EXPIRED %} {% trans 'C' %}{% endif %}" class="asdf{%if a%}b{%endif%} asdf" {%if a%}checked{%endif%}>""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== """<span {% if a %}required{% endif %}
|
||||
title="{% if eev.status == eev.STATUS_CURRENT %} {% trans 'A' %} {% elif eev.status == eev.STATUS_APPROVED %} {% trans 'B' %} {% elif eev.status == eev.STATUS_EXPIRED %} {% trans 'C' %}{% endif %}"
|
||||
class="asdf{% if a %}b{% endif %} asdf"
|
||||
{% if a %}checked{% endif %}>
|
||||
"""
|
||||
)
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div class="bg-level{% if value >= 70 %}1{% elif value >= 60 %}2{% elif value >= 50 %}3{% else %}4{% endif %}>\n</div>""",
|
||||
)
|
||||
assert output.exit_code == 0
|
||||
|
||||
# check attributes that have short if inside a attribute tag
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% block body %}
|
||||
<form action="{% if gpg -%}asdf something pretty long. can't beat this length{%- endif %}"
|
||||
method="POST">
|
||||
</form>
|
||||
{% endblock body %}
|
||||
""",
|
||||
)
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_attribute_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<ul class="menu-list{% for child in entry.children %}{% if child.url== page.url %}is-active{%else%}not-active{% endif %}{% endfor %} is-collapsible">""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== r"""<ul class="menu-list{% for child in entry.children %}{% if child.url== page.url %}is-active{% else %}not-active{% endif %}{% endfor %} is-collapsible">
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def test_attribute_include(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div {% if true %}class="test"{% endif %} {% include "django/forms/widgets/attrs.html" %}></div>""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output.text
|
||||
== r"""<div {% if true %}class="test"{% endif %}
|
||||
{% include "django/forms/widgets/attrs.html" %}></div>
|
||||
"""
|
||||
)
|
||||
|
|
@ -22,17 +22,17 @@ from .conftest import reformat
|
|||
|
||||
def test_empty_tags_on_one_line(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"{% if stuff %}\n{% endif %}")
|
||||
assert output["text"] == """{% if stuff %}{% endif %}\n"""
|
||||
assert output["exit_code"] == 1
|
||||
assert output.text == """{% if stuff %}{% endif %}\n"""
|
||||
assert output.exit_code == 1
|
||||
|
||||
|
||||
def test_dj_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file, runner, b"{# comment #}\n{% if this %}<div></div>{% endif %}"
|
||||
)
|
||||
assert output["text"] == """{# comment #}\n{% if this %}<div></div>{% endif %}\n"""
|
||||
assert output.text == """{# comment #}\n{% if this %}<div></div>{% endif %}\n"""
|
||||
# no change was required
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_reformat_asset_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -43,7 +43,7 @@ def test_reformat_asset_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
b"""{% block css %}{% assets "css_error" %}<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />{% endassets %}{% endblock css %}""",
|
||||
) # noqa: E501
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """{% block css %}
|
||||
{% assets "css_error" %}
|
||||
<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />
|
||||
|
|
@ -51,16 +51,16 @@ def test_reformat_asset_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
{% endblock css %}
|
||||
"""
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
|
||||
|
||||
def test_autoescape(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file, runner, b"{% autoescape on %}{{ body }}{% endautoescape %}"
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% autoescape on %}
|
||||
{{ body }}
|
||||
{% endautoescape %}
|
||||
|
|
@ -72,10 +72,10 @@ def test_comment(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
output = reformat(
|
||||
tmp_file, runner, b"""{% comment "Optional note" %}{{ body }}{% endcomment %}"""
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
# too short to put on multiple lines
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% comment "Optional note" %}{{ body }}{% endcomment %}
|
||||
"""
|
||||
)
|
||||
|
|
@ -85,10 +85,8 @@ def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
output = reformat(
|
||||
tmp_file, runner, b"{# <div></div> #}\n{% if this %}<div></div>{% endif %}"
|
||||
)
|
||||
assert (
|
||||
output["text"] == """{# <div></div> #}\n{% if this %}<div></div>{% endif %}\n"""
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.text == """{# <div></div> #}\n{% if this %}<div></div>{% endif %}\n"""
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -97,9 +95,9 @@ def test_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""<ul>{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes in this list.</li>{% endfor %}</ul>""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""<ul>
|
||||
{% for athlete in athlete_list %}
|
||||
<li>{{ athlete.name }}</li>
|
||||
|
|
@ -117,9 +115,9 @@ def test_filter(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% filter force_escape|lower %}This text will be HTML-escaped, and will appear in all lowercase.{% endfilter %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% filter force_escape|lower %}
|
||||
This text will be HTML-escaped, and will appear in all lowercase.
|
||||
{% endfilter %}
|
||||
|
|
@ -133,9 +131,9 @@ def test_if(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% if athlete_list %}Number of athletes: {{ athlete_list|length }}{% elif athlete_in_locker_room_list %}Athletes should be out of the locker room soon!{% else %}No athletes.{% endif %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% if athlete_list %}
|
||||
Number of athletes: {{ athlete_list|length }}
|
||||
{% elif athlete_in_locker_room_list %}
|
||||
|
|
@ -153,9 +151,9 @@ def test_ifchanged(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% for match in matches %}<div style="background-color:"pink">{% ifchanged match.ballot_id %}{% cycle "red" "blue" %}{% else %}gray{% endifchanged %}{{ match }}</div>{% endfor %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% for match in matches %}
|
||||
<div style="background-color:"pink">
|
||||
{% ifchanged match.ballot_id %}
|
||||
|
|
@ -172,9 +170,9 @@ def test_ifchanged(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
|
||||
def test_include(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"""{% include "this" %}{% include "that"%}""")
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% include "this" %}
|
||||
{% include "that" %}
|
||||
"""
|
||||
|
|
@ -187,9 +185,9 @@ def test_spaceless(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% spaceless %}<p><a href="foo/">Foo</a></p>{% endspaceless %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% spaceless %}
|
||||
<p>
|
||||
<a href="foo/">Foo</a>
|
||||
|
|
@ -205,9 +203,9 @@ def test_templatetag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}
|
||||
"""
|
||||
)
|
||||
|
|
@ -217,9 +215,9 @@ def test_verbatim(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
output = reformat(
|
||||
tmp_file, runner, b"""{% verbatim %}Still alive.{% endverbatim %}"""
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% verbatim %}
|
||||
Still alive.
|
||||
{% endverbatim %}
|
||||
|
|
@ -233,9 +231,9 @@ def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}
|
||||
"""
|
||||
)
|
||||
|
|
@ -245,9 +243,9 @@ def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% blocktranslate trimmed %}The width is: {{ width }}{% endblocktranslate %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% blocktranslate trimmed %}
|
||||
The width is: {{ width }}
|
||||
{% endblocktranslate %}
|
||||
|
|
@ -259,9 +257,9 @@ def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
|
||||
"""
|
||||
)
|
||||
|
|
@ -271,9 +269,9 @@ def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% blocktrans trimmed %}The width is: {{ width }}{% endblocktrans %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% blocktrans trimmed %}
|
||||
The width is: {{ width }}
|
||||
{% endblocktrans %}
|
||||
|
|
@ -285,13 +283,13 @@ def test_trans(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
output = reformat(
|
||||
tmp_file, runner, b"""<p>{% trans 'Please do <b>Blah</b>.' %}</p>"""
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
"""<p>
|
||||
{% trans 'Please do <b>Blah</b>.' %}
|
||||
</p>
|
||||
"""
|
||||
in output["text"]
|
||||
in output.text
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -301,9 +299,9 @@ def test_with(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{% with total=business.employees.count %}{{ total }}<div>employee</div>{{ total|pluralize }}{% endwith %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% with total=business.employees.count %}
|
||||
{{ total }}
|
||||
<div>employee</div>
|
||||
|
|
@ -313,15 +311,31 @@ def test_with(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
)
|
||||
|
||||
|
||||
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% block content %}{% load i18n %}{% endblock %}""",
|
||||
)
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output.text
|
||||
== r"""{% block content %}
|
||||
{% load i18n %}
|
||||
{% endblock %}
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def test_single_line_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% if messages|length %}{% for message in messages %}{{ message }}{% endfor %}{% endif %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% if messages|length %}
|
||||
{% for message in messages %}{{ message }}{% endfor %}
|
||||
{% endif %}
|
||||
|
|
@ -329,202 +343,15 @@ def test_single_line_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
)
|
||||
|
||||
|
||||
def test_complex_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<img data-src="{% if report.imgs.exists %}{{ report.imgs.first.get_absolute_url|size:"96x96"}}{% else %}{% static '/img/report_thumb_placeholder_400x300.png' %}{% endif %}" src="{% static '/img/loader.gif' %}" alt="report image"/>""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""<img data-src="{% if report.imgs.exists %}
|
||||
{{ report.imgs.first.get_absolute_url|size:"96x96" }}
|
||||
{% else %}
|
||||
{% static '/img/report_thumb_placeholder_400x300.png' %}
|
||||
{% endif %}"
|
||||
src="{% static '/img/loader.gif' %}"
|
||||
alt="report image"/>
|
||||
"""
|
||||
)
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<a class="asdf {% if favorite == "yes" %}favorite{% endif %} has-tooltip-arrow has-tooltip-right" data-tooltip="{% if favorite == "yes" %}Remove from Favorites {% else %}Add to Favorites{% endif %}" fav-type="report" object-id="{{ report.report_id }}"><span class="icon has-text-grey is-large "><i class="fas fa-lg fa-star"></i></span></a>""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""<a class="asdf
|
||||
{% if favorite == "yes" %}
|
||||
favorite
|
||||
{% endif %}
|
||||
has-tooltip-arrow has-tooltip-right"
|
||||
data-tooltip="{% if favorite == "yes" %}
|
||||
Remove from Favorites
|
||||
{% else %}
|
||||
Add to Favorites
|
||||
{% endif %}"
|
||||
fav-type="report"
|
||||
object-id="{{ report.report_id }}">
|
||||
<span class="icon has-text-grey is-large ">
|
||||
<i class="fas fa-lg fa-star"></i>
|
||||
</span>
|
||||
</a>
|
||||
"""
|
||||
)
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div class="media-content" {% ifchanged comment.stream_id %} comments-msg {% else %} comments-newMsgReply {% endifchanged %}>""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
print(output["text"])
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""<div class="media-content"
|
||||
{% ifchanged comment.stream_id %}
|
||||
comments-msg
|
||||
{% else %}
|
||||
comments-newMsgReply
|
||||
{% endifchanged %}>
|
||||
"""
|
||||
)
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<a class="piwik_download" href="{% static activity_version.get_win_document_with_images_file_path %}?{% now "jSFYHi" %}">""",
|
||||
)
|
||||
assert (
|
||||
output["text"]
|
||||
== """<a class="piwik_download"
|
||||
href="{% static activity_version.get_win_document_with_images_file_path %}?{% now "jSFYHi" %}">
|
||||
"""
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<span {%if a%}required{%endif%}title="{% if eev.status == eev.STATUS_CURRENT %} {% trans 'A' %} {% elif eev.status == eev.STATUS_APPROVED %} {% trans 'B' %} {% elif eev.status == eev.STATUS_EXPIRED %} {% trans 'C' %}{% endif %}" class="asdf{%if a%}b{%endif%} asdf" {%if a%}checked{%endif%}>""",
|
||||
)
|
||||
|
||||
print(output["text"])
|
||||
assert (
|
||||
output["text"]
|
||||
== """<span {% if a %}
|
||||
required
|
||||
{% endif %}
|
||||
title="{% if eev.status == eev.STATUS_CURRENT %}
|
||||
{% trans 'A' %}
|
||||
{% elif eev.status == eev.STATUS_APPROVED %}
|
||||
{% trans 'B' %}
|
||||
{% elif eev.status == eev.STATUS_EXPIRED %}
|
||||
{% trans 'C' %}
|
||||
{% endif %}"
|
||||
class="asdf
|
||||
{% if a %}
|
||||
b
|
||||
{% endif %}
|
||||
asdf"
|
||||
{% if a %}
|
||||
checked
|
||||
{% endif %}>
|
||||
"""
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div class="bg-level{% if value >= 70 %}1{% elif value >= 60 %}2{% elif value >= 50 %}3{% else %}4{% endif %}>\n</div>""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
|
||||
# check attributes that have short if inside a attribute tag
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% block body %}
|
||||
<form action="{% if gpg -%}asdf something pretty long. can't beat this length{%- endif %}"
|
||||
method="POST">
|
||||
</form>
|
||||
{% endblock body %}
|
||||
""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
|
||||
|
||||
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% block content %}{% load i18n %}{% endblock %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""{% block content %}
|
||||
{% load i18n %}
|
||||
{% endblock %}
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def test_attribute_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<ul class="menu-list{% for child in entry.children %}{% if child.url== page.url %}is-active{%else%}not-active{% endif %}{% endfor %} is-collapsible">""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""<ul class="menu-list
|
||||
{% for child in entry.children %}
|
||||
{% if child.url== page.url %}
|
||||
is-active
|
||||
{% else %}
|
||||
not-active
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
is-collapsible">
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def test_attribute_include(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<div {% if true %}class="test"{% endif %} {% include "django/forms/widgets/attrs.html" %}></div>""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== r"""<div {% if true %}
|
||||
class="test"
|
||||
{% endif %}
|
||||
{% include "django/forms/widgets/attrs.html" %}></div>
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def test_multiple_endblocks(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% block content %}{% block scripts %}{% endblock %}{% endblock %}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
"""{% block content %}\n {% block scripts %}{% endblock %}\n{% endblock %}
|
||||
"""
|
||||
== output["text"]
|
||||
== output.text
|
||||
)
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ from .conftest import reformat
|
|||
|
||||
def test_if(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"{{ if .condition }} {{ else }} {{ end }}")
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_range(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"{{ range .Items }} {{ end }}")
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ from .conftest import reformat
|
|||
|
||||
def test_handlebars_else(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"{{^}}")
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_each(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -29,10 +29,10 @@ def test_each(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{{#each people}}{{print_person}} <p>and more long stuff</p>{{/each}}""",
|
||||
)
|
||||
print(output["text"])
|
||||
assert output["exit_code"] == 1
|
||||
print(output.text)
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{{#each people }}
|
||||
{{ print_person }}
|
||||
<p>
|
||||
|
|
@ -49,9 +49,9 @@ def test_with(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
runner,
|
||||
b"""{{#with person}}<p>{{firstname}} {{lastname}}</p>{{/with}}""",
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{{#with person }}
|
||||
<p>
|
||||
{{ firstname }} {{ lastname }}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ asdf
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
# check attributes
|
||||
output = reformat(
|
||||
|
|
@ -61,7 +61,7 @@ asdf
|
|||
)
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """<div>
|
||||
<div class="field">
|
||||
<textarea class="this" name="that">asdf</textarea>
|
||||
|
|
@ -96,7 +96,7 @@ def test_script_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
b"""<script src="{% static 'common/js/foo.min.js' %}"></script>""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
|
|
@ -111,7 +111,7 @@ def test_script_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_html_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -142,10 +142,10 @@ def test_long_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
required="true" />""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """<input type="text"
|
||||
class="class one class two"
|
||||
disabled="true"
|
||||
|
|
@ -170,7 +170,7 @@ def test_long_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
# check styles when tag is first
|
||||
output = reformat(
|
||||
|
|
@ -187,7 +187,7 @@ def test_long_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
</div>
|
||||
""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_small_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -229,7 +229,7 @@ def test_hr_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
</div>
|
||||
""",
|
||||
)
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
|
|
@ -242,7 +242,7 @@ def test_hr_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_span_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -324,10 +324,10 @@ def test_ignored_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
</div>""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 1
|
||||
print(output["text"])
|
||||
assert output.exit_code == 1
|
||||
print(output.text)
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """<div class="a long list of meaningless classes"
|
||||
id="somthing_meaning_less_is_here"
|
||||
required
|
||||
|
|
@ -365,10 +365,10 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """<!-- <span> -->
|
||||
<div>
|
||||
<p>
|
||||
|
|
@ -401,10 +401,10 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== """<!-- djlint:off -->
|
||||
<div><p><span></span></p></div>
|
||||
<!-- djlint:on -->
|
||||
|
|
@ -433,14 +433,14 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
assert (
|
||||
"""<script>
|
||||
<div><p><span></span></p></div>
|
||||
</script>
|
||||
"""
|
||||
in output["text"]
|
||||
in output.text
|
||||
)
|
||||
|
||||
# check inline script includes
|
||||
|
|
@ -456,8 +456,8 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
</html>
|
||||
""",
|
||||
)
|
||||
print(output["text"])
|
||||
assert output["exit_code"] == 0
|
||||
print(output.text)
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_style_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
@ -473,7 +473,7 @@ def test_style_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
|
|
@ -486,7 +486,7 @@ def test_style_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
# check style includes
|
||||
output = reformat(
|
||||
|
|
@ -495,7 +495,7 @@ def test_style_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
b"""<link href="{% static 'common/js/foo.min.js' %}"/>""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
|
||||
|
||||
def test_self_closing_tags(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
|
|
|
|||
|
|
@ -49,9 +49,9 @@ def test_spaceless(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
b"""{%- if entry.children.length -%}<strong>{%- endif -%}""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
assert output.exit_code == 0
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{%- if entry.children.length -%}<strong>{%- endif -%}
|
||||
"""
|
||||
)
|
||||
|
|
@ -61,9 +61,9 @@ def test_macro(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
output = reformat(
|
||||
tmp_file, runner, b"{% macro 'cool' %}<div>some html</div>{% endmacro %}"
|
||||
)
|
||||
assert output["exit_code"] == 1
|
||||
assert output.exit_code == 1
|
||||
assert (
|
||||
output["text"]
|
||||
output.text
|
||||
== r"""{% macro 'cool' %}
|
||||
<div>some html</div>
|
||||
{% endmacro %}
|
||||
|
|
|
|||
Loading…
Reference in a new issue