Skip to content

Commit

Permalink
Merge pull request #3489 from quilljs/zh-get-leaf
Browse files Browse the repository at this point in the history
getLeaf() should ignore non-leaf blots
  • Loading branch information
jhchen authored Nov 29, 2021
2 parents d2bd71d + ca2c40a commit 7406491
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions blots/scroll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Scope, ScrollBlot, ContainerBlot } from 'parchment';
import { Scope, ScrollBlot, ContainerBlot, LeafBlot } from 'parchment';
import Emitter from '../core/emitter';
import Block, { BlockEmbed } from './block';
import Break from './break';
Expand Down Expand Up @@ -103,7 +103,11 @@ class Scroll extends ScrollBlot {
}

leaf(index) {
return this.path(index).pop() || [null, -1];
const last = this.path(index).pop();
if (!last || !(last[0] instanceof LeafBlot)) {
return [null, -1];
}
return last;
}

line(index) {
Expand Down
1 change: 1 addition & 0 deletions core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class Quill {
} else {
bounds = this.selection.getBounds(index.index, index.length);
}
if (!bounds) return null;
const containerBounds = this.container.getBoundingClientRect();
return {
bottom: bounds.bottom - containerBounds.top,
Expand Down
9 changes: 9 additions & 0 deletions test/unit/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,5 +630,14 @@ describe('Selection', function() {
this.bounds = selection.getBounds(0, 10);
}).not.toThrow();
});

it('empty container', function() {
const selection = this.initialize(
Selection,
'<table><tr><td data-row="a">a</td></tr></table>',
);
this.quill.updateContents([{ retain: 1 }, { insert: '\n' }]);
expect(selection.getBounds(2, 0)).toEqual(null);
});
});
});

0 comments on commit 7406491

Please sign in to comment.