@asamuzakjp/nwsapi

中文文档
2.3.9发布于 2025/03/28MITCommonJS零依赖近 30 天下载 1.3亿

Fast CSS Selectors API Engine

原始使用文档
文档目录8 节
  1. Installation
  2. Supported Selectors
  3. Features and Compliance
  4. API
  5. DOM Selection
  6. DOM Helpers
  7. Engine Configuration
  8. Examples on extending the basic functionalities

NWSAPI

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 NWSAPI 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:

  • IDS_DUPES: true - true to allow using multiple elements having the same id, false to disallow
  • LIVECACHE: true - true for caching both results and resolvers, false for caching only resolvers
  • MIXEDCASE: true - true to match tag names case insensitive, false to match using case sensitive
  • LOGERRORS: true - true to print errors and warnings to the console, false to mute both of them

Examples on extending the basic functionalities

configure( { <configuration-flag>: [ true | false ] } )

Disable logging errors/warnings to console, disallow duplicate ids. Example:

NW.Dom.configure( { LOGERRORS: false, IDS_DUPES: false } );

NOTE: NW.Dom.configure() without parameters return the current configuration.

registerCombinator( symbol, resolver )

Registers a new symbol and its matching resolver in the combinators table. Example:

NW.Dom.registerCombinator( '^', 'e.parentElement' );

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, the matching RE and the resolver function, in the selectors table. Example:

NW.Dom.registerSelector('Controls', /^\:(control)(.*)/i,
  (function(global) {
    return function(match, source, mode, callback) {
      var status = true;
      source = 'if(/^(button|input|select|textarea)/i.test(e.nodeName)){' + source + '}';
      return { 'source': source, 'status': status };
    };
  })(this));

版本与发布记录

@asamuzakjp/nwsapi 共发布 36 个版本,首次发布于 2024/05/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.9latest2025/03/281.6 年前
2.3.82025/03/221.6 年前
2.3.72025/03/191.6 年前
2.3.62025/03/181.6 年前
2.3.52025/03/181.6 年前
2.3.42025/03/121.7 年前

关于 @asamuzakjp/nwsapi 的常见问题

@asamuzakjp/nwsapi 是什么?

Fast CSS Selectors API Engine

如何安装 @asamuzakjp/nwsapi?

在项目目录运行 npm install @asamuzakjp/nwsapi,或使用 pnpm add / yarn add / bun add。

@asamuzakjp/nwsapi 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/asamuzakjp__nwsapi 是否存在。

@asamuzakjp/nwsapi 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@asamuzakjp/nwsapi 可以商用吗?

@asamuzakjp/nwsapi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@asamuzakjp/nwsapi 的下载量有多少?

根据 npm 官方下载统计,@asamuzakjp/nwsapi 在 2026/10/07 – 2026/11/05 这 30 天内被下载 128,333,115 次,日均约 4,277,771 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@asamuzakjp/nwsapi 还在维护吗?

最新版本 2.3.9 发布于 2025/03/28(1.6 年前);至今共发布 36 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。