Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
@orrisroot/react-html-parser
Advanced tools
A utility for converting HTML strings into React components. Converts standard HTML elements, attributes and inline styles into their React equivalents and provides a simple way to modify and replace the content.
This library has been forked from react-html-parser and rebranded to @orrisroot/react-html-parser for continued maintenance.
npm install @orrisroot/react-html-parser
# or
yarn add @orrisroot/react-html-parser
import React from 'react';
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from '@orrisroot/react-html-parser';
class HtmlComponent extends React.Component {
render() {
const html = '<div>Example HTML string</div>';
return <div>{ ReactHtmlParser(html) }</div>;
}
}
It is important to understand that this library should not be used as a direct replacement for using properly sanitized HTML and that it only provides the same level of protection that React does which does not provide 100% protection. All HTML should be properly sanitized using a dedicated sanitisation library (such as dompurify for node/js) before being passed to this library to ensure that you are fully protected from malicious injections.
Whilst React has a certain level of protection to injection attacks built into it, it doesn't cover everything, for example:
<iframe src="javascript:alert('xss')" />
<a href="javascript:alert('xss')">click me</a>
Click here to see these in action and how to protect yourself using dompurify in the browser.
Including a sanitizer as part of the library means it is making decisions for you that may not be correct. It is up to you to decide what level of sanitization you need and to act accordingly. Some users may already be sanitizing on the server or others may have specialized requirements that cannot be covered by a generic implementation.
Additionally, HTML sanitization is a hard thing to get right and even the most popular and actively developed sanitizers have vulnerabilities discovered from time to time. By leaving the sanitization outside of this library it gives users the ability to patch and deploy any fixes needed immediately instead of having to wait for a new version of this library to be released with the fix.
function ReactHtmlParser(html, [options])
Takes an HTML string and returns equivalent React elements
import ReactHtmlParser from '@orrisroot/react-html-parser';
html
: The HTML string to parseoptions
: Options object
htmlparser2
The transform function will be called for every node that is parsed by the library.
function transform(node, index)
node
: The node being parsed. This is the htmlparser2 node object. Full details can be found on their project page but important properties are:
type
(string): The type of node (tag, text, style etc)name
(string): The name of the nodechildren
(array): Array of children nodesnext
(node): The node's next siblingprev
(node): The node's previous siblingparent
(node): The node's parentdata
(string): The text content, if the type
is textindex
(number): The index of the node in relation to it's parentreturn null
Returning null will prevent the node and all of it's children from being rendered.
function transform(node) {
// do not render any <span> tags
if (node.type === 'tag' && node.name === 'span') {
return null;
}
}
return undefined
If the function does not return anything, or returns undefined, then the default behaviour will occur and the parser will continue was usual.
return React element
React elements can be returned directly
import React from 'react';
function transform(node) {
if (node.type === 'tag' && node.name === 'b') {
return <div>This was a bold tag</div>;
}
}
Allows pre-processing the nodes generated from the html by htmlparser2
before being passed to the library and converted to React elements.
function preprocessNodes(nodes)
nodes
: The entire node tree generated by htmlparser2
.The preprocessNodes
function should return a valid htmlparser2
node tree.
function convertNodeToElement(node, index, transform)
Processes a node and returns the React element to be rendered. This function can be used in conjunction with the previously described transform
function to continue to process a node after modifying it.
import { convertNodeToElement } from '@orrisroot/react-html-parser';
node
: The node to processindex
(number): The index of the node in relation to it's parenttransform
: The transform function as described aboveimport { convertNodeToElement } from '@orrisroot/react-html-parser';
function transform(node, index) {
// convert <ul> to <ol>
if (node.type === 'tag' && node.name === 'ul') {
node.name = 'ol';
return convertNodeToElement(node, index, transform);
}
}
htmlparser2
The library exposes the htmlparser2
library it uses. This allows consumers
to use it without having to add it as a separate dependency.
See https://github.com/fb55/htmlparser2 for full details.
v2.1.2
htmlparser2
library version to ^9.0.0
FAQs
Parse HTML into React components
We found that @orrisroot/react-html-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.