import Delta from 'quill-delta'; import { Range } from '../../../core/selection'; import Quill from '../../../core'; describe('Clipboard', function() { describe('events', function() { beforeEach(function() { this.quill = this.initialize(Quill, '

0123

5678

'); this.quill.setSelection(2, 5); }); it('paste', function(done) { this.quill.clipboard.container.innerHTML = '|'; this.quill.clipboard.onPaste({}); setTimeout(() => { expect(this.quill.root).toEqualHTML('

01|78

'); expect(this.quill.getSelection()).toEqual(new Range(3)); done(); }, 2); }); it('selection-change', function(done) { let handler = { change: function() {} }; spyOn(handler, 'change'); this.quill.on('selection-change', handler.change); this.quill.clipboard.container.innerHTML = '0'; this.quill.clipboard.onPaste({}); setTimeout(function() { expect(handler.change).not.toHaveBeenCalled(); done(); }, 2); }); }); describe('convert', function() { beforeEach(function() { let quill = this.initialize(Quill, ''); this.clipboard = quill.clipboard; }); it('plain text', function() { let delta = this.clipboard.convert('simple plain text'); expect(delta).toEqual(new Delta().insert('simple plain text')); }); it('whitespace', function() { let html = '
0
1 2 3 4
' + '
5 6 7 8
'; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert('0\n1 2 3 4\n5 6 7 8')); }); it('inline whitespace', function() { let html = '

0 1 2

'; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert('0 ').insert('1', { bold: true }).insert(' 2')); }); it('intentional whitespace', function() { let html = '1 2'; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert('0\u00a0').insert('1', { bold: true }).insert('\u00a02')); }); it('consecutive intentional whitespace', function() { let html = '  1  '; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert('\u00a0\u00a01\u00a0\u00a0', { bold: true })); }); it('break', function() { let html = '
0
1
2
3

4

5
'; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert('0\n1\n2\n3\n\n4\n\n5')); }); it('mixed inline and block', function() { let delta = this.clipboard.convert('
One
Two
'); expect(delta).toEqual(new Delta().insert('One\nTwo')); }); it('alias', function() { let delta = this.clipboard.convert('BoldItalic'); expect(delta).toEqual(new Delta().insert('Bold', { bold: true }).insert('Italic', { italic: true })); }); it('pre', function() { let html = '
01 \n 23
'; let delta = this.clipboard.convert(html); expect(delta).toEqual(new Delta().insert(' 01 \n 23 ')); }); it('nested list', function() { let delta = this.clipboard.convert('
  1. One
  2. Alpha
'); expect(delta).toEqual(new Delta().insert('One\n', { list: 'ordered' }) .insert('Alpha\n', { list: 'ordered', indent: 1 })); }); it('html nested list', function() { let delta = this.clipboard.convert('
  1. One
    1. Alpha
    2. Beta
'); expect(delta).toEqual(new Delta().insert('One\nAlpha', { list: 'ordered' }) .insert('\n', { list: 'ordered', indent: 1 }) .insert('Beta', { list: 'ordered' }) .insert('\n', { list: 'ordered', indent: 1 })); }); it('embeds', function() { let delta = this.clipboard.convert('
0134
'); let expected = new Delta() .insert('01') .insert({ image: '/assets/favicon.png' }, { height: '200', width: '300' }) .insert('34'); expect(delta).toEqual(expected); }); it('block embed', function() { let delta = this.clipboard.convert('

01

34

'); expect(delta).toEqual(new Delta().insert('01\n').insert({ video: '#' }).insert('34')); }); it('attributor and style match', function() { let delta = this.clipboard.convert('

Test

'); expect(delta).toEqual(new Delta().insert('Test\n', { direction: 'rtl' })); }); it('nested styles', function() { let delta = this.clipboard.convert('Test'); expect(delta).toEqual(new Delta().insert('Test', { color: 'blue' })); }) it('custom matcher', function() { this.clipboard.addMatcher(Node.TEXT_NODE, function(node, delta) { let index = 0; let regex = /https?:\/\/[^\s]+/g; let match = null; let composer = new Delta(); while ((match = regex.exec(node.data)) !== null) { composer.retain(match.index - index); index = regex.lastIndex; composer.retain(match[0].length, { link: match[0] }); } return delta.compose(composer); }); let delta = this.clipboard.convert('http://github.com https://quilljs.com'); let expected = new Delta().insert('http://github.com', { link: 'http://github.com' }) .insert(' ') .insert('https://quilljs.com', { link: 'https://quilljs.com' }); expect(delta).toEqual(expected); }); }); });