Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fregante/zip-text-nodes

Merge the DOM of 2 elements with the same textContent.
https://github.com/fregante/zip-text-nodes

Last synced: 29 days ago
JSON representation

Merge the DOM of 2 elements with the same textContent.

Awesome Lists containing this project

README

        

# zip-text-nodes [![][badge-gzip]][link-bundlephobia]

[badge-gzip]: https://img.shields.io/bundlephobia/minzip/zip-text-nodes.svg?label=gzipped
[link-bundlephobia]: https://bundlephobia.com/result?p=zip-text-nodes

> Merge the DOM of 2 elements with the same textContent.

Given 2 elements:

```html
Hello, world!
```

and:

```html
Hello, world!
```

they are merged into:

```html
Hello, world!
```

This can be useful when running some transformations on the content of an element and successively merging the results or restoring the original markup.

```js
const base = <>I live in Italy>;
const grammar = highlightVerb(base);
// <>I live in Italy> // e.g. the link was lost

zipTextNodes(base, grammar);
// <>I live in Italy>
// The new `em` is copied from `grammar` to `base`
```

Supports overlapping and nested elements.

## Install

```
npm install zip-text-nodes
```

## Setup

```js
const zipTextNodes = require('zip-text-nodes');
```

```js
import zipTextNodes from 'zip-text-nodes';
```

## API

### zipTextNodes(target, source)

#### target

Type: `Element` `DocumentFragment`

The element into which the new children are copied. This element is modified.

#### source

Type: `Element` `DocumentFragment`

The element from which the new children are copied.

# Related

- [insert-text-textarea](https://github.com/fregante/insert-text-textarea) - Insert text in a textarea (supports Firefox and Undo).
- [fit-textarea](https://github.com/fregante/fit-textarea) - Automatically expand a `` to fit its content, in a few bytes.
- [delegate-it](https://github.com/fregante/delegate-it) - DOM event delegation, in <1KB.