symfony/css-selector · error · ExpressionErrorException
Extension " " not registered.
Error message
Extension "%s" not registered.
What it means
ExpressionErrorException thrown by Translator::getExtension when the requested extension name is not registered on the translator instance. Extensions (Html, Function, PseudoClass, Attribute, etc.) must be explicitly added via addExtension/registerDefaultExtensions; getExtension is the internal lookup used by translator features and is also public API.
Solutions
- Register the extension first: $translator->addExtension(new HtmlExtension()); or call registerDefaultExtensions().
- Use Translator::fromCssConverter()/default constructors that pre-register the standard extensions.
- Verify the exact extension name/key (case-sensitive) used with getExtension().
- Catch ExpressionErrorException and fall back to a translator configured with defaults.
Example fix
// before
$translator = new Translator();
$ext = $translator->getExtension('html'); // throws
// after
$translator = new Translator();
$translator->registerDefaultExtensions();
$translator->addExtension(new HtmlExtension());
$ext = $translator->getExtension('html'); Defensive patterns
Strategy: try-catch
Validate before calling
function hasExtension(\Symfony\Component\CssSelector\XPath\Translator $t, string $name): bool {
try { $t->getExtension($name); return true; } catch (ExpressionErrorException $e) { return false; }
} Type guard
function extensionExists(array $extensions, string $name): bool {
return isset($extensions[$name]);
} Try / catch
try {
$ext = $translator->getExtension('html');
} catch (ExpressionErrorException $e) {
$translator->registerDefaultExtensions();
$translator->addExtension(new \Symfony\Component\CssSelector\XPath\Extension\HtmlExtension());
$ext = $translator->getExtension('html');
} Prevention
- Use registerDefaultExtensions() right after constructing a Translator.
- Add HtmlExtension explicitly when translating HTML documents.
- Check extension name spelling/casing before getExtension().
- Prefer Translator factory methods that configure extensions for you.
When it happens
Trigger: Calling $translator->getExtension('html') on a translator that was never configured with the Html extension, mistyping an extension name, or constructing Translator manually without registerDefaultExtensions().
Common situations: Building a Translator with new Translator() and no extensions before translating HTML-specific selectors, DI misconfiguration, typos like 'htm' or 'pseudo-class' with wrong casing.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- Got too deeply nested
- Invalid series: " ".
- "*:nth-of-type()" is not implemented.
- Expected a single string or identifier for :contains(), got
- Expected a single string or identifier for :lang(), got
AI-assisted analysis of symfony/css-selector@08e2905152 (2026-09-14).
Data as JSON: /api/errors/9087de3b39441c3e.
Report an issue: GitHub.
Appendix: source
Thrown at XPath/Translator.php:135
$this->extensions[$extension->getName()] = $extension;
$this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
$this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
$this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
$this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
$this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
$this->relativeCombinationTranslators = array_merge($this->relativeCombinationTranslators, $extension->getRelativeCombinationTranslators());
return $this;
}
/**
* @throws ExpressionErrorException
*/
public function getExtension(string $name): Extension\ExtensionInterface
{
if (!isset($this->extensions[$name])) {
throw new ExpressionErrorException(\sprintf('Extension "%s" not registered.', $name));
}
return $this->extensions[$name];
}
/**
* @return $this
*/
public function registerParserShortcut(ParserInterface $shortcut): static
{
$this->shortcutParsers[] = $shortcut;
return $this;
}
/**
* @throws ExpressionErrorException
*/View on GitHub (pinned to 08e2905152)