summaryrefslogtreecommitdiff
path: root/src/lxml/html
diff options
context:
space:
mode:
authorscoder <stefan_ml@behnel.de>2017-03-18 09:56:26 +0100
committerGitHub <noreply@github.com>2017-03-18 09:56:26 +0100
commit91e1d273549e29510a8f431ba561381514600575 (patch)
treee5773b4bd09ddffcbdbff42756fb19eb7b14163d /src/lxml/html
parent2802687228fd26c0d87d3aba76fb16570d448a25 (diff)
parent62b8c345779bdfb8b261a6f7de3287f4fe800bb6 (diff)
downloadpython-lxml-91e1d273549e29510a8f431ba561381514600575.tar.gz
Merge pull request #233 from ondergetekende/1673355
Fix LP1673355
Diffstat (limited to 'src/lxml/html')
-rw-r--r--src/lxml/html/html5parser.py9
-rw-r--r--src/lxml/html/tests/test_html5parser.py6
2 files changed, 14 insertions, 1 deletions
diff --git a/src/lxml/html/html5parser.py b/src/lxml/html/html5parser.py
index 7188c7ea..ba9d41b3 100644
--- a/src/lxml/html/html5parser.py
+++ b/src/lxml/html/html5parser.py
@@ -147,7 +147,14 @@ def fromstring(html, guess_charset=True, parser=None):
guess_charset=guess_charset)
# document starts with doctype or <html>, full document!
- start = html[:50].lstrip().lower()
+ start = html[:50]
+ if hasattr(start, 'decode'):
+ # In python3, we may have been presented with a bytes object.
+ # Decode in ascii, that also covers latin-1 and utf-8 for the
+ # characters we need
+ start = start.decode('ascii', 'replace')
+
+ start = start.lstrip().lower()
if start.startswith('<html') or start.startswith('<!doctype'):
return doc
diff --git a/src/lxml/html/tests/test_html5parser.py b/src/lxml/html/tests/test_html5parser.py
index fad45dc4..667c68d2 100644
--- a/src/lxml/html/tests/test_html5parser.py
+++ b/src/lxml/html/tests/test_html5parser.py
@@ -233,6 +233,12 @@ class Test_fromstring(unittest.TestCase):
self.assertEqual(self.call_it('<!DOCTYPE html>', parser=parser),
'the doc')
+ def test_returns_whole_doc_if_input_is_encoded(self):
+ parser = DummyParser(root='the doc')
+ input = '<!DOCTYPE html>'.encode('ascii')
+ self.assertEqual(self.call_it(input, parser=parser),
+ 'the doc')
+
def test_returns_whole_doc_if_head_not_empty(self, use_ns=True):
E = HTMLElementMaker(namespaceHTMLElements=use_ns)
root = E.html(E.head(E.title()))