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

TINY-10755: fix some edge cases of selection.setContent #9554

Open
wants to merge 2 commits 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: 6 additions & 0 deletions .changes/unreleased/tinymce-TINY-10755-2024-04-12.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project: tinymce
kind: Fixed
body: In some cases the selection after `setContent` was in the wrong place.
time: 2024-04-12T09:05:13.486192033+02:00
custom:
Issue: TINY-10755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Optional } from '@ephox/katamari';
import { Remove, SugarElement, SugarNode, Traverse } from '@ephox/sugar';
import { PredicateExists, PredicateFind, Remove, SugarElement, SugarNode, Traverse } from '@ephox/sugar';

import BookmarkManager from '../api/dom/BookmarkManager';
import Editor from '../api/Editor';
Expand All @@ -9,6 +9,7 @@ import CaretPosition from '../caret/CaretPosition';
import { SetSelectionContentArgs } from '../content/ContentTypes';
import { postProcessSetContent, preProcessSetContent } from '../content/PrePostProcess';
import * as MergeText from '../delete/MergeText';
import { isBogus } from '../dom/NodeType';
import * as ScrollIntoView from '../dom/ScrollIntoView';
import { needsToBeNbspLeft, needsToBeNbspRight } from '../keyboard/Nbsps';

Expand Down Expand Up @@ -74,6 +75,20 @@ const rngSetContent = (rng: Range, fragment: DocumentFragment, schema: Schema):
const prevText = firstChild.bind(Traverse.prevSibling).filter(SugarNode.isText).bind(removeEmpty);
const nextText = lastChild.bind(Traverse.nextSibling).filter(SugarNode.isText).bind(removeEmpty);

if (nextText.isNone()) {
const nextNestedChildText = lastChild.bind(Traverse.nextSibling).bind((next) => {
if (!isBogus(next.dom)) {
const candidateDescendant = PredicateFind.descendant(next, (descendant) => SugarNode.isText(descendant));
return candidateDescendant.filter((el) => !PredicateExists.ancestor(el, (desc) => isBogus(desc.dom)));
} else {
return Optional.none();
}
});
nextNestedChildText.each((text) => {
rng.setEnd(text.dom, 0);
});
}

// Join and normalize text
mergeAndNormalizeText(prevText, firstChild, rng, true, schema);
mergeAndNormalizeText(nextText, lastChild, rng, false, schema);
Expand Down
25 changes: 25 additions & 0 deletions modules/tinymce/src/core/test/ts/browser/dom/SelectionTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Keys } from '@ephox/agar';
import { context, describe, it } from '@ephox/bedrock-client';
import { PlatformDetection } from '@ephox/sand';
import { LegacyUnit, TinyAssertions, TinyHooks, TinySelections } from '@ephox/wrap-mcagar';
Expand Down Expand Up @@ -159,6 +160,30 @@ describe('browser.tinymce.core.dom.SelectionTest', () => {
editor.off('SetContent', handler);
});

it('TINY-10755: pressing enter after inserting a wrapped content before another wrapped content should workas expected', async () => {
const wrappedElement = '<pre><code>hello</code></pre>';
const h1Element = '<h1>hello</h1>';
const editor = hook.editor();

editor.setContent('<p>a</p>');
TinySelections.setCursor(editor, [ 0, 0 ], 1);
editor.selection.setContent(wrappedElement);
TinySelections.setCursor(editor, [ 0, 0 ], 1);
editor.selection.setContent(h1Element);
editor.dom.dispatch(editor.getBody(), 'keydown', { keyCode: Keys.enter() });

TinyAssertions.assertRawContent(editor, '<p>a<h1>hello</h1><pre><code data-mce-selected="inline-boundary"><br>hello</code></pre></p>');

editor.setContent('<p>a</p>');
TinySelections.setCursor(editor, [ 0, 0 ], 1);
editor.selection.setContent(h1Element);
TinySelections.setCursor(editor, [ 0, 0 ], 1);
editor.selection.setContent(wrappedElement);
editor.dom.dispatch(editor.getBody(), 'keydown', { keyCode: Keys.enter() });

TinyAssertions.assertRawContent(editor, `<p>a${wrappedElement}<h1><br data-mce-bogus="1"></h1>${h1Element}</p>`);
});

it('getStart/getEnd', () => {
const editor = hook.editor();
let rng = editor.dom.createRng();
Expand Down