1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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,
{
if let Some(label) = protocol_encoding_label {
if let Some(protocol_encoding) = E::from_label(label) {
return protocol_encoding;
};
};
let prefix = b"@charset \"";
if css.starts_with(prefix) {
let rest = &css[prefix.len()..];
if let Some(label_length) = rest.iter().position(|&b| b == b'"') {
if rest[label_length..].starts_with(b"\";") {
let label = &rest[..label_length];
if let Some(charset_encoding) = E::from_label(label) {
if E::is_utf16_be_or_le(&charset_encoding) {
return E::utf8();
} else {
return charset_encoding;
}
}
}
}
}
environment_encoding.unwrap_or_else(E::utf8)
}