# extra-dom

> Utilities for DOM

Latest version **0.7.3** (published 2026-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install extra-dom
pnpm add extra-dom
yarn add extra-dom
bun add extra-dom
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.7.3 |
| Published | 2026-01-19 |
| First published | 2020-12-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 3 |
| Unpacked size | 71.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | BlackGlory |
| Maintainers | black_glory |
| Keywords | dom, node, tree, traverse, map, flatMap, filter, find, stringify, parse |

## Links

- npm: https://www.npmjs.com/package/extra-dom
- Repository: https://github.com/BlackGlory/extra-dom
- Homepage: https://github.com/BlackGlory/extra-dom#readme
- Issues: https://github.com/BlackGlory/extra-dom/issues
- npm.io page: https://npm.io/package/extra-dom

## Dependencies (3)

- [jsdom](https://npm.io/package/jsdom.md) ^27.4.0
- [iterable-operator](https://npm.io/package/iterable-operator.md) ^5.1.0
- [@blackglory/prelude](https://npm.io/package/@blackglory/prelude.md) ^0.4.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.7.3 (latest) — 2026-01-19
- 0.7.2 — 2026-01-15
- 0.7.1 — 2026-01-15
- 0.7.0 — 2026-01-14
- 0.6.2 — 2025-08-29
- 0.6.1 — 2023-06-10
- 0.6.0 — 2023-02-23
- 0.5.4 — 2023-02-23
- 0.5.3 — 2022-11-01
- 0.5.2 — 2022-10-31
- 0.5.1 — 2022-08-01
- 0.5.0 — 2021-12-31
- 0.4.5 — 2021-12-16
- 0.4.4 — 2021-12-12
- 0.4.3 — 2021-10-14
- … 40 more at https://npm.io/package/extra-dom/versions

## README

# extra-dom
Utilities for DOM.

## Install
```sh
npm install --save extra-dom
# or
yarn add extra-dom
```

## API
### flatMap
```ts
function flatMap(node: Node, fn: (node: Node) => Node[]): Node[]
```

Traverse the node tree and do `flatMap`.

- `[]`: remove current node
- `[node]`: replace current node
- `[node1, node2, ...nodeN]`: replace current node with more nodes

### map
```ts
function map(node: Node, fn: (node: Node) => Node): Node
```

Traverse the node tree and do `map`.

### filter
```ts
function filter(node: Node, predicate: (node: Node) => unknown): Node | undefined
```

Traverse the node tree and do `filter`.

### unwrap
```ts
function unwrap(node: Node, predicate: (node: Node) => unknown): Node[]
```

Traverse the node tree and do `unwrap`.

### find
```ts
function find(node: Node, predicate: (node: Node) => unknown): Node | undefined
```

Traverse the node tree and do `find`.

### parseNodes
```ts
function parseNodes(htmlFragment: string): Node[]
```

### stringifyNodes
```ts
function stringifyNodes(nodes: Node[]): string
```

### parseFragment
```ts
function parseFragment(htmlFragment: string): DocumentFragment
```

### stringifyFragment
```ts
function stringifyFragment(fragment: DocumentFragment): string
```

### removeAllChildren
```ts
function removeAllChildren(node: Node): void
```

### removeAttributes
```ts
function removeAttributes(
  node: Node
, predicate?: (attributeName: string) => unknown
): void
```

### getBySelector
```ts
function getBySelector<T extends Element>(
  this: void | Element | Document
, selectors: string
): T
```

Return the first matched element.

If cannot find any elements, it throws.

### getAllBySelector
```ts
function getAllBySelector<T extends Element>(
  this: void | Element | Document
, selectors: string
): T[]
```

Return matched elements.

If cannot find any elements, it throws.

### traverseAncestorNodes
```ts
function traverseAncestorNodes(node: Node): Iterable<Node & ParentNode>
```

### traverseDescendantNodes
```ts
function traverseDescendantNodes(node: Node): Iterable<ChildNode>
```

### traversePrecedingSiblingNodes
```ts
function traversePrecedingSiblingNodes(node: Node): Iterable<Node>
```

### traverseFollowingSiblingNodes
```ts
function traverseFollowingSiblingNodes(node: Node): Iterable<Node>
```

### traverseDescendantNodes
```ts
function traverseDescendantNodes(node: Node): Iterable<ChildNode>
```

### findInAncestorNodes
```ts
function findInAncestorNodes(
  node: Node
, predicate: (node: Node & ParentNode) => unknown
): (Node & ParentNode) | undefined
```

### findInDescendantNodes
```ts
function find(node: Node, predicate: (node: ChildNode) => unknown): ChildNode | undefined
```

### findInPrecedingSiblingNodes
```ts
function findInPrecedingSiblingNodes(
  node: Node
, predicate: (node: Node) => unknown
): Node | undefined
```

This function uses `Node.previousSibling` to traverse the preceding sibling nodes.

### findInFollowingSiblingNodes
```ts
function findInFollowingSiblingNodes(
  node: Node
, predicate: (node: Node) => unknown
): Node | undefined
```

This function uses `Node.nextSibling` to traverse the following sibling nodes.

### parentNode
```ts
function parentNode(node: Node, distance: number = 1): (Node & ParentNode) | undefined
```

### nextSibling
```ts
function nextSibling(node: Node, distance: number = 1): ChildNode | undefined
```

### previousSibling
```ts
function previousSibling(node: Node, distance: number = 1): ChildNode | undefined
```

### nextElementSibling
```ts
function nextElementSibling(node: Node, distance: number = 1): Element | undefined
```

### previousElementSibling
```ts
function previousElementSibling(node: Node, distance: number = 1): Element | undefined
```

### isDocument
```ts
function isDocument(val: unknown): val is Document
```

### isntDocument
```ts
function isntDocument<T>(val: T): val is Exclude<T, Document>
```

### isElement
```ts
function isElement(val: unknown): val is Element
```

### isntElement
```ts
function isntElement<T>(val: T): val is Exclude<T, Element>
```

### isTextNode
```ts
function isTextNode(val: unknown): val is Text
```

### isntTextNode
```ts
function isntTextNode<T>(val: unknown): node is Exclude<T, Text>
```

### isNode
```ts
function isNode(val: unknown): val is Node
```

### isntNode
```ts
function isntNode<T>(val: T): val is Exclude<T, Node>
```

### isParentNode
```ts
function isParentNode(val: unknown): val is Node & ParentNode
```

### isntParentNode
```ts
function isntParentNode<T>(val: unknown): val is Exclude<T, Node & ParentNode>
```

### isBefore
```ts
function isBefore(subject: Node, object: Node): boolean
```

### isAfter
```ts
function isAfter(subject: Node, object: Node): boolean
```

### createDOMParser
```ts
function createDOMParser(): DOMParser
```

### NodeType
```ts
enum NodeType {
  ELEMENT_NODE
, ATTRIBUTE_NODE
, TEXT_NODE
, CDATA_SECTION_NODE
, ENTITY_REFERENCE_NODE
, ENTITY_NODE
, PROCESSING_INSTRUCTION_NODE
, COMMENT_NODE
, DOCUMENT_NODE
, DOCUMENT_TYPE_NODE
, DOCUMENT_FRAGMENT_NODE
, NOTATION_NODE
}
```

### XPathResultType
```ts
enum XPathResultType {
  ANY_TYPE
, NUMBER_TYPE
, STRING_TYPE
, BOOLEAN_TYPE
, UNORDERED_NODE_ITERATOR_TYPE
, ORDERED_NODE_ITERATOR_TYPE
, UNORDERED_NODE_SNAPSHOT_TYPE
, ORDERED_NODE_SNAPSHOT_TYPE
, ANY_UNORDERED_NODE_TYPE
, FIRST_ORDERED_NODE_TYPE
}
```

---
_Source: https://npm.io/package/extra-dom · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
