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/XML documents with familiar CSS syntax. Part of the Symfony Components ecosystem, with full docs and issue tracking in the main Symfony repository.

View on GitHub
Deep Wiki
Context7

Converts CSS selectors to XPath expressions

Frequently asked questions about Css Selector
How does Symfony CssSelector improve Laravel web scraping (e.g., spatie/laravel-web-scraper) with :is() selectors?
The package fixes critical bugs in :is() and :where() combinators (e.g., `div:is(.active, .selected) > p`), resolving parsing failures for dynamic class selectors in scraping tools. This ensures reliable extraction of elements with conditional or multiple classes, like e-commerce product listings or social media feeds. For Laravel, it directly enhances spatie/laravel-web-scraper’s selector stability without requiring custom regex workarounds.
Can I use Symfony CssSelector in Laravel 10 or older? What’s the fallback?
Symfony CssSelector requires PHP 8.4+, so Laravel 10 or below may need `league/css-to-xpath` as a fallback. The core API (`CssSelector::toXPath()`) remains stable, but the :is()/:where() fixes are exclusive to newer versions. For legacy projects, test `league/css-to-xpath` for compatibility with your existing selectors before migrating.
How do I convert a CSS selector like `div:is(.user--active, .user--premium) > span` to XPath in Laravel?
Use `CssSelector::toXPath('div:is(.user--active, .user--premium) > span')`—the package handles the conversion automatically. For Laravel, this XPath can then query `DOMDocument` or `SimpleXML` objects. Example: `$xpath = CssSelector::toXPath('selector'); $nodes = $dom->xpath($xpath);`. The fix ensures nested combinators (like `:is()` with child selectors) work reliably.
Will Symfony CssSelector break my existing Laravel tests using CSS selectors?
No, this is a non-breaking patch. Existing `CssSelector::toXPath()` calls will continue working, but you’ll gain stability for selectors using `:is()` or `:where()`. For example, tests like `$xpath = CssSelector::toXpath(':where(.error) input:invalid')` will now parse correctly, reducing flaky assertions in Pest/PHPUnit. Always validate edge cases (e.g., parent selectors) in your test suite.
Does Symfony CssSelector support complex selectors like `[attr^='value']:is(.class)` in Laravel?
Yes, the package supports attribute selectors combined with `:is()` or `:where()`, such as `[data-role^='user']:is(.active, .admin)`. This is useful for Laravel’s XML/API processing (e.g., SOAP responses) or Blade templating where dynamic attributes are common. The LRU cache optimizes performance for high-volume queries, like parsing 10K+ selectors in bulk operations.
How do I handle unsupported CSS selectors (e.g., custom pseudo-elements) in Laravel?
Use try-catch with `CssSelectorException` to gracefully fall back to regex or `league/css-to-xpath`. Example: `try { $xpath = CssSelector::toXPath('div:custom-pseudo'); } catch (CssSelectorException $e) { $xpath = preg_replace_callback(...); }`. This ensures Laravel apps degrade safely when encountering non-standard selectors.
Can Symfony CssSelector be used in Laravel Blade templates for dynamic XPath generation?
Yes, you can generate XPath dynamically in Blade using `@php` directives. Example: `@php $xpath = CssSelector::toXPath('div:is('.desktop, .mobile)'); @endphp`. This enables conditional rendering based on CSS selectors, such as `@if($dom->xpath($xpath)) ... @endif`. The package’s lightweight design makes it ideal for templating without bloating your views.
What’s the performance impact of Symfony CssSelector in high-throughput Laravel scraping tasks?
The package maintains LRU cache optimizations from Symfony’s v7.4.6+, ensuring low memory overhead for bulk operations (e.g., 10K+ selectors). Benchmark your specific use case, but the fix for `:is()`/:where() adds negligible overhead. For real-time streaming (e.g., WebSocket HTML updates), consider synchronous alternatives like `league/css-to-xpath` due to this component’s design.
How do I migrate from `league/css-to-xpath` to Symfony CssSelector in Laravel?
Replace `CssToXPath::convert()` with `CssSelector::toXPath()`. Example: `// Before: $xpath = CssToXPath::convert('div:is(.active)'); // After: $xpath = CssSelector::toXPath('div:is(.active)');`. Test selectors with `:is()`/:where()` first, as these are the primary improvements. Use a feature flag or config to toggle between libraries during migration.
Are there any known limitations with nested selectors like `ul > li:is(.item, .highlight)` in Laravel?
While `:is()`/:where()` combinators are now stable, complex nested selectors (e.g., `ul > li:is(.item, .highlight)`) may require validation against your specific DOM structure. The fix covers 99% of use cases, but test edge cases like parent-child relationships in your Laravel app’s HTML/XML. For critical paths, use `league/css-to-xpath` as a secondary validator.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai