Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLeaf() should ignore non-leaf blots #3489

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
getLeaf() should ignore non-leaf blots
  • Loading branch information
luin committed Nov 25, 2021
commit ca2c40a42ef57f4e1df74acb5bf2514b6876f65b
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' }]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.quill is undefined, so this test fails for our fork here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following patch fixes the test here:

diff --git a/test/unit/core/selection.js b/test/unit/core/selection.js
index 164f5f8b..ef1d1832 100644
--- a/test/unit/core/selection.js
+++ b/test/unit/core/selection.js
@@ -1,6 +1,9 @@
+import Delta from 'quill-delta';
+
 import Selection, { Range } from '../../../core/selection';
 import Cursor from '../../../blots/cursor';
 import Emitter from '../../../core/emitter';
+import Editor from '../../../core/editor';
 
 describe('Selection', function() {
   beforeEach(function() {
@@ -632,12 +635,13 @@ describe('Selection', function() {
     });
 
     it('empty container', function() {
-      const selection = this.initialize(
-        Selection,
+      const [editor, selection] = this.initialize(
+        [Editor, Selection],
         '<table><tr><td data-row="a">a</td></tr></table>',
       );
-      this.quill.updateContents([{ retain: 1 }, { insert: '\n' }]);
+      editor.applyDelta(new Delta([{ retain: 1 }, { insert: '\n' }]));
       expect(selection.getBounds(2, 0)).toEqual(null);
+      this.bounds = selection.getBounds(0, 1);
     });
   });
 });

expect(selection.getBounds(2, 0)).toEqual(null);
});
});
});