Fast CSS Selectors API Engine that serves as a cross-browser replacement for native CSS selection and matching functionality
npx @tessl/cli install tessl/npm-nwsapi@2.2.0NWSAPI is a fast CSS Selectors API Engine that serves as a cross-browser replacement for native CSS selection and matching functionality. It provides a right-to-left selector parser and compiler written in pure JavaScript with no external dependencies, originally developed as the successor to NWMATCHER and aimed at CSS Selectors Level 4 conformance.
The library uses regular expressions to parse CSS selector strings and metaprogramming to transform them into optimized JavaScript function resolvers, with memoization for superior performance. It provides comprehensive CSS selector support including advanced pseudo-classes like :has(), :is(), :where(), and :defined.
npm install nwsapiconst nwsapi = require("nwsapi");For AMD:
define(["nwsapi"], function(nwsapi) {
// Use nwsapi
});For browser global:
<script type="text/javascript" src="nwsapi.js"></script>
<script>
var DOM = NW.Dom;
</script>const nwsapi = require("nwsapi");
// Select all elements with class 'highlight'
const elements = nwsapi.select('.highlight', document);
// Test if element matches selector
const matches = nwsapi.match('div.container', element);
// Find first matching element
const firstElement = nwsapi.first('p:first-child', document);
// Find closest ancestor matching selector
const ancestor = nwsapi.closest('ul', element);NWSAPI is built around several key components:
Core CSS selector-based element selection functionality. Provides comprehensive querying capabilities with high performance through compiled resolvers.
function select(selector, context, callback);
function match(selector, element, callback);
function first(selector, context, callback);
function closest(selector, context, callback);Fast helper methods for common element lookup operations by ID, tag name, and class name, with optional context filtering.
function byId(id, from);
function byTag(tag, from);
function byClass(class, from);Configuration system for customizing engine behavior including duplicate ID handling, caching, case sensitivity, and error logging.
function configure(options);
function emit(message, proto);Advanced selector compilation functionality for creating optimized resolver functions and managing the compilation pipeline.
function compile(selector, source, mode);Plugin system for extending NWSAPI with custom selectors, attribute operators, and combinators.
function registerSelector(name, rexp, func);
function registerOperator(operator, resolver);
function registerCombinator(combinator, resolver);System for installing NWSAPI as a replacement for native browser querySelector methods and restoring original functionality.
function install(callback);
function uninstall(callback);// Version string
const Version: string;
// Current configuration object
const Config: ConfigOptions;
// Registered operators registry
const Operators: { [operator: string]: OperatorResolver };
// Registered selectors registry
const Selectors: { [name: string]: { Expression: RegExp; Callback: Function } };
// Engine compilation state snapshot
const Snapshot: object;// Configuration object
interface ConfigOptions {
IDS_DUPES?: boolean; // Allow duplicate IDs (default: true)
LIVECACHE?: boolean; // Cache results and resolvers (default: true)
MIXEDCASE?: boolean; // Case insensitive tag matching (default: true)
LOGERRORS?: boolean; // Log errors to console (default: true)
}
// Callback function for element operations
type ElementCallback = (element: Element) => void;
// Selector resolver function
type SelectorResolver = (match: RegExpMatchArray, source: string, mode: number, callback?: Function) => { source: string; status: boolean };
// Operator resolver configuration
interface OperatorResolver {
p1: string;
p2: string;
p3: string;
}