Browser-safe HTML text encoding utilities.
These utilities have no runtime-native dependencies and work in current browsers, Node, Deno, and Bun.
ts
import HTML from "@rcompat/html";HTML.escape#
Escapes a string for HTML text or a quoted HTML attribute value.
Replaces &, <, >, ", and ' with HTML entities. HTML encoding is
context-specific: this does not make input safe for JavaScript, CSS,
unquoted attributes, or URL protocols. Validate URLs and other structured
values before encoding them.
Parameters
input— String to escape.
Returns: The HTML-encoded string.
ts
HTML.escape(input: string): string;Example
ts
HTML.escape('<span title="me & you">');
// "<span title="me & you">"