Skip to content

Commit

Permalink
#334 Adjust if condition in set_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Apr 13, 2021
1 parent 759fd4c commit 1bd9ba6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 74 deletions.
13 changes: 5 additions & 8 deletions poleno/utils/templatetags/poleno/amend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import re
import lxml.html

from poleno.utils.template import Library
Expand Down Expand Up @@ -277,14 +276,12 @@ def action(fragment):
elements = fragment.findall(path)
for element in elements:
for key, value in kwargs.items():
if not re.match(r'^[a-z][a-z0-9_.-]*$', key):
raise ValueError(u'Invalid tag name')
if key not in element.attrib and not value:
continue
elif not value:
element.attrib.pop(key)
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, value if value != True else None)
element.set(key, str(value))
return fragment

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

def test_set_attributes_tag_add_attribute(self):
def test_set_attributes_tag(self):
rendered = self._render(
u'{% load amend set_attributes from poleno.amend %}'
u'{% amend %}'
u' <ul>'
u' <li>xxx</li>'
u' <li>yyy</li>'
u' <li>zzz</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' <li aaa="foo">xxx</li>'
u' </ul>'
u' {% set_attributes path=".//li" aaa="value" %}'
u' {% set_attributes path=".//li[2]" bbb=True %}'
u' {% set_attributes path=".//li[3]" ccc="value" ddd="value" eee=True %}'
u'{% endamend %}'
u'')
self.assertHTMLEqual(rendered,
u'<ul>'
u' <li aaa="value">xxx</li>'
u' <li aaa="value" bbb>yyy</li>'
u' <li aaa="value" ccc="value" ddd="value" eee>zzz</li>'
u'</ul>'
u'')

def test_set_attributes_tag_edit_attribute(self):
rendered = self._render(
u'{% load amend set_attributes from poleno.amend %}'
u'{% amend %}'
u' <ul>'
u' <li aaa="value">xxx</li>'
u' <li aaa="value" bbb="value" ccc="value">yyy</li>'
u' <li aaa="value">zzz</li>'
u' </ul>'
u' {% set_attributes path=".//li" aaa="new_value" %}'
u' {% set_attributes path=".//li[2]" bbb="new_value" ccc="new_value" %}'
u'{% endamend %}'
u'')
self.assertHTMLEqual(rendered,
u'<ul>'
u' <li aaa="new_value">xxx</li>'
u' <li aaa="new_value" bbb="new_value" ccc="new_value">yyy</li>'
u' <li aaa="new_value">zzz</li>'
u'</ul>'
u'')

def test_set_attributes_tag_remove_attribute(self):
rendered = self._render(
u'{% load amend set_attributes from poleno.amend %}'
u'{% amend %}'
u' <ul>'
u' <li aaa="value">xxx</li>'
u' <li aaa="value" bbb>yyy</li>'
u' <li aaa="value" ccc="value" ddd="value" eee fff>zzz</li>'
u' </ul>'
u' {% set_attributes path=".//li" aaa=None %}'
u' {% set_attributes path=".//li[2]" bbb=False %}'
u' {% set_attributes path=".//li[3]" ccc=None ddd="" eee=None fff="" %}'
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>yyy</li>'
u' <li>zzz</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_set_attributes_tag_with_invalid_key(self):
with self.assertRaisesMessage(ValueError, u'Invalid tag name'):
rendered = self._render(
u'{% load amend set_attributes from poleno.amend %}'
u'{% amend %}'
u' <ul>'
u' <li>xxx</li>'
u' <li>yyy</li>'
u' <li>zzz</li>'
u' </ul>'
u' {% set_attributes path=".//li" Aaa=True %}'
u'{% endamend %}'
u'')

def test_amend_tag_on_plain_text(self):
rendered = self._render(
u'{% load amend prepend append from poleno.amend %}'
Expand Down

0 comments on commit 1bd9ba6

Please sign in to comment.