Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 30, 2025

The {# djlint:off #} directive was not being respected when used inside <script> tags, causing JavaScript code within ignore blocks to be formatted anyway. This could break JavaScript syntax and functionality.

Problem:

<script>
{# djlint:off #}
function test() {
    var x = 1;
        var y = 2;  // This bad indentation got "fixed" incorrectly
}
{# djlint:on #}
function other() {
    var a = 1;
    var b = 2;
}
</script>

The root cause was in src/djlint/formatter/js.py where the launch_formatter function only checked if the entire script tag was within an unformatted block using child_of_unformatted_block(), but didn't detect unformatted blocks within the script content itself.

Solution:
Enhanced the JavaScript formatter to:

  1. Detect {# djlint:off #} blocks within script content using the same regex patterns as the rest of djLint
  2. Preserve unformatted sections while still formatting the remaining JavaScript code
  3. Handle multiple ignore blocks within a single script tag
  4. Maintain backward compatibility for scripts that are entirely within ignore blocks

After the fix:

<script>
{# djlint:off #}
function test() {
    var x = 1;
        var y = 2;  // Bad indentation preserved ✅
}
{# djlint:on #}
function other() {
    var a = 1;      // Still formatted properly ✅
    var b = 2;
}
</script>

The fix properly filters out empty regex matches and uses the correct flags (RE_FLAGS_IMSX) for pattern matching with verbose regex expressions.

Fixes #868.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link

netlify bot commented Jul 30, 2025

Deploy Preview for djlint failed.

Name Link
🔨 Latest commit f6f4df2
🔍 Latest deploy log https://app.netlify.com/projects/djlint/deploys/688a6c6120a16b0008141f87

@Copilot Copilot AI changed the title [WIP] [BUG] [Formatter] formatting not disabled inside {# djlint:off #} inside script tag Fix djlint:off formatting not being respected inside script tags Jul 30, 2025
@Copilot Copilot AI requested a review from monosans July 30, 2025 19:05
Copilot finished work on behalf of monosans July 30, 2025 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] [Formatter] formatting not disabled inside {# djlint:off #} inside script tag
2 participants