{"record":{"id":"a26693b2ca670e7e","repo":"gchq/CyberChef","slug":"invalid-css-selector-details-err-message","errorCode":null,"errorMessage":"Invalid CSS Selector. Details:\n${err.message}","messagePattern":"Invalid CSS Selector\\. Details:\n(.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/CSSSelector.mjs","lineNumber":68,"sourceCode":"            parser = new xmldom.DOMParser();\n        let dom,\n            result;\n\n        if (!query.length || !input.length) {\n            return \"\";\n        }\n\n        try {\n            dom = parser.parseFromString(input);\n        } catch (err) {\n            throw new OperationError(\"Invalid input HTML.\");\n        }\n\n        try {\n            const matcher = nwmatcher({document: dom});\n            result = matcher.select(query, dom);\n        } catch (err) {\n            throw new OperationError(\"Invalid CSS Selector. Details:\\n\" + err.message);\n        }\n\n        const nodeToString = function(node) {\n            return node.toString();\n            /* xmldom does not return the outerHTML value.\n            switch (node.nodeType) {\n                case node.ELEMENT_NODE: return node.outerHTML;\n                case node.ATTRIBUTE_NODE: return node.value;\n                case node.TEXT_NODE: return node.wholeText;\n                case node.COMMENT_NODE: return node.data;\n                case node.DOCUMENT_NODE: return node.outerHTML;\n                default: throw new Error(\"Unknown Node Type: \" + node.nodeType);\n            }*/\n        };\n\n        return result\n            .map(nodeToString)\n            .join(delimiter);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/CSSSelector.mjs#L50-L86","documentation":"Thrown by the CSS Selector operation when nwmatcher fails to evaluate the supplied CSS selector against the parsed DOM. The input HTML is parsed first (a separate error covers malformed HTML); this error means the selector string itself was syntactically or semantically invalid for the nwmatcher engine.","triggerScenarios":"Calling CSSSelector.run with a query that nwmatcher.select() rejects — e.g. unbalanced brackets (':nth-child('), unsupported pseudo-classes, invalid combinator sequences, or selectors with characters nwmatcher does not tolerate. The DOM must parse successfully first, so only malformed selectors reach this catch.","commonSituations":"User pastes a jQuery/Sizzle-only pseudo selector (':eq(0)', ':has(> div)'), uses a selector version nwmatcher predates, or has an unterminated attribute selector. Quotes/brackets in dynamic selectors built from untrusted input frequently trigger it.","solutions":["Inspect the appended err.message to find the exact nwmatcher parse error and the offending token.","Simplify the selector to CSS2/CSS3 subset supported by nwmatcher (avoid jQuery extensions like :eq, :contains special forms).","Quote attribute values and balance all parentheses/brackets.","If you need modern selectors, pre-filter input or use a different matching strategy outside this operation."],"exampleFix":"// before\nmatcher.select(\"div:eq(0)\", dom);  // nwmatcher rejects :eq\n// after\nmatcher.select(\"div:nth-of-type(1)\", dom);  // supported pseudo-class","handlingStrategy":"try-catch","validationCode":"function isValidCssSelector(sel) {\n  // nwmatcher supports roughly CSS2/3; reject jQuery/Sizzle extensions and unbalanced brackets\n  if (/[(){}[\\]]/.test(sel)) {\n    const open = (sel.match(/[([{]/g)||[]).length;\n    const close = (sel.match(/[)\\]}]/g)||[]).length;\n    if (open !== close) return false;\n  }\n  if (/:(eq|contains\\(|has\\()/.test(sel)) return false; // common unsupported pseudos\n  return sel.trim().length > 0;\n}","typeGuard":"null","tryCatchPattern":"try {\n  result = matcher.select(query, dom);\n} catch (err) {\n  throw new OperationError(\"Invalid CSS Selector. Details:\\n\" + err.message);\n}","preventionTips":["Pre-validate selectors against the CSS2/3 subset nwmatcher supports.","Avoid jQuery-only pseudo-classes (:eq, :contains with parens, :has).","Balance brackets and quote attribute values before matching.","Surface err.message so users can locate the offending token."],"tags":["css","selector","nwmatcher","html-parsing"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}