|
13 | 13 | from .exceptions import BootstrapError
|
14 | 14 | from .utils import add_css_class
|
15 | 15 |
|
| 16 | +try: |
| 17 | + from html.parser import HTMLParser |
| 18 | +except ImportError: |
| 19 | + from HTMLParser import HTMLParser |
| 20 | + |
16 | 21 |
|
17 | 22 | RADIO_CHOICES = (
|
18 | 23 | ('1', 'Radio 1'),
|
@@ -47,10 +52,15 @@ class TestForm(forms.Form):
|
47 | 52 | )
|
48 | 53 | password = forms.CharField(widget=forms.PasswordInput)
|
49 | 54 | message = forms.CharField(required=False, help_text='<i>my_help_text</i>')
|
50 |
| - sender = forms.EmailField(label='Sender © unicode') |
| 55 | + sender = forms.EmailField( |
| 56 | + label='Sender © unicode', |
| 57 | + help_text='E.g., "[email protected]"') |
51 | 58 | secret = forms.CharField(initial=42, widget=forms.HiddenInput)
|
52 | 59 | cc_myself = forms.BooleanField(
|
53 |
| - required=False, help_text='You will get a copy in your mailbox.') |
| 60 | + required=False, |
| 61 | + help_text='cc stands for "carbon copy." ' |
| 62 | + 'You will get a copy in your mailbox.' |
| 63 | + ) |
54 | 64 | select1 = forms.ChoiceField(choices=RADIO_CHOICES)
|
55 | 65 | select2 = forms.MultipleChoiceField(
|
56 | 66 | choices=RADIO_CHOICES,
|
@@ -136,6 +146,23 @@ def render_field(field, **context_args):
|
136 | 146 | return render_template('{% bootstrap_field field %}', **context_args)
|
137 | 147 |
|
138 | 148 |
|
| 149 | +def get_title_from_html(html): |
| 150 | + class GetTitleParser(HTMLParser): |
| 151 | + def __init__(self): |
| 152 | + HTMLParser.__init__(self) |
| 153 | + self.title = None |
| 154 | + |
| 155 | + def handle_starttag(self, tag, attrs): |
| 156 | + for attr, value in attrs: |
| 157 | + if attr == 'title': |
| 158 | + self.title = value |
| 159 | + |
| 160 | + parser = GetTitleParser() |
| 161 | + parser.feed(html) |
| 162 | + |
| 163 | + return parser.title |
| 164 | + |
| 165 | + |
139 | 166 | class SettingsTest(TestCase):
|
140 | 167 | def test_settings(self):
|
141 | 168 | from .bootstrap import BOOTSTRAP3
|
@@ -265,6 +292,14 @@ def test_show_help(self):
|
265 | 292 | res = render_template('{% bootstrap_field form.subject show_help=0 %}')
|
266 | 293 | self.assertNotIn('my_help_text', res)
|
267 | 294 |
|
| 295 | + def test_help_with_quotes(self): |
| 296 | + # Checkboxes get special handling, so test a checkbox and something else |
| 297 | + res = render_form_field('sender') |
| 298 | + self.assertEqual(get_title_from_html(res), TestForm.base_fields['sender'].help_text) |
| 299 | + |
| 300 | + res = render_form_field('cc_myself') |
| 301 | + self.assertEqual(get_title_from_html(res), TestForm.base_fields['cc_myself'].help_text) |
| 302 | + |
268 | 303 | def test_subject(self):
|
269 | 304 | res = render_form_field('subject')
|
270 | 305 | self.assertIn('type="text"', res)
|
|
0 commit comments