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

Keep cursor unlinked to avoid elements being pushed down inside #3534

Merged
merged 1 commit into from
Feb 18, 2022
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
28 changes: 27 additions & 1 deletion blots/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class Cursor extends EmbedBlot {
restore() {
if (this.selection.composing || this.parent == null) return null;
const range = this.selection.getNativeRange();
// Link format will insert text outside of anchor tag
// Browser may push down styles/nodes inside the cursor blot.
// https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#push-down-values
while (
this.domNode.lastChild != null &&
this.domNode.lastChild !== this.textNode
Expand Down Expand Up @@ -144,6 +145,31 @@ class Cursor extends EmbedBlot {
}
}

// Avoid .ql-cursor being a descendant of `<a/>`.
// The reason is Safari pushes down `<a/>` on text insertion.
// That will cause DOM nodes not sync with the model.
//
// For example ({I} is the caret), given the markup:
// <a><span class="ql-cursor">\uFEFF{I}</span></a>
// When typing a char "x", `<a/>` will be pushed down inside the `<span>` first:
// <span class="ql-cursor"><a>\uFEFF{I}</a></span>
// And then "x" will be inserted after `<a/>`:
// <span class="ql-cursor"><a>\uFEFF</a>d{I}</span>
optimize(context) {
super.optimize(context);

let { parent } = this;
while (parent) {
if (parent.domNode.tagName === 'A') {
this.savedLength = Cursor.CONTENTS.length;
parent.isolate(this.offset(parent), this.length()).unwrap();
this.savedLength = 0;
break;
}
parent = parent.parent;
}
}

value() {
return '';
}
Expand Down
3 changes: 2 additions & 1 deletion modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class Clipboard extends Module {
if (!html && files.length > 0) {
this.quill.uploader.upload(range, files);
return;
} else if (html && files.length > 0) {
}
if (html && files.length > 0) {
const doc = new DOMParser().parseFromString(html, 'text/html');
if (
doc.body.childElementCount === 1 &&
Expand Down
34 changes: 34 additions & 0 deletions test/unit/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,40 @@ describe('Selection', function() {
<p>01<em><span class="ql-cursor">${Cursor.CONTENTS}</span></em>23</p>
`);
});

describe('unlink cursor', function() {
const cursorHTML = `<span class="ql-cursor">${Cursor.CONTENTS}</span>`;

it('one level', function() {
this.setup(
'<p><strong><a href="https://example.com">link</a></strong></p><p><br></p>',
4,
);
this.selection.format('bold', false);
expect(this.container).toEqualHTML(`
<p><strong><a href="https://example.com">link</a></strong>${cursorHTML}</p><p><br></p>
`);
});

it('nested formats', function() {
this.setup(
'<p><strong><em><a href="https://example.com">bold</a></em></strong></p><p><br></p>',
4,
);
this.selection.format('italic', false);
expect(this.container).toEqualHTML(`
<p><strong><em><a href="https://example.com">bold</a></em>${cursorHTML}</strong></p><p><br></p>
`);
});

it('ignore link format', function() {
this.setup('<p><strong>bold</strong></p><p><br></p>', 4);
this.selection.format('link', 'https://example.com');
expect(this.container).toEqualHTML(`
<p><strong>bold${cursorHTML}</strong></p><p><br></p>
`);
});
});
});

describe('getBounds()', function() {
Expand Down