diff --git a/blots/scroll.js b/blots/scroll.js index 2418cfae51..e958735ff6 100644 --- a/blots/scroll.js +++ b/blots/scroll.js @@ -72,7 +72,7 @@ class Scroll extends Parchment.Scroll { } leaf(index) { - return this.path(index).pop(); + return this.path(index).pop() || [null, -1]; } line(index) { diff --git a/test/unit/blots/scroll.js b/test/unit/blots/scroll.js index c191f62ba8..7ad14e40d1 100644 --- a/test/unit/blots/scroll.js +++ b/test/unit/blots/scroll.js @@ -1,5 +1,7 @@ import Parchment from 'parchment'; import Emitter from '../../../core/emitter'; +import Selection, { Range } from '../../../core/selection'; +import Cursor from '../../../blots/cursor'; import Scroll from '../../../blots/scroll'; @@ -34,4 +36,43 @@ describe('Scroll', function() { scroll.formatAt(6, 5, 'italic', true); expect(scroll.domNode.firstChild).toEqualHTML('Hello World!'); }); + + describe('leaf()', function() { + it('text', function() { + let scroll = this.initialize(Scroll, '

Tests

'); + let [leaf, offset] = scroll.leaf(2); + expect(leaf.value()).toEqual('Tests'); + expect(offset).toEqual(2); + }); + + it('precise', function() { + let scroll = this.initialize(Scroll, '

01234

'); + let [leaf, offset] = scroll.leaf(3); + expect(leaf.value()).toEqual('2'); + expect(offset).toEqual(1); + }); + + it('newline', function() { + let scroll = this.initialize(Scroll, '

0123

5678

'); + let [leaf, offset] = scroll.leaf(4); + expect(leaf.value()).toEqual('0123'); + expect(offset).toEqual(4); + }); + + it('cursor', function() { + let selection = this.initialize(Selection, '

012

'); + selection.setRange(new Range(2)); + selection.format('strike', true); + let [leaf, offset] = selection.scroll.leaf(2); + expect(leaf instanceof Cursor).toBe(true); + expect(offset).toEqual(0); + }); + + it('beyond document', function() { + let scroll = this.initialize(Scroll, '

Test

'); + let [leaf, offset] = scroll.leaf(10); + expect(leaf).toEqual(null); + expect(offset).toEqual(-1); + }); + }); }); diff --git a/test/unit/core/selection.js b/test/unit/core/selection.js index 53f9d1fae4..9dd32f72d9 100644 --- a/test/unit/core/selection.js +++ b/test/unit/core/selection.js @@ -1,5 +1,5 @@ import Delta from 'rich-text/lib/delta'; -import Selection, { Range, findLeaf } from '../../../core/selection'; +import Selection, { Range } from '../../../core/selection'; import Cursor from '../../../blots/cursor'; import Scroll from '../../../blots/scroll'; @@ -12,44 +12,6 @@ describe('Selection', function() { }; }); - describe('findLeaf', function() { - it('text', function() { - let scroll = this.initialize(Scroll, '

Tests

'); - let [leaf, offset] = findLeaf(scroll, 2); - expect(leaf.value()).toEqual('Tests'); - expect(offset).toEqual(2); - }); - - it('precise', function() { - let scroll = this.initialize(Scroll, '

01234

'); - let [leaf, offset] = findLeaf(scroll, 3); - expect(leaf.value()).toEqual('2'); - expect(offset).toEqual(1); - }); - - it('newline', function() { - let scroll = this.initialize(Scroll, '

0123

5678

'); - let [leaf, offset] = findLeaf(scroll, 4); - expect(leaf.value()).toEqual('0123'); - expect(offset).toEqual(4); - }); - - it('cursor', function() { - this.setup('

012

', 2); - this.selection.format('strike', true); - let [leaf, offset] = findLeaf(this.selection.scroll, 2); - expect(leaf instanceof Cursor).toBe(true); - expect(offset).toEqual(0); - }); - - it('beyond document', function() { - let scroll = this.initialize(Scroll, '

Test

'); - let [leaf, offset] = findLeaf(scroll, 10); - expect(leaf).toEqual(null); - expect(offset).toEqual(-1); - }); - }); - describe('focus()', function() { beforeEach(function() { this.initialize(HTMLElement, '
');