Skip to content

Commit f681002

Browse files
committed
add support for html5lib below 0.95, isso-comments#168
1 parent c3c519a commit f681002

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

isso/tests/test_html.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_github_flavoured_markdown(self):
6262
print("Hello, World")
6363
</code></pre>""")
6464

65-
@unittest.skipIf(html.html5lib_version == "0.95", "backport")
65+
@unittest.skipIf(html.HTML5LIB_VERSION <= html.HTML5LIB_SIMPLETREE, "backport")
6666
def test_sanitizer(self):
6767
sanitizer = html.Sanitizer(elements=[], attributes=[])
6868
examples = [
@@ -75,7 +75,7 @@ def test_sanitizer(self):
7575
for (input, expected) in examples:
7676
self.assertEqual(html.sanitize(sanitizer, input), expected)
7777

78-
@unittest.skipIf(html.html5lib_version == "0.95", "backport")
78+
@unittest.skipIf(html.HTML5LIB_VERSION <= html.HTML5LIB_SIMPLETREE, "backport")
7979
def test_sanitizer_extensions(self):
8080
sanitizer = html.Sanitizer(elements=["img"], attributes=["src"])
8181
examples = [

isso/utils/html.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
from __future__ import unicode_literals
44

5-
import pkg_resources
65
import operator
6+
import pkg_resources
7+
8+
from distutils.version import LooseVersion as Version
9+
10+
HTML5LIB_VERSION = Version(pkg_resources.get_distribution("html5lib").version)
11+
HTML5LIB_SIMPLETREE = Version("0.9.5")
712

813
from isso.compat import reduce
914

1015
import html5lib
11-
html5lib_version = pkg_resources.get_distribution("html5lib").version
12-
1316
from html5lib.sanitizer import HTMLSanitizer
1417
from html5lib.serializer import HTMLSerializer
1518

@@ -45,7 +48,11 @@ def sanitize(tokenizer, document):
4548
parser = html5lib.HTMLParser(tokenizer=tokenizer)
4649
domtree = parser.parseFragment(document)
4750

48-
builder = "simpletree" if html5lib_version == "0.95" else "etree"
51+
if HTML5LIB_VERSION > HTML5LIB_SIMPLETREE:
52+
builder = "etree"
53+
else:
54+
builder = "simpletree"
55+
4956
stream = html5lib.treewalkers.getTreeWalker(builder)(domtree)
5057
serializer = HTMLSerializer(quote_attr_values=True, omit_optional_tags=False)
5158

0 commit comments

Comments
 (0)