scroll.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import Parchment from 'parchment';
  2. import Emitter from '../../../core/emitter';
  3. import Selection, { Range } from '../../../core/selection';
  4. import Cursor from '../../../blots/cursor';
  5. import Scroll from '../../../blots/scroll';
  6. describe('Scroll', function() {
  7. it('initialize empty document', function() {
  8. let scroll = this.initialize(Scroll, '');
  9. expect(scroll.domNode).toEqualHTML('<p><br></p>');
  10. });
  11. it('api change', function() {
  12. let scroll = this.initialize(Scroll, '<p>Hello World!</p>');
  13. spyOn(scroll.emitter, 'emit').and.callThrough();
  14. scroll.insertAt(5, '!');
  15. expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_OPTIMIZE, jasmine.any(Array), jasmine.any(Object));
  16. });
  17. it('user change', function(done) {
  18. let scroll = this.initialize(Scroll, '<p>Hello World!</p>');
  19. spyOn(scroll.emitter, 'emit').and.callThrough();
  20. scroll.domNode.firstChild.appendChild(document.createTextNode('!'));
  21. setTimeout(function() {
  22. expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_OPTIMIZE, jasmine.any(Array), jasmine.any(Object));
  23. expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_UPDATE, Emitter.sources.USER, jasmine.any(Array));
  24. done();
  25. }, 1);
  26. });
  27. it('whitelist', function() {
  28. let scroll = Parchment.create('scroll', { emitter: new Emitter(), whitelist: ['bold'] });
  29. scroll.insertAt(0, 'Hello World!');
  30. scroll.formatAt(0, 5, 'bold', true);
  31. scroll.formatAt(6, 5, 'italic', true);
  32. expect(scroll.domNode.firstChild).toEqualHTML('<strong>Hello</strong> World!');
  33. });
  34. describe('leaf()', function() {
  35. it('text', function() {
  36. let scroll = this.initialize(Scroll, '<p>Tests</p>');
  37. let [leaf, offset] = scroll.leaf(2);
  38. expect(leaf.value()).toEqual('Tests');
  39. expect(offset).toEqual(2);
  40. });
  41. it('precise', function() {
  42. let scroll = this.initialize(Scroll, '<p><u>0</u><s>1</s><u>2</u><s>3</s><u>4</u></p>');
  43. let [leaf, offset] = scroll.leaf(3);
  44. expect(leaf.value()).toEqual('2');
  45. expect(offset).toEqual(1);
  46. });
  47. it('newline', function() {
  48. let scroll = this.initialize(Scroll, '<p>0123</p><p>5678</p>');
  49. let [leaf, offset] = scroll.leaf(4);
  50. expect(leaf.value()).toEqual('0123');
  51. expect(offset).toEqual(4);
  52. });
  53. it('cursor', function() {
  54. let selection = this.initialize(Selection, '<p><u>0</u>1<u>2</u></p>');
  55. selection.setRange(new Range(2));
  56. selection.format('strike', true);
  57. let [leaf, offset] = selection.scroll.leaf(2);
  58. expect(leaf instanceof Cursor).toBe(true);
  59. expect(offset).toEqual(0);
  60. });
  61. it('beyond document', function() {
  62. let scroll = this.initialize(Scroll, '<p>Test</p>');
  63. let [leaf, offset] = scroll.leaf(10);
  64. expect(leaf).toEqual(null);
  65. expect(offset).toEqual(-1);
  66. });
  67. });
  68. });