html-react-parser 0.8.0 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +56 -13
- package/dist/html-react-parser.js +1634 -269
- package/dist/html-react-parser.min.js +2 -2
- package/dist/html-react-parser.min.js.map +1 -1
- package/index.d.ts +10 -11
- package/index.js +7 -10
- package/lib/attributes-to-props.js +47 -42
- package/lib/dom-to-react.js +4 -4
- package/package.json +17 -11
- package/lib/property-config.js +0 -50
|
@@ -1,59 +1,61 @@
|
|
|
1
|
-
var
|
|
2
|
-
var propertyConfig = require('./property-config');
|
|
1
|
+
var reactProperty = require('react-property');
|
|
3
2
|
var styleToObject = require('style-to-object');
|
|
4
3
|
var utilities = require('./utilities');
|
|
5
4
|
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
var camelCase = utilities.camelCase;
|
|
6
|
+
|
|
7
|
+
var htmlProperties = reactProperty.html;
|
|
8
|
+
var svgProperties = reactProperty.svg;
|
|
9
|
+
var isCustomAttribute = reactProperty.isCustomAttribute;
|
|
10
|
+
|
|
11
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* Converts HTML/SVG DOM attributes to React props.
|
|
14
15
|
*
|
|
15
|
-
* @param {Object} [attributes={}] - The attributes.
|
|
16
|
-
* @return {Object} - The props.
|
|
16
|
+
* @param {Object} [attributes={}] - The HTML/SVG DOM attributes.
|
|
17
|
+
* @return {Object} - The React props.
|
|
17
18
|
*/
|
|
18
19
|
function attributesToProps(attributes) {
|
|
19
20
|
attributes = attributes || {};
|
|
21
|
+
|
|
22
|
+
var attributeName;
|
|
23
|
+
var attributeNameLowerCased;
|
|
24
|
+
var attributeValue;
|
|
25
|
+
var property;
|
|
20
26
|
var props = {};
|
|
21
|
-
var propertyName;
|
|
22
|
-
var propertyValue;
|
|
23
|
-
var reactProperty;
|
|
24
27
|
|
|
25
|
-
for (
|
|
26
|
-
|
|
28
|
+
for (attributeName in attributes) {
|
|
29
|
+
attributeValue = attributes[attributeName];
|
|
27
30
|
|
|
28
|
-
// custom
|
|
29
|
-
if (isCustomAttribute(
|
|
30
|
-
props[
|
|
31
|
+
// ARIA (aria-*) or custom data (data-*) attribute
|
|
32
|
+
if (isCustomAttribute(attributeName)) {
|
|
33
|
+
props[attributeName] = attributeValue;
|
|
31
34
|
continue;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
) {
|
|
44
|
-
props[reactProperty] = true;
|
|
45
|
-
} else {
|
|
46
|
-
props[reactProperty] = propertyValue;
|
|
47
|
-
}
|
|
37
|
+
// convert HTML attribute to React prop
|
|
38
|
+
attributeNameLowerCased = attributeName.toLowerCase();
|
|
39
|
+
if (hasOwnProperty.call(htmlProperties, attributeNameLowerCased)) {
|
|
40
|
+
property = htmlProperties[attributeNameLowerCased];
|
|
41
|
+
props[property.propertyName] =
|
|
42
|
+
property.hasBooleanValue ||
|
|
43
|
+
(property.hasOverloadedBooleanValue && !attributeValue)
|
|
44
|
+
? true
|
|
45
|
+
: attributeValue;
|
|
48
46
|
continue;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
props[
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
// convert SVG attribute to React prop
|
|
50
|
+
if (hasOwnProperty.call(svgProperties, attributeName)) {
|
|
51
|
+
property = svgProperties[attributeName];
|
|
52
|
+
props[property.propertyName] = attributeValue;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// preserve custom attribute if React >=16
|
|
57
|
+
if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) {
|
|
58
|
+
props[attributeName] = attributeValue;
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
@@ -61,6 +63,7 @@ function attributesToProps(attributes) {
|
|
|
61
63
|
if (attributes.style != null) {
|
|
62
64
|
props.style = cssToJs(attributes.style);
|
|
63
65
|
}
|
|
66
|
+
|
|
64
67
|
return props;
|
|
65
68
|
}
|
|
66
69
|
|
|
@@ -74,14 +77,16 @@ function cssToJs(style) {
|
|
|
74
77
|
if (typeof style !== 'string') {
|
|
75
78
|
throw new TypeError('First argument must be a string.');
|
|
76
79
|
}
|
|
80
|
+
|
|
77
81
|
var styleObj = {};
|
|
78
82
|
|
|
79
|
-
styleToObject(style, function(
|
|
80
|
-
//
|
|
81
|
-
if (
|
|
82
|
-
styleObj[
|
|
83
|
+
styleToObject(style, function(property, value) {
|
|
84
|
+
// skip if it's a comment node
|
|
85
|
+
if (property && value) {
|
|
86
|
+
styleObj[camelCase(property)] = value;
|
|
83
87
|
}
|
|
84
88
|
});
|
|
89
|
+
|
|
85
90
|
return styleObj;
|
|
86
91
|
}
|
|
87
92
|
|
package/lib/dom-to-react.js
CHANGED
|
@@ -56,11 +56,11 @@ function domToReact(nodes, options) {
|
|
|
56
56
|
|
|
57
57
|
children = null;
|
|
58
58
|
|
|
59
|
+
// node type for <script> is "script"
|
|
59
60
|
// node type for <style> is "style"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
|
|
61
|
+
if (node.type === 'script' || node.type === 'style') {
|
|
62
|
+
// prevent text in <script> or <style> from being escaped
|
|
63
|
+
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
|
|
64
64
|
if (node.children[0]) {
|
|
65
65
|
props.dangerouslySetInnerHTML = {
|
|
66
66
|
__html: node.children[0].data
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-react-parser",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.9.2",
|
|
4
|
+
"description": "HTML to React parser.",
|
|
5
5
|
"author": "Mark <mark@remarkablemark.org>",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"benchmark": "node benchmark",
|
|
9
9
|
"build": "npm run clean && npm run build:min && npm run build:unmin",
|
|
10
|
-
"build:min": "cross-env NODE_ENV=production
|
|
11
|
-
"build:unmin": "cross-env NODE_ENV=development
|
|
10
|
+
"build:min": "cross-env NODE_ENV=production rollup --config --file dist/html-react-parser.min.js --sourcemap",
|
|
11
|
+
"build:unmin": "cross-env NODE_ENV=development rollup --config --file dist/html-react-parser.js",
|
|
12
12
|
"clean": "rimraf dist",
|
|
13
13
|
"coveralls": "nyc report --reporter=text-lcov | coveralls",
|
|
14
|
-
"lint": "eslint --ignore-path .gitignore .",
|
|
14
|
+
"lint": "eslint --ignore-path .gitignore --ignore-pattern /examples/ .",
|
|
15
15
|
"lint:fix": "npm run lint -- --fix",
|
|
16
16
|
"dtslint": "dtslint .",
|
|
17
|
-
"prepublishOnly": "npm run build",
|
|
17
|
+
"prepublishOnly": "npm run lint && npm run dtslint && npm test && npm run build",
|
|
18
18
|
"release": "standard-version --no-verify",
|
|
19
19
|
"test": "mocha",
|
|
20
20
|
"test:coverage": "nyc npm test",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@types/domhandler": "2.4.1",
|
|
39
|
-
"html-dom-parser": "0.2.
|
|
40
|
-
"react-
|
|
39
|
+
"html-dom-parser": "0.2.3",
|
|
40
|
+
"react-property": "1.0.1",
|
|
41
41
|
"style-to-object": "0.2.3"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -58,9 +58,11 @@
|
|
|
58
58
|
"react": "^16",
|
|
59
59
|
"react-dom": "^16",
|
|
60
60
|
"rimraf": "^2.6.3",
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
61
|
+
"rollup": "^1.16.2",
|
|
62
|
+
"rollup-plugin-commonjs": "^10.0.0",
|
|
63
|
+
"rollup-plugin-node-resolve": "^5.0.4",
|
|
64
|
+
"rollup-plugin-uglify": "^6.0.2",
|
|
65
|
+
"standard-version": "^6.0.1"
|
|
64
66
|
},
|
|
65
67
|
"peerDependencies": {
|
|
66
68
|
"react": "^0.14 || ^15 || ^16"
|
|
@@ -70,5 +72,9 @@
|
|
|
70
72
|
"/lib",
|
|
71
73
|
"index.d.ts"
|
|
72
74
|
],
|
|
75
|
+
"collective": {
|
|
76
|
+
"type": "opencollective",
|
|
77
|
+
"url": "https://opencollective.com/html-react-parser"
|
|
78
|
+
},
|
|
73
79
|
"license": "MIT"
|
|
74
80
|
}
|
package/lib/property-config.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
var HTMLDOMPropertyConfig = require('react-dom-core/lib/HTMLDOMPropertyConfig');
|
|
2
|
-
var SVGDOMPropertyConfig = require('react-dom-core/lib/SVGDOMPropertyConfig');
|
|
3
|
-
var utilities = require('./utilities');
|
|
4
|
-
|
|
5
|
-
var config = {
|
|
6
|
-
html: {},
|
|
7
|
-
svg: {}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
var propertyName;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* HTML DOM property config.
|
|
14
|
-
*
|
|
15
|
-
* https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/HTMLDOMPropertyConfig.js
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
// first map out the HTML attribute names
|
|
19
|
-
// e.g., { className: 'class' } => { 'class': 'className' }
|
|
20
|
-
config.html = utilities.invertObject(HTMLDOMPropertyConfig.DOMAttributeNames);
|
|
21
|
-
|
|
22
|
-
// then map out the rest of the HTML properties
|
|
23
|
-
// e.g., { readOnly: 0 } => { readonly: 'readOnly' }
|
|
24
|
-
for (propertyName in HTMLDOMPropertyConfig.Properties) {
|
|
25
|
-
// lowercase to make matching property names easier
|
|
26
|
-
config.html[propertyName.toLowerCase()] = propertyName;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* SVG DOM property config.
|
|
31
|
-
*
|
|
32
|
-
* https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/SVGDOMPropertyConfig.js
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
// first map out the SVG attribute names
|
|
36
|
-
// e.g., { fontSize: 'font-size' } => { 'font-size': 'fontSize' }
|
|
37
|
-
config.svg = utilities.invertObject(SVGDOMPropertyConfig.DOMAttributeNames);
|
|
38
|
-
|
|
39
|
-
// then map out the rest of the SVG properties
|
|
40
|
-
// e.g., { fillRule: 0 } => { fillRule: 'fillRule' }
|
|
41
|
-
for (propertyName in SVGDOMPropertyConfig.Properties) {
|
|
42
|
-
// do not lowercase as some svg properties are camel cased
|
|
43
|
-
config.html[propertyName] = propertyName;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = {
|
|
47
|
-
config: config,
|
|
48
|
-
HTMLDOMPropertyConfig: HTMLDOMPropertyConfig,
|
|
49
|
-
SVGDOMPropertyConfig: SVGDOMPropertyConfig
|
|
50
|
-
};
|