Enhance smart_tokenize function to better support nested tags and quotes (Fixes #321)
#322
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.
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:
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.pyto 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):
django_var_depthcounter for{{ }}blocksdjango_tag_depthcounter for{% %}blocks%}handling to distinguish nested blocks from Cotton tag endingsWhat Now Works ✅
{% trans %},{% blocktrans %},{% if %},{% for %},{% url %},{% comment %},{% verbatim %}<c-alert message="{{ "test"|add:'123'|add:"abc" }}"with quoted argumentsTest Results
Files Changed
django_cotton/nested_tag_support.py- Tokenizer enhancementdjango_cotton/tests/regression/test_nested_quotes_issue_321.py- Comprehensive test suite (26 tests)