Skip to main content

parsec_css/
bytes.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! A stylesheet's bytes into text, CSS Syntax §3.2: a byte order mark,
5//! then the charset HTTP gave, then an `@charset "…";` at the very start,
6//! then the encoding of the document that linked it, then UTF-8.
7
8use std::borrow::Cow;
9
10use encoding_rs::{Encoding, UTF_8, UTF_16BE, UTF_16LE};
11
12/// The fallback encoding: what the bytes are decoded with unless they
13/// start with a byte order mark. `protocol` is a charset label from HTTP
14/// or the like; `environment` is the linking document's encoding.
15pub fn fallback_encoding(
16    css: &[u8],
17    protocol: Option<&str>,
18    environment: Option<&'static Encoding>,
19) -> &'static Encoding {
20    if let Some(e) = protocol.and_then(|label| Encoding::for_label(label.as_bytes())) {
21        return e;
22    }
23    // Exactly `@charset "label";`, in the first 1,024 bytes.
24    let head = &css[..css.len().min(1024)];
25    if let Some(rest) = head.strip_prefix(b"@charset \"") {
26        let len = rest
27            .iter()
28            .take_while(|&&b| matches!(b, 0x00..=0x21 | 0x23..=0x7f))
29            .count();
30        if rest[len..].starts_with(b"\";") {
31            match Encoding::for_label(&rest[..len]) {
32                Some(e) if e == UTF_16BE || e == UTF_16LE => return UTF_8,
33                Some(e) => return e,
34                None => {}
35            }
36        }
37    }
38    environment.unwrap_or(UTF_8)
39}
40
41/// Decodes a stylesheet's bytes, returning the text and the encoding it
42/// was in. Malformed bytes become U+FFFD.
43///
44/// ```
45/// let (text, encoding) = parsec_css::decode_stylesheet(b"@charset \"iso-8859-5\"; @\xe9", None, None);
46/// assert_eq!((&*text, encoding.name()), ("@charset \"iso-8859-5\"; @щ", "ISO-8859-5"));
47/// ```
48pub fn decode_stylesheet<'b>(
49    css: &'b [u8],
50    protocol: Option<&str>,
51    environment: Option<&'static Encoding>,
52) -> (Cow<'b, str>, &'static Encoding) {
53    let (text, encoding, _) = fallback_encoding(css, protocol, environment).decode(css);
54    (text, encoding)
55}