Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#334 Create set_attributes template tag #366

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions poleno/utils/templatetags/poleno/amend.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,45 @@ def action(fragment):

context[u'_amend'].append(action)
return u''

@register.simple_tag(takes_context=True)
def set_attributes(context, path, **kwargs):
u"""
Select elements specified by XPath and add, remove or edit their attributes.

Example:
{% amend %}
<ul>
<li>xxx</li>
<li ccc="value" ddd>yyy</li>
<li eee="value">zzz</li>
</ul>
{% set_attributes path=".//li[1]" aaa="value" bbb=True %}
{% set_attributes path=".//li[2]" ccc=None ddd=False %}
{% set_attributes path=".//li[3]" eee="new_value" %}
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
{% endamend %}

Result:
<ul>
<li aaa="value" bbb>xxx</li>
<li>yyy</li>
<li eee="new_value">zzz</li>
</ul>
"""
if u'_amend' not in context:
context[u'_amend'] = []

def action(fragment):
elements = fragment.findall(path)
for element in elements:
for key, value in kwargs.items():
if value is None or value is False:
element.attrib.pop(key, None)
elif value is True:
element.set(key, None)
else:
element.set(key, str(value))
return fragment

context[u'_amend'].append(action)
return u''
51 changes: 51 additions & 0 deletions poleno/utils/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,34 @@ def test_delete_tag(self):
u'</ul>'
u'')

def test_set_attributes_tag(self):
rendered = self._render(
u'{% load amend set_attributes from poleno.amend %}'
u'{% amend %}'
u' <ul>'
u' <li aaa="foo">xxx</li>'
u' <li aaa="foo">xxx</li>'
u' <li aaa="foo">xxx</li>'
u' <li aaa="foo">xxx</li>'
u' <li aaa="foo">xxx</li>'
u' </ul>'
u' {% set_attributes path=".//li[1]" aaa=None bbb=None %}'
u' {% set_attributes path=".//li[2]" aaa=False bbb=False %}'
u' {% set_attributes path=".//li[3]" aaa=True bbb=True %}'
u' {% set_attributes path=".//li[4]" aaa="bar" bbb="baz" ccc="" %}'
u' {% set_attributes path=".//li[5]" aaa=1 bbb=2 ccc=0 %}'
u'{% endamend %}'
u'')
self.assertHTMLEqual(rendered,
u'<ul>'
u' <li>xxx</li>'
u' <li>xxx</li>'
u' <li aaa bbb>xxx</li>'
u' <li aaa="bar" bbb="baz" ccc="">xxx</li>'
u' <li aaa="1" bbb="2" ccc="0">xxx</li>'
u'</ul>'
u'')

def test_amend_tag_on_plain_text(self):
rendered = self._render(
u'{% load amend prepend append from poleno.amend %}'
Expand Down Expand Up @@ -485,3 +513,26 @@ def test_multiple_amend_tags(self):
u' <li>xxx!</li>'
u'</ul>'
u'')

def test_without_amend_tag(self):
rendered = self._render(
u'{% load prepend append before after delete set_attributes from poleno.amend %}'
u'<ul>'
u' <li>aaa</li>'
u' <li>bbb</li>'
u'</ul>'
u'<p>foobar</p>'
u'{% prepend path=".//ul" %}<li>ccc</li>{% endprepend %}'
u'{% append path=".//ul" %}<li>ddd</li>{% endappend %}'
u'{% before path=".//li[2]" %}<li>eee</li>{% endbefore %}'
u'{% after path=".//li[2]" %}<li>fff</li>{% endafter %}'
u'{% delete path=".//p" %}'
viliambalaz marked this conversation as resolved.
Show resolved Hide resolved
u'{% set_attributes path=".//ul" att=True %}'
u'')
self.assertHTMLEqual(rendered,
u'<ul>'
u' <li>aaa</li>'
u' <li>bbb</li>'
u'</ul>'
u'<p>foobar</p>'
u'')