Skip to content

Commit

Permalink
Merge pull request #347 from Riverside-Healthcare/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering authored Aug 22, 2022
2 parents b482dda + 79cd013 commit fc68a51
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 327 deletions.
624 changes: 314 additions & 310 deletions docs/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "djlint_docs",
"version": "1.0.24",
"version": "1.0.25",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -25,7 +25,7 @@
"md5": "^2.3.0"
},
"devDependencies": {
"@11ty/eleventy": "1.0.1",
"@11ty/eleventy": "1.0.2",
"@11ty/eleventy-img": "2.0.1",
"@11ty/eleventy-plugin-syntaxhighlight": "4.1.0",
"@fontsource/inter": "4.5.12",
Expand All @@ -40,7 +40,7 @@
"cz-conventional-changelog": "3.3.0",
"eleventy-plugin-edit-on-github": "1.1.0",
"eleventy-plugin-metagen": "1.7.11",
"esbuild": "0.15.3",
"esbuild": "0.15.5",
"eslint": "8.22.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.26.0",
Expand All @@ -55,7 +55,7 @@
"postcss-nested": "5.0.6",
"prettier": "2.7.1",
"prismjs": "1.28.0",
"sass": "1.54.4",
"sass": "1.54.5",
"slugify": "1.6.5"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cssbeautifier==1.14.4
dill==0.3.5.1; python_full_version >= "3.7.2"
editorconfig==0.12.3
execnet==1.9.0; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"
flake8==4.0.1; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
flake8==3.9.2; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
html-tag-names==0.1.2; python_version >= "3.7" and python_version < "4.0"
html-void-elements==0.1.0; python_version >= "3.7" and python_version < "4.0"
importlib-metadata==4.11.0; python_version >= "3.7"
Expand Down
8 changes: 4 additions & 4 deletions src/djlint/formatter/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def should_i_move_template_tag(out_format: str, match: re.Match) -> str:

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

if not re.findall(
r"\<(?:"
+ str(config.indent_html_tags)
+ r")\b(?:[^>]|{%[^(?:%}]*?%}|{{[^(?:}}]*?}})*?"
+ re.escape(match.group(1))
+ "$",
+ r")\b(?:\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{\#[^\#]*\#}|[^>{}])*?"
# original
# + r")\b(?:[^>]|{%[^(?:%}]*?%}|{{[^(?:}}]*?}})*?"
+ re.escape(match.group(1)) + "$",
html[: match.end()],
re.MULTILINE | re.VERBOSE,
):
Expand Down
30 changes: 30 additions & 0 deletions src/djlint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
)


def overlaps_ignored_block(config: Config, html: str, match: re.Match) -> bool:
"""Do not add whitespace if the tag is in a non indent block."""
return any(
# don't require the match to be fully inside the ignored block.
# poorly build html will probably span ignored blocks and should be ignored.
(
ignored_match.start(0) <= match.start()
and match.start() <= ignored_match.end()
)
or (
ignored_match.start(0) <= match.end() and match.end() <= ignored_match.end()
)
for ignored_match in list(
re.finditer(
re.compile(
config.ignored_blocks,
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
),
html,
)
)
+ list(
re.finditer(
re.compile(config.ignored_inline_blocks, re.IGNORECASE | re.VERBOSE),
html,
)
)
)


def inside_ignored_rule(config: Config, html: str, match: re.Match, rule: str) -> bool:
"""Check if match is inside an ignored pattern."""
for rule_regex in config.ignored_rules:
Expand Down
6 changes: 3 additions & 3 deletions src/djlint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import regex as re

from .helpers import inside_ignored_block, inside_ignored_rule
from .helpers import inside_ignored_rule, overlaps_ignored_block
from .settings import Config

