{"record":{"id":"9f989bec4dfe8f62","repo":"swc-project/swc","slug":"expected-number-function-or-percentage-token","errorCode":null,"errorMessage":"Expected number, function or percentage token","messagePattern":"Expected number, function or percentage token","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/swc_css_parser/src/parser/values_and_units/mod.rs","lineNumber":2509,"sourceCode":"        match cur!(self) {\n            tok!(\"number\") => Ok(Hue::Number(self.parse()?)),\n            tok!(\"dimension\") => Ok(Hue::Angle(self.parse()?)),\n            _ => {\n                unreachable!()\n            }\n        }\n    }\n}\n\nimpl<I> Parse<CmykComponent> for Parser<I>\nwhere\n    I: ParserInput,\n{\n    fn parse(&mut self) -> PResult<CmykComponent> {\n        if !is_one_of!(self, \"number\", \"percentage\", \"function\") {\n            let span = self.input.cur_span();\n\n            return Err(Error::new(\n                span,\n                ErrorKind::Expected(\"number, function or percentage token\"),\n            ));\n        }\n\n        match cur!(self) {\n            tok!(\"number\") => Ok(CmykComponent::Number(self.parse()?)),\n            tok!(\"percentage\") => Ok(CmykComponent::Percentage(self.parse()?)),\n            Token::Function { value, .. } => {\n                if !is_math_function(value) {\n                    let span = self.input.cur_span();\n\n                    return Err(Error::new(span, ErrorKind::Expected(\"math function token\")));\n                }\n\n                Ok(CmykComponent::Function(self.parse()?))\n            }\n            _ => {","sourceCodeStart":2491,"sourceCodeEnd":2527,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_parser/src/parser/values_and_units/mod.rs#L2491-L2527","documentation":"Thrown by Parse<CmykComponent> in swc_css_parser when a device-cmyk() component is expected but the current token is neither a number, a percentage, nor a function. Even when a function token is present, the impl additionally requires is_math_function(value) (calc/clamp/min/max/...), so var(--c) or rgb() in a component slot is rejected. The span points at the offending token.","triggerScenarios":"'device-cmyk(red 61% 87% 48%)', 'device-cmyk(var(--c) 0 0 0)', 'device-cmyk(, 0 0 0)' (comma/EOF in a component slot), or a dimension like '10px' in a component position.","commonSituations":"Printer-oriented CSS generated from design tools that insert color keywords or CSS variables into device-cmyk() components, and empty components from buggy string joining.","solutions":["Use numbers or percentages for cmyk components: 'device-cmyk(0 61% 87% 48%)'.","Resolve var() references to concrete values before parsing, or wrap math in calc()/clamp() which are accepted functions.","Validate each component against ^(\\d+\\.?\\d*%?|calc\\(.+\\))$ before emitting device-cmyk()."],"exampleFix":"/* before */\na { color: device-cmyk(red 61% 87% 48%); }\n\n/* after */\na { color: device-cmyk(0 61% 87% 48%); }","handlingStrategy":"validation","validationCode":"const CMYK_COMPONENT = /^-?(\\d+\\.?\\d*|\\.\\d+)%?$|^calc\\(.+\\)$/;\nfunction isValidCmykComponent(v: string): boolean {\n  return CMYK_COMPONENT.test(v.trim());\n}","typeGuard":"fn is_cmyk_component_literal(v: &str) -> bool {\n    let t = v.strip_suffix('%').unwrap_or(v);\n    t.parse::<f64>().is_ok()\n        || (v.starts_with(\"calc(\") && v.ends_with(')'))\n}","tryCatchPattern":"use swc_css_parser::error::ErrorKind;\nif let Err(e) = parse_input::<Stylesheet>(src, opts) {\n    if matches!(e.kind(), ErrorKind::Expected(\"number, function or percentage token\")) {\n        return Err(format!(\"bad device-cmyk component at {:?}\", e.span()).into());\n    }\n    return Err(e.into());\n}","preventionTips":["Serialize device-cmyk components from numeric channels only; resolve var() to numbers first.","When math is needed, wrap it in calc()/clamp()/min()/max() - other functions are rejected.","Skip empty components when joining strings (',,0,0' style bugs trigger this error)."],"tags":["css","swc","parser","color","cmyk","print"],"backgroundTag":"css-invalid-cmyk-component","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}