https://github.com/brettz9/handle-node
Simple callback delegator based on DOM Node type
https://github.com/brettz9/handle-node
delegator dom dom-node reducer
Last synced: 28 days ago
JSON representation
Simple callback delegator based on DOM Node type
- Host: GitHub
- URL: https://github.com/brettz9/handle-node
- Owner: brettz9
- License: mit
- Created: 2015-01-30T18:03:31.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2025-11-06T08:24:00.000Z (4 months ago)
- Last Synced: 2025-11-06T08:31:56.407Z (4 months ago)
- Topics: delegator, dom, dom-node, reducer
- Language: JavaScript
- Size: 766 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE-MIT.txt
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/handle-node)
[](https://david-dm.org/brettz9/handle-node)
[](https://david-dm.org/brettz9/handle-node?type=dev)
[](https://github.com/brettz9/handle-node/actions)
[](https://github.com/brettz9/handle-node/actions)
[](https://snyk.io/test/github/brettz9/handle-node)
[](https://lgtm.com/projects/g/brettz9/handle-node/alerts)
[](https://lgtm.com/projects/g/brettz9/handle-node/context:javascript)
[](LICENSE-MIT.txt)
# handle-node
Simple callback delegator based on DOM Node type.
Provides an alternative to switches, numeric constant-based delegation, and
DOM `NodeIterator`s or `TreeWalker`s.
## Installation
```shell
npm install handle-node
```
### Native ESM
```js
import handleNode from 'handle-node';
```
### CommonJS
```js
const handleNode = require('handle-node');
```
### Browser (any)
```html
```
### Browser (ESM - modern browsers only)
```html
import handleNode from './node_modules/handle-node/dist/index-esm.js';
```
## Usage
Supply a node followed by a handler object whose all optional properties
(human-readable Node type names) should be set to a callback which will
be passed the supplied Node and, always as the last argument, a reference
to the object on which the callbacks exist. The return value will be
`undefined` if a handler is missing, but otherwise will be the result of
invoking the callback which corresponds to the supplied node's type.
Here is a demonstration reimplementing `textContent` (if only element
and text types are known to be present):
```js
const textContent = handleNode(node, { // This object is `textSerializer`
element ({childNodes}, textSerializer) {
return [...childNodes].reduce((str, node) => {
return str + handleNode(node, textSerializer);
}, '');
},
text: ({nodeValue}) => nodeValue
});
```
Other arguments can also be passed in after the
handler object, and these will also be supplied to the callbacks:
```js
const textContent = handleNode(
node,
{ // This object is `textSerializer`
element ({childNodes}, arg1, arg2, textSerializer) {
return [...childNodes].reduce((str, node) => {
return str + handleNode(node, arg1, arg2, textSerializer);
}, '');
},
text: ({nodeValue}, arg1, arg2) => nodeValue
}, arg1, arg2
);
```
## API
The handler object can take the following optional properties:
- `element`
- `attribute`
- `text`
- `cdata`
- `entityReference`
- `entity`
- `processingInstruction`
- `comment`
- `document`
- `documentType`
- `documentFragment`
- `notation`