@@ -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
}
@@ -59,6 +74,26 @@ function genFragment(
59
74
return { chunk : getAtomicBlockChunk ( entityId ) } ;
60
75
}
61
76
77
+ if (
78
+ nodeName === 'video' &&
79
+ node instanceof HTMLVideoElement
80
+ ) {
81
+ const entityConfig = { } ;
82
+ entityConfig . src = node . getAttribute ? node . getAttribute ( 'src' ) || node . src : node . src ;
83
+ entityConfig . alt = node . alt ;
84
+ entityConfig . height = node . style . height ;
85
+ entityConfig . width = node . style . width ;
86
+ if ( node . style . float ) {
87
+ entityConfig . alignment = node . style . float ;
88
+ }
89
+ const entityId = Entity . __create (
90
+ 'VIDEO' ,
91
+ 'MUTABLE' ,
92
+ entityConfig ,
93
+ ) ;
94
+ return { chunk : getAtomicBlockChunk ( entityId ) } ;
95
+ }
96
+
62
97
if (
63
98
nodeName === 'iframe' &&
64
99
node instanceof HTMLIFrameElement
@@ -114,27 +149,27 @@ function genFragment(
114
149
let child = node . firstChild ;
115
150
while ( child ) {
116
151
const entityId = getEntityId ( child ) ;
117
- const { chunk : generatedChunk } = genFragment ( child , inlineStyle , depth , lastList , ( entityId || inEntity ) ) ;
152
+ const { chunk : generatedChunk } = genFragment ( child , inlineStyle , depth , lastList , ( entityId || inEntity ) , customChunkGenerator ) ;
118
153
chunk = joinChunks ( chunk , generatedChunk ) ;
119
154
const sibling = child . nextSibling ;
120
155
child = sibling ;
121
156
}
122
157
return { chunk } ;
123
158
}
124
159
125
- function getChunkForHTML ( html : string ) : Object {
160
+ function getChunkForHTML ( html : string , customChunkGenerator : ? CustomChunkGenerator ) : Object {
126
161
const sanitizedHtml = html . trim ( ) . replace ( REGEX_NBSP , SPACE ) ;
127
162
const safeBody = getSafeBodyFromHTML ( sanitizedHtml ) ;
128
163
if ( ! safeBody ) {
129
164
return null ;
130
165
}
131
166
firstBlock = true ;
132
- const { chunk } = genFragment ( safeBody , new OrderedSet ( ) , - 1 , '' , undefined ) ;
167
+ const { chunk } = genFragment ( safeBody , new OrderedSet ( ) , - 1 , '' , undefined , customChunkGenerator ) ;
133
168
return { chunk } ;
134
169
}
135
170
136
- export default function htmlToDraft ( html : string ) : Object {
137
- const chunkData = getChunkForHTML ( html ) ;
171
+ export default function htmlToDraft ( html : string , customChunkGenerator : ? CustomChunkGenerator ) : Object {
172
+ const chunkData = getChunkForHTML ( html , customChunkGenerator ) ;
138
173
if ( chunkData ) {
139
174
const { chunk } = chunkData ;
140
175
let entityMap = new OrderedMap ( { } ) ;
0 commit comments