Skip to content

Commit

Permalink
Deprecate the sanitizer and recommend Bleach (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnedders authored Jun 21, 2020
1 parent 7e52b16 commit 8df6cc9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
13 changes: 10 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ Breaking changes:
* Drop support for Python 3.3. (#358)
* Drop support for Python 3.4. (#421)

Deprecations:

* Deprecate the ``html5lib`` sanitizer (``html5lib.serialize(sanitize=True)`` and
``html5lib.filters.sanitizer``). We recommend users migrate to `Bleach
<https://github.com/mozilla/bleach>`. Please let us know if Bleach doesn't suffice for your
use. (#443)

Other changes:

* Try to import from `collections.abc` to remove DeprecationWarning and ensure
`html5lib` keeps working in future Python versions. (#403)
* Drop optional `datrie` dependency. (#442)
* Try to import from ``collections.abc`` to remove DeprecationWarning and ensure
``html5lib`` keeps working in future Python versions. (#403)
* Drop optional ``datrie`` dependency. (#442)


1.0.1
Expand Down
20 changes: 20 additions & 0 deletions html5lib/filters/sanitizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"""Deprecated from html5lib 1.1.
See `here <https://github.com/html5lib/html5lib-python/issues/443>`_ for
information about its deprecation; `Bleach <https://github.com/mozilla/bleach>`_
is recommended as a replacement. Please let us know in the aforementioned issue
if Bleach is unsuitable for your needs.
"""
from __future__ import absolute_import, division, unicode_literals

import re
import warnings
from xml.sax.saxutils import escape, unescape

from six.moves import urllib_parse as urlparse
Expand All @@ -11,6 +20,14 @@
__all__ = ["Filter"]


_deprecation_msg = (
"html5lib's sanitizer is deprecated; see " +
"https://github.com/html5lib/html5lib-python/issues/443 and please let " +
"us know if Bleach is unsuitable for your needs"
)

warnings.warn(_deprecation_msg, DeprecationWarning)

allowed_elements = frozenset((
(namespaces['html'], 'a'),
(namespaces['html'], 'abbr'),
Expand Down Expand Up @@ -750,6 +767,9 @@ def __init__(self,
"""
super(Filter, self).__init__(source)

warnings.warn(_deprecation_msg, DeprecationWarning)

self.allowed_elements = allowed_elements
self.allowed_attributes = allowed_attributes
self.allowed_css_properties = allowed_css_properties
Expand Down
17 changes: 9 additions & 8 deletions html5lib/tests/sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ def runtest(self):
expected = self.test["output"]

parsed = parseFragment(input)
serialized = serialize(parsed,
sanitize=True,
omit_optional_tags=False,
use_trailing_solidus=True,
space_before_trailing_solidus=False,
quote_attr_values="always",
quote_char="'",
alphabetical_attributes=True)
with pytest.deprecated_call():
serialized = serialize(parsed,
sanitize=True,
omit_optional_tags=False,
use_trailing_solidus=True,
space_before_trailing_solidus=False,
quote_attr_values="always",
quote_char="'",
alphabetical_attributes=True)
errorMsg = "\n".join(["\n\nInput:", input,
"\nExpected:", expected,
"\nReceived:", serialized])
Expand Down
17 changes: 9 additions & 8 deletions html5lib/tests/test_sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

def sanitize_html(stream):
parsed = parseFragment(stream)
serialized = serialize(parsed,
sanitize=True,
omit_optional_tags=False,
use_trailing_solidus=True,
space_before_trailing_solidus=False,
quote_attr_values="always",
quote_char='"',
alphabetical_attributes=True)
with pytest.deprecated_call():
serialized = serialize(parsed,
sanitize=True,
omit_optional_tags=False,
use_trailing_solidus=True,
space_before_trailing_solidus=False,
quote_attr_values="always",
quote_char='"',
alphabetical_attributes=True)
return serialized


Expand Down

0 comments on commit 8df6cc9

Please sign in to comment.