css-selector-tokenizer
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/css-selector-tokenizer package

0.8.0 • Public • Published

CSS Modules: css-selector-tokenizer

Build Status coveralls.io codecov.io

Parses and stringifies CSS selectors.

import Tokenizer from "css-selector-tokenizer";

let input = "a#content.active > div::first-line [data-content], a:not(:visited)";

Tokenizer.parse(input); // === expected
let expected = {
  type: "selectors",
  nodes: [
    {
      type: "selector",
      nodes: [
        { type: "element", name: "a" },
        { type: "id", name: "content" },
        { type: "class", name: "active" },
        { type: "operator", operator: ">", before: " ", after: " " },
        { type: "element", name: "div" },
        { type: "pseudo-element", name: "first-line" },
        { type: "spacing", value: " " },
        { type: "attribute", content: "data-content" },
      ]
    },
    {
      type: "selector",
      nodes: [
        { type: "element", name: "a" },
        { type: "nested-pseudo-class", name: "not", nodes: [
          {
            type: "selector",
            nodes: [
              { type: "pseudo-class", name: "visited" }
            ]
          }
        ] }
      ],
      before: " "
    }
  ]
}

Tokenizer.stringify(expected) // === input

// * => { type: "universal" }
// foo|element = { type: "element", name: "element", namespace: "foo" }
// *|* = { type: "universal", namespace: "*" }
// :has(h1, h2) => { type: "nested-pseudo-class", name: "has", nodes: [
//     {
//       type: "selector",
//       nodes: [
//         { type: "element", name: "h1" }
//       ]
//     },
//     {
//       type: "selector",
//       nodes: [
//         { type: "element", name: "h2" }
//       ],
//       before: " "
//     }
//   ] }

Building

npm install
npm test

Development

  • npm test -- -w will watch lib and test for changes and retest

License

MIT

With thanks

  • Mark Dalgleish
  • Glen Maddern
  • Guy Bedford

Tobias Koppers, 2015.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.8.0
    249,020
    • latest

Version History

Package Sidebar

Install

npm i css-selector-tokenizer

Weekly Downloads

2,195,434

Version

0.8.0

License

MIT

Unpacked Size

18.4 kB

Total Files

9

Last publish

Collaborators

  • markdalgleish
  • sokra
  • trysound
  • geelen
  • evilebottnawi