Skip to content

Conversation

@SamuelJennings
Copy link
Contributor

Overview

Fixes issue #321 where django-cotton failed to parse component attributes containing Django template tags with nested quotes, particularly in i18n translation strings.

Problem

Users couldn't pass Django template tags to Cotton components when those tags contained nested quotes:

<c-alert
    message="{% trans "We've sent you an email." %}"
/>

was not parsing properly and would result in:

django.template.exceptions.TemplateSyntaxError: Unclosed tag on line 20: 'cotton'. Looking for one of: endcotton.

Solution

Modified the tokenizer in nested_tag_support.py to track Django template syntax depth ({{ }} and {% %} blocks) and ignore quote characters while inside Django syntax. This prevents premature quote termination during tokenization.

Key Changes (lines 119-177):

  • Added django_var_depth counter for {{ }} blocks
  • Added django_tag_depth counter for {% %} blocks
  • Modified quote handling to only process quotes when both depths are 0
  • Enhanced %} handling to distinguish nested blocks from Cotton tag endings

What Now Works ✅

  • Original issue case: Passing trans strings with apostrophes
  • All Django template tags: {% trans %}, {% blocktrans %}, {% if %}, {% for %}, {% url %}, {% comment %}, {% verbatim %}
  • Variable filters: E.g. <c-alert message="{{ "test"|add:'123'|add:"abc" }}" with quoted arguments
  • Real-world scenarios: Practical use cases now tested

Test Results

Files Changed

  • django_cotton/nested_tag_support.py - Tokenizer enhancement
  • django_cotton/tests/regression/test_nested_quotes_issue_321.py - Comprehensive test suite (26 tests)

… in component attributes

- Modified smart_tokenize() to track Django template syntax depth
- Added django_var_depth counter for {{ }} blocks
- Added django_tag_depth counter for {% %} blocks
- Only process quotes when both depths are 0
- Enhanced %} handling to distinguish nested blocks from Cotton tag endings
- Added comprehensive regression test suite (26 tests)
- All 141 existing tests pass with no regressions
- Resolves parsing errors when using {% trans %} and other template tags with nested quotes
@SamuelJennings
Copy link
Contributor Author

Just for some context, I was having a lot of issues recently with the same thing as described in #321. I understand that not everyone has a lot of free time to work on open source code so I thought I would just have a go at fixing the issue myself.

Totally open to feedback/suggestions.

@wrabit
Copy link
Owner

wrabit commented Dec 8, 2025

@SamuelJennings looks good, thank you.

@wrabit wrabit merged commit 126bd08 into wrabit:main Dec 8, 2025
16 checks passed
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.

2 participants