Skip to content

Commit

Permalink
Support to have HTML in LabelField and LinkField
Browse files Browse the repository at this point in the history
  • Loading branch information
LeResKP committed Sep 14, 2013
1 parent 0221d6a commit 5fe84ce
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
26 changes: 26 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,39 @@ class TestLabelField(WidgetTest):
value="info" name="hidden_name" id="hid"/></span>"""
validate_params = [[None, {'hid': 'b'}, EmptyField]]

def test_escape(self):
attrs = {'value': 'line 1<br />line 2'}
expected = '<span>line 1&lt;br /&gt;line 2<input value="line 1&lt;br /&gt;line 2" type="hidden"/></span>'
for engine in self._get_all_possible_engines():
yield (self._check_rendering_vs_expected,
engine, attrs, self.params, expected)

attrs = {'value': 'line 1<br />line 2', 'escape': False}
expected = '<span>line 1<br />line 2<input value="line 1<br />line 2" type="hidden"/></span>'
for engine in self._get_all_possible_engines():
yield (self._check_rendering_vs_expected,
engine, attrs, self.params, expected)


class TestLinkField(WidgetTest):
widget = LinkField
attrs = {'css_class': 'something', 'value': 'info',
'name': 'hidden_name', 'text': 'some $', 'link': '/some/$'}
expected = """<a href="/some/info" class="something">some info</a>"""

def test_escape(self):
attrs = {'text': 'line 1<br />line 2'}
expected = '<a href="">line 1&lt;br /&gt;line 2</a>'
for engine in self._get_all_possible_engines():
yield (self._check_rendering_vs_expected,
engine, attrs, self.params, expected)

attrs = {'text': 'line 1<br />line 2', 'escape': False}
expected = '<a href="">line 1<br />line 2</a>'
for engine in self._get_all_possible_engines():
yield (self._check_rendering_vs_expected,
engine, attrs, self.params, expected)


class TestButton(WidgetTest):
widget = Button
Expand Down
2 changes: 1 addition & 1 deletion tw2/forms/templates/label_field.jinja
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<span>{{ w.value|string }}<input {{ w.attrs|xmlattr }}/></span>
<span>{% if w.escape %}{{ w.value|string }}{% else %}{{ w.value|safe }}{% endif %}<input {{ w.attrs|xmlattr }}/></span>
8 changes: 7 additions & 1 deletion tw2/forms/templates/label_field.mak
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<%namespace name="tw" module="tw2.core.mako_util"/>\
<span>${w.value or ''}<input ${tw.attrs(attrs=w.attrs)}/></span>
<span>\
% if w.escape:
${w.value or ''}\
% else:
${w.value or '' | n}\
% endif
<input ${tw.attrs(attrs=w.attrs)}/></span>
2 changes: 1 addition & 1 deletion tw2/forms/templates/link_field.jinja
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a {{ w.attrs|xmlattr }}>{{ w.text }}</a>
<a {{ w.attrs|xmlattr }}>{% if w.escape %}{{ w.text }}{% else %}{{ w.text|safe }}{% endif %}</a>
8 changes: 7 additions & 1 deletion tw2/forms/templates/link_field.mak
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<%namespace name="tw" module="tw2.core.mako_util"/>\
<a ${tw.attrs(attrs=w.attrs)}>${w.text}</a>
<a ${tw.attrs(attrs=w.attrs)}>\
% if w.escape:
${w.text}\
% else:
${w.text | n}\
% endif
</a>
2 changes: 2 additions & 0 deletions tw2/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class LabelField(InputField):
"""
type = 'hidden'
template = "tw2.forms.templates.label_field"
escape = twc.Param('Whether text shall be html-escaped or not', default=True)
validator = twc.BlankValidator


Expand All @@ -208,6 +209,7 @@ class LinkField(twc.Widget):
link = twc.Param('Link target', default='')
text = twc.Param('Link text', default='')
value = twc.Param("Value to replace $ with in the link/text")
escape = twc.Param('Whether text shall be html-escaped or not', default=True)
validator = twc.BlankValidator

def prepare(self):
Expand Down

0 comments on commit 5fe84ce

Please sign in to comment.