Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rst-selector-parser

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rst-selector-parser

A CSS-based selector parser for React Standard Tree (RST) traversal

  • 2.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8M
increased by8.07%
Maintainers
1
Weekly downloads
 
Created
Source

rst-selector-parser

This is a fork of scalpel intended for use with enzyme for traversing a React Standard Tree (RST) using CSS selectors, with minor divergences.

Usage

import {
  createGenerator,
  createParser
} from 'rst-selector-parser';

const generator = createGenerator();
const parser = createParser();

const tokens: Array<SelectorTokenType | CombinatorTokenType> = parser.parse('.foo.bar');

// [
//   {
//     type: 'selector',
//     body: [
//       {
//         type: 'classSelector',
//         name: 'foo'
//       },
//       {
//         type: 'classSelector',
//         name: 'bar'
//       }
//     ]
//   }
// ]

const selector: string = generator.generate(token);

// .foo.bar

Note:

For programmatic type definitions, refer to ./src/types.js.

Token types

TypeDescriptionExample
adjacentSiblingCombinatorAn adjacent sibling combinator..baz0 + .baz1
attributePresenceSelectorAn attribute presence selector.[qux]
attributeValueSelectorAn attribute value selector.[qux=val], [qux~=val]
childCombinatorA child combinator..baz0 > .baz1
classSelectorA class selector..baz
descendantCombinatorA descendant combinator..baz0 .baz1
generalSiblingCombinatorA general sibling combinator..baz0 ~ .baz1
idSelectorAn ID selector#bar
pseudoClassSelectorA pseudo-class selector.:corge, :corge(), :corge(val0, 'val1', "val2")
pseudoElementSelectorA pseudo-element selector.::grault
typeSelectorA type selector.foo
universalSelectorA universal selector.*

Fields

Tokens have fields that describe additional information about the token. Fields are token type specific.

adjacentSiblingCombinator

NameDescriptionExample
typeName of the token type."adjacentSiblingCombinator"

attributePresenceSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
typeName of the token type."attributePresenceSelector"

attributeValueSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
operatorOperator of the expression."*=" in [qux*=val]
typeName of the token type."attributeValueSelector"
valueValue of the expression."val" in [qux=val]

childCombinator

NameDescriptionExample
typeName of the token type."childCombinator"

classSelector

NameDescriptionExample
nameClass name."baz" in .baz[qux]
typeName of the token type."classSelector"

descendantCombinator

NameDescriptionExample
typeName of the token type."descendantCombinator"

generalSiblingCombinator

NameDescriptionExample
typeName of the token type."generalSiblingCombinator"

idSelector

NameDescriptionExample
nameName of the ID."bar" in #bar:corge()
typeName of the token type."idSelector"

pseudoClassSelector

NameDescriptionExample
nameName of the pseudo-class."corge" in #bar:corge()
parametersArray of parameter values."var0", "var1" and "var2" in :corge(var0, 'var1', "var2")
typeName of the token type."pseudoClassSelector"

pseudoElementSelector

NameDescriptionExample
nameName of the pseudo-element."grault" in #bar::grault
typeName of the token type."pseudoElementSelector"

typeSelector

NameDescriptionExample
nameName of the node."foo" in foo#bar.baz
typeName of the token type."typeSelector"

universalSelector

NameDescriptionExample
typeName of the token type."universalSelector"

Development

git pull git@github.com:gajus/scalpel.git
cd ./scalpel
npm install
npm run test

The parser grammar is in the ./src/grammar.ne file. After making changes to the parser grammar, you need to compile the parser using npm run compile-grammar command.

Note:

This parser could be extended to support the entire CSS grammar. I don't have such a use case. However, should you want to add new grammar, raise an issue.

Usage

import {
  createGenerator,
  createParser
} from 'scalpel';

const generator = createGenerator();
const parser = createParser();

const tokens: Array<SelectorTokenType | CombinatorTokenType> = parser.parse('.foo.bar');

// [
//   {
//     type: 'selector',
//     body: [
//       {
//         type: 'classSelector',
//         name: 'foo'
//       },
//       {
//         type: 'classSelector',
//         name: 'bar'
//       }
//     ]
//   }
// ]

const selector: string = generator.generate(token);

// .foo.bar

Note:

For programmatic type definitions, refer to ./src/types.js.

Token types

TypeDescriptionExample
adjacentSiblingCombinatorAn adjacent sibling combinator..baz0 + .baz1
attributePresenceSelectorAn attribute presence selector.[qux]
attributeValueSelectorAn attribute value selector.[qux=val], [qux~=val]
childCombinatorA child combinator..baz0 > .baz1
classSelectorA class selector..baz
descendantCombinatorA descendant combinator..baz0 .baz1
generalSiblingCombinatorA general sibling combinator..baz0 ~ .baz1
idSelectorAn ID selector#bar
pseudoClassSelectorA pseudo-class selector.:corge, :corge(), :corge(val0, 'val1', "val2")
pseudoElementSelectorA pseudo-element selector.::grault
typeSelectorA type selector.foo
universalSelectorA universal selector.*

Fields

Tokens have fields that describe additional information about the token. Fields are token type specific.

