PureHTML.Encoding (PureHTML v0.5.0)

View Source

WHATWG encoding sniffing for HTML byte streams.

Detects the character encoding of an HTML document by examining, in order:

  1. BOM (Byte Order Mark)
  2. A transport encoding (HTTP Content-Type), if it is a supported label
  3. A prescan of the byte stream (<meta charset>, http-equiv, XML declaration)

The prescan runs to the end of the given binary, not the first 1024 bytes the standard encourages streaming user agents to stop at. The result is the Encoding Standard name of the encoding, lowercased.

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. Used only when it is a supported Encoding Standard label, and only after a BOM.

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"