djlint / djLint

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

[BUG] [Formatter] In a file starting with `{# djlint:off #}`, the formatter still does some modifications #703

Open malo-malo opened 1 year ago

malo-malo commented 1 year ago

System Info

- OS: ubuntu 20.04
- Python Version 3.8.10
- djLint Version 1.31.1
- template language: django templates

Issue

Followup on this issue https://github.com/Riverside-Healthcare/djLint/issues/655 Most cases have been fixed, but some parts of supposedly ignored templates are still being formatted.

How To Reproduce

Here is a list of the problems I have encountered and that I reproduced in the online version, with the default configuration:

Initial Formatted
```html {# djlint:off #}
{% comment %} {% endcomment %}
{% blocktranslate trimmed %}{% endblocktranslate %} ```
```html {# djlint:off #}
{% comment %} {% endcomment %}
{% blocktranslate trimmed %}{% endblocktranslate %} ```
  1. Some multi-line tags are being collapsed
  2. Any line with a </style> or </script> tag gets un-indented
  3. Any line with a {% endcomment %} get un-indented
  4. The presence of a blocktranslate trimmed seems to have an influence on the formatting of following lines, in the entire file (the blocktranslate and <a> lines are a minimal reproduction)
  5. An extra line is added at the end of the file (or some are removed if there are several)
malo-malo commented 1 year ago

The </script> part seems to be similar to this issue https://github.com/Riverside-Healthcare/djLint/issues/697

christopherpickering commented 1 year ago

Note to self for future fix..

the error is coming in formatter/condense.py. The strip_space function allows updates if we are a safe closing tag. This should only happen if we are not inside another ignored...

we684123 commented 1 year ago

Add old example from #569 😢

and after enable {% comment %} djlint:on {% endcomment %} , something html tag will be abnormally indented

System Info

https://djlint.com/demo/ Python 3.10.2; djLint 1.31.1

<nav class="navbar navbar-expand-lg navbar-light bg-light main-nav p-3">
    <div class="collapse navbar-collapse" id="navBar">
        <div class="navbar-nav ml-auto">
            <ul class="navbar-nav mb-2 mb-lg-0">
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle mr-4"
                       href="#"
                       id="navbarDropdownMenuLink"
                       data-bs-toggle="dropdown"
                       aria-haspopup="true"
                       aria-expanded="false">
                        {{ request.user.name }}
                    </a>
                    <div class="dropdown-menu"
                         style="min-width: 105px;
                                width: 105px"
                         aria-labelledby="navbarDropdownMenuLink">
                        {% comment %} djlint:off {% endcomment %}
                        <a class="dropdown-item"
                           href="http://user/login">變更密碼</a>
                        {% comment %} djlint:on {% endcomment %}
                    </div>
                </li>
            </ul>
        </div>
    </div>
</nav>

2023-07-19 09_05_38-HTML Template Linter and Formatter _ djLint — Mozilla Firefox

christopherpickering commented 1 year ago

I reopened the other as this is probably more relevant there 👍🏼 thanks for reporting!