Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Formatter] Jinja variable within a <script> tag #613

Closed
3 tasks done
davidbgk opened this issue Apr 25, 2023 · 11 comments · Fixed by #644
Closed
3 tasks done

[BUG] [Formatter] Jinja variable within a <script> tag #613

davidbgk opened this issue Apr 25, 2023 · 11 comments · Fixed by #644
Labels

Comments

@davidbgk
Copy link

System Info

  • OS: macOS Ventura
  • Python 3.11.2
  • djlint, version 1.25.0
  • Jinja2

Issue

First time user, I tried to reformat a template containing that code:

<script id="search-index" type="application/json">
  {{ search_index }}
</script>

And it always produces:

  <script id="search-index" type="application/json">
    {
      {
        search_index
      }
    }
  </script>

Whatever the kind of ignoring comment I put around that part of the HTML (which is probably another bug(?).

How To Reproduce

The command I use is the following:

djlint templates --profile=jinja --reformat --format-css --indent-css 2 --format-js --indent-js 2 --indent 2

I tried with or without the script's attributes (id and type) and there is no difference.

Hope it helps! (and I can provide more details on demand)

@davidbgk davidbgk added 🦠 bug Something isn't working 🧽 formatter labels Apr 25, 2023
@welcome
Copy link

welcome bot commented Apr 25, 2023

Thanks for opening your first issue here!

@christopherpickering
Copy link
Contributor

hey @davidbgk the js formatter is from jsbeautify and does not support template syntax yet. You'll hve to run w/out the --format-js flag to make that work.

does that help?

@davidbgk
Copy link
Author

@christopherpickering oh I get it, thanks! I do confirm that it doesn't try to reformat the block if I remove the --format-js option.

I'm still wondering why ignoring that whole HTML block leads to its evaluation though 🤔 that would have come handy for my case.

Feel free to close the issue if you think it's not a real problem given that the main issue is explained!

@christopherpickering
Copy link
Contributor

Hmm if you tried to ignore the block in djlint, it should have ignored it. Can you put the code you tried, and I will take a look deeper.

@davidbgk
Copy link
Author

I tried that one and other combinations ({# djlint:off #} and {{ /* djlint:off */ }}) without any success:

  <!-- djlint:off -->
  <script id="search-index" type="application/json">{{ search_index }}</script>
  <!-- djlint:on -->

@Benik3
Copy link

Benik3 commented May 10, 2023

Hello.
I can confirm this problem. As you mentioned, javascript is formatted by jsbeautifier.
I found that this settings works:

/* beautify preserve:start */
var foo = {{ bar }}
/* beautify preserve:end */

@christopherpickering
Copy link
Contributor

It looks like the issue is that the --format-js tag is not checking to see if it is inside ignored code... I'll see about getting this fixed. Thanks!

christopherpickering added a commit that referenced this issue May 11, 2023
@davidbgk
Copy link
Author

I confirm the fix works for my case on the dev branch, thank you!

christopherpickering pushed a commit that referenced this issue May 11, 2023
## [1.27.2](v1.27.1...v1.27.2) (2023-05-11)

### Bug Fixes

* **formatter:** fixed an edge case wild regex grab ([0d57e75](0d57e75)), closes [#640](#640)
* **formatter:** fixed formatting of plural tag in blocktrans ([92c083b](92c083b)), closes [#641](#641)
* **formatter:** fixed js/css being formatted even inside ignored blocks ([cbf5796](cbf5796)), closes [#613](#613)
* **linter:** fixed a few false positives/missing positives, added tests ([30f8d31](30f8d31)), closes [#643](#643) [#642](#642)
* **linter:** fixed false postive in alpinejs tags for H037 ([6520f63](6520f63)), closes [#639](#639)
@christopherpickering
Copy link
Contributor

🎉 This issue has been resolved in version 1.27.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nexgen
Copy link

nexgen commented May 19, 2023

I'm currently using v1.28 of djlint but the problem still seems to work with javascript code.

/* beautify preserve:start */ --> Works
but other variation of djlint ignore like: {# djlint:off #} etc... is still not working.

@christopherpickering
Copy link
Contributor

@nexgen please open a new issue with a code example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants