fix: keep template variable in HTML attribute name on one line (#1185) - #2206
Merged
monosans merged 2 commits intoJul 11, 2026
Merged
Conversation
…t#1185) The attribute tokenizer split an attribute name that embeds a template variable, e.g. `data-{{ st_controller }}-target="..."`, into three separate tokens (`data-`, `{{ st_controller }}`, `-target="..."`). Because each token is emitted on its own line during attribute expansion, the resulting output was corrupted. The attribute-name subpattern only matched plain name characters, so a `{{ ... }}` (or `{% ... %}`) in the middle of a name terminated the run and the remainder was picked up by the standalone-template-tag branch. Allow an embedded template tag inside an attribute name (anchored by a leading name character, using an atomic group to avoid backtracking) so `data-{{ x }}-target` is recognised as a single attribute name. Pure standalone template tags (`{{ x }}`, `{% if %}...{% endif %}`) still fall through to the standalone branch, preserving existing behaviour.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe attribute-pattern regex now recognizes template expressions within HTML attribute names. Three parametrized tests cover embedded, leading, and complete template-based names, and the changelog documents the fix. ChangesTemplated Attribute Name Formatting Fix
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
monosans
enabled auto-merge (squash)
July 11, 2026 18:52
This was referenced Jul 13, 2026
Closed
[BUG] [Formatter] Line break inserted mid-token in class attribute breaks BEM modifier classes
#2240
Closed
Closed
Closed
This was referenced Jul 21, 2026
Closed
This was referenced Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1185
Root cause
The attribute tokenizer (
Config.attribute_patterninsrc/djlint/settings.py) matched an attribute name with the subpattern(?:\w|-|\.|\:|@|/(?!>))+, which only accepts plain name characters. When a template variable is embedded in an attribute name — e.g.data-{{ st_controller }}-target— the run stops at the{{, sodata-is matched as one attribute name, the{{ st_controller }}is then picked up by the standalone-template-tag branch, and-target="documentFile"is matched as a third, separate attribute.During attribute expansion (
src/djlint/formatter/attributes.py), each token is emitted on its own line, so the single attribute is corrupted into three lines.Reproduction
Input:
Actual (wrong) output on
1.36.4and currentmaster:Corrected output with this change:
Fix
Extend the attribute-name subpattern so a template tag (
{{ ... }}or{% ... %}) may appear inside a name. The name must still start with a real name character, and the embedded tag uses an atomic group(?>...)to avoid pathological backtracking:The change is localized to the name portion of the pattern only. Because a name must begin with a name character, a standalone template tag (
{{ x }},{% if %}...{% endif %}) still falls through to the existing standalone branch, so that behaviour is unchanged. Values, quoting, and boolean attributes are untouched.Tests
Added a regression case
template_var_in_attribute_nametotests/test_html/test_attributes.py. It fails onmaster(producing the three-line output shown above) and passes with this fix.The full test suite passes locally (678 passed; the one unrelated failure,
test_python_call, fails identically on unmodifiedmasterin this environment because it invokes a barepythonbinary that is not on PATH here).ruff checkandruff format --checkare clean on the changed files. I also smoke-tested standalone template tags,{% if %}-wrapped booleans, template vars in values, and multiple template vars within one name to confirm no regressions.Disclosure: prepared with AI assistance; reviewed and verified locally.