UNPKG

react-safe-html-parser

Version:

A secure, lightweight HTML parser for React with XSS protection and SSR support

82 lines 2.64 kB
import type { ReactElement, ReactNode, ComponentType, CSSProperties } from 'react'; export interface ParserOptions { /** Allow custom component replacements for HTML tags */ components?: ComponentMap; /** Custom transform function to modify nodes during parsing */ transform?: TransformFunction; /** Sanitization settings */ sanitize?: SanitizeOptions; /** Whether to decode HTML entities */ decodeHtmlEntities?: boolean; /** Whether to convert style strings to objects */ parseStyle?: boolean; /** Custom HTML entity decoder */ entityDecoder?: (entity: string) => string; } export interface ComponentMap { [tagName: string]: ComponentType<any>; } export interface TransformFunction { (node: ParsedNode, index: number, parent?: ParsedNode): ReactNode | null; } export interface SanitizeOptions { /** Allowed HTML tags */ allowedTags?: string[]; /** Allowed HTML attributes */ allowedAttributes?: string[]; /** Allowed CSS properties */ allowedStyles?: string[]; /** Whether to remove all script tags and event handlers */ removeScripts?: boolean; /** Whether to remove all style tags */ removeStyles?: boolean; /** Whether to remove all comments */ removeComments?: boolean; } export interface ParsedNode { type: 'element' | 'text' | 'comment'; tagName?: string; attributes?: Record<string, string>; children?: ParsedNode[]; text?: string; nodeType: number; } export interface HtmlParserProps { /** HTML string to parse */ html: string; /** Parser options */ options?: ParserOptions; /** CSS class name for the wrapper element */ className?: string; /** CSS styles for the wrapper element */ style?: CSSProperties; /** Fallback content when parsing fails */ fallback?: ReactNode; /** Whether to show parsing errors */ showErrors?: boolean; } export interface ParseResult { /** Parsed React elements */ elements: ReactElement[]; /** Any parsing errors */ errors: Error[]; /** Whether parsing was successful */ success: boolean; } export interface IsomorphicDOMParser { parseHTML: (html: string) => { document: Document; window: Window; }; } export interface SecurityConfig { /** Dangerous attributes to remove */ dangerousAttributes: string[]; /** Dangerous protocols to remove */ dangerousProtocols: string[]; /** Dangerous tags to remove */ dangerousTags: string[]; /** Allowed attributes by tag */ allowedAttributesByTag: Record<string, string[]>; } //# sourceMappingURL=types.d.ts.map