Skip to content

Commit

Permalink
Merge pull request #583 from azmeuk/issue-581
Browse files Browse the repository at this point in the history
Ascending compatibility with markupsafe
  • Loading branch information
davidism committed Apr 22, 2020
2 parents 55009f7 + aa54aee commit 4f4f079
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Unreleased

- Fixed a bug when :class:`~wtforms.fields.core.SelectField` choices
is ``None``. :issue:`572, 585`
- Restored :class:`~widgets.core.HTMLString` and
:func:`~widgets.core.escape_html` as aliases for MarkupSafe
functions. Their use shows a ``DeprecationWarning``. :issue:`581`,
:pr:`583`


Version 2.3.0
Expand Down
21 changes: 21 additions & 0 deletions tests/deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import warnings
import unittest

from wtforms.widgets import HTMLString, escape_html
import markupsafe


class DeprecationTest(unittest.TestCase):
def test_htmlstring(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.assertEqual(markupsafe.Markup("foobar"), HTMLString("foobar"))
self.assertEqual(len(w), 1)
assert issubclass(w[-1].category, DeprecationWarning)

def test_escape(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.assertEqual(markupsafe.escape("foobar"), escape_html("foobar"))
self.assertEqual(len(w), 1)
assert issubclass(w[-1].category, DeprecationWarning)
2 changes: 1 addition & 1 deletion tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from unittest import defaultTestLoader, TextTestRunner, TestSuite

TESTS = ('form', 'fields', 'validators', 'widgets', 'webob_wrapper', 'csrf', 'ext_csrf', 'i18n')
TESTS = ('form', 'fields', 'validators', 'widgets', 'webob_wrapper', 'csrf', 'ext_csrf', 'i18n', 'deprecations')

OPTIONAL_TESTS = ('ext_django.tests', 'ext_sqlalchemy', 'ext_dateutil', 'locale_babel')

Expand Down
24 changes: 23 additions & 1 deletion wtforms/widgets/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@
from markupsafe import escape, Markup

from wtforms.compat import text_type, iteritems
import warnings


__all__ = (
'CheckboxInput', 'FileInput', 'HiddenInput', 'ListWidget', 'PasswordInput',
'RadioInput', 'Select', 'SubmitInput', 'TableWidget', 'TextArea',
'TextInput', 'Option'
'TextInput', 'Option', 'HTMLString', 'escape_html',
)


def HTMLString(*args, **kwargs):
warnings.warn(
"'HTMLString' will be removed in WTForms 3.0. Use"
" 'markupsafe.Markup' instead.",
DeprecationWarning,
stacklevel=2,
)
return Markup(*args, **kwargs)


def escape_html(*args, **kwargs):
warnings.warn(
"'escape_html' will be removed in WTForms 3.0. Use"
" 'markupsafe.escape' instead.",
DeprecationWarning,
stacklevel=2,
)
return escape(*args, **kwargs)


def html_params(**kwargs):
"""
Generate HTML attribute syntax from inputted keyword arguments.
Expand Down

0 comments on commit 4f4f079

Please sign in to comment.