Sanitize Html

screenshot of Sanitize Html

Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance

Overview

sanitize-html provides an elegant solution for sanitizing HTML content, offering a straightforward and robust API that is particularly useful for developers. Designed especially for cleaning up HTML fragments from rich text editors like CKEditor or when dealing with pasted content from Word, it ensures that only desired elements and attributes are retained. This makes it an essential tool for maintaining clean and safe HTML in applications where user-generated content is involved.

Its main strength lies in its flexibility and precision in allowing developers to define permissible tags and their respective attributes. By efficiently removing unwanted CSS and validating URLs, sanitize-html allows for streamlined and secure content management.

Features

  • Customizable Tag Management: Specify which HTML tags to permit and control the attributes for each, ensuring only approved elements remain in the final output.

  • Cleaning Syntax Issues: Automatically corrects poorly closed p and img tags, enhancing the quality of the HTML without manual intervention.

  • URL Validation: Validates href attributes to allow only safe protocols like http, https, ftp, and mailto, with support for relative URLs.

  • Secure Iframe Support: Enables filtering of hostnames for allowed URLs in src attributes within iframe tags, enhancing security.

  • Efficient Text Escaping: Automatically escapes all text content, converting special characters to their corresponding HTML character references, such as converting & to &.

  • No Browser Trust: Designed with the principle that servers should never trust browsers, ensuring that sanitization should occur server-side for security.

  • Node.js Compatibility: Built for use with Node.js, supporting versions 10 and above, with all dependencies written in pure JavaScript.

  • Community Support for TypeScript: While not natively written in TypeScript, there are community-supported types available for developers who wish to integrate it with TypeScript projects.

eslint
Eslint

ESLint is a linter for JavaScript that analyzes code to detect and report on potential problems and errors, as well as enforce consistent code style and best practices, helping developers to write cleaner, more maintainable code.

postcss
Postcss

PostCSS is a popular open-source tool that enables web developers to transform CSS styles with JavaScript plugins. It allows for efficient processing of CSS styles, from applying vendor prefixes to improving browser compatibility, ultimately resulting in cleaner, faster, and more maintainable code.