npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

html-escape-lite-ts

v1.0.0

Published

Ultra-fast (<1 KB), zero-dependency HTML entity encoder and decoder for preventing XSS in templates and text rendering.

Readme

html-escape-lite-ts

Blazing-fast, zero-dependency HTML entity escape and unescape utility with HTML tag stripping in TypeScript.

npm version npm downloads license bundle size sponsor


💡 Why html-escape-lite-ts?

Preventing Cross-Site Scripting (XSS) and preparing text for safe HTML rendering is a critical security requirement. However, existing packages are often:

  • Overcomplicated with heavy regular expression dependencies.
  • Tied specifically to client-side DOM APIs like document.createElement('div') which instantly crash in Node.js, Next.js SSR, and Edge runtimes.
  • Missing support for unescaping named and numeric entities (e.g. &#39;, &#x2F;, &nbsp;).

html-escape-lite-ts is:

  • Zero Dependencies: Pure TypeScript with no external libraries.
  • Isomorphic: Runs identically in Browser, Node.js, Deno, Bun, Cloudflare Workers, and Vercel Edge.
  • Blazing Fast: Optimized character replacement regexes designed for high throughput.
  • Complete Suite: Includes escapeHtml, unescapeHtml, and stripHtml functions.

📦 Installation

# npm
npm install html-escape-lite-ts

# pnpm
pnpm add html-escape-lite-ts

# yarn
yarn add html-escape-lite-ts

# bun
bun add html-escape-lite-ts

🚀 Quick Start

import { escapeHtml, unescapeHtml, stripHtml } from 'html-escape-lite-ts';

// 1. Escape unsafe HTML entities
const rawUserInput = '<script>alert("XSS")</script> & "cookies"';
const safeHtml = escapeHtml(rawUserInput);
console.log(safeHtml);
// => '&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt; &amp; &quot;cookies&quot;'

// 2. Unescape entities back to standard text
const decoded = unescapeHtml('&lt;b&gt;Bold &amp; Beautiful&lt;/b&gt;');
console.log(decoded);
// => '<b>Bold & Beautiful</b>'

// 3. Strip all HTML tags entirely
const cleaned = stripHtml('<p>Welcome to <strong>Antigravity</strong>! Click <a href="#">here</a>.</p>');
console.log(cleaned);
// => 'Welcome to Antigravity! Click here.'

🛠️ Real-World Examples

1. Server-Side Rendering (SSR) Template Injection Guard

Safely inject user-generated comments or profiles into raw HTML emails or templates without triggering XSS vulnerabilities:

import { escapeHtml } from 'html-escape-lite-ts';

interface UserComment {
  author: string;
  body: string;
}

export function renderCommentHtml(comment: UserComment): string {
  const safeAuthor = escapeHtml(comment.author);
  const safeBody = escapeHtml(comment.body);

  return `
    <div class="comment">
      <span class="author">${safeAuthor}</span>
      <p class="body">${safeBody}</p>
    </div>
  `;
}

2. Generating Plain-Text Email Previews from Rich-Text HTML

Extract preview snippets for email notifications or push notifications by stripping all markup tags:

import { stripHtml, unescapeHtml } from 'html-escape-lite-ts';

export function makeEmailSnippet(htmlContent: string, maxLength = 100): string {
  // Strip tags and decode entities
  const plainText = unescapeHtml(stripHtml(htmlContent)).trim();
  
  if (plainText.length <= maxLength) {
    return plainText;
  }
  return plainText.slice(0, maxLength).trim() + '...';
}

const richEmail = '<h1>Welcome!</h1><p>We are <em>thrilled</em> to have you on board &amp; ready to start.</p>';
console.log(makeEmailSnippet(richEmail, 45));
// => 'Welcome! We are thrilled to have you on board...'

3. XML / SVG Attribute Sanitization

Sanitize dynamic parameters before placing them into XML feeds or SVG graphics:

import { escapeHtml } from 'html-escape-lite-ts';

export function createSvgBadge(label: string, value: string): string {
  return `
    <svg xmlns="http://www.w3.org/2000/svg" width="120" height="20">
      <text x="5" y="14">${escapeHtml(label)}</text>
      <text x="60" y="14">${escapeHtml(value)}</text>
    </svg>
  `;
}

4. Handling Numeric and Hexadecimal HTML Entities

Decode legacy HTML feeds containing numeric unicode codes:

import { unescapeHtml } from 'html-escape-lite-ts';

const legacyText = 'Tom &#38; Jerry &#39;Special&#39; &#x2F; 2026';
console.log(unescapeHtml(legacyText));
// => "Tom & Jerry 'Special' / 2026"

📖 API Reference

escapeHtml(input)

Replaces &, <, >, ", and ' with their corresponding safe HTML entities:

  • & → &amp;

  • < → &lt;

  • > → &gt;

  • " → &quot;

  • ' → &#39;

  • Parameters: input (string | null | undefined)

  • Returns: string (escaped string, returns empty string for nullish inputs)


unescapeHtml(input)

Decodes common HTML named entities (&amp;, &lt;, &gt;, &quot;, &#39;, &apos;, &nbsp;) as well as numeric decimal (&#NN;) and hexadecimal (&#xHH;) unicode entities back to their raw characters.

  • Parameters: input (string | null | undefined)
  • Returns: string (decoded text)

stripHtml(input)

Strips all opening and closing HTML/XML tags (<...> and comments <!-- ... -->) from a string.

  • Parameters: input (string | null | undefined)
  • Returns: string (tag-free plain text)

🧪 Testing

Every release passes 100% test coverage verified with Vitest:

npm test

💖 Support the Project

If html-escape-lite-ts saved your web app from security vulnerabilities, please consider sponsoring:

👉 Sponsor @Piratha97 on GitHub Sponsors


📄 License

MIT © 2026 Pirapaakaran Pirathapan