{"record":{"id":"21cdf690654f1e7c","repo":"swc-project/swc","slug":"invalid-selector","errorCode":null,"errorMessage":"Invalid selector","messagePattern":"Invalid selector","errorType":"validation","errorClass":"swc_css_parser::error::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_css_parser/src/parser/selectors/mod.rs","lineNumber":465,"sourceCode":"            subclass_selectors.push(pseudo_element);\n\n            loop {\n                if !(is!(self, \":\") && !peeked_is!(self, \":\")) {\n                    break;\n                }\n\n                let pseudo_element = SubclassSelector::PseudoClass(self.parse()?);\n\n                subclass_selectors.push(pseudo_element);\n            }\n        }\n\n        if !self.ctx.in_global_or_local_selector\n            && nesting_selector.is_none()\n            && type_selector.is_none()\n            && subclass_selectors.is_empty()\n        {\n            return Err(Error::new(start_span, ErrorKind::InvalidSelector));\n        }\n\n        let span = span!(self, start_pos);\n\n        Ok(CompoundSelector {\n            span,\n            nesting_selector,\n            type_selector,\n            subclass_selectors,\n        })\n    }\n}\n\nimpl<I> Parse<TypeSelector> for Parser<I>\nwhere\n    I: ParserInput,\n{\n    fn parse(&mut self) -> PResult<TypeSelector> {","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_parser/src/parser/selectors/mod.rs#L447-L483","documentation":"A compound selector must contain something: a type selector, a nesting selector ('&'), or at least one subclass selector (id/class/attribute/pseudo). This check is skipped only when the parser is inside a :global()/:local() context (CSS modules), where empty compounds are permitted. Here the compound was completely empty outside that context, so the whole selector is invalid.","triggerScenarios":"A dangling combinator with nothing after it ('.a > { color: red; }'), a leading combinator ('> .b {}') in non-nesting mode, an empty selector between commas ('a, , b {}'), or a rule whose selector is just '{...}' — all outside :local/:global.","commonSituations":"User-authored CSS from CMS/DB fields; CSS written for postcss-nesting where leading combinators were legal and then fed to a parser context that does not allow them; conditional templating that renders an empty selector string.","solutions":["Remove dangling/leading combinators or complete the missing compound: '.a > .b {}'","Remove empty selectors between commas","If you need leading-combinator nesting syntax, enable the nesting-capable parse options or wrap the rule for a nesting-aware toolchain first","Validate selector strings with a CSS selector library before handing them to swc"],"exampleFix":"/* before */\n.a > { color: red; }\n/* after */\n.a > .b { color: red; }","handlingStrategy":"validation","validationCode":"// Reject empty/dangling selector strings before parse\nfunction selectorLooksNonEmpty(sel) {\n  const s = sel.trim();\n  return s !== '' && !/^[>+~]|[,>~+\\s]\\s*$|,\\s*,/.test(s);\n}","typeGuard":null,"tryCatchPattern":"// JS\ntry { parseSelector(sel, opts); } catch (e) { if (/Invalid selector/.test(e.message)) dropRuleWithWarning(sel); else throw e; }","preventionTips":["Skip rendering <style> blocks whose selector template resolves to empty","Post-process nesting through a nesting-aware tool before strict parsing","Strip empty comma segments in generated selector lists"],"tags":["css","selector","syntax","combinator"],"backgroundTag":"css-selector-syntax-error","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"}