HTML Processing
Functions for processing HTML strings: removing tags, escaping characters, parsing, and cleaning fragments.
Method List
HTML Cleaning
| Function | Signature | Description |
|---|---|---|
stripHtml | (string: string): string | Removes all HTML tags from a string. |
stripFragment | (html: string): string | Removes fragment tags from an HTML string. |
HTML Escaping and Decoding
| Function | Signature | Description |
|---|---|---|
escapeHtml | (input: string): string | Converts special characters to HTML entities. |
decodeHtml | (input: string): string | Converts HTML entities back to characters. |
HTML Parsing
| Function | Signature | Description |
|---|---|---|
parseHtml | (html: string, isStripFragment?: boolean): Node[] | Parses an HTML string into an array of DOM nodes. |
Usage Examples
HTML Cleaning
1. stripHtml
typescript
import { stripHtml } from 'snappykit';
const htmlString = '<p>Hello, <strong>world!</strong></p>';
console.log(stripHtml(htmlString)); // "Hello, world!"2. stripFragment
typescript
import { stripFragment } from 'snappykit';
const htmlWithFragment = '<!--StartFragment--><p>Text fragment</p><!--EndFragment-->';
console.log(stripFragment(htmlWithFragment)); // "<p>Text fragment</p>"HTML Escaping and Decoding
3. escapeHtml
typescript
import { escapeHtml } from 'snappykit';
const unsafeString = '<script>alert("XSS")</script>';
console.log(escapeHtml(unsafeString)); // "<script>alert("XSS")</script>"4. decodeHtml
typescript
import { decodeHtml } from 'snappykit';
const encodedString = '<p>Hello, world!</p>';
console.log(decodeHtml(encodedString)); // "<p>Hello, world!</p>"HTML Parsing
5. parseHtml
typescript
import { parseHtml } from 'snappykit';
const htmlString = '<div><p>Hello</p><span>World</span></div>';
const nodes = parseHtml(htmlString);
console.log(nodes.length); // 2 (nodes <p> and <span>)
// With fragments
const htmlWithFragment = '<!--StartFragment--><p>Fragment</p><!--EndFragment-->';
const fragmentNodes = parseHtml(htmlWithFragment, true);
console.log(fragmentNodes.length); // 1 (node <p>)