Skip to content

Fix embed selection issues #4464

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions packages/quill/src/core/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ class Selection {
const blot = this.scroll.find(node, true);
// @ts-expect-error Fix me later
const index = blot.offset(this.scroll);
if (offset === 0) {
return index;
}
if (blot instanceof LeafBlot) {
return index + blot.index(node, offset);
}
if (offset === 0) {
return index;
}
// @ts-expect-error Fix me later
return index + blot.length();
});
Expand Down
58 changes: 58 additions & 0 deletions packages/quill/test/unit/core/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import Strike from '../../../src/formats/strike.js';
import { ColorStyle } from '../../../src/formats/color.js';
import { BackgroundStyle } from '../../../src/formats/background.js';
import { SizeClass } from '../../../src/formats/size.js';
import Embed from '../../../src/blots/embed';
import Scroll from '../../../src/blots/scroll';

class TestEmbed extends Embed {
static blotName = 'TestEmbed';
static tagName = 'span';
static className = 'test-embed-blot';

constructor(scroll: Scroll, node: Node) {
super(scroll, node);
}
}

const createSelection = (html: string, container = document.body) => {
const scroll = createScroll(
Expand All @@ -26,6 +38,7 @@ const createSelection = (html: string, container = document.body) => {
ColorStyle,
BackgroundStyle,
SizeClass,
TestEmbed
]),
container,
);
Expand Down Expand Up @@ -226,6 +239,51 @@ describe('Selection', () => {
const [range] = selection.getRange();
expect(range).toEqual(null);
});

test('between inner sides of embed guards', () => {
const selection = createSelection(
`<p><span class="test-embed-blot"></span></p>`,
);
const leftGuard = selection.root.querySelector('.test-embed-blot')?.firstChild as Node;
const rightGuard = selection.root.querySelector('.test-embed-blot')?.lastChild as Node;
selection.setNativeRange(
leftGuard,
1,
rightGuard,
0,
);
const [range] = selection.getRange();
expect(range?.index).toEqual(0);
expect(range?.length).toEqual(1);
});

test('on inner side of embed end guard', () => {
const selection = createSelection(
`<p><span class="test-embed-blot"></span></p>`,
);
const rightGuard = selection.root.querySelector('.test-embed-blot')?.lastChild as Node;
selection.setNativeRange(
rightGuard,
0
);
const [range] = selection.getRange();
expect(range?.index).toEqual(1);
expect(range?.length).toEqual(0);
});

test('on inner side of embed start guard', () => {
const selection = createSelection(
`<p><span class="test-embed-blot"></span></p>`,
);
const leftGuard = selection.root.querySelector('.test-embed-blot')?.firstChild as Node;
selection.setNativeRange(
leftGuard,
1
);
const [range] = selection.getRange();
expect(range?.index).toEqual(0);
expect(range?.length).toEqual(0);
});
});

describe('setRange()', () => {
Expand Down