Slick CSS Selector Engine

1.12.2 · abandoned · verified Tue Apr 21

Slick is a standalone JavaScript library designed for parsing CSS selectors into an object representation and finding DOM nodes based on those selectors. It provides a 'Finder' component for querying the DOM (with methods like `search` and `find`) and a 'Parser' component for converting CSS selector strings into a structured JavaScript object. The current stable version, 1.12.2, was released nine years ago, indicating the project is no longer actively maintained. Key differentiators include its ability to parse selectors into a detailed object, support for custom pseudo-classes, and functionality for searching within XML documents. Its primary use case is in environments where a lightweight, self-contained selector engine is required without external dependencies. This library predates modern browser native selector APIs and module systems, making it primarily a legacy solution.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Slick in a Node.js environment. It shows how to initialize `slick` using CommonJS `require`, parse a CSS selector string into an object, and then use the `search`, `find`, and `matches` methods to query a simulated DOM structure provided by `jsdom`. The `jsdom` library is used here solely for demonstration purposes to create a browser-like DOM context within Node.js, and is not a dependency of `slick` itself.

const { JSDOM } = require('jsdom');
const slick = require('slick');

// Setup a simple DOM environment for demonstration
const dom = new JSDOM(`
  <!DOCTYPE html>
  <html>
  <body>
    <div id="container">
      <ul class="list">
        <li>Item 1</li>
        <li class="active">Item 2</li>
      </ul>
      <p>A paragraph</p>
      <ul class="list">
        <li>Another Item</li>
      </ul>
    </div>
  </body>
  </html>
`);
const document = dom.window.document;

console.log('--- Using slick.parse ---\n');
const selectorObject = slick.parse('ul.list > li.active');
console.log('Parsed Selector Object:', JSON.stringify(selectorObject, null, 2));

console.log('\n--- Using slick.search ---\n');
const foundElements = slick.search('ul.list > li.active', document);
console.log('Found Elements:', foundElements.map(el => el.outerHTML));

console.log('\n--- Using slick.find ---\n');
const firstFoundElement = slick.find('li:first-child', document);
console.log('First Found Element:', firstFoundElement ? firstFoundElement.outerHTML : 'None');

console.log('\n--- Using slick.matches ---\n');
const targetElement = document.querySelector('.active');
const matches = slick.matches(targetElement, 'li.active');
console.log(`Does <li class="active"> match 'li.active'? ${matches}`);

view raw JSON →