Skip to content

Commit

Permalink
#334 Create set_attributes template tag (#366)
Browse files Browse the repository at this point in the history
* #334 Create set_attributes template tag

* #334 Adjust if condition in set_attributes

* #334 Add test_without_amend_tag

* #334 Edit set_attributes tag exmaple
  • Loading branch information
viliambalaz authored Apr 15, 2021
1 parent 52bd412 commit 325ffcf
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
48 changes: 48 additions & 0 deletions poleno/utils/templatetags/poleno/amend.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,51 @@ 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 aaa="foo">xxx</li>
<li aaa="foo">xxx</li>
<li aaa="foo">xxx</li>
<li aaa="foo">xxx</li>
<li aaa="foo">xxx</li>
</ul>
{% set_attributes path=".//li[1]" aaa=None bbb=None %}
{% set_attributes path=".//li[2]" aaa=False bbb=False %}
{% set_attributes path=".//li[3]" aaa=True bbb=True %}
{% set_attributes path=".//li[4]" aaa="bar" bbb="baz" ccc="" %}
{% set_attributes path=".//li[5]" aaa=1 bbb=2 ccc=0 %}
{% endamend %}
Result:
<ul>
<li>xxx</li>
<li>xxx</li>
<li aaa bbb>xxx</li>
<li aaa="bar" bbb="baz" ccc="">xxx</li>
<li aaa="1" bbb="2" ccc="0">xxx</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" %}'
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'')

0 comments on commit 325ffcf

Please sign in to comment.