forked from HubSpot/draft-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertFromHTML.js
724 lines (624 loc) · 18 KB
/
convertFromHTML.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the /src directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import { List, Map, OrderedSet } from 'immutable'
import {
BlockMapBuilder,
CharacterMetadata,
ContentBlock,
ContentState,
Entity,
SelectionState,
genKey,
} from 'draft-js'
import getSafeBodyFromHTML from './util/parseHTML'
import rangeSort from './util/rangeSort'
const SPACE = ' '
// Arbitrary max indent
const MAX_DEPTH = 4
// used for replacing characters in HTML
const REGEX_CR = /\r/g
const REGEX_LF = /\n/g
const REGEX_NBSP = / /g
const REGEX_BLOCK_DELIMITER = /\r/g
// Block tag flow is different because LIs do not have
// a deterministic style ;_;
const blockTags = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'pre']
const inlineTags = {
b: 'BOLD',
code: 'CODE',
del: 'STRIKETHROUGH',
em: 'ITALIC',
i: 'ITALIC',
s: 'STRIKETHROUGH',
strike: 'STRIKETHROUGH',
strong: 'BOLD',
u: 'UNDERLINE',
}
const handleMiddleware = (maybeMiddleware, base) => {
if (maybeMiddleware && maybeMiddleware.__isMiddleware === true) {
return maybeMiddleware(base)
}
return maybeMiddleware
}
const defaultHTMLToBlock = (nodeName, node, lastList) => undefined
const defaultHTMLToStyle = (nodeName, node, currentStyle) => currentStyle
const defaultHTMLToEntity = (nodeName, node) => undefined
const defaultTextToEntity = (text) => []
const nullthrows = (x) => {
if (x != null) {
return x
}
throw new Error('Got unexpected null or undefined')
}
const sanitizeDraftText = (input) => input.replace(REGEX_BLOCK_DELIMITER, '')
function getEmptyChunk() {
return {
text: '',
inlines: [],
entities: [],
blocks: [],
}
}
function getWhitespaceChunk(inEntity) {
const entities = new Array(1)
if (inEntity) {
entities[0] = inEntity
}
return {
text: SPACE,
inlines: [OrderedSet()],
entities,
blocks: [],
}
}
function getSoftNewlineChunk(block, depth, flat = false, data = Map()) {
if (flat === true) {
return {
text: '\r',
inlines: [OrderedSet()],
entities: new Array(1),
blocks: [
{
type: block,
data,
depth: Math.max(0, Math.min(MAX_DEPTH, depth)),
},
],
isNewline: true,
}
}
return {
text: '\n',
inlines: [OrderedSet()],
entities: new Array(1),
blocks: [],
}
}
function getBlockDividerChunk(block, depth, data = Map()) {
return {
text: '\r',
inlines: [OrderedSet()],
entities: new Array(1),
blocks: [
{
type: block,
data,
depth: Math.max(0, Math.min(MAX_DEPTH, depth)),
},
],
}
}
function getBlockTypeForTag(tag, lastList) {
switch (tag) {
case 'h1':
return 'header-one'
case 'h2':
return 'header-two'
case 'h3':
return 'header-three'
case 'h4':
return 'header-four'
case 'h5':
return 'header-five'
case 'h6':
return 'header-six'
case 'li':
if (lastList === 'ol') {
return 'ordered-list-item'
}
return 'unordered-list-item'
case 'blockquote':
return 'blockquote'
case 'pre':
return 'code-block'
case 'div':
case 'p':
return 'unstyled'
default:
return null
}
}
function baseCheckBlockType(nodeName, node, lastList) {
return getBlockTypeForTag(nodeName, lastList)
}
function processInlineTag(tag, node, currentStyle) {
const styleToCheck = inlineTags[tag]
if (styleToCheck) {
currentStyle = currentStyle.add(styleToCheck).toOrderedSet()
} else if (node instanceof HTMLElement) {
const htmlElement = node
currentStyle = currentStyle
.withMutations((style) => {
if (htmlElement.style.fontWeight === 'bold') {
style.add('BOLD')
}
if (htmlElement.style.fontStyle === 'italic') {
style.add('ITALIC')
}
if (htmlElement.style.textDecoration === 'underline') {
style.add('UNDERLINE')
}
if (htmlElement.style.textDecoration === 'line-through') {
style.add('STRIKETHROUGH')
}
})
.toOrderedSet()
}
return currentStyle
}
function baseProcessInlineTag(tag, node, inlineStyles = OrderedSet()) {
return processInlineTag(tag, node, inlineStyles)
}
function joinChunks(A, B, flat = false) {
// Sometimes two blocks will touch in the DOM and we need to strip the
// extra delimiter to preserve niceness.
const firstInB = B.text.slice(0, 1)
const lastInA = A.text.slice(-1)
const adjacentDividers = lastInA === '\r' && firstInB === '\r'
const isJoiningBlocks = A.text !== '\r' && B.text !== '\r' // when joining two full blocks like this we want to pop one divider
const addingNewlineToEmptyBlock = A.text === '\r' && !A.isNewline && B.isNewline // when joining a newline to an empty block we want to remove the newline
if (adjacentDividers && (isJoiningBlocks || addingNewlineToEmptyBlock)) {
A.text = A.text.slice(0, -1)
A.inlines.pop()
A.entities.pop()
A.blocks.pop()
}
// Kill whitespace after blocks if flat mode is on
if (A.text.slice(-1) === '\r' && flat === true) {
if (B.text === SPACE || B.text === '\n') {
return A
}
if (firstInB === SPACE || firstInB === '\n') {
B.text = B.text.slice(1)
B.inlines.shift()
B.entities.shift()
}
}
const isNewline = A.text.length === 0 && B.isNewline
return {
text: A.text + B.text,
inlines: A.inlines.concat(B.inlines),
entities: A.entities.concat(B.entities),
blocks: A.blocks.concat(B.blocks),
isNewline,
}
}
/*
* Check to see if we have anything like <p> <blockquote> <h1>... to create
* block tags from. If we do, we can use those and ignore <div> tags. If we
* don't, we can treat <div> tags as meaningful (unstyled) blocks.
*/
function containsSemanticBlockMarkup(html) {
return blockTags.some((tag) => html.indexOf(`<${tag}`) !== -1)
}
function genFragment(
node,
inlineStyle,
lastList,
inBlock,
fragmentBlockTags,
depth,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options,
inEntity
) {
let nodeName = node.nodeName.toLowerCase()
let newBlock = false
let nextBlockType = 'unstyled'
// Base Case
if (nodeName === '#text') {
let text = node.textContent
if (text.trim() === '' && inBlock === null) {
return getEmptyChunk()
}
if (text.trim() === '' && inBlock !== 'code-block') {
return getWhitespaceChunk(inEntity)
}
if (inBlock !== 'code-block') {
// Can't use empty string because MSWord
text = text.replace(REGEX_LF, SPACE)
}
const entities = Array(text.length).fill(inEntity)
let offsetChange = 0
const textEntities = checkEntityText(
text,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData
).sort(rangeSort)
textEntities.forEach(({ entity, offset, length, result }) => {
const adjustedOffset = offset + offsetChange
if (result === null || result === undefined) {
result = text.substr(adjustedOffset, length)
}
const textArray = text.split('')
textArray.splice.bind(textArray, adjustedOffset, length).apply(textArray, result.split(''))
text = textArray.join('')
entities.splice
.bind(entities, adjustedOffset, length)
.apply(entities, Array(result.length).fill(entity))
offsetChange += result.length - length
})
return {
text,
inlines: Array(text.length).fill(inlineStyle),
entities,
blocks: [],
}
}
// BR tags
if (nodeName === 'br') {
const blockType = inBlock
if (blockType === null) {
// BR tag is at top level, treat it as an unstyled block
return getSoftNewlineChunk('unstyled', depth, true)
}
return getSoftNewlineChunk(blockType || 'unstyled', depth, options.flat)
}
let chunk = getEmptyChunk()
let newChunk = null
// Inline tags
inlineStyle = processInlineTag(nodeName, node, inlineStyle)
inlineStyle = processCustomInlineStyles(nodeName, node, inlineStyle)
// Handle lists
if (nodeName === 'ul' || nodeName === 'ol') {
if (lastList) {
depth += 1
}
lastList = nodeName
inBlock = null
}
// Block Tags
let blockInfo = checkBlockType(nodeName, node, lastList, inBlock)
let blockType
let blockDataMap
if (blockInfo === false) {
return getEmptyChunk()
}
blockInfo = blockInfo || {}
if (typeof blockInfo === 'string') {
blockType = blockInfo
blockDataMap = Map()
} else {
blockType = typeof blockInfo === 'string' ? blockInfo : blockInfo.type
blockDataMap = blockInfo.data ? Map(blockInfo.data) : Map()
}
if (!inBlock && (fragmentBlockTags.indexOf(nodeName) !== -1 || blockType)) {
chunk = getBlockDividerChunk(
blockType || getBlockTypeForTag(nodeName, lastList),
depth,
blockDataMap
)
inBlock = blockType || getBlockTypeForTag(nodeName, lastList)
newBlock = true
} else if (
lastList &&
(inBlock === 'ordered-list-item' || inBlock === 'unordered-list-item') &&
nodeName === 'li'
) {
const listItemBlockType = getBlockTypeForTag(nodeName, lastList)
chunk = getBlockDividerChunk(listItemBlockType, depth)
inBlock = listItemBlockType
newBlock = true
nextBlockType = lastList === 'ul' ? 'unordered-list-item' : 'ordered-list-item'
} else if (inBlock && inBlock !== 'atomic' && blockType === 'atomic') {
inBlock = blockType
newBlock = true
chunk = getSoftNewlineChunk(
blockType,
depth,
true, // atomic blocks within non-atomic blocks must always be split out
blockDataMap
)
}
// Recurse through children
let child = node.firstChild
// hack to allow conversion of atomic blocks from HTML (e.g. <figure><img
// src="..." /></figure>). since metadata must be stored on an entity text
// must exist for the entity to apply to. the way chunks are joined strips
// whitespace at the end so it cannot be a space character.
if (child == null && inEntity && (blockType === 'atomic' || inBlock === 'atomic')) {
child = document.createTextNode('a')
}
if (child != null) {
nodeName = child.nodeName.toLowerCase()
}
let entityId = null
while (child) {
entityId = checkEntityNode(
nodeName,
child,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData
)
newChunk = genFragment(
child,
inlineStyle,
lastList,
inBlock,
fragmentBlockTags,
depth,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options,
entityId || inEntity
)
chunk = joinChunks(chunk, newChunk, options.flat)
const sibling = child.nextSibling
// Put in a newline to break up blocks inside blocks
if (sibling && fragmentBlockTags.indexOf(nodeName) >= 0 && inBlock) {
let newBlockInfo = checkBlockType(nodeName, child, lastList, inBlock)
let newBlockType
let newBlockData
if (newBlockInfo !== false) {
newBlockInfo = newBlockInfo || {}
if (typeof newBlockInfo === 'string') {
newBlockType = newBlockInfo
newBlockData = Map()
} else {
newBlockType = newBlockInfo.type || getBlockTypeForTag(nodeName, lastList)
newBlockData = newBlockInfo.data ? Map(newBlockInfo.data) : Map()
}
chunk = joinChunks(
chunk,
getSoftNewlineChunk(newBlockType, depth, options.flat, newBlockData),
options.flat
)
}
}
if (sibling) {
nodeName = sibling.nodeName.toLowerCase()
}
child = sibling
}
if (newBlock) {
chunk = joinChunks(chunk, getBlockDividerChunk(nextBlockType, depth, Map()), options.flat)
}
return chunk
}
function getChunkForHTML(
html,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options,
DOMBuilder
) {
html = html.trim().replace(REGEX_CR, '').replace(REGEX_NBSP, SPACE)
const safeBody = DOMBuilder(html)
if (!safeBody) {
return null
}
// Sometimes we aren't dealing with content that contains nice semantic
// tags. In this case, use divs to separate everything out into paragraphs
// and hope for the best.
const workingBlocks = containsSemanticBlockMarkup(html) ? blockTags.concat(['div']) : ['div']
// Start with -1 block depth to offset the fact that we are passing in a fake
// UL block to sta rt with.
let chunk = genFragment(
safeBody,
OrderedSet(),
'ul',
null,
workingBlocks,
-1,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options
)
// join with previous block to prevent weirdness on paste
if (chunk.text.indexOf('\r') === 0) {
chunk = {
text: chunk.text.slice(1),
inlines: chunk.inlines.slice(1),
entities: chunk.entities.slice(1),
blocks: chunk.blocks,
}
}
// Kill block delimiter at the end
if (chunk.text.slice(-1) === '\r') {
chunk.text = chunk.text.slice(0, -1)
chunk.inlines = chunk.inlines.slice(0, -1)
chunk.entities = chunk.entities.slice(0, -1)
chunk.blocks.pop()
}
// If we saw no block tags, put an unstyled one in
if (chunk.blocks.length === 0) {
chunk.blocks.push({ type: 'unstyled', data: Map(), depth: 0 })
}
// Sometimes we start with text that isn't in a block, which is then
// followed by blocks. Need to fix up the blocks to add in
// an unstyled block for this content
if (chunk.text.split('\r').length === chunk.blocks.length + 1) {
chunk.blocks.unshift({ type: 'unstyled', data: Map(), depth: 0 })
}
return chunk
}
function convertFromHTMLtoContentBlocks(
html,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options,
DOMBuilder,
generateKey
) {
// Be ABSOLUTELY SURE that the dom builder you pass hare won't execute
// arbitrary code in whatever environment you're running this in. For an
// example of how we try to do this in-browser, see getSafeBodyFromHTML.
const chunk = getChunkForHTML(
html,
processCustomInlineStyles,
checkEntityNode,
checkEntityText,
checkBlockType,
createEntity,
getEntity,
mergeEntityData,
replaceEntityData,
options,
DOMBuilder,
generateKey
)
if (chunk == null) {
return []
}
let start = 0
return chunk.text.split('\r').map((textBlock, blockIndex) => {
// Make absolutely certain that our text is acceptable.
textBlock = sanitizeDraftText(textBlock)
const end = start + textBlock.length
const inlines = nullthrows(chunk).inlines.slice(start, end)
const entities = nullthrows(chunk).entities.slice(start, end)
const characterList = List(
inlines.map((style, entityIndex) => {
const data = { style, entity: null }
if (entities[entityIndex]) {
data.entity = entities[entityIndex]
}
return CharacterMetadata.create(data)
})
)
start = end + 1
return new ContentBlock({
key: generateKey(),
type: nullthrows(chunk).blocks[blockIndex].type,
data: nullthrows(chunk).blocks[blockIndex].data,
depth: nullthrows(chunk).blocks[blockIndex].depth,
text: textBlock,
characterList,
})
})
}
const convertFromHTML =
({
htmlToStyle = defaultHTMLToStyle,
htmlToEntity = defaultHTMLToEntity,
textToEntity = defaultTextToEntity,
htmlToBlock = defaultHTMLToBlock,
}) =>
(
html,
options = {
flat: false,
},
DOMBuilder = getSafeBodyFromHTML,
generateKey = genKey
) => {
let contentState = ContentState.createFromText('')
const createEntityWithContentState = (...args) => {
if (contentState.createEntity) {
contentState = contentState.createEntity(...args)
return contentState.getLastCreatedEntityKey()
}
return Entity.create(...args)
}
const getEntityWithContentState = (...args) => {
if (contentState.getEntity) {
return contentState.getEntity(...args)
}
return Entity.get(...args)
}
const mergeEntityDataWithContentState = (...args) => {
if (contentState.mergeEntityData) {
contentState = contentState.mergeEntityData(...args)
return
}
Entity.mergeData(...args)
}
const replaceEntityDataWithContentState = (...args) => {
if (contentState.replaceEntityData) {
contentState = contentState.replaceEntityData(...args)
return
}
Entity.replaceData(...args)
}
const contentBlocks = convertFromHTMLtoContentBlocks(
html,
handleMiddleware(htmlToStyle, baseProcessInlineTag),
handleMiddleware(htmlToEntity, defaultHTMLToEntity),
handleMiddleware(textToEntity, defaultTextToEntity),
handleMiddleware(htmlToBlock, baseCheckBlockType),
createEntityWithContentState,
getEntityWithContentState,
mergeEntityDataWithContentState,
replaceEntityDataWithContentState,
options,
DOMBuilder,
generateKey
)
const blockMap = BlockMapBuilder.createFromArray(contentBlocks)
const firstBlockKey = contentBlocks[0].getKey()
return contentState.merge({
blockMap,
selectionBefore: SelectionState.createEmpty(firstBlockKey),
selectionAfter: SelectionState.createEmpty(firstBlockKey),
})
}
export default (...args) => {
if (args.length >= 1 && typeof args[0] === 'string') {
return convertFromHTML({})(...args)
}
return convertFromHTML(...args)
}