/
selection-post-fixer.ts
316 lines (264 loc) · 12.1 KB
/
selection-post-fixer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/utils/selection-post-fixer
*/
import Position from '../position.js';
import Range from '../range.js';
import type DocumentFragment from '../documentfragment.js';
import type Model from '../model.js';
import type Node from '../node.js';
import type Schema from '../schema.js';
import type Writer from '../writer.js';
import type Element from '../element.js';
/**
* Injects selection post-fixer to the model.
*
* The role of the selection post-fixer is to ensure that the selection is in a correct place
* after a {@link module:engine/model/model~Model#change `change()`} block was executed.
*
* The correct position means that:
*
* * All collapsed selection ranges are in a place where the {@link module:engine/model/schema~Schema}
* allows a `$text`.
* * None of the selection's non-collapsed ranges crosses a {@link module:engine/model/schema~Schema#isLimit limit element}
* boundary (a range must be rooted within one limit element).
* * Only {@link module:engine/model/schema~Schema#isSelectable selectable elements} can be selected from the outside
* (e.g. `[<paragraph>foo</paragraph>]` is invalid). This rule applies independently to both selection ends, so this
* selection is correct: `<paragraph>f[oo</paragraph><imageBlock></imageBlock>]`.
*
* If the position is not correct, the post-fixer will automatically correct it.
*
* ## Fixing a non-collapsed selection
*
* See as an example a selection that starts in a P1 element and ends inside the text of a TD element
* (`[` and `]` are range boundaries and `(l)` denotes an element defined as `isLimit=true`):
*
* ```
* root
* |- element P1
* | |- "foo" root
* |- element TABLE (l) P1 TABLE P2
* | |- element TR (l) f o[o TR TR b a r
* | | |- element TD (l) TD TD
* | | |- "aaa" a]a a b b b
* | |- element TR (l)
* | | |- element TD (l) ||
* | | |- "bbb" ||
* |- element P2 VV
* | |- "bar"
* root
* P1 TABLE] P2
* f o[o TR TR b a r
* TD TD
* a a a b b b
* ```
*
* In the example above, the TABLE, TR and TD are defined as `isLimit=true` in the schema. The range which is not contained within
* a single limit element must be expanded to select the outermost limit element. The range end is inside the text node of the TD element.
* As the TD element is a child of the TR and TABLE elements, where both are defined as `isLimit=true` in the schema, the range must be
* expanded to select the whole TABLE element.
*
* **Note** If the selection contains multiple ranges, the method returns a minimal set of ranges that are not intersecting after expanding
* them to select `isLimit=true` elements.
*/
export function injectSelectionPostFixer( model: Model ): void {
model.document.registerPostFixer( writer => selectionPostFixer( writer, model ) );
}
/**
* The selection post-fixer.
*/
function selectionPostFixer( writer: Writer, model: Model ): boolean {
const selection = model.document.selection;
const schema = model.schema;
const ranges = [];
let wasFixed = false;
for ( const modelRange of selection.getRanges() ) {
// Go through all ranges in selection and try fixing each of them.
// Those ranges might overlap but will be corrected later.
const correctedRange = tryFixingRange( modelRange, schema );
// "Selection fixing" algorithms sometimes get lost. In consequence, it may happen
// that a new range is returned but, in fact, it has the same positions as the original
// range anyway. If this range is not discarded, a new selection will be set and that,
// for instance, would destroy the selection attributes. Let's make sure that the post-fixer
// actually worked first before setting a new selection.
//
// https://github.com/ckeditor/ckeditor5/issues/6693
if ( correctedRange && !correctedRange.isEqual( modelRange ) ) {
ranges.push( correctedRange );
wasFixed = true;
} else {
ranges.push( modelRange );
}
}
// If any of ranges were corrected update the selection.
if ( wasFixed ) {
writer.setSelection( mergeIntersectingRanges( ranges ), { backward: selection.isBackward } );
}
return false;
}
/**
* Tries fixing a range if it's incorrect.
*
* **Note:** This helper is used by the selection post-fixer and to fix the `beforeinput` target ranges.
*
* @returns Returns fixed range or null if range is valid.
*/
export function tryFixingRange( range: Range, schema: Schema ): Range | null {
if ( range.isCollapsed ) {
return tryFixingCollapsedRange( range, schema );
}
return tryFixingNonCollapsedRage( range, schema );
}
/**
* Tries to fix collapsed ranges.
*
* * Fixes situation when a range is in a place where $text is not allowed
*
* @param range Collapsed range to fix.
* @returns Returns fixed range or null if range is valid.
*/
function tryFixingCollapsedRange( range: Range, schema: Schema ) {
const originalPosition = range.start;
const nearestSelectionRange = schema.getNearestSelectionRange( originalPosition );
// This might be null, i.e. when the editor data is empty or the selection is inside a limit element
// that doesn't allow text inside.
// In the first case, there is no need to fix the selection range.
// In the second, let's go up to the outer selectable element
if ( !nearestSelectionRange ) {
const ancestorObject = originalPosition.getAncestors().reverse().find( ( item ): item is Element => schema.isObject( item ) );
if ( ancestorObject ) {
return Range._createOn( ancestorObject );
}
return null;
}
if ( !nearestSelectionRange.isCollapsed ) {
return nearestSelectionRange;
}
const fixedPosition = nearestSelectionRange.start;
// Fixed position is the same as original - no need to return corrected range.
if ( originalPosition.isEqual( fixedPosition ) ) {
return null;
}
return new Range( fixedPosition );
}
/**
* Tries to fix an expanded range.
*
* @param range Expanded range to fix.
* @returns Returns fixed range or null if range is valid.
*/
function tryFixingNonCollapsedRage( range: Range, schema: Schema ) {
const { start, end } = range;
const isTextAllowedOnStart = schema.checkChild( start, '$text' );
const isTextAllowedOnEnd = schema.checkChild( end, '$text' );
const startLimitElement = schema.getLimitElement( start );
const endLimitElement = schema.getLimitElement( end );
// Ranges which both end are inside the same limit element (or root) might needs only minor fix.
if ( startLimitElement === endLimitElement ) {
// Range is valid when both position allows to place a text:
// - <block>f[oobarba]z</block>
// This would be "fixed" by a next check but as it will be the same it's better to return null so the selection stays the same.
if ( isTextAllowedOnStart && isTextAllowedOnEnd ) {
return null;
}
// Range that is on non-limit element (or is partially) must be fixed so it is placed inside the block around $text:
// - [<block>foo</block>] -> <block>[foo]</block>
// - [<block>foo]</block> -> <block>[foo]</block>
// - <block>f[oo</block>] -> <block>f[oo]</block>
// - [<block>foo</block><selectable></selectable>] -> <block>[foo</block><selectable></selectable>]
if ( checkSelectionOnNonLimitElements( start, end, schema ) ) {
const isStartBeforeSelectable = start.nodeAfter && schema.isSelectable( start.nodeAfter );
const fixedStart = isStartBeforeSelectable ? null : schema.getNearestSelectionRange( start, 'forward' );
const isEndAfterSelectable = end.nodeBefore && schema.isSelectable( end.nodeBefore );
const fixedEnd = isEndAfterSelectable ? null : schema.getNearestSelectionRange( end, 'backward' );
// The schema.getNearestSelectionRange might return null - if that happens use original position.
const rangeStart = fixedStart ? fixedStart.start : start;
const rangeEnd = fixedEnd ? fixedEnd.end : end;
return new Range( rangeStart, rangeEnd );
}
}
const isStartInLimit = startLimitElement && !startLimitElement.is( 'rootElement' );
const isEndInLimit = endLimitElement && !endLimitElement.is( 'rootElement' );
// At this point we eliminated valid positions on text nodes so if one of range positions is placed inside a limit element
// then the range crossed limit element boundaries and needs to be fixed.
if ( isStartInLimit || isEndInLimit ) {
const bothInSameParent = ( start.nodeAfter && end.nodeBefore ) && start.nodeAfter.parent === end.nodeBefore.parent;
const expandStart = isStartInLimit && ( !bothInSameParent || !isSelectable( start.nodeAfter, schema ) );
const expandEnd = isEndInLimit && ( !bothInSameParent || !isSelectable( end.nodeBefore, schema ) );
// Although we've already found limit element on start/end positions we must find the outer-most limit element.
// as limit elements might be nested directly inside (ie table > tableRow > tableCell).
let fixedStart = start;
let fixedEnd = end;
if ( expandStart ) {
fixedStart = Position._createBefore( findOutermostLimitAncestor( startLimitElement, schema ) );
}
if ( expandEnd ) {
fixedEnd = Position._createAfter( findOutermostLimitAncestor( endLimitElement, schema ) );
}
return new Range( fixedStart, fixedEnd );
}
// Range was not fixed at this point so it is valid - ie it was placed around limit element already.
return null;
}
/**
* Finds the outer-most ancestor.
*/
function findOutermostLimitAncestor( startingNode: Node, schema: Schema ): Node {
let isLimitNode = startingNode;
let parent: Node | DocumentFragment = isLimitNode;
// Find outer most isLimit block as such blocks might be nested (ie. in tables).
while ( schema.isLimit( parent ) && parent.parent ) {
isLimitNode = parent;
parent = parent.parent;
}
return isLimitNode;
}
/**
* Checks whether any of range boundaries is placed around non-limit elements.
*/
function checkSelectionOnNonLimitElements( start: Position, end: Position, schema: Schema ) {
const startIsOnBlock = ( start.nodeAfter && !schema.isLimit( start.nodeAfter ) ) || schema.checkChild( start, '$text' );
const endIsOnBlock = ( end.nodeBefore && !schema.isLimit( end.nodeBefore ) ) || schema.checkChild( end, '$text' );
// We should fix such selection when one of those nodes needs fixing.
return startIsOnBlock || endIsOnBlock;
}
/**
* Returns a minimal non-intersecting array of ranges without duplicates.
*
* @param ranges Ranges to merge.
* @returns Array of unique and non-intersecting ranges.
*/
export function mergeIntersectingRanges( ranges: Array<Range> ): Array<Range> {
const rangesToMerge = [ ...ranges ];
const rangeIndexesToRemove = new Set();
let currentRangeIndex = 1;
while ( currentRangeIndex < rangesToMerge.length ) {
const currentRange = rangesToMerge[ currentRangeIndex ];
const previousRanges = rangesToMerge.slice( 0, currentRangeIndex );
for ( const [ previousRangeIndex, previousRange ] of previousRanges.entries() ) {
if ( rangeIndexesToRemove.has( previousRangeIndex ) ) {
continue;
}
if ( currentRange.isEqual( previousRange ) ) {
rangeIndexesToRemove.add( previousRangeIndex );
} else if ( currentRange.isIntersecting( previousRange ) ) {
rangeIndexesToRemove.add( previousRangeIndex );
rangeIndexesToRemove.add( currentRangeIndex );
const mergedRange = currentRange.getJoined( previousRange );
rangesToMerge.push( mergedRange! );
}
}
currentRangeIndex++;
}
const nonIntersectingRanges = rangesToMerge.filter( ( _, index ) => !rangeIndexesToRemove.has( index ) );
return nonIntersectingRanges;
}
/**
* Checks if node exists and if it's a selectable.
*/
function isSelectable( node: Node, schema: Schema ) {
return node && schema.isSelectable( node );
}