clipboard.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import Delta from 'quill-delta';
  2. import { Range } from '../../../core/selection';
  3. import Quill from '../../../core';
  4. describe('Clipboard', function() {
  5. describe('events', function() {
  6. beforeEach(function() {
  7. this.quill = this.initialize(Quill, '<h1>0123</h1><p>5<em>67</em>8</p>');
  8. this.quill.setSelection(2, 5);
  9. });
  10. it('paste', function(done) {
  11. this.quill.clipboard.container.innerHTML = '<strong>|</strong>';
  12. this.quill.clipboard.onPaste({});
  13. setTimeout(() => {
  14. expect(this.quill.root).toEqualHTML('<p>01<strong>|</strong><em>7</em>8</p>');
  15. expect(this.quill.getSelection()).toEqual(new Range(3));
  16. done();
  17. }, 2);
  18. });
  19. it('selection-change', function(done) {
  20. let handler = {
  21. change: function() {}
  22. };
  23. spyOn(handler, 'change');
  24. this.quill.on('selection-change', handler.change);
  25. this.quill.clipboard.container.innerHTML = '0';
  26. this.quill.clipboard.onPaste({});
  27. setTimeout(function() {
  28. expect(handler.change).not.toHaveBeenCalled();
  29. done();
  30. }, 2);
  31. });
  32. });
  33. describe('convert', function() {
  34. beforeEach(function() {
  35. let quill = this.initialize(Quill, '');
  36. this.clipboard = quill.clipboard;
  37. });
  38. it('plain text', function() {
  39. let delta = this.clipboard.convert('simple plain text');
  40. expect(delta).toEqual(new Delta().insert('simple plain text'));
  41. });
  42. it('whitespace', function() {
  43. let html =
  44. '<div> 0 </div><div> <div> 1 2 <span> 3 </span> 4 </div> </div>' +
  45. '<div><span>5 </span><span>6 </span><span> 7</span><span> 8</span></div>';
  46. let delta = this.clipboard.convert(html);
  47. expect(delta).toEqual(new Delta().insert('0\n1 2 3 4\n5 6 7 8'));
  48. });
  49. it('inline whitespace', function() {
  50. let html = '<p>0 <strong>1</strong> 2</p>';
  51. let delta = this.clipboard.convert(html);
  52. expect(delta).toEqual(new Delta().insert('0 ').insert('1', { bold: true }).insert(' 2'));
  53. });
  54. it('intentional whitespace', function() {
  55. let html = '<span>0&nbsp;<strong>1</strong>&nbsp;2</span>';
  56. let delta = this.clipboard.convert(html);
  57. expect(delta).toEqual(new Delta().insert('0\u00a0').insert('1', { bold: true }).insert('\u00a02'));
  58. });
  59. it('consecutive intentional whitespace', function() {
  60. let html = '<strong>&nbsp;&nbsp;1&nbsp;&nbsp;</strong>';
  61. let delta = this.clipboard.convert(html);
  62. expect(delta).toEqual(new Delta().insert('\u00a0\u00a01\u00a0\u00a0', { bold: true }));
  63. });
  64. it('break', function() {
  65. let html = '<div>0<br>1</div><div>2<br></div><div>3</div><div><br>4</div><div><br></div><div>5</div>';
  66. let delta = this.clipboard.convert(html);
  67. expect(delta).toEqual(new Delta().insert('0\n1\n2\n3\n\n4\n\n5'));
  68. });
  69. it('mixed inline and block', function() {
  70. let delta = this.clipboard.convert('<div>One<div>Two</div></div>');
  71. expect(delta).toEqual(new Delta().insert('One\nTwo'));
  72. });
  73. it('alias', function() {
  74. let delta = this.clipboard.convert('<b>Bold</b><i>Italic</i>');
  75. expect(delta).toEqual(new Delta().insert('Bold', { bold: true }).insert('Italic', { italic: true }));
  76. });
  77. it('pre', function() {
  78. let html = '<div style="white-space: pre;"> 01 \n 23 </div>';
  79. let delta = this.clipboard.convert(html);
  80. expect(delta).toEqual(new Delta().insert(' 01 \n 23 '));
  81. });
  82. it('nested list', function() {
  83. let delta = this.clipboard.convert('<ol><li>One</li><li class="ql-indent-1">Alpha</li></ol>');
  84. expect(delta).toEqual(new Delta().insert('One\n', { list: 'ordered' })
  85. .insert('Alpha\n', { list: 'ordered', indent: 1 }));
  86. });
  87. it('html nested list', function() {
  88. let delta = this.clipboard.convert('<ol><li>One<ol><li>Alpha</li><li>Beta</li></ol></li></ol>');
  89. expect(delta).toEqual(new Delta().insert('One\nAlpha', { list: 'ordered' })
  90. .insert('\n', { list: 'ordered', indent: 1 })
  91. .insert('Beta', { list: 'ordered' })
  92. .insert('\n', { list: 'ordered', indent: 1 }));
  93. });
  94. it('embeds', function() {
  95. let delta = this.clipboard.convert('<div>01<img src="/assets/favicon.png" height="200" width="300">34</div>');
  96. let expected = new Delta()
  97. .insert('01')
  98. .insert({ image: '/assets/favicon.png' }, { height: '200', width: '300' })
  99. .insert('34');
  100. expect(delta).toEqual(expected);
  101. });
  102. it('block embed', function() {
  103. let delta = this.clipboard.convert('<p>01</p><iframe src="#"></iframe><p>34</p>');
  104. expect(delta).toEqual(new Delta().insert('01\n').insert({ video: '#' }).insert('34'));
  105. });
  106. it('attributor and style match', function() {
  107. let delta = this.clipboard.convert('<p style="direction:rtl;">Test</p>');
  108. expect(delta).toEqual(new Delta().insert('Test\n', { direction: 'rtl' }));
  109. });
  110. it('nested styles', function() {
  111. let delta = this.clipboard.convert('<span style="color: red;"><span style="color: blue;">Test</span></span>');
  112. expect(delta).toEqual(new Delta().insert('Test', { color: 'blue' }));
  113. })
  114. it('custom matcher', function() {
  115. this.clipboard.addMatcher(Node.TEXT_NODE, function(node, delta) {
  116. let index = 0;
  117. let regex = /https?:\/\/[^\s]+/g;
  118. let match = null;
  119. let composer = new Delta();
  120. while ((match = regex.exec(node.data)) !== null) {
  121. composer.retain(match.index - index);
  122. index = regex.lastIndex;
  123. composer.retain(match[0].length, { link: match[0] });
  124. }
  125. return delta.compose(composer);
  126. });
  127. let delta = this.clipboard.convert('http://github.com https://quilljs.com');
  128. let expected = new Delta().insert('http://github.com', { link: 'http://github.com' })
  129. .insert(' ')
  130. .insert('https://quilljs.com', { link: 'https://quilljs.com' });
  131. expect(delta).toEqual(expected);
  132. });
  133. });
  134. });