@kids-reporter/draft-renderer 0.4.35 → 0.4.36
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.
|
@@ -8,7 +8,6 @@ exports.EmbeddedCodeInArticleBody = EmbeddedCodeInArticleBody;
|
|
|
8
8
|
var _react = _interopRequireWildcard(require("react"));
|
|
9
9
|
var _styledComponents = _interopRequireDefault(require("styled-components"));
|
|
10
10
|
var _mediaQuery = require("../utils/media-query");
|
|
11
|
-
var _nodeHtmlParser = require("node-html-parser");
|
|
12
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
12
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
13
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
@@ -43,67 +42,45 @@ const EmbeddedCodeBlock = ({
|
|
|
43
42
|
embeddedCode
|
|
44
43
|
} = data;
|
|
45
44
|
const embedded = (0, _react.useRef)(null);
|
|
45
|
+
(0, _react.useEffect)(() => {
|
|
46
|
+
if (!embedded.current) return;
|
|
47
|
+
const node = embedded.current;
|
|
48
|
+
const fragment = document.createDocumentFragment();
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// 1. Since react use setInnerHtml to append the htmlStirng received from
|
|
59
|
-
// dangerouslySetInnerHtml, scripts won't be triggered.
|
|
60
|
-
// 2. Although the setInnerhtml way won't trigger script tags, those script tags
|
|
61
|
-
// will still show on the HTML provided from SSR. When the browser parse the
|
|
62
|
-
// html it will run those script and produce unexpected behavior.
|
|
63
|
-
const nodes = (0, _react.useMemo)(() => {
|
|
64
|
-
const ele = (0, _nodeHtmlParser.parse)(`<div id="draft-embed">${embeddedCode}</div>`);
|
|
50
|
+
// `embeddedCode` is a string, which may includes
|
|
51
|
+
// multiple '<script>' tags and other html tags.
|
|
52
|
+
// For executing '<script>' tags on the browser,
|
|
53
|
+
// we need to extract '<script>' tags from `embeddedCode` string first.
|
|
54
|
+
//
|
|
55
|
+
// The approach we have here is to parse html string into elements,
|
|
56
|
+
// and we could use DOM element built-in functions,
|
|
57
|
+
// such as `querySelectorAll` method, to query '<script>' elements,
|
|
58
|
+
// and other non '<script>' elements.
|
|
59
|
+
const parser = new DOMParser();
|
|
60
|
+
const ele = parser.parseFromString(`<div id="draft-embed">${embeddedCode}</div>`, 'text/html');
|
|
65
61
|
const scripts = ele.querySelectorAll('script');
|
|
62
|
+
const nonScripts = ele.querySelectorAll('div#draft-embed > :not(script)');
|
|
63
|
+
nonScripts.forEach(ele => {
|
|
64
|
+
fragment.appendChild(ele);
|
|
65
|
+
});
|
|
66
66
|
scripts.forEach(s => {
|
|
67
|
-
|
|
67
|
+
const scriptEle = document.createElement('script');
|
|
68
|
+
const attrs = s.attributes;
|
|
69
|
+
for (let i = 0; i < attrs.length; i++) {
|
|
70
|
+
scriptEle.setAttribute(attrs[i].name, attrs[i].value);
|
|
71
|
+
}
|
|
72
|
+
scriptEle.text = s.text || '';
|
|
73
|
+
fragment.appendChild(scriptEle);
|
|
68
74
|
});
|
|
69
|
-
|
|
70
|
-
const nonScriptsHtml = nonScripts.reduce((prev, next) => prev + next.toString(), '');
|
|
71
|
-
return {
|
|
72
|
-
scripts,
|
|
73
|
-
nonScripts,
|
|
74
|
-
nonScriptsHtml
|
|
75
|
-
};
|
|
75
|
+
node.appendChild(fragment);
|
|
76
76
|
}, [embeddedCode]);
|
|
77
|
-
const {
|
|
78
|
-
scripts,
|
|
79
|
-
nonScriptsHtml
|
|
80
|
-
} = nodes;
|
|
81
|
-
(0, _react.useEffect)(() => {
|
|
82
|
-
if (embedded.current) {
|
|
83
|
-
const node = embedded.current;
|
|
84
|
-
const fragment = document.createDocumentFragment();
|
|
85
|
-
scripts.forEach(s => {
|
|
86
|
-
const scriptEle = document.createElement('script');
|
|
87
|
-
const attrs = s.attributes;
|
|
88
|
-
for (const key in attrs) {
|
|
89
|
-
scriptEle.setAttribute(key, attrs[key]);
|
|
90
|
-
}
|
|
91
|
-
scriptEle.text = s.text || '';
|
|
92
|
-
fragment.appendChild(scriptEle);
|
|
93
|
-
});
|
|
94
|
-
node.appendChild(fragment);
|
|
95
|
-
}
|
|
96
|
-
}, [scripts]);
|
|
97
77
|
return /*#__PURE__*/_react.default.createElement("div", {
|
|
98
78
|
className: className
|
|
99
79
|
}, /*#__PURE__*/_react.default.createElement("input", {
|
|
100
80
|
hidden: true,
|
|
101
81
|
disabled: true
|
|
102
82
|
}), /*#__PURE__*/_react.default.createElement(Block, {
|
|
103
|
-
ref: embedded
|
|
104
|
-
dangerouslySetInnerHTML: {
|
|
105
|
-
__html: nonScriptsHtml
|
|
106
|
-
}
|
|
83
|
+
ref: embedded
|
|
107
84
|
}), caption && /*#__PURE__*/_react.default.createElement(Caption, null, caption));
|
|
108
85
|
};
|
|
109
86
|
exports.EmbeddedCodeBlock = EmbeddedCodeBlock;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kids-reporter/draft-renderer",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.36",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"babel-loader": "^8.2.5",
|
|
27
27
|
"body-scroll-lock": "3.1.5",
|
|
28
28
|
"html-webpack-plugin": "^5.5.0",
|
|
29
|
-
"node-html-parser": "^6.1.5",
|
|
30
29
|
"webpack": "^5.72.1",
|
|
31
30
|
"webpack-cli": "^4.9.2"
|
|
32
31
|
},
|
|
@@ -45,4 +44,4 @@
|
|
|
45
44
|
"react-dom": "18.2.0",
|
|
46
45
|
"styled-components": "5.3.5"
|
|
47
46
|
}
|
|
48
|
-
}
|
|
47
|
+
}
|