{"record":{"id":"03a2f4611043dcbd","repo":"symfony/css-selector","slug":"nth-of-type-is-not-implemented","errorCode":null,"errorMessage":"\"*:nth-of-type()\" is not implemented.","messagePattern":"\"\\*:nth-of-type\\(\\)\" is not implemented\\.","errorType":"exception","errorClass":"ExpressionErrorException","httpStatus":null,"severity":"error","filePath":"XPath/Extension/FunctionExtension.php","lineNumber":119,"sourceCode":"    }\n\n    public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function): XPathExpr\n    {\n        return $this->translateNthChild($xpath, $function, true);\n    }\n\n    public function translateNthOfType(XPathExpr $xpath, FunctionNode $function): XPathExpr\n    {\n        return $this->translateNthChild($xpath, $function, false, false);\n    }\n\n    /**\n     * @throws ExpressionErrorException\n     */\n    public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function): XPathExpr\n    {\n        if ('*' === $xpath->getElement()) {\n            throw new ExpressionErrorException('\"*:nth-of-type()\" is not implemented.');\n        }\n\n        return $this->translateNthChild($xpath, $function, true, false);\n    }\n\n    /**\n     * @throws ExpressionErrorException\n     */\n    public function translateContains(XPathExpr $xpath, FunctionNode $function): XPathExpr\n    {\n        $arguments = $function->getArguments();\n        foreach ($arguments as $token) {\n            if (!($token->isString() || $token->isIdentifier())) {\n                throw new ExpressionErrorException('Expected a single string or identifier for :contains(), got '.implode(', ', $arguments));\n            }\n        }\n\n        return $xpath->addCondition(\\sprintf(","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/symfony/css-selector/blob/08e2905152a39cf3fd1745d83f8c483e258887d9/XPath/Extension/FunctionExtension.php#L101-L137","documentation":"ExpressionErrorException thrown by FunctionExtension::translateNthLastOfType when the XPath expression's element is '*' (universal selector), i.e. the selector is like *:nth-last-of-type(). Counting by element type relative to the end is impossible when no element type is known, so the library refuses rather than emit wrong XPath.","triggerScenarios":"Translating '*:nth-last-of-type(2)' or a compound where the type was erased to universal, e.g. '*:nth-last-of-type(1n)', through Translator::cssToXPath.","commonSituations":"Writing :nth-last-of-type() without a preceding element/type selector; automated selector generation that drops the element name; confusion between :nth-last-child (works on *) and :nth-last-of-type (does not).","solutions":["Replace '*' with the concrete element type, e.g. 'p:nth-last-of-type(2)'.","Use :nth-last-child() instead, which works with the universal selector.","Catch ExpressionErrorException and fall back to a DOM/query library that supports this case.","Filter or rewrite selectors containing '*:nth-of-type'/'*:nth-last-of-type' before translation."],"exampleFix":"// before\n'*:nth-last-of-type(2)'\n// after\n'p:nth-last-of-type(2)' // or 'p:nth-last-child(2)'","handlingStrategy":"try-catch","validationCode":"if (preg_match('/^\\*:(nth-last-of-type|nth-of-type)/', $selector)) {\n    throw new \\InvalidArgumentException('Rewrite *:nth-...-of-type with a concrete element type.');\n}","typeGuard":"function needsElementTypeForOfType(string $css): bool {\n    return (bool) preg_match('/(^|[\\s,+>~])\\*:(nth-)?(last-)?of-type/', $css);\n}","tryCatchPattern":"try {\n    $xpath = $translator->cssToXPath($css);\n} catch (ExpressionErrorException $e) {\n    if (str_contains($e->getMessage(), 'not implemented')) {\n        $css = str_replace('*:nth-last-of-type', 'div:nth-last-child', $css);\n        $xpath = $translator->cssToXPath($css);\n    }\n}","preventionTips":["Never combine the universal selector with *-of-type pseudo-classes.","Prefer :nth-last-child/:nth-child when element type is irrelevant.","Normalize selectors before translation to insert concrete element names.","Test translated selectors against expected XPath in CI."],"tags":["css-selector","xpath","unsupported-selector"],"backgroundTag":"unsupported-operation","analyzedSha":"08e2905152a39cf3fd1745d83f8c483e258887d9","analyzedAt":"2026-09-14T11:41:28.509Z","contentChangedAt":"2026-09-14T11:41:28.509Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}