pub trait EncodingSupport {
type Encoding;
fn from_label(ascii_label: &[u8]) -> Option<Self::Encoding>;
fn utf8() -> Self::Encoding;
fn is_utf16_be_or_le(encoding: &Self::Encoding) -> bool;
}
pub fn stylesheet_encoding<E>(css: &[u8], protocol_encoding_label: Option<&[u8]>,
environment_encoding: Option<E::Encoding>)
-> E::Encoding
where E: EncodingSupport {
match protocol_encoding_label {
None => (),
Some(label) => match E::from_label(label) {
None => (),
Some(protocol_encoding) => return protocol_encoding
}
}
let prefix = b"@charset \"";
if css.starts_with(prefix) {
let rest = &css[prefix.len()..];
match rest.iter().position(|&b| b == b'"') {
None => (),
Some(label_length) => if rest[label_length..].starts_with(b"\";") {
let label = &rest[..label_length];
match E::from_label(label) {
None => (),
Some(charset_encoding) => if E::is_utf16_be_or_le(&charset_encoding) {
return E::utf8()
} else {
return charset_encoding
}
}
}
}
}
environment_encoding.unwrap_or_else(E::utf8)
}