/* eslint-disable max-len */ /* tslint:disable:max-line-length */ import {getContentStateFromHtml} from '../from-html'; import {convertFromRaw, ContentState, ContentBlock} from 'draft-js'; /** * @description Returns the set of blocks corresponding to the content state * resulting from the conversion of the given HTML. */ function blocksFor(html: string): { contentState: ContentState, blocks: Array } { const contentState = getContentStateFromHtml(html); const blocks = contentState.getBlockMap().toArray(); return {contentState, blocks}; } describe('core.editor3.html.from-html', () => { it('should parse simple HTML', () => { const {blocks} = blocksFor('
some text

some header

some paragraph

'); expect(blocks.length).toBe(3); expect(blocks[0].getText()).toBe('some text'); expect(blocks[0].getType()).toBe('unstyled'); expect(blocks[1].getText()).toBe('some header'); expect(blocks[1].getType()).toBe('header-two'); expect(blocks[2].getText()).toBe('some paragraph'); expect(blocks[2].getType()).toBe('unstyled'); }); it('should parse HTML with tables', () => { const {blocks, contentState} = blocksFor(`

some header

some paragraph

123
456
789
101112
`); expect(blocks.length).toBe(3); expect(blocks[0].getText()).toBe('some header'); expect(blocks[0].getType()).toBe('header-two'); expect(blocks[1].getText()).toBe('some paragraph'); expect(blocks[1].getType()).toBe('unstyled'); expect(blocks[2].getText()).toBe(' '); expect(blocks[2].getType()).toBe('atomic'); const entityKey = blocks[2].getEntityAt(0); const entity = contentState.getEntity(entityKey); const {data} = entity.getData(); expect(data.numCols).toEqual(3); expect(data.numRows).toEqual(4); const expected = [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12'], ]; data.cells.forEach((row, i) => row.forEach((cell, j) => expect(convertFromRaw(data.cells[i][j]).getPlainText('')) .toEqual(expected[i][j]))); }); it('should parse editor2 inline styles', () => { const {blocks} = blocksFor('123'); expect(blocks[0].getText()).toBe('123'); expect(blocks[0].getInlineStyleAt(0).toArray()).toEqual(['SUBSCRIPT']); expect(blocks[0].getInlineStyleAt(1).toArray()).toEqual(['SUPERSCRIPT']); expect(blocks[0].getInlineStyleAt(2).toArray()).toEqual(['STRIKETHROUGH']); }); it('should parse editor2 block styles', () => { const {blocks} = blocksFor('
text
'); expect(blocks[0].getType()).toBe('code-block'); }); it('should create an empty content state if html contains only invisible characters', () => { const {contentState} = blocksFor(` `); expect(contentState.getPlainText()).toEqual(''); }), it('should parse Google Docs special paste', () => { const {blocks} = blocksFor('

bold

italic

underline


'); expect(blocks[0].getInlineStyleAt(0).toArray()).toEqual(['BOLD']); expect(blocks[1].getInlineStyleAt(0).toArray()).toEqual(['ITALIC']); expect(blocks[2].getInlineStyleAt(0).toArray()).toEqual(['UNDERLINE']); expect(blocks[0].getText()).toEqual('bold'); expect(blocks[1].getText()).toEqual('italic'); expect(blocks[2].getText()).toEqual('underline'); }); });