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

Add custom stimulus tag to make it easier to create stimuli in django… #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
15 changes: 14 additions & 1 deletion sockpuppet/templatetags/sockpuppet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import template
from django.template import Template
from django.utils.safestring import mark_safe

register = template.Library()

Expand All @@ -25,7 +26,19 @@ def render(self, context):
elif node.token.token_type.name == 'VAR':
raw = '{{ ' + node.token.contents + ' }}'
else:
msg ='{} is not yet handled'.format(node.token.token_type.name)
msg = '{} is not yet handled'.format(node.token.token_type.name)
raise Exception(msg)
output = output + raw
return output


@register.simple_tag
def stimulus(reflex, **kwargs):
JulianFeinauer marked this conversation as resolved.
Show resolved Hide resolved
"""
Adds the necessary data-reflex tag to handle a click element on the respective element
:param reflex: Name of the Reflex Controller and Method ({controller}#{handler}).
:param kwargs: Further data- attributes that should be passed to the handler
"""
# TODO Validate that the reflex is present and can be handled
data = ' '.join([f'data-{key}="{val}"' for key, val in kwargs.items()])
return mark_safe(f'data-reflex="click->{reflex}" {data}')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments here

  • You can trigger a reflex on a lot of different actions besides click. This is one example on the extreme end. "cable-ready:after-morph@document->chat#scroll" (see https://sockpuppet.argpar.se/patterns#capture-all-dom-update-events).
  • Do we need mark_safe here? If the developer for whatever reason decides to put user data in this template tag and that user data happens to be a script. I'd rather see some broken html than open it up to a XSS attack.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I think having click as a 'default' action is perfectly fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathan-s these are both cood comments. I used click as its the most "natural" one. But agree that it would be cool to have another method to enter that.

To your seccond command.. we need it at least for the double-ticks (") otherwise they would be escaped. But I agree that it would be better to "safify" the value parts. Do you agree with that approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added several tests now. Do you agree with them?