Skip to main content

parse_bytes

Function parse_bytes 

Source
pub fn parse_bytes(bytes: &[u8], options: Options) -> Result<Parsed, ParseError>
Expand description

Parses a page given as bytes, as a browser would: sniffs its encoding (sniff), decodes it and parses it.

When the encoding was only a guess and the parser meets a <meta> declaring another one (past the first 1,024 bytes, say), the spec has the browser start again in the declared encoding. Here that’s a second decode and parse of the same bytes, once at most, since the encoding is certain after it. The time budget covers both parses together.

use parsec_encoding::{Options, parse_bytes};
let page = b"<meta charset=shift_jis><title>\x93\xfa\x96\x7b</title>";
let parsed = parse_bytes(page, Options::default()).unwrap();
assert_eq!(parsed.encoding, encoding_rs::SHIFT_JIS);
assert_eq!(parsed.document.text(parsed.document.root()), "日本");