DOM manipulation and HTML escaping utilities for XSS prevention.
import { HtmlEscaper, DomHelper } from '@zappzarapp/browser-utils/html';
// Escape user input for safe HTML insertion
const safe = HtmlEscaper.escape(userInput);
element.innerHTML = `<span>${safe}</span>`;
// Better: use textContent (automatically safe)
DomHelper.setText(element, userInput);
// Create elements safely
const span = DomHelper.create('span', { class: 'highlight' }, userInput);| Export | Description |
|---|---|
HtmlEscaper |
Static methods for HTML escaping and sanitization |
DomHelper |
Safe DOM manipulation utilities |
Escape HTML special characters to prevent XSS:
const userInput = '<script>alert("xss")</script>';
const safe = HtmlEscaper.escape(userInput);
// '<script>alert("xss")</script>'More aggressive escaping for attribute context:
const safe = HtmlEscaper.escapeAttr(userInput);Escape and truncate text for display:
const safe = HtmlEscaper.truncate(longText, 100);
// Truncates at 100 chars and adds '...'
const custom = HtmlEscaper.truncate(text, 50, ' [more]');Build safe HTML tags with escaped content:
const html = HtmlEscaper.tag('span', { class: 'highlight' }, userInput);
// Content is automatically escaped
// Self-closing tag
const img = HtmlEscaper.tag('img', { src: '/image.png', alt: 'Photo' }, null);Security Features:
- Dangerous attributes are automatically filtered (onclick, onerror, etc.)
- Attribute values are escaped
- Content is escaped
Check if a URL is safe (not javascript:, data:, etc.):
if (HtmlEscaper.isSafeUrl(url)) {
// Safe to use
}Sanitize URL for safe use in href/src:
const safeUrl = HtmlEscaper.sanitizeUrl(userUrl);
// Returns empty string if URL is dangerous// Set text safely (uses textContent)
DomHelper.setText(element, userInput);// Create element with safe text content
const span = DomHelper.create('span', { class: 'label' }, 'Safe Text');
// Create with attributes
const link = DomHelper.create(
'a',
{
href: 'https://example.com',
target: '_blank',
},
'Click me'
);
// Create text node
const text = DomHelper.createText('Some text');// Append children (strings become text nodes)
DomHelper.append(parent, child1, 'text', child2);
// Clear element
DomHelper.clear(element);// Type-safe query
const button = DomHelper.query('button', container);
// Query all
const buttons = DomHelper.queryAll('button', container);// Add listener with automatic cleanup
const cleanup = DomHelper.on(element, 'click', (e) => {
handleClick(e);
});
// Remove listener
cleanup();// Toggle class
DomHelper.toggleClass(element, 'active');
DomHelper.toggleClass(element, 'visible', true); // Force add
// Check class
if (DomHelper.hasClass(element, 'active')) {
}
// Add/remove classes
DomHelper.addClass(element, 'class1', 'class2');
DomHelper.removeClass(element, 'class1');DomHelper.setData(element, 'userId', '123');
const userId = DomHelper.getData(element, 'userId');DomHelper.show(element);
DomHelper.show(element, 'flex'); // Custom display value
DomHelper.hide(element);
if (DomHelper.isVisible(element)) {
}function displayComment(comment: string): void {
const container = document.getElementById('comments')!;
// Option 1: Using textContent (safest)
const div = DomHelper.create('div', { class: 'comment' }, comment);
// Option 2: Using HtmlSanitizer for rich HTML
// import { HtmlSanitizer } from '@zappzarapp/browser-utils/sanitize';
// HtmlSanitizer.setHtmlContent(container, sanitizedHtml);
}function createLink(url: string, text: string): HTMLAnchorElement | null {
if (!HtmlEscaper.isSafeUrl(url)) {
return null;
}
return DomHelper.create('a', { href: url }, text);
}-
Prefer textContent - Use
DomHelper.setText()or element'stextContentinstead ofinnerHTMLwhenever possible -
Escape All User Input - Always escape user-provided content before HTML insertion
-
Validate URLs - Use
HtmlEscaper.isSafeUrl()to block javascript:, data:, vbscript: URLs -
Dangerous Attributes Filtered -
HtmlEscaper.tag()automatically filters:- Event handlers (onclick, onerror, etc.)
- URL attributes that could execute JavaScript (href, src, action, formaction)
- Other dangerous attributes (srcdoc, xlink:href)
-
Defense in Depth - Combine escaping with Content Security Policy (CSP) headers