Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qfox/php-css-selector-parser
Simple CSS Selector Parser. Port of node-css-selector-parser
https://github.com/qfox/php-css-selector-parser
Last synced: 23 days ago
JSON representation
Simple CSS Selector Parser. Port of node-css-selector-parser
- Host: GitHub
- URL: https://github.com/qfox/php-css-selector-parser
- Owner: qfox
- License: mit
- Created: 2014-10-27T09:49:35.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-27T13:58:10.000Z (about 10 years ago)
- Last Synced: 2024-07-09T08:20:17.463Z (4 months ago)
- Language: PHP
- Size: 137 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
php-css-selector-parser
=======================Fast and low memory CSS selector parser.
Parses CSS selector into object-model.
Installation
------------```
composer require zxqfox/css-selector-parser
```Usage
-----```php
use CSSSelectorParser\Parser;$parser = new Parser();
$parser->registerSelectorPseudos('has');
$parser->registerNestingOperators('>', '+', '~');
$parser->registerAttrEqualityMods('^', '$', '*', '~');
$parser->enableSubstitutes();$css = 'a[href^=/], .container:has(nav) > a[href]:lt($var)';
echo json_encode($parser->parse($css), JSON_PRETTY_PRINT);
```Produces:
```
{ type: 'selectors',
selectors:
[ { type: 'ruleSet',
rule:
{ tagName: 'a',
attrs: [ { name: 'href', operator: '^=', valueType: 'string', value: '/' } ],
type: 'rule' } },
{ type: 'ruleSet',
rule:
{ classNames: [ 'container' ],
pseudos:
[ { name: 'has',
valueType: 'selector',
value: { type: 'ruleSet', rule: { tagName: 'nav', type: 'rule' } } } ],
type: 'rule',
rule:
{ tagName: 'a',
attrs: [ { name: 'href' } ],
pseudos: [ { name: 'lt', valueType: 'substitute', value: 'var' } ],
nestingOperator: '>',
type: 'rule' } } } ] }
```Token description
-----------------*type* may be one of:
* *selectors* — list of selectors, token contains *selectors* array of *ruleSet* tokens (based on "," operator).
* *ruleSet* — selector, token contains *rule* field with *rule*-type object.
* *rule* — single rule.Fields for *rule* type.
* *tagName* — tag name for the rule (e.g. "div"), may be '*'.
* *classNames* — list of CSS class names for the rule.
* *attrs* — list of attribute rules; rule may contain fields:
* *name* — attribute name, required field.
* *valueType* — type of comparison value ("string" or "substitute").
* *operator* — attribute value comparison operator.
* *value* — comparison attribute value.
* *pseudos* — list of pseudo class rules; rule may contain fields:
* *name* — pseudo name, required field.
* *valueType* — argument type ("string", "selector" or "substitute").
* *value* — pseudo argument.
* *nestingOperator* — the operator used to nest this rule (e.g. in selector "tag1 > tag2", tag2 will have nestingOperator=">")
* *rule* — nested rule.License
-------MIT