npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xmlxyz/html-purify

v0.0.5

Published

### Description

Downloads

8

Readme

@xmlxyz/purify-html

Description

@xmlxyz/purify-html is an open-source package that provides a powerful HTML sanitization and purifying utility. It allows you to detect and handle malicious HTML content and produce sanitized output using a customizable set of middleware functions. This package is designed to protect your application from potential security vulnerabilities by ensuring that only trusted and allowed tags, attributes, and text are retained in the HTML.

Installation

You can install @xmlxyz/purify-html using npm or yarn:

npm install @xmlxyz/purify-html
# or
yarn add @xmlxyz/purify-html

Usage

import { createPurifier } from "@xmlxyz/purify-html";

// Create a new instance of PurifyHTML with custom middleware
const purify = createPurifier("https://example.com/workbench/", [
	/* Add your PurifierMiddleware instances here */
]);

// Sanitize and purify HTML
const inputHTML = '<p onclick="foo()">hello world</p>';
const sanitizedHTML = purify.cleanSync(inputHTML);
console.log(sanitizedHTML);
// Output: "<p>hello world</p>"

PurifierMiddleware Interface

The PurifierMiddleware interface provides a structure to define custom middleware functions for @xmlxyz/purify-html. Middleware functions enable customization and filtering of allowed tags, attributes, and text within the HTML. The interface consists of the following properties:

  • allowedTags: A Set containing strings representing the HTML tags allowed in the sanitized output.
  • allowedAttributes: A Set containing strings representing the attributes allowed in the sanitized output.
  • onTag?: An optional function to handle individual HTML tags. It takes two parameters - tag (string) representing the tag name and attrs (object) representing the tag attributes as key-value pairs. The onTag function can modify the tag or attributes or return void to allow the tag as-is.
  • onAttribute?: An optional function to handle individual HTML attributes. It takes two parameters - attr (string) representing the attribute name and value (string) representing the attribute value. The onAttribute function can modify the attribute value or return void to allow the attribute as-is.
  • onText?: An optional function to handle text content within the HTML. It takes a single parameter - text (string) representing the text content. The onText function can modify the text content or return the modified text.

PurifyHTML Class

The PurifyHTML class is the core of this package and provides the HTML sanitization functionality. It utilizes the set of middleware functions to detect and handle malicious HTML content. The class follows a step-by-step process to sanitize the HTML:

  1. Read HTML: Provide the HTML content to the cleanSync method of PurifyHTML.
  2. Detect Malicious HTML: The class uses the specified middleware functions to detect and handle potentially malicious tags and attributes.
  3. Sanitize Malicious HTML: Detected malicious HTML is sanitized and purified based on the middleware rules.
  4. Return Sanitized HTML: The sanitized and cleaned HTML content is returned as the output.

Contributing

Contributions to @xmlxyz/purify-html are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This package is open-source and released under the Unlicense License. See the LICENSE file for more details.