GitXplorerGitXplorer
q

php-css-selector-parser

public
3 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
3330f029ce777ffd061e13a51a72d84b519e42c6

bump version

committed 10 years ago
Unverified
06bcf0b5502b6ebfc75349c302a16794b837865e

fixup star parsing/rendering bugs

committed 10 years ago
Unverified
5d4d50ad79f6e72a50709b353967e7074c0d17b7

add test case with nested star, php-invoker for testing

committed 10 years ago
Unverified
7ceed2f59b05d571206b93e2ecd4b37b53a7dab4

drop php5.3 support, bump version

committed 10 years ago
Unverified
302046181b4162b54a88b5a0a527ea35e2153b26

fixup 5.3 support

committed 10 years ago
Unverified
c0c2a342bc50b6decd1aa3c9982af93ece390b47

fixup readme, bump version

committed 10 years ago

README

The README file for this repository.

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

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