Skip to content

Commit 89843f4

Browse files
committed
Fix: Side-effect on attributes
1 parent f7a740b commit 89843f4

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

djangocms_frontend/models.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ def add_attribute(self, attr, value=None):
7979

8080
def get_attributes(self):
8181
attributes = self.config.get("attributes", {})
82-
classes = set(attributes.get("class", "").split())
83-
classes.update(self._additional_classes)
84-
if classes:
85-
attributes["class"] = " ".join(classes)
86-
parts = (f'{item}="{conditional_escape(value)}"' if value else f"{item}" for item, value in attributes.items())
87-
attributes_string = " ".join(parts)
82+
classes = set(attributes.get("class", "").split()) # classes added in attriutes
83+
classes.update(self._additional_classes) # add additional classes
84+
classes = (f'class="{conditional_escape(" ".join(classes))}"') if classes else "" # to string
85+
parts = (
86+
f'{item}="{conditional_escape(value)}"' if value else f"{item}"
87+
for item, value in attributes.items()
88+
if item != "class"
89+
)
90+
attributes_string = (classes + " ".join(parts)).strip()
8891
return mark_safe(" " + attributes_string) if attributes_string else ""
8992

9093
def save(self, *args, **kwargs):

tests/test_plugin_tag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_tag_rendering_with_paramter(self):
2424
{% load frontend cms_tags %}
2525
{% plugin "alert" alert_context="secondary" alert_dismissible=True %}Alert{% endplugin %}
2626
""")
27-
expected_result = """<div class="alert alert-secondary alert-dismissible alert-primary" role="alert">
27+
expected_result = """<div class="alert alert-secondary alert-dismissible" role="alert">
2828
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button><div>Alert
2929
</div></div>"""
3030

@@ -62,7 +62,7 @@ def test_complex_tags(self):
6262
expected_result = """
6363
<div class="card text-primary text-center border-info h-100">
6464
<div class="text-start card-header"><h4>Card title</h4></div>
65-
<div class="card-body text-start text-center card-header">
65+
<div class="card-body text-center">
6666
Some quick example text to build on the card title and make up the
6767
bulk of the card's content.
6868
</div>

0 commit comments

Comments
 (0)