Socket
Book a DemoInstallSign in
Socket

react-safe-html

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-safe-html

render user provided html safely by using react components

0.6.1
latest
Source
npmnpm
Version published
Weekly downloads
387
-15.87%
Maintainers
1
Weekly downloads
 
Created
Source

react-safe-html allows you to render user provided html (e.g. from ckeditor) safely. You choose how each element renders and which attributes get passed through. It has defaults for basic elements and attributes but is fully customizable.

Note: you may wish to use a more popular library like react-html-parse or safe-html.

It uses a fast but flexible parser (htmlparser2) and implements shouldComponentUpdate for performance.

status: alpha

Install

You can install it with npm:

npm install --save react-safe-html

And require it:

var ReactSafeHtml = require('react-safe-html');
// ...
<ReactSafeHtml html={html} />

Customization

You can create a custom element set to allow.

// the default allowed components
var components = ReactSafeHtml.components.makeElements({});

The argument is a mapping of allowed properties for all elements, for example you may pass {style: true} to allow style props on all elements.

You may also pass a function which gets the attribute value and returns a tupple of [propName, propValue]. This is the same as {style: true}: {style: (theStyleString) => ['style', theStyleString]}.

Adding/replacing elements

You may want to add extra elements to the allowed set, or remove some.

createSimpleElement takes an object like the one described above.

delete components.img;
components.input = ReactSafeHtml.components.createSimpleElement('input', {
  value: true,
  placeholder: true,
  'tab-index': (index) => ['tabIndex', index],
});

You can override the behavior for text nodes with a special component type '#text'.

components['#text'] = (string) => <p>{string}</p>;

When you're done customizing, pass it as an extra prop to ReactSafeHtml.

<ReactSafeHtml html={html} components={components} />

Keywords

react

FAQs

Package last updated on 12 Jan 2019

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.