{"record":{"id":"25526e66fd14523f","repo":"swc-project/swc","slug":"percentage-number-function-math-functions-or-i","errorCode":null,"errorMessage":"percentage, number, function (math functions) or ident (with 'none' value) token","messagePattern":"percentage, number, function \\(math functions\\) or ident \\(with 'none' value\\) token","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/swc_css_parser/src/parser/values_and_units/mod.rs","lineNumber":431,"sourceCode":"                                    Ok(Some(ComponentValue::Function(parser.parse()?)))\n                                }\n                                tok!(\"ident\") => {\n                                    is_legacy_syntax = false;\n\n                                    let ident: Box<Ident> = parser.parse()?;\n\n                                    if ident.value.eq_ignore_ascii_case(\"none\") {\n                                        Ok(Some(ComponentValue::Ident(ident)))\n                                    } else {\n                                        Err(Error::new(\n                                            ident.span,\n                                            ErrorKind::Expected(\"'none' value of an ident token\"),\n                                        ))\n                                    }\n                                }\n                                _ => {\n                                    if !has_variable_before {\n                                        Err(Error::new(\n                                            parser.input.cur_span(),\n                                            ErrorKind::Expected(\n                                                \"percentage, number, function (math functions) or \\\n                                                 ident (with 'none' value) token\",\n                                            ),\n                                        ))\n                                    } else {\n                                        Ok(None)\n                                    }\n                                }\n                            },\n                            &mut has_variable,\n                        )?;\n\n                        if let Some(percentage_or_number_or_none) = percentage_or_number_or_none {\n                            values.push(percentage_or_number_or_none);\n                        }\n","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_parser/src/parser/values_and_units/mod.rs#L413-L449","documentation":"Thrown by swc_css_parser when the FIRST channel of an rgb()/rgba() function starts with a token that is none of: percentage, number, math function (calc/min/max/...), or ident (crates/swc_css_parser/src/parser/values_and_units/mod.rs:429-441). The parser reports ErrorKind::Expected(\"percentage, number, function (math functions) or ident (with 'none' value) token\") at the current token's span. The fallback arm only errors when has_variable_before is false, i.e. when no var()/env()/constant() has appeared in this function yet.","triggerScenarios":"parse_string on CSS like 'rgb(#fff 0 0)' (hash token), 'rgb(\"x\" 0 0)' (string), 'rgb(, 0, 0)' (comma first), 'rgb(255px 0 0)' (dimension — not accepted for rgb channels), or 'rgb(/ 50%)'. All fall into the '_' arm at line 429 and fail at cur_span().","commonSituations":"Pasting hex colors into rgb(); CSS minifiers/design tools emitting dimensions where channels are expected; typos or truncated values after manual edits; generators emitting an empty first argument before a comma; older CSS round-tripped through preprocessors that wrote 'rgb(50%,0,0)' with stray units.","solutions":["Use the correct channel types: 'rgb(255 0 0)' or 'rgb(100% 0% 0%)'; convert hex to 'rgb()' channels or keep the hex form '#fff'.","Strip units/dimensions from channel values — rgb channels accept only plain numbers or percentages.","Wrap dynamic or foreign content in var(): 'rgb(var(--x) 0 0)' bypasses the strict token check.","Use the reported span (err.into_inner().0) to jump to the offending character in the stylesheet."],"exampleFix":"/* before */\ncolor: rgb(#fff 0 0);\ncolor: rgb(255px 0 0);\n\n/* after */\ncolor: #fff;\ncolor: rgb(255 0 0);\ncolor: rgb(100% 0% 0%);","handlingStrategy":"try-catch","validationCode":"fn rgb_channel_token_ok(arg: &str) -> bool {\n    let a = arg.trim();\n    !a.is_empty()\n        && (a.parse::<f64>().is_ok()\n            || a.ends_with('%')\n            || a.eq_ignore_ascii_case(\"none\")\n            || a.starts_with(\"var(\")\n            || a.starts_with(\"env(\")\n            || [\"calc(\",\"min(\",\"max(\",\"clamp(\"].contains(&a.to_ascii_lowercase().as_str()))\n}\n// reject '#fff', '255px', '\"x\"', '' before they reach the parser","typeGuard":"fn looks_like_rgb_channel(tok_text: &str) -> bool {\n    let t = tok_text.trim();\n    t.chars().next().map_or(false, |c| c.is_ascii_digit() || c == '.' || c == '-' || c == '+')\n        || t.ends_with('%')\n        || t.eq_ignore_ascii_case(\"none\")\n        || t.starts_with(\"var(\")\n}","tryCatchPattern":"match swc_css_parser::parse_string::<Stylesheet>(css, config) {\n    Ok(sheet) => sheet,\n    Err(err) => {\n        if matches!(&err.kind(), ErrorKind::Expected(m) if m.contains(\"percentage, number, function (math functions) or ident\")) {\n            // first rgb()/rgba() channel has a wrong token type; surface span and skip/repair the declaration\n        }\n        return Err(err.into());\n    }\n}","preventionTips":["Convert hex colors to channels (or keep hex) before building rgb() strings.","Strip units from numbers destined for color channels.","Pre-validate every interpolated channel in template code; treat parse errors as fatal to catch regressions in tests."],"tags":["css","swc","css-parser","rgb","color-function","unexpected-token"],"backgroundTag":"css-color-function-invalid-token","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}