Skip to content

Commit

Permalink
fix(linter): added missing positive on H021
Browse files Browse the repository at this point in the history
closes #678
  • Loading branch information
christopherpickering committed Jun 2, 2023
1 parent 3b2e522 commit 221c31f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 48 deletions.
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|A|DD|DT|BLOCKQUOTE|SELECT|FORM|FIELDSET|OPTGROUP|LEGEND|LABEL|HEADER|CACHE|MAIN|ASIDE|FOOTER|SECTION|NAME|FIGURE|FIGCAPTION|VIDEO|G|SVG|BUTTON|PATH|PICTURE|SCRIPT|STYLE|DETAILS|SUMMARY)\b
- rule:
name: H010
message: Attribute names should be lowercase.
Expand Down Expand Up @@ -161,7 +161,7 @@
- rule:
name: H021
message: Inline styles should be avoided.
flags: re.I
flags: re.I|re.DOTALL
patterns:
- <\w+\s(?:[^>]*\s)?style=(?=((?!>|{{|{%).)*>)
- rule:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_linter/test_h005.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
),
id="one",
),
pytest.param(
("<a\n>"),
(
[
{
"code": "H025",
"line": "1:0",
"match": "<a\n>",
"message": "Tag seems to be an orphan.",
}
]
),
id="one",
),
]


Expand Down
5 changes: 5 additions & 0 deletions tests/test_linter/test_h006.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
),
id="one",
),
pytest.param(
('<img \n alt="test" width="10" height="10"/>'),
([]),
id="line break",
),
pytest.param(
(
'{# [INFO][JINJA] I use syntax "{% if <img alt=""\n'
Expand Down
20 changes: 20 additions & 0 deletions tests/test_linter/test_h008.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@
),
id="one",
),
pytest.param(
("<div class='test\nclass-two'>"),
(
[
{
"code": "H008",
"line": "1:0",
"match": "<div class='test\ncla",
"message": "Attributes should be double quoted.",
},
{
"code": "H025",
"line": "1:0",
"match": "<div class='test\ncla",
"message": "Tag seems to be an orphan.",
},
]
),
id="line break",
),
pytest.param(
(
'<link rel="stylesheet" href="styles.css" media="print" onload="this.media=\'all\'" media='
Expand Down
20 changes: 20 additions & 0 deletions tests/test_linter/test_h009.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@
),
id="opening",
),
pytest.param(
("<A\n>"),
(
[
{
"code": "H009",
"line": "1:1",
"match": "A",
"message": "Tag names should be lowercase.",
},
{
"code": "H025",
"line": "1:0",
"match": "<A\n>",
"message": "Tag seems to be an orphan.",
},
]
),
id="line break",
),
]


Expand Down
90 changes: 90 additions & 0 deletions tests/test_linter/test_h021.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Test linter code H010.
poetry run pytest tests/test_linter/test_h021.py
"""
import pytest

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

test_data = [
pytest.param(
('<div style="asdf"></div>'),
(
[
{
"code": "H021",
"line": "1:0",
"match": "<div style=",
"message": "Inline styles should be avoided.",
}
]
),
id="simple",
),
pytest.param(
(
'<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet" />'
),
([]),
id="missing",
),
pytest.param(
('<acronym title="Cascading Style Sheets">CSS</acronym>'),
([]),
id="outside tag",
),
pytest.param(
('<div style="test {%"><div style="test {{">'),
(
[
{
"code": "H025",
"line": "1:21",
"match": '<div style="test {{"',
"message": "Tag seems to be an orphan.",
},
{
"code": "H025",
"line": "1:0",
"match": '<div style="test {%"',
"message": "Tag seems to be an orphan.",
},
]
),
id="template syntax in style",
),
pytest.param(
('<acronym title="Cascading Style Sheets">CSS</acronym>'),
([]),
id="outside tag",
),
pytest.param(
('<div style="color:green"\n' ' class="foo">\n' "</div>"),
(
[
{
"code": "H021",
"line": "1:0",
"match": "<div style=",
"message": "Inline styles should be avoided.",
}
]
),
id="line breaks",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, basic_config):
filename = "test.html"
output = linter(basic_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
46 changes: 0 additions & 46 deletions tests/test_linter/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@
from tests.conftest import write_to_file


def test_H009(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<H1>")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H009 1:" in result.output


def test_H010(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<img HEIGHT="12">')
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H010 1:" in result.output

write_to_file(tmp_file.name, b"<li>ID=username</li>")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0


def test_H011(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<div class=test></div>")
result = runner.invoke(djlint, [tmp_file.name])
Expand Down Expand Up @@ -223,34 +205,6 @@ def test_H020(runner: CliRunner, tmp_file: TextIO) -> None:
assert "H020" not in result.output


def test_H021(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<div style="asdf"></div>')
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H021 1:" in result.output

write_to_file(
tmp_file.name,
b'<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet" />',
)
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
assert "H021" not in result.output

write_to_file(
tmp_file.name,
b'<acronym title="Cascading Style Sheets">CSS</acronym>',
)
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
assert "H021" not in result.output

# allow template syntax inside styles
write_to_file(tmp_file.name, b'<div style="test {%"><div style="test {{">')
result = runner.invoke(djlint, [tmp_file.name])
assert "H021" not in result.output


def test_H022(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<a href="http://">')
result = runner.invoke(djlint, [tmp_file.name])
Expand Down

0 comments on commit 221c31f

Please sign in to comment.