Skip to content

Commit f3be384

Browse files
committed
Initial commit
0 parents  commit f3be384

35 files changed

+1083
-0
lines changed

.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
"react",
4+
"es2015"
5+
],
6+
"plugins": [
7+
"transform-object-assign"
8+
]
9+
}

.eslintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"ecmaFeatures": {
4+
"jsx": true,
5+
"modules": true
6+
},
7+
"env": {
8+
"browser": true,
9+
"node": true,
10+
"jasmine": true,
11+
"es6": true
12+
},
13+
"plugins": [
14+
"react"
15+
],
16+
"rules": {
17+
"semi": [2, "always"],
18+
"quotes": [2, "single"],
19+
"indent": [2, 2, {"SwitchCase": 1}],
20+
"eol-last": 2
21+
}
22+
}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
npm-debug.log
2+
.idea
3+
node_modules
4+
dist/
5+
coverage/
6+
lib/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# React HTML Parser

karma.conf.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var webpackConfig = require('./webpack.config.test');
2+
3+
module.exports = function(karma) {
4+
5+
karma.set({
6+
files: [
7+
{
8+
pattern: 'tests.webpack.js',
9+
watched: true
10+
}
11+
],
12+
autoWatch: true,
13+
logLevel: karma.LOG_INFO,
14+
basePath: '',
15+
singleRun: false,
16+
frameworks: [
17+
'jasmine',
18+
'jasmine-expect-jsx',
19+
'phantomjs-shim'
20+
],
21+
preprocessors: {
22+
'tests.webpack.js': 'webpack'
23+
},
24+
browsers: ['PhantomJS'],
25+
colors: true,
26+
browserNoActivityTimeout: 100000,
27+
webpackMiddleware: {
28+
noInfo: true
29+
},
30+
webpack: webpackConfig,
31+
reporters: [ 'spec', 'coverage' ],
32+
coverageReporter: {
33+
type: 'lcov',
34+
dir: 'coverage/',
35+
subdir: '.',
36+
includeAllSources: true
37+
},
38+
specReporter: {
39+
suppressSkipped: true
40+
}
41+
});
42+
43+
};

package.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"name": "react-html-parser",
3+
"version": "1.0.0",
4+
"description": "Parse HTML into React components",
5+
"main": "lib/index.js",
6+
"jsnext:main": "src/index.js",
7+
"scripts": {
8+
"clean": "rimraf coverage dist lib",
9+
"check": "npm run lint && npm run test",
10+
"test": "karma start --single-run",
11+
"test:watch": "karma start",
12+
"lint": "eslint src test",
13+
"build": "npm run build:lib && npm run build:umd && npm run build:umd:min",
14+
"build:lib": "rimraf lib && babel ./src -d lib",
15+
"build:umd": "webpack src/index.js dist/react-html-parser.js --config webpack.config.development.js",
16+
"build:umd:min": "webpack src/index.js -p dist/react-html-parser.min.js --config webpack.config.production.js",
17+
"prerelease": "npm run clean && npm run check",
18+
"release": "npm run build",
19+
"postrelease": "echo DONE",
20+
"postreleases": "git push --follow-tags",
21+
"release:patch": "npm run release",
22+
"release:minor": "npm version minor && npm run release",
23+
"release:major": "npm version major && npm run release"
24+
},
25+
"keywords": [
26+
"react",
27+
"html"
28+
],
29+
"author": "Peter Newnham",
30+
"license": "MIT",
31+
"devDependencies": {
32+
"babel": "^6.3.26",
33+
"babel-cli": "^6.4.0",
34+
"babel-core": "^6.4.0",
35+
"babel-loader": "^6.2.1",
36+
"babel-plugin-transform-object-assign": "^6.3.13",
37+
"babel-preset-es2015": "^6.3.13",
38+
"babel-preset-react": "^6.3.13",
39+
"eslint-plugin-react": "3.15.0",
40+
"html-webpack-plugin": "^1.7.0",
41+
"inject-loader": "2.0.1",
42+
"isparta-loader": "^2.0.0",
43+
"jasmine-core": "^2.4.1",
44+
"json-loader": "^0.5.4",
45+
"karma": "^0.13.19",
46+
"karma-coverage": "^0.5.3",
47+
"karma-jasmine": "^0.3.6",
48+
"karma-phantomjs-shim": "^1.2.0",
49+
"karma-spec-reporter": "0.0.23",
50+
"karma-webpack": "^1.7.0",
51+
"phantomjs": "^1.9.19",
52+
"react": "^0.14.6",
53+
"react-addons-test-utils": "^0.14.6",
54+
"react-dom": "^0.14.6",
55+
"rimraf": "^2.5.0",
56+
"webpack": "^1.12.11",
57+
"webpack-dev-server": "^1.14.1"
58+
},
59+
"dependencies": {
60+
"htmlparser2": "^3.9.0"
61+
},
62+
"peerDependencies": {
63+
"react": "^0.13.0 || ^0.14.0"
64+
}
65+
}

src/HtmlParser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import htmlparser2 from 'htmlparser2';
2+
import ProcessNodes from './utils/ProcessNodes';
3+
4+
/**
5+
* Parses a HTML string and returns a list of React components generated from it
6+
*
7+
* @param {String} html The HTML to convert into React components
8+
* @returns {Array} List of top level React elements
9+
*/
10+
export default function HtmlParser(html) {
11+
const nodes = htmlparser2.parseDOM(html);
12+
return ProcessNodes(nodes);
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import GenerateElementProps from '../utils/GeneratePropsFromAttributes';
3+
4+
/**
5+
* Converts a <style> element to a React element
6+
*
7+
* @param {Object} node The style node
8+
* @param {String} key The key to give the React element
9+
* @returns {React.Element} The React style element
10+
*/
11+
export default function StyleElementType(node, key) {
12+
13+
// The style element only ever has a single child which is the styles so try and find this to add as
14+
// a child to the style element that will be created
15+
let styles;
16+
if (node.children.length > 0) {
17+
styles = node.children[0].data;
18+
}
19+
20+
// generate props
21+
const props = GenerateElementProps(node.attribs, key);
22+
23+
// create and return the element
24+
return React.createElement('style', props, styles);
25+
26+
}

src/elementTypes/TagElementType.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import ProcessNodes from '../utils/ProcessNodes';
3+
import GeneratePropsFromAttributes from '../utils/GeneratePropsFromAttributes';
4+
import TransformTagName from '../utils/TransformTagName';
5+
6+
/**
7+
* Converts any element (excluding style - see StyleElementType - and script) to a react element.
8+
*
9+
* @param {Object} node The tag node
10+
* @param {String} key The key to give the React element
11+
* @returns {React.Element} The React tag element
12+
*/
13+
export default function TagElementType(node, key) {
14+
15+
// If the node has children process them
16+
const children = ProcessNodes(node.children);
17+
18+
// generate props
19+
const props = GeneratePropsFromAttributes(node.attribs, key);
20+
21+
// transform the tag name if needed
22+
const tagName = TransformTagName(node.name);
23+
24+
// create and return the element
25+
return React.createElement(tagName, props, children);
26+
}

0 commit comments

Comments
 (0)