11# safe-html-react-parser
22
3- A secure wrapper for ** html-react-parser** with ** isomorphic-dompurify ** that automatically sanitizes HTML before parsing.
3+ A secure wrapper for ** html-react-parser** with ** DOMPurify ** that automatically sanitizes HTML before parsing.
44
55## What it does
66
77- 🛡️ ** Security** : Automatically sanitizes malicious HTML using DOMPurify
88- ⚛️ ** React** : Seamlessly integrates with html-react-parser
99- 🌐 ** Universal** : Works in both browser and Node.js (SSR) environments
1010- 🏷️ ** Custom Tags** : Handles project-specific tags like ` <custom> ` safely
11-
12- ## Requirements
13-
14- - Node.js >=20.19.5: isomorphic-dompurify@^2.30.1
11+ - 🚀 ** Flexible** : Choose your DOM implementation (jsdom, happy-dom, or linkedom)
12+ - ⚡ ** Optimized** : Built-in caching and memory management
1513
1614## Installation
1715
1816``` bash
1917npm install @naverpay/safe-html-react-parser
2018```
2119
20+ ### Choosing a DOM Implementation (Server-Side Only)
21+
22+ For server-side rendering, you need to install one of the following DOM implementations:
23+
24+ ``` bash
25+ # Option 1: jsdom (most complete, heavier)
26+ npm install jsdom
27+
28+ # Option 2: happy-dom (faster, lighter, recommended)
29+ npm install happy-dom
30+
31+ # Option 3: linkedom (fastest, lightest)
32+ npm install linkedom
33+ ```
34+
35+ The library will automatically detect and use the first available implementation in this order: jsdom → happy-dom → linkedom.
36+
2237## Basic Usage
2338
2439``` tsx
@@ -119,6 +134,63 @@ const result = safeParse(html, {
119134})
120135```
121136
137+ ### Configuring DOM Implementation (Server-Side)
138+
139+ You have two ways to configure the DOM implementation:
140+
141+ #### Method 1: Per-call configuration (Recommended)
142+
143+ Pass ` domPurifyOptions ` directly to ` safeParse() ` :
144+
145+ ``` tsx
146+ import { safeParse } from ' @naverpay/safe-html-react-parser'
147+ import { Window } from ' happy-dom'
148+
149+ const result = safeParse (htmlString , {
150+ domPurifyOptions: {
151+ domWindowFactory : () => new Window (),
152+ enableCache: true ,
153+ maxCacheSize: 100
154+ }
155+ })
156+ ```
157+
158+ #### Method 2: Global configuration
159+
160+ Configure once at app initialization:
161+
162+ ``` tsx
163+ import { configureDOMPurify } from ' @naverpay/safe-html-react-parser'
164+
165+ // Using jsdom
166+ import { JSDOM } from ' jsdom'
167+ configureDOMPurify ({
168+ domWindowFactory : () => new JSDOM (' <!DOCTYPE html>' ),
169+ enableCache: true ,
170+ maxCacheSize: 100 ,
171+ recreateInterval: 1000 // Recreate DOM instance every 1000 sanitizations
172+ })
173+
174+ // Using happy-dom (recommended for better performance)
175+ import { Window } from ' happy-dom'
176+ configureDOMPurify ({
177+ domWindowFactory : () => new Window (),
178+ enableCache: true ,
179+ recreateInterval: 500
180+ })
181+
182+ // Using linkedom (fastest, minimal footprint)
183+ import { parseHTML } from ' linkedom'
184+ configureDOMPurify ({
185+ domWindowFactory : () => parseHTML (' <!DOCTYPE html>' ),
186+ enableCache: true
187+ })
188+ ```
189+
190+ > [ !NOTE]
191+ >
192+ > If you don't configure anything, the library will automatically try jsdom → happy-dom → linkedom in that order.
193+
122194## Default Allowed Tags
123195
124196By default, the following HTML tags are allowed:
@@ -132,6 +204,37 @@ ALLOWED_TAGS: [
132204]
133205```
134206
207+ ## Performance Optimization
208+
209+ ### Caching
210+
211+ By default, caching is enabled to improve performance:
212+
213+ ``` tsx
214+ configureDOMPurify ({
215+ enableCache: true , // Default: true
216+ maxCacheSize: 100 , // Default: 100
217+ })
218+ ```
219+
220+ ### Memory Management
221+
222+ The DOM instance is automatically recreated periodically to prevent memory leaks:
223+
224+ ``` tsx
225+ configureDOMPurify ({
226+ recreateInterval: 1000 // Default: 1000 sanitization calls
227+ })
228+ ```
229+
230+ ### DOM Implementation Comparison
231+
232+ | Implementation | Speed | Memory | Completeness | Recommended For |
233+ | ----------------| -------| --------| --------------| -----------------|
234+ | ** jsdom** | Slower | Higher | Most complete | Maximum compatibility |
235+ | ** happy-dom** | Fast | Medium | Good | ** Balanced (Recommended)** |
236+ | ** linkedom** | Fastest | Lowest | Basic | Performance-critical apps |
237+
135238## Security Notes
136239
137240- All HTML is sanitized by DOMPurify before parsing
@@ -142,7 +245,8 @@ ALLOWED_TAGS: [
142245## Built with
143246
144247- [ html-react-parser@^5.2.7] ( https://github.com/remarkablemark/html-react-parser ) - HTML string to React element parser
145- - [ isomorphic-dompurify@^2.30.1] ( https://github.com/kkomelin/isomorphic-dompurify ) - Universal XSS sanitizer
248+ - [ dompurify@^3.3.0] ( https://github.com/cure53/DOMPurify ) - XSS sanitizer
249+ - Optional: [ jsdom] ( https://github.com/jsdom/jsdom ) , [ happy-dom] ( https://github.com/capricorn86/happy-dom ) , or [ linkedom] ( https://github.com/WebReflection/linkedom )
146250
147251## License
148252
0 commit comments