0.1.0 • Published 5 years ago

@ryanmorr/css-selector-parser v0.1.0

Weekly downloads
1
License
Unlicense
Repository
github
Last release
5 years ago

css-selector-parser

Version Badge Build Status License

Tokenize a CSS selector string

Install

Download the development or minified version, or install via NPM:

npm install @ryanmorr/css-selector-parser

Usage

Provide a selector string and get a two-dimensional array composed of each selector group in the first array and the tokens for a selector sequence in the second array.

import parse from '@ryanmorr/css-selector-parser';

parse('#foo[attr=value] > div:empty, .foo.bar + [attr$="value" i]:not(.baz[qux])');

Generates the following structure:

[
    [
        {
            attributes: [
                {
                    name: 'id',
                    operator: '=',
                    value: 'foo',
                    ignoreCase: false
                },
                {
                    name: 'attr',
                    operator: '=',
                    value: 'value',
                    ignoreCase: false
                }
            ],
            pseudos: []
        },
        '>',
        {
            nodeName: 'div',
            attributes: [],
            pseudos: [
                {
                    name: 'empty',
                    value: ''
                }
            ]
        }
    ],
    [
        {
            attributes: [
                {
                    name: 'class',
                    operator: '~=',
                    value: 'foo',
                    ignoreCase: false
                },
                {
                    name: 'class',
                    operator: '~=',
                    value: 'bar',
                    ignoreCase: false
                }
            ],
            pseudos: []
        },
        '+',
        {
            attributes: [
                {
                    name: 'attr',
                    operator: '$=',
                    value: 'value',
                    ignoreCase: true
                }
            ],
            pseudos: [
                {
                    name: 'not',
                    value: '.baz[qux]'
                }
            ]
        }
    ]
]

License

This project is dedicated to the public domain as described by the Unlicense.