Skip to content

Commit

Permalink
fix(image tag): added image tag to template break tag list. srcset fo…
Browse files Browse the repository at this point in the history
…rmatting

closes #444
  • Loading branch information
christopherpickering committed Nov 3, 2022
1 parent fceff25 commit 0d170aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/djlint/formatter/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,24 @@ def format_attributes(config: Config, html: str, match: re.match) -> str:
# for the equals sign
quote_length += 1

if config.format_attribute_template_tags:
join_space = "\n" + spacing
else:
join_space = (
"\n" + spacing + int(quote_length + len(attrib_name or "")) * " "
)

# format style attribute
if attrib_name and attrib_name.lower() == "style":
if config.format_attribute_template_tags:
join_space = "\n" + spacing
else:
join_space = (
"\n" + spacing + int(quote_length + len(attrib_name or "")) * " "
)
attrib_value = (";" + join_space).join(
[value.strip() for value in attrib_value.split(";") if value.strip()]
)

elif attrib_name and attrib_name.lower() in ["srcset", "data-srcset", "sizes"]:
attrib_value = ("," + join_space).join(
[value.strip() for value in attrib_value.split(",") if value.strip()]
)

# format template stuff
if config.format_attribute_template_tags:
if attrib_value and attrib_name not in config.ignored_attributes:
Expand Down
1 change: 1 addition & 0 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ def __init__(
| endraw
| call
| endcall
| image
"""
+ self.custom_blocks
+ r"""
Expand Down
24 changes: 24 additions & 0 deletions tests/test_html/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@
from tests.conftest import reformat


def test_srcset(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<img {% image value.image fill-640x360 as block_image_640 %}
{% image value.image fill-768x432 as block_image_768 %}
{% image value.image fill-1024x576 as block_image_1024 %}
{% image value.image fill-1600x900 as block_image_1600 %}
data-src="{{ block_image_640.url }}"
data-srcset="{{ block_image_640.url }} 640w,
{{ block_image_768.url }} 768w,
{{ block_image_1024.url }} 1024w,
{{ block_image_1600.url }} 1600w"
sizes="(min-width: 1200px) 458px,
(min-width: 992px) 374px,
(min-width: 768px) 720px,
calc(100vw - 30px)"
class="richtext-image imageblock overflow {{ value.image_position }} lazy"
title="{{ value.image.title }}"
alt="Block image"/>""",
)
assert output.exit_code == 0


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

0 comments on commit 0d170aa

Please sign in to comment.