| metadata |
|
|---|
Complete reference for all public exports from @darylcecile/sanitizer.
Sanitize an HTML string. Runs in safe mode by default, which strips scripts, event handlers, and dangerous URLs.
function sanitize(html: string, options?: SanitizeOptions): string;Parameters:
| Parameter | Type | Description |
|---|---|---|
html |
string |
The HTML string to sanitize |
options |
SanitizeOptions |
Optional configuration (see below) |
Returns: The sanitized HTML string.
SanitizeOptions:
| Property | Type | Default | Description |
|---|---|---|---|
sanitizer |
Sanitizer | SanitizerConfig | "default" |
"default" |
The sanitizer configuration to use |
safe |
boolean |
true |
When true, applies removeUnsafe() to strip all script-executing content |
Examples:
// Default safe mode
sanitize('<script>alert(1)</script><p>Safe</p>');
// → '<p>Safe</p>'
// Custom config
sanitize('<div><b>bold</b></div>', {
sanitizer: { elements: ["div", "b"], attributes: [] },
});
// → '<div><b>bold</b></div>'
// With a Sanitizer instance
const s = new Sanitizer({ elements: ["p"], attributes: [] });
sanitize('<p>text</p><div>gone</div>', { sanitizer: s });
// → '<p>text</p>'Sanitize an HTML string without safety guarantees. Equivalent to calling sanitize(html, { ...options, safe: false }).
function sanitizeUnsafe(
html: string,
options?: Omit<SanitizeOptions, "safe">,
): string;Examples:
// Everything passes through
sanitizeUnsafe('<script>alert(1)</script>');
// → '<script>alert(1)</script>'
// With filtering but no safety layer
sanitizeUnsafe('<b>bold</b><i>italic</i>', {
sanitizer: { elements: ["b"], attributes: [] },
});
// → '<b>bold</b>'new Sanitizer(config?: SanitizerConfig | SanitizerPresets)| Parameter | Type | Description |
|---|---|---|
config |
SanitizerConfig | "default" |
Configuration dictionary or "default" preset |
Throws TypeError if the configuration is invalid (for example, using both elements and removeElements).
const s1 = new Sanitizer(); // default preset
const s2 = new Sanitizer("default"); // same as above
const s3 = new Sanitizer({ elements: ["p", "b"], attributes: ["class"] });Returns the current configuration as a SanitizerConfig dictionary with keys sorted alphabetically.
get(): SanitizerConfigAdd an element to the allow-list. If the element is in removeElements or replaceWithChildrenElements, it is moved to the allow-list.
allowElement(element: SanitizerElement): booleanReturns true if the configuration changed.
Block an element. Removes it from elements or replaceWithChildrenElements if present, and adds it to removeElements.
removeElement(element: SanitizerElement): booleanReturns true if the configuration changed.
Mark an element for unwrapping. The tag is removed but its children are preserved.
replaceElementWithChildren(element: SanitizerElement): booleanReturns true if the configuration changed. Returns false and does nothing if the element is "html".
Add an attribute to the global allow-list.
allowAttribute(attribute: SanitizerAttribute): booleanReturns true if the configuration changed.
Block an attribute globally.
removeAttribute(attribute: SanitizerAttribute): booleanReturns true if the configuration changed.
Allow or disallow HTML comments.
setComments(allow: boolean): booleanReturns true if the configuration changed.
Allow or disallow data-* attributes.
setDataAttributes(allow: boolean): booleanReturns true if the configuration changed.
Strip all script-executing elements and event handler attributes from the configuration. This is the same transformation applied automatically in safe mode.
removeUnsafe(): booleanReturns true if the configuration changed.
interface SanitizerConfig {
elements?: SanitizerElementWithAttributes[];
removeElements?: SanitizerElement[];
replaceWithChildrenElements?: SanitizerElement[];
attributes?: SanitizerAttribute[];
removeAttributes?: SanitizerAttribute[];
comments?: boolean;
dataAttributes?: boolean;
}Constraints:
- Cannot have both
elementsandremoveElements - Cannot have both
attributesandremoveAttributes dataAttributescan only betruewhenattributesis present
type SanitizerElement = string | SanitizerElementNamespace;When a string is passed, it is treated as an HTML element name (namespace defaults to http://www.w3.org/1999/xhtml).
interface SanitizerElementNamespace {
name: string;
namespace?: string | null;
}Extends SanitizerElement with optional per-element attribute control.
type SanitizerElementWithAttributes =
| string
| SanitizerElementNamespaceWithAttributes;
interface SanitizerElementNamespaceWithAttributes {
name: string;
namespace?: string | null;
attributes?: SanitizerAttributeNamespace[];
}type SanitizerAttribute = string | SanitizerAttributeNamespace;interface SanitizerAttributeNamespace {
name: string;
namespace?: string | null;
}type SanitizerPresets = "default";The full W3C safe default configuration containing approximately 100 HTML, SVG, and MathML elements with their recommended attributes.
import { BUILT_IN_SAFE_DEFAULT_CONFIG } from "@darylcecile/sanitizer";The baseline configuration defining which elements and attributes are always considered unsafe. Used internally by removeUnsafe().
import { BUILT_IN_SAFE_BASELINE_CONFIG } from "@darylcecile/sanitizer";Parse an HTML string into a DocumentNode AST.
function parseHTML(html: string): DocumentNode;The Parser class provides low-level control over HTML parsing. Construct it with an HTML string and a DocumentNode, then call parse().
import { Parser, createDocument } from "@darylcecile/sanitizer";
const doc = createDocument();
const parser = new Parser("<p>Hello</p>", doc);
parser.parse();Serialize a DocumentNode or ElementNode back to an HTML string.
function serialize(node: DocumentNode | ElementNode): string;| Function | Description |
|---|---|
createDocument() |
Create an empty DocumentNode |
createElement(tagName, namespace, attributes?) |
Create an ElementNode |
createText(value) |
Create a TextNode |
createComment(value) |
Create a CommentNode |
createDocumentType(name, publicId?, systemId?) |
Create a DocumentTypeNode |
| Function | Description |
|---|---|
appendChild(parent, child) |
Append a child node to a parent |
removeChild(parent, child) |
Remove a child from its parent |
replaceWithChildren(parent, element) |
Remove an element but keep its children in place |
| Export | Value | Description |
|---|---|---|
NodeType.Document |
0 |
Document root |
NodeType.Element |
1 |
HTML/SVG/MathML element |
NodeType.Text |
3 |
Text content |
NodeType.Comment |
8 |
HTML comment |
NodeType.DocumentType |
10 |
DOCTYPE declaration |
See the Parser Guide for detailed usage and examples.