Performance: Sanitizing user input
HIGH IMPACT
Sanitizing user input affects page security and rendering stability by preventing injection attacks and layout shifts caused by malicious or malformed input.
import DOMPurify from 'dompurify'; const userInput = '<img src=x onerror=alert(1)>Hello'; const safeInput = DOMPurify.sanitize(userInput); <template> <div v-html="safeInput"></div> </template>
const userInput = '<img src=x onerror=alert(1)>Hello'; // Directly binding with v-html <template> <div v-html="userInput"></div> </template>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct v-html with unsanitized input | Adds unpredictable nodes and event handlers | Multiple reflows if images/scripts load or fail | High paint cost due to layout shifts | [X] Bad |
| v-html with sanitized input (e.g., DOMPurify) | Adds only safe nodes, no scripts | Single reflow on initial render | Low paint cost, stable layout | [OK] Good |