mirror of
https://github.com/wrabit/django-cotton.git
synced 2025-08-04 15:18:20 +00:00
raise value error when multiple c-vars provided
This commit is contained in:
parent
93db26b03d
commit
accb0ce5c5
2 changed files with 41 additions and 2 deletions
|
@ -121,13 +121,26 @@ class CottonCompiler:
|
|||
return replacements
|
||||
|
||||
def process_c_vars(self, html: str) -> Tuple[str, str]:
|
||||
"""Extract c-vars content and remove c-vars tags from the html"""
|
||||
match = self.c_vars_pattern.search(html)
|
||||
"""
|
||||
Extract c-vars content and remove c-vars tags from the html.
|
||||
Raises ValueError if more than one c-vars tag is found.
|
||||
"""
|
||||
# Find all matches of c-vars tags
|
||||
matches = list(self.c_vars_pattern.finditer(html))
|
||||
|
||||
if len(matches) > 1:
|
||||
raise ValueError(
|
||||
f"Multiple c-vars tags found in component template. Only one c-vars tag is allowed per template."
|
||||
)
|
||||
|
||||
# Process single c-vars tag if present
|
||||
match = matches[0] if matches else None
|
||||
if match:
|
||||
attrs = match.group(1)
|
||||
vars_content = f"{{% vars {attrs.strip()} %}}"
|
||||
html = self.c_vars_pattern.sub("", html) # Remove all c-vars tags
|
||||
return vars_content, html
|
||||
|
||||
return "", html
|
||||
|
||||
def process(self, html: str) -> str:
|
||||
|
|
|
@ -70,3 +70,29 @@ class CompileTests(CottonTestCase):
|
|||
"{% cotton_verbatim %}" not in compiled,
|
||||
"Compilation should not leave {% cotton_verbatim %} tags in the output",
|
||||
)
|
||||
|
||||
def test_raises_error_on_duplicate_cvars(self):
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
get_compiled(
|
||||
"""
|
||||
<c-vars />
|
||||
<c-vars />
|
||||
"""
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
str(cm.exception),
|
||||
"Multiple c-vars tags found in component template. Only one c-vars tag is allowed per template.",
|
||||
)
|
||||
|
||||
def test_raises_on_slots_without_name(self):
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
get_compiled(
|
||||
"""
|
||||
<c-slot />
|
||||
"""
|
||||
)
|
||||
|
||||
self.assertTrue(
|
||||
"c-slot tag must have a name attribute:" in str(cm.exception),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue