{"library":"nwmatcher","title":"NWMatcher CSS Selector Engine","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nwmatcher"],"cli":null},"imports":["import * as nwmatcher from 'nwmatcher';","import * as nwmatcher from 'nwmatcher'; const NW = nwmatcher.init(window);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { JSDOM } from 'jsdom';\nimport * as nwmatcher from 'nwmatcher'; // For TypeScript with CommonJS interop\n\n// 1. Setup a JSDOM environment to simulate a browser DOM\nconst dom = new JSDOM(`<!DOCTYPE html>\n<html>\n  <head>\n    <title>NWMatcher Example</title>\n  </head>\n  <body>\n    <div id=\"container\">\n      <p class=\"text important\">First paragraph</p>\n      <span class=\"text\">A span element</span>\n      <p class=\"text\">Second paragraph</p>\n      <a href=\"#\" class=\"link\">Click here</a>\n    </div>\n    <div id=\"other-container\">\n      <p>Another paragraph in a different container</p>\n      <button id=\"myButton\" disabled>Submit</button>\n    </div>\n  </body>\n</html>`);\n\nconst { window } = dom;\nconst document = window.document;\n\n// 2. Initialize NWMatcher with the JSDOM window object for headless environments.\n// In a browser, NW.Dom would typically be available globally.\nconst NW = nwmatcher.init(window);\n\nconsole.log('--- DOM Selection Examples ---');\n\n// Select all elements matching a selector\nconst allParagraphs = NW.select('p', document);\nconsole.log(`Found ${allParagraphs.length} paragraphs.`);\n// Expected: 3 paragraphs\n\n// Select the first element matching a selector\nconst firstImportantParagraph = NW.first('p.important', document);\nconsole.log(`First important paragraph text: \"${firstImportantParagraph?.textContent}\"`);\n// Expected: \"First paragraph\"\n\n// Match an element against a selector\nconst targetElement = document.getElementById('myButton');\nconst matchesDisabled = NW.match(targetElement, ':disabled');\nconsole.log(`Button matches ':disabled': ${matchesDisabled}`);\n// Expected: true\n\n// Using context for selection\nconst container = document.getElementById('container');\nconst paragraphsInContainer = NW.select('.text', container);\nconsole.log(`Found ${paragraphsInContainer.length} elements with class 'text' within #container.`);\n// Expected: 3 (p.text.important, span.text, p.text)\n\nconsole.log('\\n--- Engine Configuration Example ---');\n// Disable native Query Selector API usage and suppress errors for demonstration\nNW.configure({ USE_QSAPI: false, VERBOSITY: false, LOGERRORS: false });\nconsole.log('NWMatcher configured to not use native QSA and suppress errors.');\n\n// Re-run a selection with new configuration\nconst allLinks = NW.select('a.link');\nconsole.log(`Found ${allLinks.length} links after re-configuration.`);\n// Expected: 1 link\n\n// Example of DOM helper method\nconst myButtonElement = NW.byId('myButton');\nconsole.log(`Found button by ID: ${myButtonElement?.id}`);\n// Expected: \"myButton\"\n\n// Example of attribute getter\nconst linkHref = NW.getAttribute(allLinks[0], 'href');\nconsole.log(`Href of the link: ${linkHref}`);\n// Expected: \"#\" ","lang":"typescript","description":"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`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}