Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expand-text-nodes

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expand-text-nodes

Expand text within HTML into individual text nodes for each character.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

expand-text-nodes

Expand text within HTML into individual text nodes for each character.

Why?

By default, text embedded in HTML is rendered as a single text node for each continuous sequence of text. For example, <p>Hello.</p> is rendered as a paragraph element with a single text node within:

unexpanded text nodes example

But if you're handling text character-by-character, or performing a text animation like TypeIt, you might need that text to be broken up as individual text nodes -- one node per character:

expanded text nodes example

This little library does that.

Usage

Pass a node that contains text into expandTextNodes(), and you're done.

<p>Hello.</p>

<script type="module">
  import expandTextNodes from "path/to/expand-text-nodes";

  expandTextNodes(document.querySelector("p"));
</script>

It also handles nested HTML. For example, something like this:

<p>Hi, <em>pal.</em></p>

<script type="module">
  import expandTextNodes from "path/to/expand-text-nodes";

  expandTextNodes(document.querySelector("p"));
</script>

...will render as something like this:

nested text nodes example

Works for emojis too!

emoji example

The End

There's not much to it.

Keywords

FAQs

Package last updated on 24 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc