Skip to content

Commit

Permalink
Merge pull request #590 from Riverside-Healthcare/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering authored Apr 12, 2023
2 parents f131f9e + 53d9b29 commit ab4ed74
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 153 deletions.
52 changes: 50 additions & 2 deletions src/djlint/formatter/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,69 @@ def _flatten_attributes(match: re.Match) -> str:
if child_of_ignored_block(config, html, match):
return match.group()

close = match.group(3) if "/" not in match.group(3) else f" {match.group(3)}"

# pylint: disable=C0209
return "{} {}{}".format(
match.group(1),
" ".join(x.strip() for x in match.group(2).strip().splitlines()),
match.group(3),
close,
)

# put attributes on one line
html = re.sub(
re.compile(
rf"(<(?:{config.indent_html_tags}))\s((?:\"[^\"]*\"|'[^']*'|{{{{(?:(?!}}}}).)*}}}}|{{%(?:(?!%}}).)*%}}|[^'\">{{}}])+)(/?>)",
rf"(<(?:{config.indent_html_tags}))\s((?:\s*?(?:\"[^\"]*\"|'[^']*'|{{{{(?:(?!}}}}).)*}}}}|{{%(?:(?!%}}).)*%}}|[^'\">{{}}\/\s]))+)\s*?(/?>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
_flatten_attributes,
html,
)

# put closing tags back on one line
# <a ...
# >
html = re.sub(
re.compile(
rf"(</(?:{config.indent_html_tags}))\s*?(>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
r"\1\2",
html,
)

# remove extra space from empty tags
# <a >
# ^
html = re.sub(
re.compile(
rf"(<(?:{config.indent_html_tags}))\s*?(>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
r"\1\2",
html,
)

# ensure space before closing tag
# <a />
# ^
html = re.sub(
re.compile(
rf"(<(?:{config.indent_html_tags}))\s*?(/>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
r"\1 \2",
html,
)

# cleanup whitespace in doctype
html = re.sub(
re.compile(
r"(<!(?:doctype))\s((?:\s*?(?:\"[^\"]*\"|'[^']*'|{{(?:(?!}}).)*}}|{%(?:(?!%}).)*%}|[^'\">{}\/\s]))+)\s*?(>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
r"\1 \2\3",
html,
)

return html
2 changes: 1 addition & 1 deletion src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def indent_html(rawcode: str, config: Config) -> str:

tmp = re.sub(
re.compile(
rf"(\s*?)(<(?:{config.indent_html_tags})\b)((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}])+?)(/?>)",
rf"(\s*?)(<(?:{config.indent_html_tags})\b)((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}\/])+?)(\s?/?>)",
re.VERBOSE | re.IGNORECASE,
),
func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
then something neat
{% else %}
that is long stuff asdf and more even
{% endif %}"/>
{% 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"/>
alt="report image" />
<a class="asdf
{% if favorite == "yes" %}favorite{% endif %}
has-tooltip-arrow has-tooltip-right"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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',
b'<input class="{% if this %}then something neat{% else %}that is long stuff asdf and more even{% endif %}" />\n',
)

assert output.exit_code == 0
Expand All @@ -58,7 +58,7 @@ def test_without_config(runner: CliRunner, tmp_file: TextIO) -> None:
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"/>
alt="report image" />
"""
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_config/test_preserve_blank_lines/html_one.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% block someblock %}{% endblock %}
<br/>
<br/>
<br />
<br />
29 changes: 14 additions & 15 deletions tests/test_html/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
' calc(100vw - 30px)"\n'
' class="richtext-image imageblock overflow {{ value.image_position }} lazy"\n'
' title="{{ value.image.title }}"\n'
' alt="Block image"/>\n'
' alt="Block image" />\n'
),
id="srcset",
),
Expand All @@ -62,7 +62,7 @@
' style="width:100px;\n'
" cursor: text;\n"
' border:1px solid pink"\n'
' required="true"/>\n'
' required="true" />\n'
),
id="long_attributes",
),
Expand Down Expand Up @@ -195,7 +195,7 @@
' class="form-control"\n'
' type="text"\n'
' name="driver_id"\n'
" value=\"{{ id|default(' sample_text ') }}\"/>\n"
" value=\"{{ id|default(' sample_text ') }}\" />\n"
),
id="another_boolean_after_tag",
),
Expand Down Expand Up @@ -477,11 +477,10 @@
' target="_blank"\n'
' role="tab"\n'
' href="https://yandex.ru/news?msid=1581089780.29024.161826.172442&mlid=1581088893.glob_225"\n'
' rel="noopener">...</a\n' # broken tags not handled in djlint currently.
" >\n"
" </h1>\n"
" </div>\n"
' rel="noopener">...</a>\n'
" </h1>\n"
" </div>\n"
"</div>\n"
),
id="class_bem2",
),
Expand Down Expand Up @@ -594,7 +593,7 @@
' src="https://s.yimg.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage_2x.png"\n'
' height="58px"\n'
' width="205px"\n'
' alt="Yahoo"/>\n'
' alt="Yahoo" />\n'
" </a>\n"
" </h1>\n"
" </div>\n"
Expand Down Expand Up @@ -755,23 +754,23 @@
' /assets/visual.png 805w"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="/assets/visual.png"\n'
' srcset="/assets/[email protected] 400w,\n'
" /assets/visual.png 805w,\n"
" /assets/[email protected] 1610w,\n"
' /assets/[email protected] 2415w"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="/assets/visual.png"\n'
' srcset="/assets/[email protected] 0.5x,\n'
" /assets/visual.png 1111x,\n"
" /assets/[email protected] 2x,\n"
' /assets/[email protected] 3.3333x"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img srcset=" /media/examples/surfer-240-200.jpg ">\n'
"<!-- #8150 -->\n"
'<img sizes="(max-width: 1400px) 100vw,\n'
Expand Down Expand Up @@ -800,23 +799,23 @@
' /assets/visual.png 805w"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="/assets/visual.png"\n'
' srcset="/assets/[email protected] 400w,\n'
" /assets/visual.png 805w,\n"
" /assets/[email protected] 1610w,\n"
' /assets/[email protected] 2415w"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="/assets/visual.png"\n'
' srcset="/assets/[email protected] 0.5x,\n'
" /assets/visual.png 1111x,\n"
" /assets/[email protected] 2x,\n"
' /assets/[email protected] 3.3333x"\n'
' sizes="(max-width: 66rem) 100vw,\n'
' 66rem"\n'
' alt=""/>\n'
' alt="" />\n'
'<img srcset="/media/examples/surfer-240-200.jpg" />\n'
"<!-- #8150 -->\n"
'<img sizes="(max-width: 1400px) 100vw,\n'
Expand All @@ -837,7 +836,7 @@
" _20200401_145009_szrhju_c_scale,w_1398.jpg 1398w,\n"
' _20200401_145009_szrhju_c_scale,w_1400.jpg 1400w"\n'
' src="_20200401_145009_szrhju_c_scale,w_1400.jpg"\n'
' alt=""/>\n'
' alt="" />\n'
),
id="src_set",
),
Expand Down
24 changes: 12 additions & 12 deletions tests/test_html/test_bracket_same_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@
),
(
'<img long_long_attribute="long_long_long_long_long_long_long_long_long_long_long_value"\n'
' src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
' src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
),
id="void",
),
Expand All @@ -157,12 +157,12 @@
),
(
'<img long_long_attribute="long_long_long_long_long_long_long_long_long_long_long_value"\n'
' src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
'<img src="./1.jpg"/>\n'
' src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
'<img src="./1.jpg" />\n'
),
id="void_braket_same_line",
),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_html/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@
" <body></body>\n"
"</html>\n"
'<body width="100%" align="center">\n'
" <center >\n"
" <center>\n"
' <!--[if (gte mso 9)|(IE)]><table cellpadding="0" cellspacing="0" border="0" width="600" align="center"><tr><td><![endif]-->\n'
" <div></div>\n"
" <!--[if (gte mso 9)|(IE)]></td></tr></table><![endif]-->\n"
" </center >\n"
" </center>\n"
"</body>\n"
"<!DOCTYPE html>\n"
'<!--[if lt IE 9]><html class="legacy-ie"><![endif]-->\n'
Expand Down
6 changes: 3 additions & 3 deletions tests/test_html/test_doctype_declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
),
pytest.param(
("<!DocType htMl >"),
("<!DocType htMl >\n"),
("<!DocType htMl>\n"),
id="case_2",
),
pytest.param(
Expand All @@ -38,7 +38,7 @@
"<html>\n"
" <head>\n"
" <title>An HTML standard template</title>\n"
' <meta charset="utf-8" />\n'
' <meta charset="utf-8" />\n'
" </head>\n"
" <body>\n"
" <p>… Your HTML content here …</p>\n"
Expand Down Expand Up @@ -173,7 +173,7 @@
' width="400"\n'
' height="250"\n'
' alt="Beep"\n'
' vspace="20"/>\n'
' vspace="20" />\n'
" </div>\n"
' <p align="center" style="font-size:17px">\n'
" Bar Foo,\n"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_html/test_ignored.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
(
"<html>\n"
" <head>\n"
' <link href="{% static \'foo/bar.css\' %}" rel="stylesheet"/>\n'
' <link href="{% static \'foo/bar.css\' %}" rel="stylesheet" />\n'
" <!--JS-->\n"
" <script src=\"{% static 'foo/bar.js' %}\"></script>\n"
" </head>\n"
Expand Down
19 changes: 6 additions & 13 deletions tests/test_html/test_next_line_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,16 @@
"</div>\n"
),
(
"<div>\n"
"</div\n"
">\n"
"<div></div>\n"
"<span></span>\n"
"<div>\n"
"</div\n"
">\n"
"<div></div>\n"
"<span></span>\n"
"<div>\n"
"</div\n"
">\n"
"<div></div>\n"
"<span></span>\n"
"<div>\n"
' <a href="#123123123123123131231312321312312312312312312312312313123123123123123">123123123123</a\n'
" >\n"
" 123123\n"
" </div>\n"
' <a href="#123123123123123131231312321312312312312312312312312313123123123123123">123123123123</a>\n'
" 123123\n"
"</div>\n"
),
id="standalone_end_marker",
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_html/test_single_attribute_per_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'<img src="/images/foo.png" />\n'
'<img src="/images/foo.png" alt="bar" />\n'
'<img src="/images/foo.png"\n'
' alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>\n'
' alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit." />\n'
),
id="single_attrib_per_line_enabled",
),
Expand All @@ -58,7 +58,7 @@
"</div>\n"
'<img src="/images/foo.png" />\n'
'<img src="/images/foo.png" alt="bar" />\n'
'<img src="/images/foo.png" alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit." />\n'
'<img src="/images/foo.png" alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit." />\n'
),
(
'<div data-a="1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>\n'
Expand All @@ -72,7 +72,7 @@
'<img src="/images/foo.png" />\n'
'<img src="/images/foo.png" alt="bar" />\n'
'<img src="/images/foo.png"\n'
' alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>\n'
' alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit." />\n'
),
id="single_attrib_per_line_disabled",
),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_html/test_srcset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
" should-not-format 400w 100h,\n"
" should-not-format 500w 200h\n"
'"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="a"\n'
'srcset="\n'
" should-not-format ,, should-not-format 0q,,,\n"
Expand All @@ -25,10 +25,10 @@
(
'<img src="a"\n'
' srcset="should-not-format 400w 100h, should-not-format 500w 200h"\n'
' alt=""/>\n'
' alt="" />\n'
'<img src="a"\n'
' srcset="should-not-format ,, should-not-format 0q,,,"\n'
' alt=""/>\n'
' alt="" />\n'
),
id="invalid",
),
Expand Down
Loading

0 comments on commit ab4ed74

Please sign in to comment.