added rule H019. Closes #45, Closes #43

This commit is contained in:
Christopher Pickering 2021-10-04 13:32:56 +02:00
parent d01f866a29
commit bdc3645415
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 41 additions and 6 deletions

View file

@ -45,6 +45,8 @@ Codes
+--------+-------------------------------------------------------------------------+
| J018 | (Jinja) Internal links should use the {% url ... %} pattern. |
+--------+-------------------------------------------------------------------------+
| H019 | Replace 'javascript:abc()' with on_ event and real url. |
+--------+-------------------------------------------------------------------------+
Adding Rules
------------

View file

@ -247,12 +247,19 @@
message: (Django) Internal links should use the {% url ... %} pattern.
flags: re.DOTALL|re.I
patterns:
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url)=[\"|'](?!https?://)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!https?://)[\w|/]+
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url)=[\"|'](?!(?:https?://)|javascript:|on\w+:)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!(?:https?://)|javascript:|on\w+:)[\w|/]+
- rule:
name: J018
message: (Jinja) Internal links should use the {{ url_for() ... }} pattern.
flags: re.DOTALL|re.I
patterns:
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url)=[\"|'](?!https?://)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!https?://)[\w|/]+
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url)=[\"|'](?!(?:https?://)|javascript:|on\w+:)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!(?:https?://)|javascript:|on\w+:)[\w|/]+
- rule:
name: H019
message: Replace 'javascript:abc()' with on_ event and real url.
flags: re.DOTALL|re.I
patterns:
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url)=[\"|']javascript:[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|']javascript:[\w|/]+

View file

@ -7,7 +7,7 @@ run::
# for a single test
pytest tests/test_linter.py::test_H011 --cov=src/djlint --cov-branch \
pytest tests/test_linter.py::test_DJ018 --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
"""
@ -203,12 +203,38 @@ def test_H017(runner: CliRunner, tmp_file: TextIO) -> None:
def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,
b'<a class="drop-link" href="/Collections?handler=RemoveAgreement&id=@a.Id">',
b'<a href="/Collections?handler=RemoveAgreement&id=@a.Id">\n<form action="/Collections">',
)
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "D018 1:" in result.output
assert "J018 1:" in result.output
assert "D018 2:" in result.output
assert "J018 2:" in result.output
# test javascript functions
write_to_file(
tmp_file.name,
b'<a href="javascript:abc()">\n<form action="javascript:abc()">',
)
result = runner.invoke(djlint, [tmp_file.name])
# don't check status code. will fail on other rules here.
assert "D018 1:" not in result.output
assert "J018 1:" not in result.output
assert "D018 2:" not in result.output
assert "J018 2:" not in result.output
# test on_ events
write_to_file(
tmp_file.name,
b'<a href="onclick:abc()">\n<form action="onclick:abc()">',
)
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
assert "D018 1:" not in result.output
assert "J018 1:" not in result.output
assert "D018 2:" not in result.output
assert "J018 2:" not in result.output
def test_rules_not_matched_in_ignored_block(