Slick CSS Selector Engine
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
-
TypeError: slick.search is not a function
cause Attempting to call `slick.search` after an incorrect ESM import, or if `slick` was not properly assigned the module's export.fixEnsure you are importing the module correctly using `const slick = require('slick');` in a CommonJS environment. -
ReferenceError: slick is not defined
cause Trying to access the `slick` object in a Node.js environment without explicitly importing it first, or assuming it's a global variable.fixAdd `const slick = require('slick');` at the top of your JavaScript file to properly import and define the `slick` object. -
SyntaxError: Named export 'search' not found...
cause Attempting to use named imports (e.g., `import { search } from 'slick'`) from the `slick` module. Slick is a CommonJS module that exports a single object, not named exports.fixImport the entire module as a single object using `const slick = require('slick');` and then access methods as properties, e.g., `slick.search()`.
Warnings
- breaking The `slick` project appears to be abandoned, with no updates or commits in over nine years. This means there will be no bug fixes, performance improvements, or security patches for any discovered vulnerabilities.
- gotcha Slick is a CommonJS-only module and does not natively support ECMAScript Modules (ESM) `import` statements. Attempting to use `import` will likely result in module resolution errors or undefined behavior.
- gotcha Due to its age, `slick` may not fully support modern CSS selectors (e.g., `:is()`, `:where()`, `:has()`) or might have compatibility issues with newer browser features or edge cases. Its performance might also lag behind native browser implementations.
- gotcha The `slick` package does not provide TypeScript declaration files (`.d.ts`). Using it in a TypeScript project will result in type errors and lack of autocompletion without manual type declarations.
Install
-
npm install slick -
yarn add slick -
pnpm add slick
Imports
- slick
import slick from 'slick'
const slick = require('slick') - search
import { search } from 'slick'const slick = require('slick'); slick.search(...) - parse
import { parse } from 'slick'const slick = require('slick'); slick.parse(...)
Quickstart
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}`);