adjacentSiblingCombinator

NameDescriptionExample
typeName of the token type."adjacentSiblingCombinator"

attributePresenceSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
typeName of the token type."attributePresenceSelector"

attributeValueSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
operatorOperator of the expression."*=" in [qux*=val]
typeName of the token type."attributeValueSelector"
valueValue of the expression."val" in [qux=val]

childCombinator

NameDescriptionExample
typeName of the token type."childCombinator"

classSelector

NameDescriptionExample
nameClass name."baz" in .baz[qux]
typeName of the token type."classSelector"

descendantCombinator

NameDescriptionExample
typeName of the token type."descendantCombinator"

generalSiblingCombinator

NameDescriptionExample
typeName of the token type."generalSiblingCombinator"

idSelector

NameDescriptionExample
nameName of the ID."bar" in #bar:corge()
typeName of the token type."idSelector"

pseudoClassSelector

NameDescriptionExample
nameName of the pseudo-class."corge" in #bar:corge()
parametersArray of parameter values."var0", "var1" and "var2" in :corge(var0, 'var1', "var2")
typeName of the token type."pseudoClassSelector"

pseudoElementSelector

NameDescriptionExample
nameName of the pseudo-element."grault" in #bar::grault
typeName of the token type."pseudoElementSelector"

typeSelector

NameDescriptionExample
nameName of the node."foo" in foo#bar.baz
typeName of the token type."typeSelector"

universalSelector

NameDescriptionExample
typeName of the token type."universalSelector"

Development

git pull git@github.com:gajus/scalpel.git
cd ./scalpel
npm install
npm run test

The parser grammar is in the ./src/grammar.ne file. After making changes to the parser grammar, you need to compile the parser using npm run compile-grammar command.

Note:

This parser could be extended to support the entire CSS grammar. I don't have such a use case. However, should you want to add new grammar, raise an issue.

Usage

import {
  createGenerator,
  createParser
} from 'scalpel';

const generator = createGenerator();
const parser = createParser();

const tokens: Array<SelectorTokenType | CombinatorTokenType> = parser.parse('.foo.bar');

// [
//   {
//     type: 'selector',
//     body: [
//       {
//         type: 'classSelector',
//         name: 'foo'
//       },
//       {
//         type: 'classSelector',
//         name: 'bar'
//       }
//     ]
//   }
// ]

const selector: string = generator.generate(token);

// .foo.bar

Note:

For programmatic type definitions, refer to ./src/types.js.

Token types

TypeDescriptionExample
adjacentSiblingCombinatorAn adjacent sibling combinator..baz0 + .baz1
attributePresenceSelectorAn attribute presence selector.[qux]
attributeValueSelectorAn attribute value selector.[qux=val], [qux~=val]
childCombinatorA child combinator..baz0 > .baz1
classSelectorA class selector..baz
descendantCombinatorA descendant combinator..baz0 .baz1
generalSiblingCombinatorA general sibling combinator..baz0 ~ .baz1
idSelectorAn ID selector#bar
pseudoClassSelectorA pseudo-class selector.:corge, :corge(), :corge(val0, 'val1', "val2")
pseudoElementSelectorA pseudo-element selector.::grault
typeSelectorA type selector.foo
universalSelectorA universal selector.*

Fields

Tokens have fields that describe additional information about the token. Fields are token type specific.

adjacentSiblingCombinator

NameDescriptionExample
typeName of the token type."adjacentSiblingCombinator"

attributePresenceSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
typeName of the token type."attributePresenceSelector"

attributeValueSelector

NameDescriptionExample
nameName of the element attribute."qux" in [qux]
operatorOperator of the expression."*=" in [qux*=val]
typeName of the token type."attributeValueSelector"
valueValue of the expression."val" in [qux=val]

childCombinator

NameDescriptionExample
typeName of the token type."childCombinator"

classSelector

NameDescriptionExample
nameClass name."baz" in .baz[qux]
typeName of the token type."classSelector"

descendantCombinator

NameDescriptionExample
typeName of the token type."descendantCombinator"

generalSiblingCombinator

NameDescriptionExample
typeName of the token type."generalSiblingCombinator"

idSelector

NameDescriptionExample
nameName of the ID."bar" in #bar:corge()
typeName of the token type."idSelector"

pseudoClassSelector

NameDescriptionExample
nameName of the pseudo-class."corge" in #bar:corge()
parametersArray of parameter values."var0", "var1" and "var2" in :corge(var0, 'var1', "var2")
typeName of the token type."pseudoClassSelector"

pseudoElementSelector

NameDescriptionExample
nameName of the pseudo-element."grault" in #bar::grault
typeName of the token type."pseudoElementSelector"

typeSelector

NameDescriptionExample
nameName of the node."foo" in foo#bar.baz
typeName of the token type."typeSelector"

universalSelector

NameDescriptionExample
typeName of the token type."universalSelector"

Development

git pull git@github.com:gajus/scalpel.git
cd ./scalpel
npm install
npm run test

The parser grammar is in the ./src/grammar.ne file. After making changes to the parser grammar, you need to compile the parser using npm run compile-grammar command.

Keywords

FAQs

Package last updated on 20 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc