remove-attribute-from-html

1.1.0 • Public • Published

remove-attribute-from-html

This NPM package provides a versatile function for sanitizing HTML strings 🧼. Given a string of HTML and an array of attribute names, the function efficiently removes all occurrences of these attributes from the HTML string, ensuring that the output is safe and free of unwanted attributes 🔒. The function preserves the original structure and styling of the HTML 💻. This package is ideal for developers looking to enhance the security and cleanliness of their HTML content effortlessly 👌.

API

The function RemoveAttributesFromHTML takes two parameters:

  • html (string): The HTML string representing the HTML content.
  • attributes (string[]): An array of attribute names to be removed.

Returns:

  • (string) The modified HTML content with the specified attributes removed.

Install

$ npm install remove-attribute-from-html

Examples

Example 1

const { RemoveAttributesFromHTML } = require('remove-attribute-from-html');

let html = `<div class="container-fluid bg-primary p-3" data-custom="example" style="font-size: 24px;">
                <p style="border: 2px solid black;" class="text-center font-weight-bold">Hello, World!</p>
            </div>`;
let cleanedHTML = RemoveAttributesFromHTML(html, ['class', 'style']);

The resulting cleanedHTML string contains the same HTML structure but without the class and style attribute removed from both the <div> and <p> tags.

Example 2

const { RemoveAttributesFromHTML } = require('remove-attribute-from-html');

let html = `<div class="container-fluid bg-primary p-3" data-custom="example" style="font-size: 24px;">
                <p style="border: 2px solid black;" class="text-center font-weight-bold">Hello, World!</p>
            </div>`;
let cleanedHTML = RemoveAttributesFromHTML(html, ['data-custom', 'style']);

The resulting cleanedHTML string contains the same HTML structure but without the data-custom and style attribute removed from both the <div> and <p> tags.

Example 3

const { RemoveAttributesFromHTML } = require('remove-attribute-from-html');

let html = `<div class="container-fluid bg-primary p-3" data-custom="example" style="font-size: 24px;">
                <p style="border: 2px solid black;" class="text-center font-weight-bold">Hello, World!</p>
            </div>`;
let cleanedHTML = RemoveAttributesFromHTML(html, ['style']);

The resulting cleanedHTML string contains the same HTML structure but without the style attribute removed from both the <div> and <p> tags.

Package Sidebar

Install

npm i remove-attribute-from-html

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

4.99 kB

Total Files

3

Last publish

Collaborators

  • nati_grossman