Skip to content

Commit

Permalink
Merge pull request #644 from Riverside-Healthcare/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering authored May 11, 2023
2 parents 073b0cb + 92c083b commit 1ecee77
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 74 deletions.
8 changes: 6 additions & 2 deletions src/djlint/formatter/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import regex as re
from jsbeautifier.javascript.options import BeautifierOptions

from ..helpers import child_of_unformatted_block
from ..settings import Config


def format_css(html: str, config: Config) -> str:
"""Format css inside <style> tags."""

def launch_formatter(config: Config, match: re.Match) -> str:
def launch_formatter(config: Config, html: str, match: re.Match) -> str:
"""Add break after if not in ignored block."""
if child_of_unformatted_block(config, html, match):
return match.group()

if not match.group(3).strip():
return match.group()

Expand Down Expand Up @@ -44,7 +48,7 @@ def launch_formatter(config: Config, match: re.Match) -> str:

return match.group(1) + match.group(2) + beautified + "\n" + indent

func = partial(launch_formatter, config)
func = partial(launch_formatter, config, html)

return re.sub(
re.compile(
Expand Down
14 changes: 7 additions & 7 deletions src/djlint/formatter/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def add_html_line(out_format: str, match: re.Match) -> str:
# template tag breaks
def should_i_move_template_tag(out_format: str, match: re.Match) -> str:
# ensure template tag is not inside an html tag and also not the first line of the file

if inside_ignored_block(config, html, match):
return match.group(1)

if not re.findall(
r"\<(?:"
+ str(config.indent_html_tags)
+ r")\b(?:\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{\#[^\#]*\#}|[^>{}])*?"
# original
# + r")\b(?:[^>]|{%[^(?:%}]*?%}|{{[^(?:}}]*?}})*?"
+ re.escape(match.group(1)) + "$",
r"\<(?:" + str(config.indent_html_tags)
# added > as not allowed inside a "" or '' to prevent invalid wild html matches
# for issue #640
+ r")\b(?:\"[^\">]*\"|'[^'>]*'|{{[^}]*}}|{%[^%]*%}|{\#[^\#]*\#}|[^>{}])*?"
+ re.escape(match.group(1))
+ "$",
html[: match.end()],
re.MULTILINE | re.VERBOSE,
):
Expand Down
8 changes: 6 additions & 2 deletions src/djlint/formatter/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import regex as re
from jsbeautifier.javascript.options import BeautifierOptions

from ..helpers import child_of_unformatted_block
from ..settings import Config


def format_js(html: str, config: Config) -> str:
"""Format javascript inside <script> tags."""

def launch_formatter(config: Config, match: re.Match) -> str:
def launch_formatter(config: Config, html: str, match: re.Match) -> str:
"""Add break after if not in ignored block."""
if child_of_unformatted_block(config, html, match):
return match.group()

if not match.group(3).strip():
return match.group()

Expand Down Expand Up @@ -42,7 +46,7 @@ def launch_formatter(config: Config, match: re.Match) -> str:

return match.group(1) + match.group(2) + beautified + "\n" + indent

func = partial(launch_formatter, config)
func = partial(launch_formatter, config, html)

return re.sub(
re.compile(
Expand Down
4 changes: 2 additions & 2 deletions src/djlint/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
message: Tag names should be lowercase.
flags: re.DOTALL
patterns:
- (?<=<)(?:HTML|BODY|DIV|P|SPAN|TABLE|TR|TD|TH|THEAD|TBODY|CODE|UL|OL|LI|H1|H2|H3|H4|H5|H6)
- (?<=<)/?(?:HTML|BODY|DIV|P|SPAN|TABLE|TR|TD|TH|THEAD|TBODY|CODE|UL|OL|LI|H1|H2|H3|H4|H5|H6)
- rule:
name: H010
message: Attribute names should be lowercase.
Expand Down Expand Up @@ -276,4 +276,4 @@
message: Duplicate attribute found.
flags: re.I
patterns:
- <\w[^>]*?\s\K([a-z-][a-z-]*?)(?==[^>]+?[^-]\1=[^>]*?>)
- <\w[^>]*?\s\K([a-z-:][a-z-]*?)(?==(?:\"[^\"]*\"|'[^']*'|{{(?:(?!}}).)*}}|{%(?:(?!%}).)*%}|{#(?:(?!#}).)*#}|[^'\">{}])*[^-:]\1=[^>]*?>)
3 changes: 2 additions & 1 deletion src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def __init__(

# these tags should be unindented and next line will be indented
self.tag_unindent_line: str = r"""
(?:\{%-?[ ]*?(?:elif|else|empty))
(?:\{%-?[ ]*?(?:elif|else|empty|plural))
| (?:
\{\{[ ]*?
(
Expand Down Expand Up @@ -675,6 +675,7 @@ def __init__(
| block(?!trans)
| endblock(?!trans)
| else
| plural
| spaceless
| endspaceless
| compress
Expand Down
14 changes: 14 additions & 0 deletions tests/test_config/test_format_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@
({"format_css": True}),
id="ignore",
),
pytest.param(
(
"<!-- djlint:off -->\n"
" <style> body{color:{{ search_index }};}</style><div></div>\n"
" <!-- djlint:on -->"
),
(
"<!-- djlint:off -->\n"
" <style> body{color:{{ search_index }};}</style><div></div>\n"
"<!-- djlint:on -->\n"
),
({"format_js": True}),
id="ignored blocks",
),
]


Expand Down
14 changes: 14 additions & 0 deletions tests/test_config/test_format_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@
({"format_js": True, "indent_js": 3}),
id="literals",
),
pytest.param(
(
"<!-- djlint:off -->\n"
' <script id="search-index" type="application/json">{{ search_index }}</script><div></div>\n'
" <!-- djlint:on -->"
),
(
"<!-- djlint:off -->\n"
' <script id="search-index" type="application/json">{{ search_index }}</script><div></div>\n'
"<!-- djlint:on -->\n"
),
({"format_js": True}),
id="ignored blocks",
),
]


Expand Down
15 changes: 15 additions & 0 deletions tests/test_django/test_blocktrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@
),
id="blocktrans_autoescape_two",
),
pytest.param(
(
"{% blocktranslate count counter=list|length %}\n"
"There is only one {{ name }} object.\n"
" {% plural %} There are {{ counter }} {{ name }} objects.\n"
"{% endblocktranslate %}\n"
),
(
"{% blocktranslate count counter=list|length %}\n"
"There is only one {{ name }} object.\n"
" {% plural %} There are {{ counter }} {{ name }} objects.\n"
"{% endblocktranslate %}\n"
),
id="plural not formatted",
),
]


Expand Down
25 changes: 21 additions & 4 deletions tests/test_django/test_blocktrans_trimmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@
" (\n"
" {% blocktranslate count counter=images|length trimmed %}\n"
" {{ counter }} photo\n"
" {% plural %}\n"
" {% plural %}\n"
" {{ counter }} photos\n"
" {% endblocktranslate %}\n"
" )\n"
" (\n"
" {% blocktrans count counter=images|length trimmed %}\n"
" {{ counter }} photo\n"
" {% plural %}\n"
" {% plural %}\n"
" {{ counter }} photos\n"
" {% endblocktrans %}\n"
" )\n"
Expand Down Expand Up @@ -156,15 +156,15 @@
" (\n"
" {% blocktranslate count counter=images|length trimmed %}\n"
" {{ counter }} photo\n"
" {% plural %}\n"
" {% plural %}\n"
" asdf\n"
" {{ counter }} photos\n"
" {% endblocktranslate %}\n"
" )\n"
" (\n"
" {% blocktrans count counter=images|length trimmed %}\n"
" {{ counter }} photo\n"
" {% plural %}\n"
" {% plural %}\n"
" {{ counter }} photos\n"
" {% endblocktrans %}\n"
" )\n"
Expand Down Expand Up @@ -254,6 +254,23 @@
({"max_line_length": 10}),
id="blocktrans_autoescape_two",
),
pytest.param(
(
"{% blocktranslate count counter=list|length trimmed %}\n"
"There is only one {{ name }} object.\n"
" {% plural %} There are {{ counter }} {{ name }} objects.\n"
"{% endblocktranslate %}\n"
),
(
"{% blocktranslate count counter=list|length trimmed %}\n"
" There is only one {{ name }} object.\n"
"{% plural %}\n"
" There are {{ counter }} {{ name }} objects.\n"
"{% endblocktranslate %}\n"
),
({}),
id="plural formatted",
),
]


Expand Down
34 changes: 34 additions & 0 deletions tests/test_django/test_quoted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Test django quoted tags.
poetry run pytest tests/test_django/test_filter.py
"""
import pytest

from src.djlint.reformat import formatter
from tests.conftest import printer

test_data = [
pytest.param(
(
"<h1>\n"
' {% if condition1 %}<span class="cls"></span>{% endif %}\n'
' {% if condition2 %}"{{ text }}"{% endif %}\n'
" </h1>\n"
),
(
"<h1>\n"
' {% if condition1 %}<span class="cls"></span>{% endif %}\n'
' {% if condition2 %}"{{ text }}"{% endif %}\n'
"</h1>\n"
),
id="issue #640",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, django_config):
output = formatter(django_config, source)

printer(expected, source, output)
assert expected == output
45 changes: 45 additions & 0 deletions tests/test_linter/test_h005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test linter code H005.
poetry run pytest tests/test_linter/test_h005.py
"""
import pytest

from src.djlint.lint import linter
from tests.conftest import lint_printer

test_data = [
pytest.param(
("<!DOCTYPE html>\n<html>"),
(
[
{
"code": "H005",
"line": "2:0",
"match": "<html>",
"message": "Html tag should have lang attribute.",
},
{
"code": "H025",
"line": "2:0",
"match": "<html>",
"message": "Tag seems to be an orphan.",
},
]
),
id="one",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, nunjucks_config):
filename = "test.html"
output = linter(nunjucks_config, source, filename, filename)

lint_printer(source, expected, output[filename])

mismatch = list(filter(lambda x: x not in expected, output[filename])) + list(
filter(lambda x: x not in output[filename], expected)
)

assert len(mismatch) == 0
49 changes: 49 additions & 0 deletions tests/test_linter/test_h006.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Test linter code H006.
poetry run pytest tests/test_linter/test_h006.py
"""
import pytest

from src.djlint.lint import linter
from tests.conftest import lint_printer

test_data = [
pytest.param(
('<img alt="test"/>'),
(
[
{
"code": "H006",
"line": "1:0",
"match": '<img alt="test"/>',
"message": "Img tag should have height and width attributes.",
}
]
),
id="one",
),
pytest.param(
(
'{# [INFO][JINJA] I use syntax "{% if <img alt=""\n'
' if I want that something happened solely if "img" exists in the content of my articles #}\n'
"\n"
' <script src="script.js" defer></script>\n'
),
([]),
id="partial ignored",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, nunjucks_config):
filename = "test.html"
output = linter(nunjucks_config, source, filename, filename)

lint_printer(source, expected, output[filename])

mismatch = list(filter(lambda x: x not in expected, output[filename])) + list(
filter(lambda x: x not in output[filename], expected)
)

assert len(mismatch) == 0
Loading

0 comments on commit 1ecee77

Please sign in to comment.