flags = {
Expand Down Expand Up @@ -99,7 +99,7 @@ def lint_file(config: Config, this_file: Path) -> Dict:

for match in open_tags:
if (
inside_ignored_block(config, html, match) is False
overlaps_ignored_block(config, html, match) is False
and inside_ignored_rule(config, html, match, rule["name"])
is False
):
Expand All @@ -119,7 +119,7 @@ def lint_file(config: Config, this_file: Path) -> Dict:
html,
):
if (
inside_ignored_block(config, html, match) is False
overlaps_ignored_block(config, html, match) is False
and inside_ignored_rule(config, html, match, rule["name"])
is False
):
Expand Down
10 changes: 5 additions & 5 deletions src/djlint/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
message: Attributes should be double quoted.
flags: re.DOTALL|re.I
patterns:
- (?:class|id|src|width|height|alt|style|lang|title|srcset|media)=\'[^\']*'
- <(?:\w+)\b(\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*(?:class|id|src|width|height|alt|style|lang|title|srcset|media)=\'[^\']*'
- rule:
name: H009
message: Tag names should be lowercase.
Expand Down Expand Up @@ -106,7 +106,7 @@
- <img\s(?:(?!(?:alt)=).)*/?>
- rule:
name: H014
message: More than 2 blank lines.
message: Found extra blank lines.
flags: re.DOTALL
patterns:
- "[^\n]{,10}\n{3,}"
Expand Down Expand Up @@ -185,7 +185,7 @@
message: Tag seems to be an orphan.
flags: re.I|re.DOTALL
patterns:
- <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{#[^%]*#}|[^'\">{}])*)(?<!/)>
- <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{#[^#]*#}|[^'\">{}])*)(?<!/)>
- rule:
name: H026
message: Empty id and class tags can be removed.
Expand Down Expand Up @@ -242,8 +242,8 @@
name: T032
message: Extra whitespace found in template tags.
patterns:
- "{%(([\"|'](?:(?!'|\"|%}).)*?[\"|'])|[^(?:%}|'|\")])*?\\s{2,}"
- "{{(([\"|'](?:(?!'|\"|}}).)*?[\"|'])|[^(?:}}|'|\")])*?\\s{2,}"
- "{%(([\"|'](?:(?!'|\"|%}).)*?[\"|'])|[^(?:%}|'|\"|\n)])*?[ \t]{2,}"
- "{{(([\"|'](?:(?!'|\"|}}).)*?[\"|'])|[^(?:}}|'|\"|\n)])*?[ \t]{2,}"
- rule:
name: H033
message: Extra whitespace found in form action.
Expand Down
45 changes: 45 additions & 0 deletions tests/test_linter/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ def test_H006(runner: CliRunner, tmp_file: TextIO) -> None:
assert "H006 1:" in result.output
assert "found 1 error" in result.output

# check that we don't partial match in an ignored block
write_to_file(
tmp_file.name,
b"""{# [INFO][JINJA] I use syntax "{% if <img alt=\""",
if I want that something happened solely if "img" exists in the content of my articles #}
<script src="KiraJS.js" defer></script>
""",
)
result = runner.invoke(djlint, [tmp_file.name])
assert "H006" not in result.output


def test_H007(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<html lang="en">')
Expand All @@ -105,6 +117,21 @@ def test_H008(runner: CliRunner, tmp_file: TextIO) -> None:
assert result.exit_code == 1
assert "H008 1:" in result.output

write_to_file(
tmp_file.name,
b"""<link rel="stylesheet" href="KiraCSS.css" media="print" onload="this.media='all'" media=''/>""",
)
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H008 1:" in result.output

write_to_file(
tmp_file.name,
b"""<link rel="stylesheet" href="KiraCSS.css" media="print" onload="this.media='all'"/>""",
)
result = runner.invoke(djlint, [tmp_file.name])
assert "H008 1:" not in result.output


def test_H009(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<H1>")
Expand Down Expand Up @@ -715,6 +742,24 @@ def test_T032(runner: CliRunner, tmp_file: TextIO) -> None:
result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"])
assert "T032" not in result.output

write_to_file(
tmp_file.name,
b"""{# [INFO] Simple example #}
{% set kira = [
'Goddess', 'Genius'
] %}
{# [INFO] Real example #}
{% set kira_online_scaners = [
('https://quttera.com/sitescan/', 'SashaButtonLightSkyBlue', 'Quttera'),
('https://sitecheck.sucuri.net/results/', 'SashaButtonLimeGreen', 'Sucuri'),
('https://www.isithacked.com/check/', 'SashaButtonPlum', 'Is It Hacked?'),
] %}
""",
)
result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"])
assert "T032" not in result.output


def test_H033(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
Expand Down

0 comments on commit fc68a51

Please sign in to comment.