New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

escape-html-whitelist

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

escape-html-whitelist

Escapes HTML tags with whitelist support

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

escape-html-whitelist

npm version

Escapes HTML tags with user-defined whitelist support.

Inspired by punkave/sanitize-html, but this library escapes codes instead of removing them.

Installation

npm install escape-html-whitelist

Usage

const escapeHtml = require('escape-html-whitelist');

// ...

escapeHtml(dirty, {
    allowedTags: escapeHtml.defaultOptions.concat(['img']),
    allowedAttrs: {
        'a': ['href'],
        '*': ['style']
    }
});

escapeHtml(dirty[, options])

ArgumentDefaultDescription
dirtyA dirty HTML code that will be escaped
[options.allowedTags]See index.jsSee Writing a Whitelist
[options.allowedAttrs]See index.js"
[options.allowedProtocols]See index.js"
[options.allowNullProtocol]trueWhether to allow relative url for the href value

Writing a Whitelist

Allowing Tags

You can choose tags not to escape by its name. options.allowedTags is an array of tag names that will not be escaped.

For example, following options will escape every tag except <br>

{
    allowedTags: ['br']
}

Default options are at escapeHtml.defaultOptions, so you can also extend the default whitelist.

{
    allowedTags: escapeHtml.defaultOptions.concat(['img'])
}
Allowing Tag Attributes

You can also choose attributes to leave out. Any attribute listed on options.allowedAttrs will not be removed, but escaped if needed.

You can define options.allowedAttrs as an object whose key is tag name and value is an array of attribute names. When the tag name is '*', it will match all tags.

{
    allowedAttrs: {
        'a': ['href'],
        '*': ['style']
    }
}
Allowing Protocols

Especially for href attribute, escape-html-whitelist checks its content. When its content contains URL not listed on options.allowedProtocols, the content will be removed. The key of the options.allowedProtocols object is a tag name, and the value is an array of protocol names.

For example, following options will allow any HTTP(S) link or inline-data, but deny any other thing such as a mailto link or javascript code.

{
    allowedProtocols: {
        '*': ['http', 'https', 'data']
    }
}

Contribution

If you have any bugs, suggestions, or any other questions, please create an issue.

Pull requests are always welcome. Before submitting pull requests, just make sure your changes pass the unit test by running npm test command.

License

MIT

Keywords

FAQs

Package last updated on 02 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc