NWMatcher is a fast and W3C CSS3-compliant JavaScript selector engine, currently at version 1.4.4. It provides robust methods for selecting, matching, and traversing DOM elements using CSS selectors, aiming for behavior consistent with modern web browsers. The library is actively maintained through regular bug fixes, performance enhancements, and improved compliance with CSS specifications, particularly for headless environments like Node.js with JSDOM. It distinguishes itself by offering a reliable, standalone solution for scenarios where native `querySelectorAll` might not be available, or when fine-grained control over selector parsing and matching is required. This includes comprehensive support for CSS2/CSS3 selectors, pseudo-classes, and extensive configuration options to tailor its behavior. Recent releases focus on addressing specific behavioral quirks, optimizing DOM traversal, and ensuring broad W3C compatibility across diverse environments.
npm install nwmatcherVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing NWMatcher in a Node.js JSDOM environment, performing various DOM selections, matching elements, and configuring engine options. It covers `select`, `first`, `match`, `byId`, `getAttribute` methods and `configure`.
Ensure all class names in selectors match the exact casing used in the HTML/DOM. Prior to v1.4.1, case-insensitivity might have been tolerated, leading to unexpected failures post-update.
Review selectors that might implicitly target or interact with non-Element nodes (e.g., text nodes, comments). Ensure selectors are robust and explicitly target `Element` nodes where necessary.
Always explicitly configure NWMatcher using `NW.configure({ USE_QSAPI: true, SIMPLENOT: false })` or similar, depending on your needs. For full CSS3 compliance and performance, it's often best to enable native QSA and allow complex `:not()` selectors unless specific compatibility issues arise.For environments without native `querySelectorAll` (or where it's buggy), consider using `nwmatcher-noqsa.js` and configuring NWMatcher with `USE_QSAPI: false` to force its internal engine. For Node.js with JSDOM, using the full version with `USE_QSAPI: true` is generally fine.
Instead of expecting a global `NW.Dom`, use `const nwmatcher = require('nwmatcher'); const NW = nwmatcher.init(window);` (for CommonJS) or `import * as nwmatcher from 'nwmatcher'; const NW = nwmatcher.init(window);` (for ES Modules/TypeScript with JSDOM's `window` object).Ensure that the casing of class names in your CSS selectors precisely matches the casing of the `class` attributes in your HTML/DOM structure.
Ensure `NW.configure({ VERBOSITY: false, LOGERRORS: false });` is used to suppress both thrown exceptions and console logging. If issues persist, verify if a specific 'shunt' method or configuration option was introduced or needs to be set.No dependency data recorded yet.