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, '
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
'; 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 = '0 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 = '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); }); }); });