Flexible HTML and XML entity encoding with multiple output modes for different use cases and environments. Provides both unified API and specialized encoding functions.
Main encoding function that handles both HTML and XML entities with configurable output modes.
/**
* Encodes a string with entities using configurable options
* @param input - String to encode
* @param options - Encoding configuration or EntityLevel shorthand
* @returns Encoded string with characters converted to entities
*/
function encode(
input: string,
options?: EncodingOptions | EntityLevel
): string;
interface EncodingOptions {
/** The level of entities to support (default: EntityLevel.XML) */
level?: EntityLevel;
/** Output encoding format (default: EncodingMode.Extensive) */
mode?: EncodingMode;
}
enum EntityLevel {
/** Use XML entities only (&, <, >, ", ') */
XML = 0,
/** Use HTML entities (includes named entities like , ©) */
HTML = 1
}
enum EncodingMode {
/** UTF-8 encoded output, only XML characters escaped */
UTF8,
/** ASCII-only output with HTML escaping for non-ASCII characters */
ASCII,
/** Encode all characters that have entities + non-ASCII characters */
Extensive,
/** HTML attribute escaping following WHATWG specification */
Attribute,
/** HTML text escaping following WHATWG specification */
Text
}Usage Examples:
import { encode, EntityLevel, EncodingMode } from "entities";
// Basic XML encoding (default: extensive mode)
encode('<script>alert("XSS")</script>');
// Result: "<script>alert("XSS")</script>"
// UTF-8 optimized encoding (minimal escaping)
encode("Hello & 世界", { mode: EncodingMode.UTF8 });
// Result: "Hello & 世界"
// ASCII-only output
encode("Café & 世界", {
level: EntityLevel.HTML,
mode: EncodingMode.ASCII
});
// Result: "Café & 世界"
// HTML extensive encoding with named entities
encode("© 2023 → ♥", {
level: EntityLevel.HTML,
mode: EncodingMode.Extensive
});
// Result: "© 2023 → ♥"
// HTML attribute context encoding
encode('Say "Hello" & goodbye', {
level: EntityLevel.HTML,
mode: EncodingMode.Attribute
});
// Result: "Say "Hello" & goodbye"
// HTML text context encoding
encode("2 < 3 & 4 > 1", {
level: EntityLevel.HTML,
mode: EncodingMode.Text
});
// Result: "2 < 3 & 4 > 1"Specialized functions for HTML entity encoding with different coverage levels.
/**
* Encodes all characters using HTML entities, including valid ASCII characters
* Most comprehensive encoding - encodes characters like # and ; as entities
* @param input - String to encode
* @returns Extensively encoded string
*/
function encodeHTML(input: string): string;
/**
* Encodes only non-ASCII characters and HTML-invalid characters
* More compact than encodeHTML - leaves valid ASCII characters unencoded
* @param input - String to encode
* @returns Selectively encoded string
*/
function encodeNonAsciiHTML(input: string): string;Usage Examples:
import { encodeHTML, encodeNonAsciiHTML } from "entities";
// Comprehensive HTML encoding
encodeHTML("Hello! #hashtag & café");
// Result: "Hello! #hashtag & café"
// Selective HTML encoding (more compact)
encodeNonAsciiHTML("Hello! #hashtag & café");
// Result: "Hello! #hashtag & café"The library includes deprecated aliases for backward compatibility:
// Deprecated aliases - use encodeHTML instead
function encodeHTML4(input: string): string;
function encodeHTML5(input: string): string;"&'<>)encode("Café & 日本語", { mode: EncodingMode.UTF8 });
// Result: "Café & 日本語"encode("Café & 日本語", {
level: EntityLevel.HTML,
mode: EncodingMode.ASCII
});
// Result: "Café & 日本語"encode("© 2023 → ♥", {
level: EntityLevel.HTML,
mode: EncodingMode.Extensive
});
// Result: "© 2023 → ♥""&\u00A0 (quote, ampersand, non-breaking space)encode('title="Hello & world"', { mode: EncodingMode.Attribute });
// Result: "title="Hello & world""&<>\u00A0 (ampersand, less-than, greater-than, nbsp)encode("2 < 3 & 4 > 1", { mode: EncodingMode.Text });
// Result: "2 < 3 & 4 > 1"import { encode, EntityLevel, EncodingMode } from "entities";
function encodeForContext(text: string, context: 'xml' | 'html-attr' | 'html-text') {
switch (context) {
case 'xml':
return encode(text, { level: EntityLevel.XML, mode: EncodingMode.UTF8 });
case 'html-attr':
return encode(text, { level: EntityLevel.HTML, mode: EncodingMode.Attribute });
case 'html-text':
return encode(text, { level: EntityLevel.HTML, mode: EncodingMode.Text });
default:
return encode(text);
}
}import { encode, EntityLevel, EncodingMode } from "entities";
function createHTMLTemplate(data: Record<string, string>) {
return `
<div class="user-card">
<h2>${encode(data.name, { level: EntityLevel.HTML, mode: EncodingMode.Text })}</h2>
<p title="${encode(data.bio, { level: EntityLevel.HTML, mode: EncodingMode.Attribute })}">
${encode(data.description, { level: EntityLevel.HTML, mode: EncodingMode.Text })}
</p>
</div>
`;
}import { encode, EntityLevel, EncodingMode } from "entities";
const userInputs = ['<script>', 'Hello & world', '"quotes"'];
const safeOutputs = userInputs.map(input =>
encode(input, {
level: EntityLevel.HTML,
mode: EncodingMode.Text
})
);Choose the most restrictive mode that meets your requirements for optimal performance.
// Security-focused encoding example
function secureHTMLInsertion(userInput: string, context: 'text' | 'attribute') {
const mode = context === 'text' ? EncodingMode.Text : EncodingMode.Attribute;
return encode(userInput, {
level: EntityLevel.HTML,
mode
});
}