Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| const TEMPLATE = document.createElement("template"); | |
| const ENTITIES = { | |
| "&": "&", | |
| "<": "<", | |
| ">": ">", | |
| '"': """, | |
| "'": "'", | |
| }; | |
| export default function sanitize(value) { | |
| // Parse the HTML to inert DOM. | |
| TEMPLATE.innerHTML = value; | |
| // Strip all markup. | |
| const text = TEMPLATE.content.textContent; | |
| // Any HTML entities present in the original value have been unescaped by | |
| // textContent. Sanitize the syntax-sensitive characters back to entities. | |
| return text.replace(/[&<>"']/g, ch => ENTITIES[ch]); | |
| } |