From 325ffcfc39ba133144fdafa19c629eb9158e60b8 Mon Sep 17 00:00:00 2001 From: Viliam Balaz Date: Thu, 15 Apr 2021 09:36:39 +0200 Subject: [PATCH] #334 Create set_attributes template tag (#366) * #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 --- poleno/utils/templatetags/poleno/amend.py | 48 +++++++++++++++++++++ poleno/utils/tests/test_templatetags.py | 51 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/poleno/utils/templatetags/poleno/amend.py b/poleno/utils/templatetags/poleno/amend.py index d74d3bf27..041af5725 100644 --- a/poleno/utils/templatetags/poleno/amend.py +++ b/poleno/utils/templatetags/poleno/amend.py @@ -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 %} + + {% 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: + + """ + 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'' diff --git a/poleno/utils/tests/test_templatetags.py b/poleno/utils/tests/test_templatetags.py index 570d295e6..b1c79eab0 100644 --- a/poleno/utils/tests/test_templatetags.py +++ b/poleno/utils/tests/test_templatetags.py @@ -423,6 +423,34 @@ def test_delete_tag(self): u'' u'') + def test_set_attributes_tag(self): + rendered = self._render( + u'{% load amend set_attributes from poleno.amend %}' + u'{% amend %}' + u' ' + 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'' + u'') + def test_amend_tag_on_plain_text(self): rendered = self._render( u'{% load amend prepend append from poleno.amend %}' @@ -485,3 +513,26 @@ def test_multiple_amend_tags(self): u'
  • xxx!
  • ' u'' u'') + + def test_without_amend_tag(self): + rendered = self._render( + u'{% load prepend append before after delete set_attributes from poleno.amend %}' + u'' + u'

    foobar

    ' + u'{% prepend path=".//ul" %}
  • ccc
  • {% endprepend %}' + u'{% append path=".//ul" %}
  • ddd
  • {% endappend %}' + u'{% before path=".//li[2]" %}
  • eee
  • {% endbefore %}' + u'{% after path=".//li[2]" %}
  • fff
  • {% endafter %}' + u'{% delete path=".//p" %}' + u'{% set_attributes path=".//ul" att=True %}' + u'') + self.assertHTMLEqual(rendered, + u'' + u'

    foobar

    ' + u'')