base-js-sw 1.0.3 → 1.0.4
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/components/RichTextNoHtml.js +38 -0
- package/package.json +1 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RichText } from '@wordpress/block-editor';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes unwanted HTML tags from a string, except for a whitelist
|
|
5
|
+
* @param {string} html
|
|
6
|
+
* @param {Array<string>} allowedTags
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
const stripUnwantedTags = (html, allowedTags = []) => {
|
|
10
|
+
return html.replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, (match, tag) =>
|
|
11
|
+
allowedTags.includes(tag.toLowerCase()) ? match : ''
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Extended RichText wp core component that strips unwanted HTML on paste/input.
|
|
17
|
+
*/
|
|
18
|
+
const RichTextNoHtml = ({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
allowedTags = [],
|
|
22
|
+
...props
|
|
23
|
+
}) => {
|
|
24
|
+
const handleChange = (newValue) => {
|
|
25
|
+
const cleanValue = stripUnwantedTags(newValue, allowedTags);
|
|
26
|
+
onChange(cleanValue);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<RichText
|
|
31
|
+
value={value}
|
|
32
|
+
onChange={handleChange}
|
|
33
|
+
{...props}
|
|
34
|
+
/>
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default RichTextNoHtml;
|