Skip to content

Add a usedEncoding method to HTML5Parser, fix #121 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions html5lib/html5parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ def reset(self):

self.framesetOK = True

@property
def documentEncoding(self):
"""The name of the character encoding
that was used to decode the input stream,
or :obj:`None` if that is not determined yet.

"""
if not hasattr(self, 'tokenizer'):
return None
return self.tokenizer.stream.documentEncoding

def isHTMLIntegrationPoint(self, element):
if (element.name == "annotation-xml" and
element.namespace == namespaces["mathml"]):
Expand Down
6 changes: 6 additions & 0 deletions html5lib/inputstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class HTMLUnicodeInputStream(object):

_defaultChunkSize = 10240

documentEncoding = None # No encoding involved for Unicode input.

def __init__(self, source):
"""Initialises the HTMLInputStream.

Expand Down Expand Up @@ -413,6 +415,10 @@ def __init__(self, source, encoding=None, parseMeta=True, chardet=True):
# Call superclass
self.reset()

@property
def documentEncoding(self):
return self.charEncoding[0]

def reset(self):
self.dataStream = codecs.getreader(self.charEncoding[0])(self.rawStream,
'replace')
Expand Down
25 changes: 24 additions & 1 deletion html5lib/tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,35 @@ def test_codec_name_d(self):
self.assertEqual(inputstream.codecName("ISO_8859--1"), "windows-1252")


def test_unicode_input_encoding():
p = HTMLParser()
assert p.documentEncoding is None
p.parse(b'<meta charset=latin2>', useChardet=False)
assert p.documentEncoding == 'iso8859-2'

p = HTMLParser()
assert p.documentEncoding is None
p.parse('<meta charset=latin2>')
assert p.documentEncoding is None

p = HTMLParser()
assert p.documentEncoding is None
try:
p.parse('<meta charset=latin2>', encoding='latin3')
except TypeError:
pass
else:
assert 0, 'Expected TypeError'
assert p.documentEncoding is None


def runParserEncodingTest(data, encoding):
p = HTMLParser()
assert p.documentEncoding is None
p.parse(data, useChardet=False)
encoding = encoding.lower().decode("ascii")

assert encoding == p.tokenizer.stream.charEncoding[0], errorMessage(data, encoding, p.tokenizer.stream.charEncoding[0])
assert encoding == p.documentEncoding, errorMessage(data, encoding, p.documentEncoding)


def runPreScanEncodingTest(data, encoding):
Expand Down