Loading django/forms/widgets.py +4 −3 Original line number Diff line number Diff line Loading @@ -330,12 +330,13 @@ class ClearableFileInput(FileInput): if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = (u'<a href="%s">%s</a>' % (value.url, value)) % (escape(value.url), escape(force_unicode(value)))) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = checkbox_name substitutions['clear_checkbox_id'] = checkbox_id substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id) substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id}) substitutions['clear_template'] = self.template_with_clear % substitutions Loading tests/regressiontests/forms/tests/widgets.py +22 −0 Original line number Diff line number Diff line Loading @@ -1086,6 +1086,28 @@ class ClearableFileInputTests(TestCase): self.assertEqual(widget.render('myfile', FakeFieldFile()), u'Currently: <a href="something">something</a> <input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> <label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />') def test_html_escaped(self): """ A ClearableFileInput should escape name, filename and URL when rendering HTML. Refs #15182. """ class StrangeFieldFile(object): url = "something?chapter=1§=2©=3&lang=en" def __unicode__(self): return u'''something<div onclick="alert('oops')">.jpg''' widget = ClearableFileInput() field = StrangeFieldFile() output = widget.render('my<div>file', field) self.assertFalse(field.url in output) self.assertTrue(u'href="something?chapter=1&sect=2&copy=3&lang=en"' in output) self.assertFalse(unicode(field) in output) self.assertTrue(u'something<div onclick="alert('oops')">.jpg' in output) self.assertTrue(u'my<div>file' in output) self.assertFalse(u'my<div>file' in output) def test_clear_input_renders_only_if_not_required(self): """ A ClearableFileInput with is_required=False does not render a clear Loading Loading
django/forms/widgets.py +4 −3 Original line number Diff line number Diff line Loading @@ -330,12 +330,13 @@ class ClearableFileInput(FileInput): if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = (u'<a href="%s">%s</a>' % (value.url, value)) % (escape(value.url), escape(force_unicode(value)))) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = checkbox_name substitutions['clear_checkbox_id'] = checkbox_id substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id) substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id}) substitutions['clear_template'] = self.template_with_clear % substitutions Loading
tests/regressiontests/forms/tests/widgets.py +22 −0 Original line number Diff line number Diff line Loading @@ -1086,6 +1086,28 @@ class ClearableFileInputTests(TestCase): self.assertEqual(widget.render('myfile', FakeFieldFile()), u'Currently: <a href="something">something</a> <input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> <label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />') def test_html_escaped(self): """ A ClearableFileInput should escape name, filename and URL when rendering HTML. Refs #15182. """ class StrangeFieldFile(object): url = "something?chapter=1§=2©=3&lang=en" def __unicode__(self): return u'''something<div onclick="alert('oops')">.jpg''' widget = ClearableFileInput() field = StrangeFieldFile() output = widget.render('my<div>file', field) self.assertFalse(field.url in output) self.assertTrue(u'href="something?chapter=1&sect=2&copy=3&lang=en"' in output) self.assertFalse(unicode(field) in output) self.assertTrue(u'something<div onclick="alert('oops')">.jpg' in output) self.assertTrue(u'my<div>file' in output) self.assertFalse(u'my<div>file' in output) def test_clear_input_renders_only_if_not_required(self): """ A ClearableFileInput with is_required=False does not render a clear Loading