@@ -22,15 +22,30 @@ const REGEX_NBSP = new RegExp(' ', 'g');
22
22
23
23
let firstBlock = true ;
24
24
25
+ type CustomChunkGenerator = ( nodeName : string , node : HTMLElement ) => ?{ type : string , mutability : string , data : { } } ;
26
+
25
27
function genFragment (
26
28
node : Object ,
27
29
inlineStyle : OrderedSet ,
28
30
depth : number ,
29
31
lastList : string ,
30
- inEntity : number
32
+ inEntity : number ,
33
+ customChunkGenerator : ?CustomChunkGenerator ,
31
34
) : Object {
32
35
const nodeName = node . nodeName . toLowerCase ( ) ;
33
36
37
+ if ( customChunkGenerator ) {
38
+ const value = customChunkGenerator ( nodeName , node ) ;
39
+ if ( value ) {
40
+ const entityId = Entity . __create (
41
+ value . type ,
42
+ value . mutability ,
43
+ value . data || { } ,
44
+ ) ;
45
+ return { chunk : getAtomicBlockChunk ( entityId ) } ;
46
+ }
47
+ }
48
+
34
49
if ( nodeName === '#text' && node . textContent !== '\n' ) {
35
50
return createTextChunk ( node , inlineStyle , inEntity ) ;
36
51
}
@@ -114,27 +129,27 @@ function genFragment(
114
129
let child = node . firstChild ;
115
130
while ( child ) {
116
131
const entityId = getEntityId ( child ) ;
117
- const { chunk : generatedChunk } = genFragment ( child , inlineStyle , depth , lastList , ( entityId || inEntity ) ) ;
132
+ const { chunk : generatedChunk } = genFragment ( child , inlineStyle , depth , lastList , ( entityId || inEntity ) , customChunkGenerator ) ;
118
133
chunk = joinChunks ( chunk , generatedChunk ) ;
119
134
const sibling = child . nextSibling ;
120
135
child = sibling ;
121
136
}
122
137
return { chunk } ;
123
138
}
124
139
125
- function getChunkForHTML ( html : string ) : Object {
140
+ function getChunkForHTML ( html : string , customChunkGenerator : ? CustomChunkGenerator ) : Object {
126
141
const sanitizedHtml = html . trim ( ) . replace ( REGEX_NBSP , SPACE ) ;
127
142
const safeBody = getSafeBodyFromHTML ( sanitizedHtml ) ;
128
143
if ( ! safeBody ) {
129
144
return null ;
130
145
}
131
146
firstBlock = true ;
132
- const { chunk } = genFragment ( safeBody , new OrderedSet ( ) , - 1 , '' , undefined ) ;
147
+ const { chunk } = genFragment ( safeBody , new OrderedSet ( ) , - 1 , '' , undefined , customChunkGenerator ) ;
133
148
return { chunk } ;
134
149
}
135
150
136
- export default function htmlToDraft ( html : string ) : Object {
137
- const chunkData = getChunkForHTML ( html ) ;
151
+ export default function htmlToDraft ( html : string , customChunkGenerator : ? CustomChunkGenerator ) : Object {
152
+ const chunkData = getChunkForHTML ( html , customChunkGenerator ) ;
138
153
if ( chunkData ) {
139
154
const { chunk } = chunkData ;
140
155
let entityMap = new OrderedMap ( { } ) ;
0 commit comments