{"record":{"id":"efc8b83b0b915456","repo":"symfony/css-selector","slug":"parser","errorCode":null,"errorMessage":"]","messagePattern":"\\]","errorType":"exception","errorClass":"SyntaxErrorException","httpStatus":null,"severity":"error","filePath":"Parser/Parser.php","lineNumber":472,"sourceCode":"        }\n\n        $stream->skipWhitespace();\n        $value = $stream->getNext();\n\n        if ($value->isNumber()) {\n            // if the value is a number, it's casted into a string\n            $value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());\n        }\n\n        if (!($value->isIdentifier() || $value->isString())) {\n            throw SyntaxErrorException::unexpectedToken('string or identifier', $value);\n        }\n\n        $stream->skipWhitespace();\n        $next = $stream->getNext();\n\n        if (!$next->isDelimiter([']'])) {\n            throw SyntaxErrorException::unexpectedToken('\"]\"', $next);\n        }\n\n        return new Node\\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());\n    }\n}\n","sourceCodeStart":454,"sourceCodeEnd":478,"githubUrl":"https://github.com/symfony/css-selector/blob/08e2905152a39cf3fd1745d83f8c483e258887d9/Parser/Parser.php#L454-L478","documentation":"This SyntaxErrorException is thrown at the end of an attribute selector: after the operator and value, the parser requires the closing ']' delimiter and found another token instead. The attribute selector was never properly terminated.","triggerScenarios":"Selectors like 'a[href=\"x\")', 'input[type=\"text\" >', '[data-x=\"v\" and [data-y]', or a missing ']' caused by an unclosed earlier '[' so the brackets are unbalanced by the time the value is parsed.","commonSituations":"Unbalanced brackets from dynamically assembled selectors; copy-paste truncation cutting off the final ']'; nesting confusion like '[a=\"[x]\"]' where quotes around brackets are omitted; templating engines stripping the bracket.","solutions":["Add the missing ']' so every '[' in the selector has a matching ']'.","Count/validate bracket balance (respecting quoted strings) before calling toXPath().","Quote values that contain '[' or ']' characters so they tokenize as strings.","Check for truncation if the selector comes from an external source or was sliced in code."],"exampleFix":"// before\n$xpath = $converter->toXPath('a[href=\"example.com\"');\n\n// after\n$xpath = $converter->toXPath('a[href=\"example.com\"]');","handlingStrategy":"validation","validationCode":"// check bracket balance ignoring quoted strings before conversion\nfunction bracketsBalanced(string $s): bool {\n    $depth = 0; $inStr = false; $q = '';\n    for ($i = 0, $n = strlen($s); $i < $n; $i++) {\n        $c = $s[$i];\n        if ($inStr) { if ($c === $q) $inStr = false; continue; }\n        if ($c === '\"' || $c === \"'\") { $inStr = true; $q = $c; continue; }\n        if ($c === '[') $depth++;\n        if ($c === ']') { $depth--; if ($depth < 0) return false; }\n    }\n    return $depth === 0;\n}","typeGuard":"function attributeSelectorsClosed(string $s): bool {\n    return substr_count($s, '[') === substr_count($s, ']');\n}","tryCatchPattern":"try {\n    $xpath = $converter->toXPath($selector);\n} catch (\\Symfony\\Component\\CssSelector\\Exception\\SyntaxErrorException $e) {\n    if (!bracketsBalanced($selector)) {\n        throw new InvalidArgumentException('Unbalanced [ ] in attribute selector.');\n    }\n    throw $e;\n}","preventionTips":["Quote values containing '[' or ']' so they tokenize as strings.","Check bracket balance (string-aware) on any generated selector.","Watch for template engines or string slicing that can drop the final ']'."],"tags":["css-selector","attribute-selector","syntax-error","php"],"backgroundTag":"invalid-css-selector","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"}