What is nwsapi?
The nwsapi npm package is a modern and efficient CSS selector engine that allows developers to query the DOM using CSS selectors. It is designed to be fast and fully compliant with the most recent CSS specifications.
What are nwsapi's main functionalities?
DOM Selection
This feature allows you to select DOM elements using CSS selectors. The code sample demonstrates how to select all elements with the class 'className' that are direct children of a 'div' element.
const nwsapi = require('nwsapi');
const doc = nwsapi(document);
const elements = doc.querySelectorAll('div > .className');
Matches Selector
This feature checks if a given element matches a specific CSS selector. The code sample demonstrates how to check if the element with the id 'myElement' also has the class 'someClass'.
const nwsapi = require('nwsapi');
const doc = nwsapi(document);
const element = document.getElementById('myElement');
const isMatch = doc.matchesSelector(element, '#myElement.someClass');
Custom Pseudo-classes
This feature allows you to define custom pseudo-classes for more complex queries. The code sample demonstrates how to define a custom pseudo-class that selects elements with a 'data-custom' attribute.
const nwsapi = require('nwsapi');
const doc = nwsapi(document);
doc.definePseudo('custom', function(elem) {
return elem.hasAttribute('data-custom');
});
const customElements = doc.querySelectorAll(':custom');
Other packages similar to nwsapi
sizzle
Sizzle is a pure-JavaScript CSS selector engine designed to be easily dropped in to a host library. It is one of the most well-known selector engines and was used in jQuery. Compared to nwsapi, Sizzle has a wider adoption but may not be as up-to-date with the latest CSS selector specifications.
css-select
css-select is a CSS selector engine that aims to be CSS4 compliant. It can be used to select elements from an object representing a parsed HTML or XML document. It is similar to nwsapi in terms of compliance with CSS specifications but differs in API and implementation details.
qwery
Qwery is a modern selector engine which can query elements quickly and efficiently. It is smaller and more focused on modern browsers compared to nwsapi, which might make it a better choice for projects that do not require legacy browser support.
Fast CSS Selectors API Engine
NWSAPI is the development progress of NWMATCHER aiming at Selectors Level 4 conformance. It has been completely reworked to be easily extended and maintained. It is a right-to-left selector parser and compiler written in pure Javascript with no external dependencies. It was initially thought as a cross browser library to improve event delegation and web page scraping in various frameworks but it has become a popular replacement of the native CSS selection and matching functionality in newer browsers and headless environments.
It uses regular expressions to parse CSS selector strings and metaprogramming to transforms these selector strings into Javascript function resolvers. This process is executed only once for each selector string allowing memoization of the function resolvers and achieving unmatched performances.
Installation
To include NWSAPI in a standard web page:
<script type="text/javascript" src="nwsapi.js"></script>
To include NWSAPI in a standard web page and automatically replace the native QSA:
<script type="text/javascript" src="nwsapi.js" onload="NW.Dom.install()"></script>
To use it with Node.js:
$ npm install nwsapi
NWSAPI currently supports browsers (as a global, NW.Dom
) and headless environments (as a CommonJS module).
Supported Selectors
Here is a list of all the CSS2/CSS3/CSS4 Supported selectors.
Features and Compliance
You can read more about NWSAPI features and compliance on the wiki.
API
DOM Selection
ancestor( selector, context, callback )
Returns a reference to the nearest ancestor element matching selector
, starting at context
. Returns null
if no element is found. If callback
is provided, it is invoked for the matched element.
first( selector, context, callback )
Returns a reference to the first element matching selector
, starting at context
. Returns null
if no element matches. If callback
is provided, it is invoked for the matched element.
match( selector, element, callback )
Returns true
if element
matches selector
, starting at context
; returns false
otherwise. If callback
is provided, it is invoked for the matched element.
select( selector, context, callback )
Returns an array of all the elements matching selector
, starting at context
; returns empty Array
otherwise. If callback
is provided, it is invoked for each matching element.
DOM Helpers
byId( id, from )
Returns a reference to the first element with ID id
, optionally filtered to descendants of the element from
.
byTag( tag, from )
Returns an array of elements having the specified tag name tag
, optionally filtered to descendants of the element from
.
byClass( class, from )
Returns an array of elements having the specified class name class
, optionally filtered to descendants of the element from
.
Engine Configuration
configure( options )
The following is the list of currently available configuration options, their default values and descriptions, they are boolean flags that can be set to true
or false
:
BUGFIX_ID
: true - true to bugfix forms when using reserved words for controlsSIMPLENOT
: true - true to disallow complex selectors nested in ':not()' classesUSE_HTML5
: true - true to use HTML5 specs for ":checked" and similar UI statesVERBOSITY
: true - true to throw exceptions, false to skip throwing exceptionsLOGERRORS
: true - true to print console errors or warnings, false to mute them
Example:
NW.Dom.configure( { LOGERRORS: false, VERBOSITY: false } );
registerCombinator( symbol, resolver )
Registers a new symbol and its matching resolver in the combinators table.
registerOperator( symbol, resolver )
Registers a new symbol and its matching resolver in the attribute operators table. Example:
NW.Dom.registerOperator( '!=', { p1: '^', p2: '$', p3: 'false' } );
registerSelector( name, rexp, func )
Registers a new selector, with the matching regular expression and the appropriate resolver function, in the selectors table.