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.
Maintainers
Readme
html-escape-lite-ts
Blazing-fast, zero-dependency HTML entity escape and unescape utility with HTML tag stripping in TypeScript.
💡 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.
',/, ).
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, andstripHtmlfunctions.
📦 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);
// => '<script>alert("XSS")</script> & "cookies"'
// 2. Unescape entities back to standard text
const decoded = unescapeHtml('<b>Bold & Beautiful</b>');
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 & 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 & Jerry 'Special' / 2026';
console.log(unescapeHtml(legacyText));
// => "Tom & Jerry 'Special' / 2026"📖 API Reference
escapeHtml(input)
Replaces &, <, >, ", and ' with their corresponding safe HTML entities:
&→&<→<>→>"→"'→'Parameters:
input(string|null|undefined)Returns:
string(escaped string, returns empty string for nullish inputs)
unescapeHtml(input)
Decodes common HTML named entities (&, <, >, ", ', ', ) 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
