@ckeditor/ckeditor5-engine 30.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +17 -0
- package/README.md +30 -0
- package/package.json +70 -0
- package/src/controller/datacontroller.js +563 -0
- package/src/controller/editingcontroller.js +149 -0
- package/src/conversion/conversion.js +644 -0
- package/src/conversion/conversionhelpers.js +40 -0
- package/src/conversion/downcastdispatcher.js +914 -0
- package/src/conversion/downcasthelpers.js +1706 -0
- package/src/conversion/mapper.js +696 -0
- package/src/conversion/modelconsumable.js +329 -0
- package/src/conversion/upcastdispatcher.js +807 -0
- package/src/conversion/upcasthelpers.js +997 -0
- package/src/conversion/viewconsumable.js +623 -0
- package/src/dataprocessor/basichtmlwriter.js +32 -0
- package/src/dataprocessor/dataprocessor.jsdoc +64 -0
- package/src/dataprocessor/htmldataprocessor.js +159 -0
- package/src/dataprocessor/htmlwriter.js +22 -0
- package/src/dataprocessor/xmldataprocessor.js +161 -0
- package/src/dev-utils/model.js +482 -0
- package/src/dev-utils/operationreplayer.js +140 -0
- package/src/dev-utils/utils.js +103 -0
- package/src/dev-utils/view.js +1091 -0
- package/src/index.js +52 -0
- package/src/model/batch.js +82 -0
- package/src/model/differ.js +1282 -0
- package/src/model/document.js +483 -0
- package/src/model/documentfragment.js +390 -0
- package/src/model/documentselection.js +1261 -0
- package/src/model/element.js +438 -0
- package/src/model/history.js +138 -0
- package/src/model/item.jsdoc +14 -0
- package/src/model/liveposition.js +182 -0
- package/src/model/liverange.js +221 -0
- package/src/model/markercollection.js +553 -0
- package/src/model/model.js +934 -0
- package/src/model/node.js +507 -0
- package/src/model/nodelist.js +217 -0
- package/src/model/operation/attributeoperation.js +202 -0
- package/src/model/operation/detachoperation.js +103 -0
- package/src/model/operation/insertoperation.js +188 -0
- package/src/model/operation/markeroperation.js +154 -0
- package/src/model/operation/mergeoperation.js +216 -0
- package/src/model/operation/moveoperation.js +209 -0
- package/src/model/operation/nooperation.js +58 -0
- package/src/model/operation/operation.js +139 -0
- package/src/model/operation/operationfactory.js +49 -0
- package/src/model/operation/renameoperation.js +155 -0
- package/src/model/operation/rootattributeoperation.js +211 -0
- package/src/model/operation/splitoperation.js +254 -0
- package/src/model/operation/transform.js +2389 -0
- package/src/model/operation/utils.js +292 -0
- package/src/model/position.js +1164 -0
- package/src/model/range.js +1049 -0
- package/src/model/rootelement.js +111 -0
- package/src/model/schema.js +1851 -0
- package/src/model/selection.js +902 -0
- package/src/model/text.js +138 -0
- package/src/model/textproxy.js +279 -0
- package/src/model/treewalker.js +414 -0
- package/src/model/utils/autoparagraphing.js +77 -0
- package/src/model/utils/deletecontent.js +528 -0
- package/src/model/utils/getselectedcontent.js +150 -0
- package/src/model/utils/insertcontent.js +824 -0
- package/src/model/utils/modifyselection.js +229 -0
- package/src/model/utils/selection-post-fixer.js +297 -0
- package/src/model/writer.js +1574 -0
- package/src/view/attributeelement.js +274 -0
- package/src/view/containerelement.js +123 -0
- package/src/view/document.js +221 -0
- package/src/view/documentfragment.js +273 -0
- package/src/view/documentselection.js +387 -0
- package/src/view/domconverter.js +1437 -0
- package/src/view/downcastwriter.js +2121 -0
- package/src/view/editableelement.js +118 -0
- package/src/view/element.js +945 -0
- package/src/view/elementdefinition.jsdoc +59 -0
- package/src/view/emptyelement.js +119 -0
- package/src/view/filler.js +161 -0
- package/src/view/item.jsdoc +14 -0
- package/src/view/matcher.js +776 -0
- package/src/view/node.js +391 -0
- package/src/view/observer/arrowkeysobserver.js +58 -0
- package/src/view/observer/bubblingemittermixin.js +307 -0
- package/src/view/observer/bubblingeventinfo.js +71 -0
- package/src/view/observer/clickobserver.js +46 -0
- package/src/view/observer/compositionobserver.js +79 -0
- package/src/view/observer/domeventdata.js +82 -0
- package/src/view/observer/domeventobserver.js +99 -0
- package/src/view/observer/fakeselectionobserver.js +118 -0
- package/src/view/observer/focusobserver.js +106 -0
- package/src/view/observer/inputobserver.js +44 -0
- package/src/view/observer/keyobserver.js +83 -0
- package/src/view/observer/mouseobserver.js +56 -0
- package/src/view/observer/mutationobserver.js +345 -0
- package/src/view/observer/observer.js +118 -0
- package/src/view/observer/selectionobserver.js +242 -0
- package/src/view/placeholder.js +285 -0
- package/src/view/position.js +426 -0
- package/src/view/range.js +533 -0
- package/src/view/rawelement.js +148 -0
- package/src/view/renderer.js +1037 -0
- package/src/view/rooteditableelement.js +107 -0
- package/src/view/selection.js +718 -0
- package/src/view/styles/background.js +73 -0
- package/src/view/styles/border.js +362 -0
- package/src/view/styles/margin.js +41 -0
- package/src/view/styles/padding.js +40 -0
- package/src/view/styles/utils.js +277 -0
- package/src/view/stylesmap.js +938 -0
- package/src/view/text.js +147 -0
- package/src/view/textproxy.js +199 -0
- package/src/view/treewalker.js +496 -0
- package/src/view/uielement.js +238 -0
- package/src/view/upcastwriter.js +484 -0
- package/src/view/view.js +721 -0
- package/theme/placeholder.css +27 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/model/utils/modifyselection
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Position from '../position';
|
|
11
|
+
import TreeWalker from '../treewalker';
|
|
12
|
+
import Range from '../range';
|
|
13
|
+
import { isInsideSurrogatePair, isInsideCombinedSymbol } from '@ckeditor/ckeditor5-utils/src/unicode';
|
|
14
|
+
import DocumentSelection from '../documentselection';
|
|
15
|
+
|
|
16
|
+
const wordBoundaryCharacters = ' ,.?!:;"-()';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Modifies the selection. Currently, the supported modifications are:
|
|
20
|
+
*
|
|
21
|
+
* * Extending. The selection focus is moved in the specified `options.direction` with a step specified in `options.unit`.
|
|
22
|
+
* Possible values for `unit` are:
|
|
23
|
+
* * `'character'` (default) - moves selection by one user-perceived character. In most cases this means moving by one
|
|
24
|
+
* character in `String` sense. However, unicode also defines "combing marks". These are special symbols, that combines
|
|
25
|
+
* with a symbol before it ("base character") to create one user-perceived character. For example, `q̣̇` is a normal
|
|
26
|
+
* letter `q` with two "combining marks": upper dot (`Ux0307`) and lower dot (`Ux0323`). For most actions, i.e. extending
|
|
27
|
+
* selection by one position, it is correct to include both "base character" and all of it's "combining marks". That is
|
|
28
|
+
* why `'character'` value is most natural and common method of modifying selection.
|
|
29
|
+
* * `'codePoint'` - moves selection by one unicode code point. In contrary to, `'character'` unit, this will insert
|
|
30
|
+
* selection between "base character" and "combining mark", because "combining marks" have their own unicode code points.
|
|
31
|
+
* However, for technical reasons, unicode code points with values above `UxFFFF` are represented in native `String` by
|
|
32
|
+
* two characters, called "surrogate pairs". Halves of "surrogate pairs" have a meaning only when placed next to each other.
|
|
33
|
+
* For example `𨭎` is represented in `String` by `\uD862\uDF4E`. Both `\uD862` and `\uDF4E` do not have any meaning
|
|
34
|
+
* outside the pair (are rendered as ? when alone). Position between them would be incorrect. In this case, selection
|
|
35
|
+
* extension will include whole "surrogate pair".
|
|
36
|
+
* * `'word'` - moves selection by a whole word.
|
|
37
|
+
*
|
|
38
|
+
* **Note:** if you extend a forward selection in a backward direction you will in fact shrink it.
|
|
39
|
+
*
|
|
40
|
+
* **Note:** Use {@link module:engine/model/model~Model#modifySelection} instead of this function.
|
|
41
|
+
* This function is only exposed to be reusable in algorithms
|
|
42
|
+
* which change the {@link module:engine/model/model~Model#modifySelection}
|
|
43
|
+
* method's behavior.
|
|
44
|
+
*
|
|
45
|
+
* @param {module:engine/model/model~Model} model The model in context of which
|
|
46
|
+
* the selection modification should be performed.
|
|
47
|
+
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
|
|
48
|
+
* The selection to modify.
|
|
49
|
+
* @param {Object} [options]
|
|
50
|
+
* @param {'forward'|'backward'} [options.direction='forward'] The direction in which the selection should be modified.
|
|
51
|
+
* @param {'character'|'codePoint'|'word'} [options.unit='character'] The unit by which selection should be modified.
|
|
52
|
+
*/
|
|
53
|
+
export default function modifySelection( model, selection, options = {} ) {
|
|
54
|
+
const schema = model.schema;
|
|
55
|
+
const isForward = options.direction != 'backward';
|
|
56
|
+
const unit = options.unit ? options.unit : 'character';
|
|
57
|
+
|
|
58
|
+
const focus = selection.focus;
|
|
59
|
+
|
|
60
|
+
const walker = new TreeWalker( {
|
|
61
|
+
boundaries: getSearchRange( focus, isForward ),
|
|
62
|
+
singleCharacters: true,
|
|
63
|
+
direction: isForward ? 'forward' : 'backward'
|
|
64
|
+
} );
|
|
65
|
+
|
|
66
|
+
const data = { walker, schema, isForward, unit };
|
|
67
|
+
|
|
68
|
+
let next;
|
|
69
|
+
|
|
70
|
+
while ( ( next = walker.next() ) ) {
|
|
71
|
+
if ( next.done ) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const position = tryExtendingTo( data, next.value );
|
|
76
|
+
|
|
77
|
+
if ( position ) {
|
|
78
|
+
if ( selection instanceof DocumentSelection ) {
|
|
79
|
+
model.change( writer => {
|
|
80
|
+
writer.setSelectionFocus( position );
|
|
81
|
+
} );
|
|
82
|
+
} else {
|
|
83
|
+
selection.setFocus( position );
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Checks whether the selection can be extended to the the walker's next value (next position).
|
|
92
|
+
// @param {{ walker, unit, isForward, schema }} data
|
|
93
|
+
// @param {module:engine/view/treewalker~TreeWalkerValue} value
|
|
94
|
+
function tryExtendingTo( data, value ) {
|
|
95
|
+
const { isForward, walker, unit, schema } = data;
|
|
96
|
+
const { type, item, nextPosition } = value;
|
|
97
|
+
|
|
98
|
+
// If found text, we can certainly put the focus in it. Let's just find a correct position
|
|
99
|
+
// based on the unit.
|
|
100
|
+
if ( type == 'text' ) {
|
|
101
|
+
if ( data.unit === 'word' ) {
|
|
102
|
+
return getCorrectWordBreakPosition( walker, isForward );
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return getCorrectPosition( walker, unit, isForward );
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Entering an element.
|
|
109
|
+
if ( type == ( isForward ? 'elementStart' : 'elementEnd' ) ) {
|
|
110
|
+
// If it's a selectable, we can select it now.
|
|
111
|
+
if ( schema.isSelectable( item ) ) {
|
|
112
|
+
return Position._createAt( item, isForward ? 'after' : 'before' );
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// If text allowed on this position, extend to this place.
|
|
116
|
+
if ( schema.checkChild( nextPosition, '$text' ) ) {
|
|
117
|
+
return nextPosition;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Leaving an element.
|
|
121
|
+
else {
|
|
122
|
+
// If leaving a limit element, stop.
|
|
123
|
+
if ( schema.isLimit( item ) ) {
|
|
124
|
+
// NOTE: Fast-forward the walker until the end.
|
|
125
|
+
walker.skip( () => true );
|
|
126
|
+
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// If text allowed on this position, extend to this place.
|
|
131
|
+
if ( schema.checkChild( nextPosition, '$text' ) ) {
|
|
132
|
+
return nextPosition;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Finds a correct position by walking in a text node and checking whether selection can be extended to given position
|
|
138
|
+
// or should be extended further.
|
|
139
|
+
//
|
|
140
|
+
// @param {module:engine/model/treewalker~TreeWalker} walker
|
|
141
|
+
// @param {String} unit The unit by which selection should be modified.
|
|
142
|
+
function getCorrectPosition( walker, unit ) {
|
|
143
|
+
const textNode = walker.position.textNode;
|
|
144
|
+
|
|
145
|
+
if ( textNode ) {
|
|
146
|
+
const data = textNode.data;
|
|
147
|
+
let offset = walker.position.offset - textNode.startOffset;
|
|
148
|
+
|
|
149
|
+
while ( isInsideSurrogatePair( data, offset ) || ( unit == 'character' && isInsideCombinedSymbol( data, offset ) ) ) {
|
|
150
|
+
walker.next();
|
|
151
|
+
|
|
152
|
+
offset = walker.position.offset - textNode.startOffset;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return walker.position;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Finds a correct position of a word break by walking in a text node and checking whether selection can be extended to given position
|
|
160
|
+
// or should be extended further.
|
|
161
|
+
//
|
|
162
|
+
// @param {module:engine/model/treewalker~TreeWalker} walker
|
|
163
|
+
// @param {Boolean} isForward Is the direction in which the selection should be modified is forward.
|
|
164
|
+
function getCorrectWordBreakPosition( walker, isForward ) {
|
|
165
|
+
let textNode = walker.position.textNode;
|
|
166
|
+
|
|
167
|
+
if ( textNode ) {
|
|
168
|
+
let offset = walker.position.offset - textNode.startOffset;
|
|
169
|
+
|
|
170
|
+
while ( !isAtWordBoundary( textNode.data, offset, isForward ) && !isAtNodeBoundary( textNode, offset, isForward ) ) {
|
|
171
|
+
walker.next();
|
|
172
|
+
|
|
173
|
+
// Check of adjacent text nodes with different attributes (like BOLD).
|
|
174
|
+
// Example : 'foofoo []bar<$text bold="true">bar</$text> bazbaz'
|
|
175
|
+
// should expand to : 'foofoo [bar<$text bold="true">bar</$text>] bazbaz'.
|
|
176
|
+
const nextNode = isForward ? walker.position.nodeAfter : walker.position.nodeBefore;
|
|
177
|
+
|
|
178
|
+
// Scan only text nodes. Ignore inline elements (like `<softBreak>`).
|
|
179
|
+
if ( nextNode && nextNode.is( '$text' ) ) {
|
|
180
|
+
// Check boundary char of an adjacent text node.
|
|
181
|
+
const boundaryChar = nextNode.data.charAt( isForward ? 0 : nextNode.data.length - 1 );
|
|
182
|
+
|
|
183
|
+
// Go to the next node if the character at the boundary of that node belongs to the same word.
|
|
184
|
+
if ( !wordBoundaryCharacters.includes( boundaryChar ) ) {
|
|
185
|
+
// If adjacent text node belongs to the same word go to it & reset values.
|
|
186
|
+
walker.next();
|
|
187
|
+
|
|
188
|
+
textNode = walker.position.textNode;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
offset = walker.position.offset - textNode.startOffset;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return walker.position;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getSearchRange( start, isForward ) {
|
|
200
|
+
const root = start.root;
|
|
201
|
+
const searchEnd = Position._createAt( root, isForward ? 'end' : 0 );
|
|
202
|
+
|
|
203
|
+
if ( isForward ) {
|
|
204
|
+
return new Range( start, searchEnd );
|
|
205
|
+
} else {
|
|
206
|
+
return new Range( searchEnd, start );
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Checks if selection is on word boundary.
|
|
211
|
+
//
|
|
212
|
+
// @param {String} data The text node value to investigate.
|
|
213
|
+
// @param {Number} offset Position offset.
|
|
214
|
+
// @param {Boolean} isForward Is the direction in which the selection should be modified is forward.
|
|
215
|
+
function isAtWordBoundary( data, offset, isForward ) {
|
|
216
|
+
// The offset to check depends on direction.
|
|
217
|
+
const offsetToCheck = offset + ( isForward ? 0 : -1 );
|
|
218
|
+
|
|
219
|
+
return wordBoundaryCharacters.includes( data.charAt( offsetToCheck ) );
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Checks if selection is on node boundary.
|
|
223
|
+
//
|
|
224
|
+
// @param {module:engine/model/text~Text} textNode The text node to investigate.
|
|
225
|
+
// @param {Number} offset Position offset.
|
|
226
|
+
// @param {Boolean} isForward Is the direction in which the selection should be modified is forward.
|
|
227
|
+
function isAtNodeBoundary( textNode, offset, isForward ) {
|
|
228
|
+
return offset === ( isForward ? textNode.endOffset : 0 );
|
|
229
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/model/utils/selection-post-fixer
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Range from '../range';
|
|
11
|
+
import Position from '../position';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Injects selection post-fixer to the model.
|
|
15
|
+
*
|
|
16
|
+
* The role of the selection post-fixer is to ensure that the selection is in a correct place
|
|
17
|
+
* after a {@link module:engine/model/model~Model#change `change()`} block was executed.
|
|
18
|
+
*
|
|
19
|
+
* The correct position means that:
|
|
20
|
+
*
|
|
21
|
+
* * All collapsed selection ranges are in a place where the {@link module:engine/model/schema~Schema}
|
|
22
|
+
* allows a `$text`.
|
|
23
|
+
* * None of the selection's non-collapsed ranges crosses a {@link module:engine/model/schema~Schema#isLimit limit element}
|
|
24
|
+
* boundary (a range must be rooted within one limit element).
|
|
25
|
+
* * Only {@link module:engine/model/schema~Schema#isSelectable selectable elements} can be selected from the outside
|
|
26
|
+
* (e.g. `[<paragraph>foo</paragraph>]` is invalid). This rule applies independently to both selection ends, so this
|
|
27
|
+
* selection is correct: `<paragraph>f[oo</paragraph><imageBlock></imageBlock>]`.
|
|
28
|
+
*
|
|
29
|
+
* If the position is not correct, the post-fixer will automatically correct it.
|
|
30
|
+
*
|
|
31
|
+
* ## Fixing a non-collapsed selection
|
|
32
|
+
*
|
|
33
|
+
* See as an example a selection that starts in a P1 element and ends inside the text of a TD element
|
|
34
|
+
* (`[` and `]` are range boundaries and `(l)` denotes an element defined as `isLimit=true`):
|
|
35
|
+
*
|
|
36
|
+
* root
|
|
37
|
+
* |- element P1
|
|
38
|
+
* | |- "foo" root
|
|
39
|
+
* |- element TABLE (l) P1 TABLE P2
|
|
40
|
+
* | |- element TR (l) f o[o TR TR b a r
|
|
41
|
+
* | | |- element TD (l) TD TD
|
|
42
|
+
* | | |- "aaa" a]a a b b b
|
|
43
|
+
* | |- element TR (l)
|
|
44
|
+
* | | |- element TD (l) ||
|
|
45
|
+
* | | |- "bbb" ||
|
|
46
|
+
* |- element P2 VV
|
|
47
|
+
* | |- "bar"
|
|
48
|
+
* root
|
|
49
|
+
* P1 TABLE] P2
|
|
50
|
+
* f o[o TR TR b a r
|
|
51
|
+
* TD TD
|
|
52
|
+
* a a a b b b
|
|
53
|
+
*
|
|
54
|
+
* In the example above, the TABLE, TR and TD are defined as `isLimit=true` in the schema. The range which is not contained within
|
|
55
|
+
* 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.
|
|
56
|
+
* 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
|
|
57
|
+
* expanded to select the whole TABLE element.
|
|
58
|
+
*
|
|
59
|
+
* **Note** If the selection contains multiple ranges, the method returns a minimal set of ranges that are not intersecting after expanding
|
|
60
|
+
* them to select `isLimit=true` elements.
|
|
61
|
+
*
|
|
62
|
+
* @param {module:engine/model/model~Model} model
|
|
63
|
+
*/
|
|
64
|
+
export function injectSelectionPostFixer( model ) {
|
|
65
|
+
model.document.registerPostFixer( writer => selectionPostFixer( writer, model ) );
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The selection post-fixer.
|
|
69
|
+
//
|
|
70
|
+
// @param {module:engine/model/writer~Writer} writer
|
|
71
|
+
// @param {module:engine/model/model~Model} model
|
|
72
|
+
function selectionPostFixer( writer, model ) {
|
|
73
|
+
const selection = model.document.selection;
|
|
74
|
+
const schema = model.schema;
|
|
75
|
+
|
|
76
|
+
const ranges = [];
|
|
77
|
+
|
|
78
|
+
let wasFixed = false;
|
|
79
|
+
|
|
80
|
+
for ( const modelRange of selection.getRanges() ) {
|
|
81
|
+
// Go through all ranges in selection and try fixing each of them.
|
|
82
|
+
// Those ranges might overlap but will be corrected later.
|
|
83
|
+
const correctedRange = tryFixingRange( modelRange, schema );
|
|
84
|
+
|
|
85
|
+
// "Selection fixing" algorithms sometimes get lost. In consequence, it may happen
|
|
86
|
+
// that a new range is returned but, in fact, it has the same positions as the original
|
|
87
|
+
// range anyway. If this range is not discarded, a new selection will be set and that,
|
|
88
|
+
// for instance, would destroy the selection attributes. Let's make sure that the post-fixer
|
|
89
|
+
// actually worked first before setting a new selection.
|
|
90
|
+
//
|
|
91
|
+
// https://github.com/ckeditor/ckeditor5/issues/6693
|
|
92
|
+
if ( correctedRange && !correctedRange.isEqual( modelRange ) ) {
|
|
93
|
+
ranges.push( correctedRange );
|
|
94
|
+
wasFixed = true;
|
|
95
|
+
} else {
|
|
96
|
+
ranges.push( modelRange );
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If any of ranges were corrected update the selection.
|
|
101
|
+
if ( wasFixed ) {
|
|
102
|
+
writer.setSelection( mergeIntersectingRanges( ranges ), { backward: selection.isBackward } );
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Tries fixing a range if it's incorrect.
|
|
107
|
+
//
|
|
108
|
+
// @param {module:engine/model/range~Range} range
|
|
109
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
110
|
+
// @returns {module:engine/model/range~Range|null} Returns fixed range or null if range is valid.
|
|
111
|
+
function tryFixingRange( range, schema ) {
|
|
112
|
+
if ( range.isCollapsed ) {
|
|
113
|
+
return tryFixingCollapsedRange( range, schema );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return tryFixingNonCollapsedRage( range, schema );
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Tries to fix collapsed ranges.
|
|
120
|
+
//
|
|
121
|
+
// * Fixes situation when a range is in a place where $text is not allowed
|
|
122
|
+
//
|
|
123
|
+
// @param {module:engine/model/range~Range} range Collapsed range to fix.
|
|
124
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
125
|
+
// @returns {module:engine/model/range~Range|null} Returns fixed range or null if range is valid.
|
|
126
|
+
function tryFixingCollapsedRange( range, schema ) {
|
|
127
|
+
const originalPosition = range.start;
|
|
128
|
+
|
|
129
|
+
const nearestSelectionRange = schema.getNearestSelectionRange( originalPosition );
|
|
130
|
+
|
|
131
|
+
// This might be null ie when editor data is empty.
|
|
132
|
+
// In such cases there is no need to fix the selection range.
|
|
133
|
+
if ( !nearestSelectionRange ) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if ( !nearestSelectionRange.isCollapsed ) {
|
|
138
|
+
return nearestSelectionRange;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const fixedPosition = nearestSelectionRange.start;
|
|
142
|
+
|
|
143
|
+
// Fixed position is the same as original - no need to return corrected range.
|
|
144
|
+
if ( originalPosition.isEqual( fixedPosition ) ) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return new Range( fixedPosition );
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Tries to fix an expanded range.
|
|
152
|
+
//
|
|
153
|
+
// @param {module:engine/model/range~Range} range Expanded range to fix.
|
|
154
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
155
|
+
// @returns {module:engine/model/range~Range|null} Returns fixed range or null if range is valid.
|
|
156
|
+
function tryFixingNonCollapsedRage( range, schema ) {
|
|
157
|
+
const { start, end } = range;
|
|
158
|
+
|
|
159
|
+
const isTextAllowedOnStart = schema.checkChild( start, '$text' );
|
|
160
|
+
const isTextAllowedOnEnd = schema.checkChild( end, '$text' );
|
|
161
|
+
|
|
162
|
+
const startLimitElement = schema.getLimitElement( start );
|
|
163
|
+
const endLimitElement = schema.getLimitElement( end );
|
|
164
|
+
|
|
165
|
+
// Ranges which both end are inside the same limit element (or root) might needs only minor fix.
|
|
166
|
+
if ( startLimitElement === endLimitElement ) {
|
|
167
|
+
// Range is valid when both position allows to place a text:
|
|
168
|
+
// - <block>f[oobarba]z</block>
|
|
169
|
+
// 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.
|
|
170
|
+
if ( isTextAllowedOnStart && isTextAllowedOnEnd ) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Range that is on non-limit element (or is partially) must be fixed so it is placed inside the block around $text:
|
|
175
|
+
// - [<block>foo</block>] -> <block>[foo]</block>
|
|
176
|
+
// - [<block>foo]</block> -> <block>[foo]</block>
|
|
177
|
+
// - <block>f[oo</block>] -> <block>f[oo]</block>
|
|
178
|
+
// - [<block>foo</block><selectable></selectable>] -> <block>[foo</block><selectable></selectable>]
|
|
179
|
+
if ( checkSelectionOnNonLimitElements( start, end, schema ) ) {
|
|
180
|
+
const isStartBeforeSelectable = start.nodeAfter && schema.isSelectable( start.nodeAfter );
|
|
181
|
+
const fixedStart = isStartBeforeSelectable ? null : schema.getNearestSelectionRange( start, 'forward' );
|
|
182
|
+
|
|
183
|
+
const isEndAfterSelectable = end.nodeBefore && schema.isSelectable( end.nodeBefore );
|
|
184
|
+
const fixedEnd = isEndAfterSelectable ? null : schema.getNearestSelectionRange( end, 'backward' );
|
|
185
|
+
|
|
186
|
+
// The schema.getNearestSelectionRange might return null - if that happens use original position.
|
|
187
|
+
const rangeStart = fixedStart ? fixedStart.start : start;
|
|
188
|
+
const rangeEnd = fixedEnd ? fixedEnd.end : end;
|
|
189
|
+
|
|
190
|
+
return new Range( rangeStart, rangeEnd );
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const isStartInLimit = startLimitElement && !startLimitElement.is( 'rootElement' );
|
|
195
|
+
const isEndInLimit = endLimitElement && !endLimitElement.is( 'rootElement' );
|
|
196
|
+
|
|
197
|
+
// At this point we eliminated valid positions on text nodes so if one of range positions is placed inside a limit element
|
|
198
|
+
// then the range crossed limit element boundaries and needs to be fixed.
|
|
199
|
+
if ( isStartInLimit || isEndInLimit ) {
|
|
200
|
+
const bothInSameParent = ( start.nodeAfter && end.nodeBefore ) && start.nodeAfter.parent === end.nodeBefore.parent;
|
|
201
|
+
|
|
202
|
+
const expandStart = isStartInLimit && ( !bothInSameParent || !isSelectable( start.nodeAfter, schema ) );
|
|
203
|
+
const expandEnd = isEndInLimit && ( !bothInSameParent || !isSelectable( end.nodeBefore, schema ) );
|
|
204
|
+
|
|
205
|
+
// Although we've already found limit element on start/end positions we must find the outer-most limit element.
|
|
206
|
+
// as limit elements might be nested directly inside (ie table > tableRow > tableCell).
|
|
207
|
+
let fixedStart = start;
|
|
208
|
+
let fixedEnd = end;
|
|
209
|
+
|
|
210
|
+
if ( expandStart ) {
|
|
211
|
+
fixedStart = Position._createBefore( findOutermostLimitAncestor( startLimitElement, schema ) );
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if ( expandEnd ) {
|
|
215
|
+
fixedEnd = Position._createAfter( findOutermostLimitAncestor( endLimitElement, schema ) );
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return new Range( fixedStart, fixedEnd );
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Range was not fixed at this point so it is valid - ie it was placed around limit element already.
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Finds the outer-most ancestor.
|
|
226
|
+
//
|
|
227
|
+
// @param {module:engine/model/node~Node} startingNode
|
|
228
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
229
|
+
// @param {String} expandToDirection Direction of expansion - either 'start' or 'end' of the range.
|
|
230
|
+
// @returns {module:engine/model/node~Node}
|
|
231
|
+
function findOutermostLimitAncestor( startingNode, schema ) {
|
|
232
|
+
let isLimitNode = startingNode;
|
|
233
|
+
let parent = isLimitNode;
|
|
234
|
+
|
|
235
|
+
// Find outer most isLimit block as such blocks might be nested (ie. in tables).
|
|
236
|
+
while ( schema.isLimit( parent ) && parent.parent ) {
|
|
237
|
+
isLimitNode = parent;
|
|
238
|
+
parent = parent.parent;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return isLimitNode;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Checks whether any of range boundaries is placed around non-limit elements.
|
|
245
|
+
//
|
|
246
|
+
// @param {module:engine/model/position~Position} start
|
|
247
|
+
// @param {module:engine/model/position~Position} end
|
|
248
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
249
|
+
// @returns {Boolean}
|
|
250
|
+
function checkSelectionOnNonLimitElements( start, end, schema ) {
|
|
251
|
+
const startIsOnBlock = ( start.nodeAfter && !schema.isLimit( start.nodeAfter ) ) || schema.checkChild( start, '$text' );
|
|
252
|
+
const endIsOnBlock = ( end.nodeBefore && !schema.isLimit( end.nodeBefore ) ) || schema.checkChild( end, '$text' );
|
|
253
|
+
|
|
254
|
+
// We should fix such selection when one of those nodes needs fixing.
|
|
255
|
+
return startIsOnBlock || endIsOnBlock;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Returns a minimal non-intersecting array of ranges.
|
|
259
|
+
//
|
|
260
|
+
// @param {Array.<module:engine/model/range~Range>} ranges
|
|
261
|
+
// @returns {Array.<module:engine/model/range~Range>}
|
|
262
|
+
function mergeIntersectingRanges( ranges ) {
|
|
263
|
+
const nonIntersectingRanges = [];
|
|
264
|
+
|
|
265
|
+
// First range will always be fine.
|
|
266
|
+
nonIntersectingRanges.push( ranges.shift() );
|
|
267
|
+
|
|
268
|
+
for ( const range of ranges ) {
|
|
269
|
+
const previousRange = nonIntersectingRanges.pop();
|
|
270
|
+
|
|
271
|
+
if ( range.isEqual( previousRange ) ) {
|
|
272
|
+
// Use only one of two identical ranges.
|
|
273
|
+
nonIntersectingRanges.push( previousRange );
|
|
274
|
+
} else if ( range.isIntersecting( previousRange ) ) {
|
|
275
|
+
// Get the sum of two ranges.
|
|
276
|
+
const start = previousRange.start.isAfter( range.start ) ? range.start : previousRange.start;
|
|
277
|
+
const end = previousRange.end.isAfter( range.end ) ? previousRange.end : range.end;
|
|
278
|
+
|
|
279
|
+
const merged = new Range( start, end );
|
|
280
|
+
nonIntersectingRanges.push( merged );
|
|
281
|
+
} else {
|
|
282
|
+
nonIntersectingRanges.push( previousRange );
|
|
283
|
+
nonIntersectingRanges.push( range );
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return nonIntersectingRanges;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Checks if node exists and if it's a selectable.
|
|
291
|
+
//
|
|
292
|
+
// @param {module:engine/model/node~Node} node
|
|
293
|
+
// @param {module:engine/model/schema~Schema} schema
|
|
294
|
+
// @returns {Boolean}
|
|
295
|
+
function isSelectable( node, schema ) {
|
|
296
|
+
return node && schema.isSelectable( node );
|
|
297
|
+
}
|