├── .babelrc ├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── demo ├── .babelrc ├── package-lock.json ├── package.json ├── server.js ├── src │ ├── index.html │ ├── js │ │ ├── actions.js │ │ ├── components │ │ │ ├── App.js │ │ │ ├── Editor.js │ │ │ ├── Header.js │ │ │ └── Html.js │ │ ├── containers │ │ │ ├── Content.js │ │ │ ├── Editor.js │ │ │ └── Html.js │ │ ├── data │ │ │ ├── entities │ │ │ │ ├── html.html │ │ │ │ ├── index.js │ │ │ │ └── options.js │ │ │ ├── index.js │ │ │ ├── simple │ │ │ │ ├── html.html │ │ │ │ ├── index.js │ │ │ │ └── options.js │ │ │ └── transform │ │ │ │ ├── display.txt │ │ │ │ ├── html.html │ │ │ │ ├── index.js │ │ │ │ └── options.js │ │ ├── index.js │ │ └── reducers.js │ └── sass │ │ ├── app.scss │ │ ├── editor.scss │ │ ├── header.scss │ │ └── html.scss ├── webpack.config.base.js ├── webpack.config.development.js └── webpack.config.production.js ├── karma.conf.js ├── package-lock.json ├── package.json ├── scripts └── attributes-diff.js ├── src ├── HtmlParser.js ├── convertNodeToElement.js ├── dom │ ├── attributes │ │ ├── BooleanAttributes.js │ │ └── ReactAttributes.js │ └── elements │ │ └── VoidElements.js ├── elementTypes │ ├── StyleElementType.js │ ├── TagElementType.js │ ├── TextElementType.js │ ├── UnsupportedElementType.js │ └── index.js ├── index.js ├── processNodes.js └── utils │ ├── generatePropsFromAttributes.js │ ├── htmlAttributesToReact.js │ ├── inlineStyleToObject.js │ ├── isEmptyTextNode.js │ └── isValidTagOrAttributeName.js ├── test ├── integration │ └── integration.spec.js └── unit │ ├── HtmlParser.spec.js │ ├── convertNodeToElement.spec.js │ ├── elementTypes │ ├── StyleElementType.spec.js │ ├── TagElementType.spec.js │ ├── TextElementType.spec.js │ └── UnsupportedElementType.spec.js │ ├── processNodes.spec.js │ └── utils │ ├── generatePropsFromAttributes.spec.js │ ├── htmlAttributesToReact.spec.js │ ├── inlineStyleToObject.spec.js │ ├── isEmptyTextNode.spec.js │ └── isValidTagOrAttributeName.spec.js ├── tests.webpack.js ├── webpack.config.base.js ├── webpack.config.development.js ├── webpack.config.production.js └── webpack.config.test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "react", 4 | "es2015" 5 | ], 6 | "plugins": [ 7 | "transform-object-assign", 8 | "transform-object-rest-spread" 9 | ] 10 | } -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint:recommended", 3 | "parserOptions": { 4 | "ecmaVersion": 6, 5 | "sourceType": "module", 6 | "ecmaFeatures": { 7 | "jsx": true, 8 | "experimentalObjectRestSpread": true 9 | } 10 | }, 11 | "env": { 12 | "browser": true, 13 | "node": true, 14 | "jasmine": true, 15 | "es6": true 16 | }, 17 | "plugins": [ 18 | "react" 19 | ], 20 | "rules": { 21 | "semi": [2, "always"], 22 | "quotes": [2, "single"], 23 | "indent": [2, 2, {"SwitchCase": 1}], 24 | "eol-last": 2, 25 | "react/jsx-uses-react": 1, 26 | "react/jsx-uses-vars": 1 27 | } 28 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | .idea 3 | node_modules 4 | dist/ 5 | coverage/ 6 | lib/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .babelrc 3 | .eslintrc 4 | .travis.yml 5 | karma.conf.js 6 | tests.webpack.js 7 | webpack.config.*.js 8 | coverage/ 9 | test/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 8 5 | 6 | env: 7 | - REACT_VERSION=16 8 | - REACT_VERSION=15 9 | 10 | script: 11 | - npm run lint 12 | - npm test 13 | 14 | before_script: 15 | - npm install react@$REACT_VERSION react-dom@$REACT_VERSION 16 | 17 | after_script: 18 | - "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" # Send coverage data to Coveralls 19 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## v2.0.2 4 | ### Bug Fixes 5 | - Fix empty inline style attribute breaking rendering [#34](https://github.com/wrakky/react-html-parser/pull/34) ([kevinzwhuang](https://github.com/kevinzwhuang)) 6 | 7 | ## v2.0.1 8 | ### Updates 9 | - Added `preprocessNodes` option to ReactHTMLParser function. This function is passed the raw 10 | node tree before `react-html-parser` processes it which it should return after any modifications 11 | are made to it 12 | - Exposed `htmlparser2` library 13 | - Updated the demo site to React v16 14 | 15 | ## v2.0.0 16 | ### Breaking Changes 17 | - ReactHtmlParser now decodes html entities by default 18 | - html, head and body tags are no longer automatically converted to div tags 19 | ### Updates 20 | - Add React v16 as a peer dependency 21 | - Added new options parameter to ReactHtmlParser function with following properties: 22 | - `decodeEntities` (boolean: default true) - whether to decode HTML entities 23 | - `transform` (function) - function that can be used to transform parsed elements 24 | - Tags and attributes with invalid formats are now ignored and prevent React from blowing up 25 | ### Bug Fixes 26 | - Fixed bug where inline styles containing colons would not be generated correctly [#9](https://github.com/wrakky/react-html-parser/issues/9) 27 | 28 | ## v1.0.3 29 | ### Updates 30 | - Added React v15 as a peer dependency 31 | ### Bug Fixes 32 | - Match boolean attributes when defined with upper or lower case characters [#5](https://github.com/wrakky/react-html-parser/issues/5) 33 | 34 | ## v1.0.2 35 | ### Bug Fixes 36 | - Don't add children to void elements such as img or br [#1](https://github.com/wrakky/react-html-parser/issues/1) 37 | - Correctly render boolean attributes - disabled, checked, etc [#3](https://github.com/wrakky/react-html-parser/issues/3) 38 | 39 | ## v1.0.0 40 | ### Initial Release 41 | - Render HTML elements and text 42 | - Map HTML attribute names to React HTML prop names 43 | - Convert inline style strings to React style object format 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-present Peter Newnham 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React HTML Parser 2 | 3 | A utility for converting HTML strings into [React](https://facebook.github.io/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. 4 | 5 | [Try the Live Demo](https://wrakky.github.io/react-html-parser) 6 | 7 | [![Travis branch](https://img.shields.io/travis/wrakky/react-html-parser/master.svg)](https://travis-ci.org/wrakky/react-html-parser) 8 | [![Coveralls](https://img.shields.io/coveralls/wrakky/react-html-parser.svg)](https://coveralls.io/github/wrakky/react-html-parser) 9 | [![npm](https://img.shields.io/npm/v/react-html-parser.svg)](https://www.npmjs.com/package/react-html-parser) 10 | [![Downloads](https://img.shields.io/npm/dw/react-html-parser.svg)](https://www.npmjs.com/package/react-html-parser) 11 | [![David](https://img.shields.io/david/wrakky/react-html-parser.svg)](https://david-dm.org/wrakky/react-html-parser) 12 | 13 | ## Install 14 | 15 | ```bash 16 | npm install react-html-parser 17 | # or 18 | yarn add react-html-parser 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```javascript 24 | import React from 'react'; 25 | import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser'; 26 | 27 | class HtmlComponent extends React.Component { 28 | render() { 29 | const html = '
Example HTML string
'; 30 | return
{ ReactHtmlParser(html) }
; 31 | } 32 | } 33 | ``` 34 | 35 | ## Security 36 | 37 | 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](https://www.npmjs.com/package/dompurify) for node/js) before being passed to this library to ensure that you are fully protected from [malicious injections](https://en.wikipedia.org/wiki/Cross-site_scripting). 38 | 39 | ### What doesn't React protect me from? 40 | 41 | Whilst React has a [certain level of protection to injection attacks](https://reactjs.org/docs/introducing-jsx.html#jsx-prevents-injection-attacks) built into it, it doesn't cover everything, for example: 42 | * xss via iframe src: `