Skip to content

Commit c8e024b

Browse files
author
Marc DM
committed
fixed the unicode/str/six bug, but the test that proves it is in an alien file
1 parent 81b97d5 commit c8e024b

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

html5lib/serializer/htmlserializer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import absolute_import, division, unicode_literals
2-
from six import text_type
2+
from six import text_type, string_types
33

44
import gettext
55
_ = gettext.gettext
@@ -154,14 +154,14 @@ def __init__(self, **kwargs):
154154
self.strict = False
155155

156156
def encode(self, string):
157-
assert(isinstance(string, text_type))
157+
assert(isinstance(string, string_types))
158158
if self.encoding:
159159
return string.encode(self.encoding, unicode_encode_errors)
160160
else:
161161
return string
162162

163163
def encodeStrict(self, string):
164-
assert(isinstance(string, text_type))
164+
assert(isinstance(string, string_types))
165165
if self.encoding:
166166
return string.encode(self.encoding, "strict")
167167
else:

html5lib/tests/test_six_encoding.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
from html5lib import html5parser, treewalkers, serializer
3+
from nose.tools import eq_
4+
5+
def test_treewalker6():
6+
"""Str/Unicode mix. If str attrs added to tree"""
7+
8+
text = '<a href="http://example.com">Example</a>'
9+
end_text = '<a href="http://example.com" class="test123">Example</a>'
10+
parser = html5parser.HTMLParser()
11+
walker = treewalkers.getTreeWalker('etree')
12+
serializr = serializer.HTMLSerializer(quote_attr_values=True)
13+
domtree = parser.parseFragment(text)
14+
15+
# at this point domtree should be a DOCUMENT_FRAGMENT
16+
domtree[0].set('class', 'test123')
17+
eq_(end_text, serializr.render(walker(domtree)))

html5lib/treewalkers/_base.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import absolute_import, division, unicode_literals
2-
from six import text_type
2+
from six import text_type, string_types
33

44
import gettext
55
_ = gettext.gettext
@@ -19,43 +19,43 @@ def error(self, msg):
1919
return {"type": "SerializeError", "data": msg}
2020

2121
def emptyTag(self, namespace, name, attrs, hasChildren=False):
22-
assert namespace is None or isinstance(namespace, text_type), type(namespace)
23-
assert isinstance(name, text_type), type(name)
24-
assert all((namespace is None or isinstance(namespace, text_type)) and
25-
isinstance(name, text_type) and
26-
isinstance(value, text_type)
22+
assert namespace is None or isinstance(namespace, string_types), type(namespace)
23+
assert isinstance(name, string_types), type(name)
24+
assert all((namespace is None or isinstance(namespace, string_types)) and
25+
isinstance(name, string_types) and
26+
isinstance(value, string_types)
2727
for (namespace, name), value in attrs.items())
2828

29-
yield {"type": "EmptyTag", "name": name,
30-
"namespace": namespace,
29+
yield {"type": "EmptyTag", "name": text_type(name),
30+
"namespace": text_type(namespace),
3131
"data": attrs}
3232
if hasChildren:
3333
yield self.error(_("Void element has children"))
3434

3535
def startTag(self, namespace, name, attrs):
36-
assert namespace is None or isinstance(namespace, text_type), type(namespace)
37-
assert isinstance(name, text_type), type(name)
38-
assert all((namespace is None or isinstance(namespace, text_type)) and
39-
isinstance(name, text_type) and
40-
isinstance(value, text_type)
36+
assert namespace is None or isinstance(namespace, string_types), type(namespace)
37+
assert isinstance(name, string_types), type(name)
38+
assert all((namespace is None or isinstance(namespace, string_types)) and
39+
isinstance(name, string_types) and
40+
isinstance(value, string_types)
4141
for (namespace, name), value in attrs.items())
4242

4343
return {"type": "StartTag",
44-
"name": name,
45-
"namespace": namespace,
44+
"name": text_type(name),
45+
"namespace": text_type(namespace),
4646
"data": attrs}
4747

4848
def endTag(self, namespace, name):
49-
assert namespace is None or isinstance(namespace, text_type), type(namespace)
50-
assert isinstance(name, text_type), type(namespace)
49+
assert namespace is None or isinstance(namespace, string_types), type(namespace)
50+
assert isinstance(name, string_types), type(namespace)
5151

5252
return {"type": "EndTag",
53-
"name": name,
54-
"namespace": namespace,
53+
"name": text_type(name),
54+
"namespace": text_type(namespace),
5555
"data": {}}
5656

5757
def text(self, data):
58-
assert isinstance(data, text_type), type(data)
58+
assert isinstance(data, string_types), type(data)
5959

6060
data = data
6161
middle = data.lstrip(spaceCharacters)
@@ -71,14 +71,14 @@ def text(self, data):
7171
yield {"type": "SpaceCharacters", "data": right}
7272

7373
def comment(self, data):
74-
assert isinstance(data, text_type), type(data)
74+
assert isinstance(data, string_types), type(data)
7575

7676
return {"type": "Comment", "data": data}
7777

7878
def doctype(self, name, publicId=None, systemId=None, correct=True):
79-
assert name is None or isinstance(name, text_type), type(name)
80-
assert publicId is None or isinstance(publicId, text_type), type(publicId)
81-
assert systemId is None or isinstance(systemId, text_type), type(systemId)
79+
assert name is None or isinstance(name, string_types), type(name)
80+
assert publicId is None or isinstance(publicId, string_types), type(publicId)
81+
assert systemId is None or isinstance(systemId, string_types), type(systemId)
8282

8383
return {"type": "Doctype",
8484
"name": name if name is not None else "",
@@ -87,7 +87,7 @@ def doctype(self, name, publicId=None, systemId=None, correct=True):
8787
"correct": correct}
8888

8989
def entity(self, name):
90-
assert isinstance(name, text_type), type(name)
90+
assert isinstance(name, string_types), type(name)
9191

9292
return {"type": "Entity", "name": name}
9393

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy