BigPizzaV3/CodexPlusPlus · error

theme.css 选择器或规则语法无效

Error message

theme.css 选择器或规则语法无效

What it means

parse_safe_css parses only a flat rule list. For each rule it locates the first '{', and rejects the rule if the selector is empty, contains ',', '@', ';' or '}', or if after the opening brace another '{' appears before the matching '}' (i.e. nested blocks like @media or @supports). Any of these bails with 'theme.css 选择器或规则语法无效'.

Solutions

  1. Unwrap any @media/@supports/@keyframes blocks — list the inner rules flat at top level
  2. Split comma-grouped selectors into separate rules, one [data-ds-part] selector each
  3. Count braces to ensure every '{' is matched by exactly one '}' with no nesting, and that a non-empty selector precedes each '{'

Example fix

// before
@media (prefers-color-scheme: dark) {
  [data-ds-part="main"] { background-color: #111; }
}
// after
[data-ds-part="main"] { background-color: #111111; }
Defensive patterns

Strategy: validation

Validate before calling

fn is_flat_rule(css: &str) -> bool {
    let mut depth = 0i32;
    for c in css.chars() {
        match c {
            '{' => { depth += 1; if depth > 1 { return false; } }
            '}' => depth -= 1,
            _ => {}
        }
        if depth < 0 { return false; }
    }
    depth == 0 && !css.contains('@') && !css.contains(',')
}

Try / catch

match validate_safe_css(&css) {
    Err(e) if e.to_string().contains("选择器或规则语法无效") => Err("flatten @media blocks and split comma selectors".into()),
    other => other,
}

Prevention

When it happens

Trigger: Wrapping rules in @media or @supports blocks; grouping selectors with a comma (`[data-ds-part="main"], [data-ds-part="sidebar"]`); keyframes; an unbalanced or stray '}' or '{'; an empty selector before '{'.

Common situations: Authors writing ordinary CSS habits (media queries for dark mode, comma-grouped selectors) into the restricted Safe CSS format; minifiers outputting at-rules; manual edits leaving a dangling brace.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/d470c90d2dbabef0. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-core/src/dream_skin_package.rs:321

    let mut rest = css;
    loop {
        rest = rest.trim_start_matches(|character: char| {
            character.is_ascii_whitespace() || character == '\u{000c}'
        });
        if rest.is_empty() {
            break;
        }
        let open = rest.find('{').context("theme.css 规则缺少开始括号")?;
        let selector = rest[..open].trim();
        if selector.is_empty()
            || selector.contains([',', '@', ';', '}'])
            || rest[open + 1..].find('{').is_some_and(|nested| {
                rest[open + 1..]
                    .find('}')
                    .is_none_or(|close| nested < close)
            })
        {
            bail!("theme.css 选择器或规则语法无效");
        }
        let close = open
            + 1
            + rest[open + 1..]
                .find('}')
                .context("theme.css 规则缺少结束括号")?;
        let Some(after_prefix) = selector.strip_prefix("[data-ds-part=\"") else {
            bail!("theme.css 只能选择 Skin API 注册的 data-ds-part");
        };
        let (part, suffix) = after_prefix
            .split_once("\"]")
            .context("theme.css data-ds-part 语法无效")?;
        if !allowed_parts.contains(&part)
            || (!suffix.is_empty() && suffix != ":hover" && suffix != ":focus-visible")
        {
            bail!("theme.css 使用了未注册的 Skin API part 或状态");
        }

View on GitHub (pinned to b1ed92e5e4)