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.textfrom detection rather than re-decoding the bytes yourself
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| BOM appears in decoded text | Manual decode instead of detect_encoding() | Detection strips BOM bytes from result.text |
| Label comparison fails | Alias vs canonical name mismatch | Normalize with get_canonical_name() |
UnsupportedEncodingError raised | Input encoding outside the registry | Handle 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/Method | Description |
|---|---|
detect_encoding | Detect encoding and decode bytes |
EncodingDetectionResult | Detection outcome object |
sniff_bom | Byte-order-mark inspection |
prescan_meta_charset | Standards meta-prescan |
decode_bytes | Decode with a chosen encoding |
get_canonical_name | Canonicalize an encoding label |