Skip to content

Commit 808d102

Browse files
SimonSapingsnedders
authored andcommitted
Add a documentEncoding property to HTML5Parser, fix #121.
1 parent 3c3dd75 commit 808d102

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

html5lib/html5parser.py

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ def reset(self):
129129

130130
self.framesetOK = True
131131

132+
@property
133+
def documentEncoding(self):
134+
"""The name of the character encoding
135+
that was used to decode the input stream,
136+
or :obj:`None` if that is not determined yet.
137+
138+
"""
139+
if not hasattr(self, 'tokenizer'):
140+
return None
141+
return self.tokenizer.stream.charEncoding[0]
142+
132143
def isHTMLIntegrationPoint(self, element):
133144
if (element.name == "annotation-xml" and
134145
element.namespace == namespaces["mathml"]):

html5lib/tests/test_encoding.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def test_codec_name_d(self):
2828

2929
def runParserEncodingTest(data, encoding):
3030
p = HTMLParser()
31+
assert p.documentEncoding is None
3132
p.parse(data, useChardet=False)
3233
encoding = encoding.lower().decode("ascii")
3334

34-
assert encoding == p.tokenizer.stream.charEncoding[0], errorMessage(data, encoding, p.tokenizer.stream.charEncoding[0])
35+
assert encoding == p.documentEncoding, errorMessage(data, encoding, p.documentEncoding)
3536

3637

3738
def runPreScanEncodingTest(data, encoding):

0 commit comments

Comments
 (0)