Encoding Detection

Encoding Detection

HTML arrives as bytes, and the encoding layer answers the first question of any pipeline: which encoding, and what text? Detection follows the standards approach — byte-order marks first, then in-content declarations.


Detecting from Bytes

detect_encoding() sniffs a byte stream and returns an EncodingDetectionResult carrying the detected encoding, a confidence level, and the decoded text. A UTF-8 BOM detects as certain, and BOM bytes are stripped from the decoded output:

from aspose_html.encoding.detection import detect_encoding
result = detect_encoding(b"\xef\xbb\xbf<p>x</p>")
print(result.encoding)
print(result.confidence)
print(result.text)

The Detection Toolkit

sniff_bom() inspects byte-order marks alone; prescan_meta_charset() applies the standards meta-prescan for in-document charset declarations; decode_bytes() performs the decode once an encoding is chosen. Unsupported encodings raise the typed UnsupportedEncodingError.

Canonical Names

Encoding labels vary in the wild — latin1, iso-8859-1, and us-ascii all mean windows-1252 per the encoding standard. get_canonical_name() maps any registered label to its canonical name.


Tips and Best Practices

  • Trust the BOM: when present, it is definitive and the detector reports certain confidence
  • Normalize labels through get_canonical_name() before comparing encodings
  • Use result.text from detection rather than re-decoding the bytes yourself

Common Issues

IssueCauseFix
BOM appears in decoded textManual decode instead of detect_encoding()Detection strips BOM bytes from result.text
Label comparison failsAlias vs canonical name mismatchNormalize with get_canonical_name()
UnsupportedEncodingError raisedInput encoding outside the registryHandle the typed error; re-encode upstream

FAQ

What does detection return?

An EncodingDetectionResult with encoding, confidence, and decoded text.

Are legacy labels handled?

Yes — the alias table maps labels like latin1 and us-ascii to their canonical encodings.

Does detection read meta tags?

prescan_meta_charset() implements the standards meta-prescan and participates in detection.


API Reference Summary

Class/MethodDescription
detect_encodingDetect encoding and decode bytes
EncodingDetectionResultDetection outcome object
sniff_bomByte-order-mark inspection
prescan_meta_charsetStandards meta-prescan
decode_bytesDecode with a chosen encoding
get_canonical_nameCanonicalize an encoding label

See Also