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

Php Css Parser Laravel Package

sabberworm/php-css-parser

Parse and manipulate CSS in PHP with a fast, flexible parser. Convert CSS into an object model, inspect and edit rules, selectors, and declarations, then render back to CSS. Useful for minifying, rewriting assets, or building CSS tooling.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer:

composer require sabberworm/php-css-parser  

Start by parsing a CSS string or file:

use Sabberworm\CSS\Parser;  

$parser = new Parser($cssString);  
$cssDocument = $parser->parse();  

The parsed $cssDocument is an object tree: CSSDocumentRuleSets (e.g., RuleSet, AtRule) → RulesValues.
First common use case: extract and inspect selectors/rules — e.g., find all font-size declarations across a stylesheet.

Implementation Patterns

  • Read & Analyze: Traverse the document tree to audit styles (e.g., unused selectors, specificity mapping).
  • Programmatic Editing: Modify values or add rules dynamically — e.g., inject vendor prefixes or adapt to theme variables:
    $ruleSet = new RuleSet();  
    $ruleSet->addRule(new Rule('color', new Color('red')));  
    $mediaQuery = new AtRule('media', 'screen and (min-width: 768px)');  
    $mediaQuery->getRuleSets()->addRuleSet($ruleSet);  
    $cssDocument->addRuleSet($mediaQuery);  
    
  • Output & Optimization: Render optimized CSS:
    echo $cssDocument->render(); // minify via ->render(new OutputFormat())  
    
  • Integrate with Build Tools: Use as part of a preprocessing pipeline — e.g., generate critical CSS, inline styles, or generate CSS variable maps from existing styles.

Gotchas and Tips

  • Parsing errors throw Sabberworm\CSS\Parser\Exception\ParserException — wrap parse() in try/catch for robustness.
  • Empty rule sets (e.g., div {}) are preserved — filter them manually if needed (getRuleSets()getRules()).
  • Vendor-specific values (e.g., -webkit-box) may parse inconsistently — always validate AST structure before modification.
  • Custom values (e.g., calc(), var(--custom)) are represented as CSSString or Value objects — avoid naive string concatenation.
  • Extensions: Extend OutputFormat to control minification (e.g., preserve line breaks for debugging).
  • Performance: Parsing large files (e.g., Bootstrap compiled) can be memory-intensive — consider streaming or chunking in CLI tools.
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