link.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Delta from 'quill-delta';
  2. import Editor from '../../../core/editor';
  3. import Link from '../../../formats/link';
  4. describe('Link', function() {
  5. it('add', function() {
  6. const editor = this.initialize(Editor, '<p>0123</p>');
  7. editor.formatText(1, 2, { link: 'https://quilljs.com' });
  8. expect(editor.getDelta()).toEqual(
  9. new Delta()
  10. .insert('0')
  11. .insert('12', { link: 'https://quilljs.com' })
  12. .insert('3\n'),
  13. );
  14. expect(editor.scroll.domNode).toEqualHTML(
  15. '<p>0<a href="https://quilljs.com" target="_blank" rel="noopener noreferrer">12</a>3</p>',
  16. );
  17. });
  18. it('add invalid', function() {
  19. const editor = this.initialize(Editor, '<p>0123</p>');
  20. editor.formatText(1, 2, { link: 'javascript:alert(0);' }); // eslint-disable-line no-script-url
  21. expect(editor.getDelta()).toEqual(
  22. new Delta()
  23. .insert('0')
  24. .insert('12', { link: Link.SANITIZED_URL })
  25. .insert('3\n'),
  26. );
  27. });
  28. it('add non-whitelisted protocol', function() {
  29. const editor = this.initialize(Editor, '<p>0123</p>');
  30. editor.formatText(1, 2, { link: 'gopher://quilljs.com' });
  31. expect(editor.getDelta()).toEqual(
  32. new Delta()
  33. .insert('0')
  34. .insert('12', { link: Link.SANITIZED_URL })
  35. .insert('3\n'),
  36. );
  37. expect(editor.scroll.domNode).toEqualHTML(
  38. '<p>0<a href="about:blank" target="_blank" rel="noopener noreferrer">12</a>3</p>',
  39. );
  40. });
  41. it('change', function() {
  42. const editor = this.initialize(
  43. Editor,
  44. '<p>0<a href="https://github.com" target="_blank" rel="noopener noreferrer">12</a>3</p>',
  45. );
  46. editor.formatText(1, 2, { link: 'https://quilljs.com' });
  47. expect(editor.getDelta()).toEqual(
  48. new Delta()
  49. .insert('0')
  50. .insert('12', { link: 'https://quilljs.com' })
  51. .insert('3\n'),
  52. );
  53. expect(editor.scroll.domNode).toEqualHTML(
  54. '<p>0<a href="https://quilljs.com" target="_blank" rel="noopener noreferrer">12</a>3</p>',
  55. );
  56. });
  57. it('remove', function() {
  58. const editor = this.initialize(
  59. Editor,
  60. '<p>0<a class="ql-size-large" href="https://quilljs.com" target="_blank" rel="noopener noreferrer">12</a>3</p>',
  61. );
  62. editor.formatText(1, 2, { link: false });
  63. const delta = new Delta()
  64. .insert('0')
  65. .insert('12', { size: 'large' })
  66. .insert('3\n');
  67. expect(editor.getDelta()).toEqual(delta);
  68. expect(editor.scroll.domNode).toEqualHTML(
  69. '<p>0<span class="ql-size-large">12</span>3</p>',
  70. );
  71. });
  72. });