PureHTML.Encoding (PureHTML v0.4.0)

View Source

WHATWG encoding sniffing for HTML byte streams.

Detects the character encoding of an HTML document by examining:

  1. BOM (Byte Order Mark)
  2. <meta charset> declarations
  3. <meta http-equiv="content-type"> declarations

Uses binary pattern matching for efficient byte-level scanning.

Summary

Functions

Sniffs the encoding of an HTML byte stream.

Functions

sniff(bytes, opts \\ [])

@spec sniff(
  binary(),
  keyword()
) :: String.t()

Sniffs the encoding of an HTML byte stream.

Returns the detected encoding name as a lowercase string. Defaults to "windows-1252" if no encoding is detected.

Options

  • :transport_encoding - encoding from HTTP Content-Type header (takes precedence)

Examples

iex> PureHTML.Encoding.sniff(<<0xEF, 0xBB, 0xBF, "<html>">>)
"utf-8"

iex> PureHTML.Encoding.sniff("<meta charset='utf-8'>")
"utf-8"

iex> PureHTML.Encoding.sniff("<html>")
"windows-1252"