JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1404355
  • Score
    100M100P100Q267159F
  • License MIT

Powerful and compliant CSS selector parser.

Package Exports

  • css-selector-parser

Readme

css-selector-parser

Important: Migrating from 1.x.

Installation

npm install css-selector-parser

Usage

Parsing

import {createParser} from 'css-selector-parser';

const parse = createParser();
const selector = parse('a[href^="/"], .container:has(nav) > a[href]:nth-child(2)');

console.log(selector);

Produces:

({
  type: 'Selector',
  rules: [
    {
      type: 'Rule',
      tag: { type: 'TagName', name: 'a' },
      attributes: [
        {
          type: 'Attribute',
          name: 'href',
          operator: '^=',
          value: { type: 'String', value: '/' }
        }
      ]
    },
    {
      type: 'Rule',
      classNames: [ 'container' ],
      pseudoClasses: [
        {
          type: 'PseudoClass',
          name: 'has',
          argument: {
            type: 'Selector',
            rules: [ { type: 'Rule', tag: { type: 'TagName', name: 'nav' } } ]
          }
        }
      ],
      nestedRule: {
        type: 'Rule',
        combinator: '>',
        tag: { type: 'TagName', name: 'a' },
        attributes: [ { type: 'Attribute', name: 'href' } ],
        pseudoClasses: [
          {
            type: 'PseudoClass',
            name: 'nth-child',
            argument: { type: 'Formula', a: 0, b: 2 }
          }
        ]
      }
    }
  ]
})

Constructing AST and rendering

import {ast, render} from 'css-selector-parser';

const selector = ast.selector({
    rules: [
        ast.rule({
            tag: ast.tagName({name: 'a'}),
            attributes: [
                ast.attribute({name: 'href', operator: '^=', value: ast.string({value: '/'})})
            ]
        }),
        ast.rule({
            classNames: ['container'],
            pseudoClasses: [
                ast.pseudoClass({
                    name: 'has',
                    argument: ast.selector({
                        rules: [
                            ast.rule({tag: ast.tagName({name: 'nav'})})
                        ]
                    })
                })
            ],
            nestedRule: ast.rule({
                combinator: '>',
                tag: ast.tagName({name: 'a'}),
                attributes: [ast.attribute({name: 'href'})],
                pseudoClasses: [
                    ast.pseudoClass({
                        name: 'nth-child',
                        argument: ast.formula({a: 0, b: 2})
                    })
                ],
                pseudoElement: 'before'
            })
        })
    ]
});

console.log(render(selector)); // a[href^="/"], .container:has(nav) > a[href]:nth-child(2)::before

API

LICENSE

MIT

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.