18
18
* External dependencies
19
19
*/
20
20
import { EditorState } from 'draft-js' ;
21
+ import { filterEditorState } from 'draftjs-filters' ;
21
22
22
23
/**
23
24
* Internal dependencies
@@ -28,6 +29,16 @@ import customImport from './customImport';
28
29
import customExport from './customExport' ;
29
30
import { getSelectionForAll } from './util' ;
30
31
import type { StyleSetter , AllowedSetterArgs } from './types' ;
32
+ import {
33
+ ITALIC ,
34
+ UNDERLINE ,
35
+ WEIGHT ,
36
+ COLOR ,
37
+ LETTERSPACING ,
38
+ UPPERCASE ,
39
+ GRADIENT_COLOR ,
40
+ } from './customConstants' ;
41
+ import { getPrefixStylesInSelection } from './styleManipulation' ;
31
42
32
43
/**
33
44
* Return an editor state object with content set to parsed HTML
@@ -60,8 +71,7 @@ function updateAndReturnHTML(
60
71
...args : [ AllowedSetterArgs ]
61
72
) {
62
73
const stateWithUpdate = updater ( getSelectAllStateFromHTML ( html ) , ...args ) ;
63
- const renderedHTML = customExport ( stateWithUpdate ) ;
64
- return renderedHTML ;
74
+ return customExport ( stateWithUpdate ) ;
65
75
}
66
76
67
77
const getHTMLFormatter =
@@ -90,3 +100,32 @@ export function getHTMLInfo(html: string) {
90
100
const htmlStateInfo = getStateInfo ( getSelectAllStateFromHTML ( html ) ) ;
91
101
return htmlStateInfo ;
92
102
}
103
+
104
+ export function sanitizeEditorHtml ( html : string ) {
105
+ const editorState = getSelectAllStateFromHTML ( html ) ;
106
+
107
+ const styles : string [ ] = [
108
+ ...getPrefixStylesInSelection ( editorState , ITALIC ) ,
109
+ ...getPrefixStylesInSelection ( editorState , UNDERLINE ) ,
110
+ ...getPrefixStylesInSelection ( editorState , WEIGHT ) ,
111
+ ...getPrefixStylesInSelection ( editorState , COLOR ) ,
112
+ ...getPrefixStylesInSelection ( editorState , LETTERSPACING ) ,
113
+ ...getPrefixStylesInSelection ( editorState , UPPERCASE ) ,
114
+ ...getPrefixStylesInSelection ( editorState , GRADIENT_COLOR ) ,
115
+ ] ;
116
+
117
+ return (
118
+ customExport (
119
+ filterEditorState (
120
+ {
121
+ blocks : [ ] ,
122
+ styles,
123
+ entities : [ ] ,
124
+ maxNesting : 1 ,
125
+ whitespacedCharacters : [ ] ,
126
+ } ,
127
+ editorState
128
+ )
129
+ ) || ''
130
+ ) ;
131
+ }
0 commit comments