Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Css Selector

Css Selector Laravel Package

symfony/css-selector

Symfony CssSelector converts CSS selectors into XPath expressions, making it easy to query DOM documents with familiar CSS syntax. Useful for HTML/XML scraping, testing, and DOM traversal, with documentation and support via the main Symfony repository.

View on GitHub
Deep Wiki
Context7

The symfony/css-selector component converts CSS selectors into equivalent XPath expressions, making it easy to query DOM/XML documents using familiar CSS syntax while leveraging XPath-capable engines.

It’s a lightweight utility commonly used alongside Symfony’s DOM tooling, providing reliable selector parsing and translation for scraping, testing, and document processing.

  • Converts CSS selectors to optimized XPath
  • Supports a wide range of CSS selector syntax
  • Useful for querying HTML/XML via DOM implementations
  • Integrates cleanly with Symfony’s ecosystem
  • Mature, battle-tested component with solid documentation
Frequently asked questions about Css Selector
How do I install Symfony CssSelector in a Laravel project?
Use Composer to install the package with `composer require symfony/css-selector`. No additional Laravel-specific setup is required, as it’s a standalone component. For Laravel 11+, ensure your project uses PHP 8.4+ to meet the package’s requirements.
Will this package work with Laravel’s built-in DOMDocument or SimpleXML?
Yes, Symfony CssSelector is designed to work with any DOM implementation. Convert CSS selectors to XPath and use them directly with `DOMDocument->xpath()` or `SimpleXML->xpath()`. It bridges the gap between developer-friendly CSS and the XPath syntax required by these tools.
Does Symfony CssSelector support advanced CSS selectors like `:has()` or `:is()`?
The package supports most modern CSS selectors, including `:has()`, `:is()`, and `:where()`, as long as they are part of the [CSS Selectors Level 4 specification](https://www.w3.org/TR/selectors-4/). Check Symfony’s [documentation](https://symfony.com/doc/current/components/css_selector.html) for a full list of supported selectors.
Can I use this package without pulling in other Symfony components?
Absolutely. Symfony CssSelector is a standalone component with no dependencies beyond PHP. You can use it independently in Laravel without introducing Symfony’s full framework or other components like `symfony/dom-crawler`.
What’s the performance impact of converting CSS to XPath in high-throughput scraping?
The package includes an LRU cache (since v7.4.6) to optimize repeated conversions, reducing memory overhead. For scraping pipelines, test under load to ensure the cache behaves as expected. If performance is critical, benchmark your specific use case, as complex selectors may still introduce minor latency.
How do I integrate Symfony CssSelector into Laravel’s service container?
Register the converter as a service in Laravel’s `AppServiceProvider` using `app()->bind()` or `app()->singleton()`. For example: `app()->bind(CssSelectorConverter::class, function ($app) { return new CssSelectorConverter(); });`. This allows dependency injection in controllers or services.
Is Symfony CssSelector compatible with Laravel 10 or older versions?
No, Symfony CssSelector requires PHP 8.4+, which aligns with Laravel 11+. For Laravel 10 or older (PHP 8.2–8.3), you’ll need to either upgrade your project or use a fallback like a custom XPath parser or a simpler alternative like `spatie/array-to-xml` for basic needs.
How can I test if the XPath output matches my expectations?
Use PHPUnit or Pest to assert the correctness of converted selectors. For example, compare the XPath output against known results or validate DOM queries using `assertSelectorTextContains()` in testing frameworks. Symfony’s [documentation](https://symfony.com/doc/current/components/css_selector.html) includes examples for testing selector conversions.
What are the alternatives to Symfony CssSelector for Laravel?
Alternatives include `php-dom` (for basic XPath), `spatie/array-to-xml` (for XML manipulation), or `simplehtmldom/simple-html-dom` (for HTML parsing). However, none offer the same level of CSS selector support or integration with Symfony’s ecosystem. If you’re already using Symfony components, this package is the most robust choice.
Will using Symfony CssSelector introduce dependency conflicts in Laravel?
No critical conflicts exist, but if you also use `symfony/dom-crawler`, you’ll pull in additional Symfony components. Scope these dependencies to specific services or use Composer’s `replace` directive to avoid bloat. For most Laravel projects, the package remains lightweight and conflict-free.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport