djlint / djLint

✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang
https://djLint.com
GNU General Public License v3.0
664 stars 81 forks source link

[BUG] [Linter] false positives for orphan tags when django templating is used #874

Open thacoon opened 1 month ago

thacoon commented 1 month ago

System Info

Issue

djlint reports false positives orphan tags for a snippet in which django templating is used. If I remove the django templating no more orphan tags are reported.

How To Reproduce

Detects two orphan tags for this snippet:

{% load tailwind_filters %}

<div class="relative flex items-center">
  <div class="flex h-6 items-center">
    <input id="{{ field.id_for_label }}" name="{{ field.html_name }}" {% if field.value %}checked{% endif %} type="checkbox" class="h-5 w-5 rounded border-gray-300 text-primary-black focus:ring-primary-black" {{ field|build_attrs }} />
  </div>
  <label for="{{ field.id_for_label }}" class="ml-3 text-sm font-normal text-typography-default">
    {{ field.label|safe }}
    {% if field.field.required %}<span>*</span>{% endif %}
  </label>
</div>

For this snippet it displays the following error:

H025 3:0 Tag seems to be an orphan. <div class="relative
H025 11:2 Tag seems to be an orphan. </label>

No orphan tags are reported when django templating is not used:

<div class="relative flex items-center">
  <div class="flex h-6 items-center">
    <input type="checkbox" class="h-5 w-5 rounded border-gray-300 text-primary-black focus:ring-primary-black" />
  </div>
  <label class="ml-3 text-sm font-normal text-typography-default">
    Label
    <span>*</span>
  </label>
</div>

Contents of .djlintrc/pyproject.toml [tool.djlint]

[tool.djlint]
ignore = "H006,H030,H031"
include = "H017,H035"
indent = 2
blank_line_after_tag = "load,extends"
profile = "django"
max_line_length = 120
format_attrite_template_tags = true