@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,414 @@
|
|
|
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/treewalker
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Text from './text';
|
|
11
|
+
import TextProxy from './textproxy';
|
|
12
|
+
import Element from './element';
|
|
13
|
+
import {
|
|
14
|
+
default as Position,
|
|
15
|
+
getTextNodeAtPosition,
|
|
16
|
+
getNodeAfterPosition,
|
|
17
|
+
getNodeBeforePosition
|
|
18
|
+
} from './position';
|
|
19
|
+
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Position iterator class. It allows to iterate forward and backward over the document.
|
|
23
|
+
*/
|
|
24
|
+
export default class TreeWalker {
|
|
25
|
+
/**
|
|
26
|
+
* Creates a range iterator. All parameters are optional, but you have to specify either `boundaries` or `startPosition`.
|
|
27
|
+
*
|
|
28
|
+
* @constructor
|
|
29
|
+
* @param {Object} [options={}] Object with configuration.
|
|
30
|
+
* @param {'forward'|'backward'} [options.direction='forward'] Walking direction.
|
|
31
|
+
* @param {module:engine/model/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
|
|
32
|
+
* @param {module:engine/model/position~Position} [options.startPosition] Starting position.
|
|
33
|
+
* @param {Boolean} [options.singleCharacters=false] Flag indicating whether all consecutive characters with the same attributes
|
|
34
|
+
* should be returned one by one as multiple {@link module:engine/model/textproxy~TextProxy} (`true`) objects or as one
|
|
35
|
+
* {@link module:engine/model/textproxy~TextProxy} (`false`).
|
|
36
|
+
* @param {Boolean} [options.shallow=false] Flag indicating whether iterator should enter elements or not. If the
|
|
37
|
+
* iterator is shallow child nodes of any iterated node will not be returned along with `elementEnd` tag.
|
|
38
|
+
* @param {Boolean} [options.ignoreElementEnd=false] Flag indicating whether iterator should ignore `elementEnd`
|
|
39
|
+
* tags. If the option is true walker will not return a parent node of start position. If this option is `true`
|
|
40
|
+
* each {@link module:engine/model/element~Element} will be returned once, while if the option is `false` they might be returned
|
|
41
|
+
* twice: for `'elementStart'` and `'elementEnd'`.
|
|
42
|
+
*/
|
|
43
|
+
constructor( options = {} ) {
|
|
44
|
+
if ( !options.boundaries && !options.startPosition ) {
|
|
45
|
+
/**
|
|
46
|
+
* Neither boundaries nor starting position of a `TreeWalker` have been defined.
|
|
47
|
+
*
|
|
48
|
+
* @error model-tree-walker-no-start-position
|
|
49
|
+
*/
|
|
50
|
+
throw new CKEditorError(
|
|
51
|
+
'model-tree-walker-no-start-position',
|
|
52
|
+
null
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const direction = options.direction || 'forward';
|
|
57
|
+
|
|
58
|
+
if ( direction != 'forward' && direction != 'backward' ) {
|
|
59
|
+
/**
|
|
60
|
+
* Only `backward` and `forward` direction allowed.
|
|
61
|
+
*
|
|
62
|
+
* @error model-tree-walker-unknown-direction
|
|
63
|
+
*/
|
|
64
|
+
throw new CKEditorError( 'model-tree-walker-unknown-direction', options, { direction } );
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Walking direction. Defaults `'forward'`.
|
|
69
|
+
*
|
|
70
|
+
* @readonly
|
|
71
|
+
* @member {'backward'|'forward'} module:engine/model/treewalker~TreeWalker#direction
|
|
72
|
+
*/
|
|
73
|
+
this.direction = direction;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Iterator boundaries.
|
|
77
|
+
*
|
|
78
|
+
* When the iterator is walking `'forward'` on the end of boundary or is walking `'backward'`
|
|
79
|
+
* on the start of boundary, then `{ done: true }` is returned.
|
|
80
|
+
*
|
|
81
|
+
* If boundaries are not defined they are set before first and after last child of the root node.
|
|
82
|
+
*
|
|
83
|
+
* @readonly
|
|
84
|
+
* @member {module:engine/model/range~Range} module:engine/model/treewalker~TreeWalker#boundaries
|
|
85
|
+
*/
|
|
86
|
+
this.boundaries = options.boundaries || null;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Iterator position. This is always static position, even if the initial position was a
|
|
90
|
+
* {@link module:engine/model/liveposition~LivePosition live position}. If start position is not defined then position depends
|
|
91
|
+
* on {@link #direction}. If direction is `'forward'` position starts form the beginning, when direction
|
|
92
|
+
* is `'backward'` position starts from the end.
|
|
93
|
+
*
|
|
94
|
+
* @readonly
|
|
95
|
+
* @member {module:engine/model/position~Position} module:engine/model/treewalker~TreeWalker#position
|
|
96
|
+
*/
|
|
97
|
+
if ( options.startPosition ) {
|
|
98
|
+
this.position = options.startPosition.clone();
|
|
99
|
+
} else {
|
|
100
|
+
this.position = Position._createAt( this.boundaries[ this.direction == 'backward' ? 'end' : 'start' ] );
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Reset position stickiness in case it was set to other value, as the stickiness is kept after cloning.
|
|
104
|
+
this.position.stickiness = 'toNone';
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Flag indicating whether all consecutive characters with the same attributes should be
|
|
108
|
+
* returned as one {@link module:engine/model/textproxy~TextProxy} (`true`) or one by one (`false`).
|
|
109
|
+
*
|
|
110
|
+
* @readonly
|
|
111
|
+
* @member {Boolean} module:engine/model/treewalker~TreeWalker#singleCharacters
|
|
112
|
+
*/
|
|
113
|
+
this.singleCharacters = !!options.singleCharacters;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Flag indicating whether iterator should enter elements or not. If the iterator is shallow child nodes of any
|
|
117
|
+
* iterated node will not be returned along with `elementEnd` tag.
|
|
118
|
+
*
|
|
119
|
+
* @readonly
|
|
120
|
+
* @member {Boolean} module:engine/model/treewalker~TreeWalker#shallow
|
|
121
|
+
*/
|
|
122
|
+
this.shallow = !!options.shallow;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Flag indicating whether iterator should ignore `elementEnd` tags. If the option is true walker will not
|
|
126
|
+
* return a parent node of the start position. If this option is `true` each {@link module:engine/model/element~Element} will
|
|
127
|
+
* be returned once, while if the option is `false` they might be returned twice:
|
|
128
|
+
* for `'elementStart'` and `'elementEnd'`.
|
|
129
|
+
*
|
|
130
|
+
* @readonly
|
|
131
|
+
* @member {Boolean} module:engine/model/treewalker~TreeWalker#ignoreElementEnd
|
|
132
|
+
*/
|
|
133
|
+
this.ignoreElementEnd = !!options.ignoreElementEnd;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Start boundary cached for optimization purposes.
|
|
137
|
+
*
|
|
138
|
+
* @private
|
|
139
|
+
* @member {module:engine/model/element~Element} module:engine/model/treewalker~TreeWalker#_boundaryStartParent
|
|
140
|
+
*/
|
|
141
|
+
this._boundaryStartParent = this.boundaries ? this.boundaries.start.parent : null;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* End boundary cached for optimization purposes.
|
|
145
|
+
*
|
|
146
|
+
* @private
|
|
147
|
+
* @member {module:engine/model/element~Element} module:engine/model/treewalker~TreeWalker#_boundaryEndParent
|
|
148
|
+
*/
|
|
149
|
+
this._boundaryEndParent = this.boundaries ? this.boundaries.end.parent : null;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parent of the most recently visited node. Cached for optimization purposes.
|
|
153
|
+
*
|
|
154
|
+
* @private
|
|
155
|
+
* @member {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
|
|
156
|
+
* module:engine/model/treewalker~TreeWalker#_visitedParent
|
|
157
|
+
*/
|
|
158
|
+
this._visitedParent = this.position.parent;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Iterable interface.
|
|
163
|
+
*
|
|
164
|
+
* @returns {Iterable.<module:engine/model/treewalker~TreeWalkerValue>}
|
|
165
|
+
*/
|
|
166
|
+
[ Symbol.iterator ]() {
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`.
|
|
172
|
+
*
|
|
173
|
+
* For example:
|
|
174
|
+
*
|
|
175
|
+
* walker.skip( value => value.type == 'text' ); // <paragraph>[]foo</paragraph> -> <paragraph>foo[]</paragraph>
|
|
176
|
+
* walker.skip( () => true ); // Move the position to the end: <paragraph>[]foo</paragraph> -> <paragraph>foo</paragraph>[]
|
|
177
|
+
* walker.skip( () => false ); // Do not move the position.
|
|
178
|
+
*
|
|
179
|
+
* @param {Function} skip Callback function. Gets {@link module:engine/model/treewalker~TreeWalkerValue} and should
|
|
180
|
+
* return `true` if the value should be skipped or `false` if not.
|
|
181
|
+
*/
|
|
182
|
+
skip( skip ) {
|
|
183
|
+
let done, value, prevPosition, prevVisitedParent;
|
|
184
|
+
|
|
185
|
+
do {
|
|
186
|
+
prevPosition = this.position;
|
|
187
|
+
prevVisitedParent = this._visitedParent;
|
|
188
|
+
|
|
189
|
+
( { done, value } = this.next() );
|
|
190
|
+
} while ( !done && skip( value ) );
|
|
191
|
+
|
|
192
|
+
if ( !done ) {
|
|
193
|
+
this.position = prevPosition;
|
|
194
|
+
this._visitedParent = prevVisitedParent;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Gets the next tree walker's value.
|
|
200
|
+
*
|
|
201
|
+
* @returns {module:engine/model/treewalker~TreeWalkerValue} Next tree walker's value.
|
|
202
|
+
*/
|
|
203
|
+
next() {
|
|
204
|
+
if ( this.direction == 'forward' ) {
|
|
205
|
+
return this._next();
|
|
206
|
+
} else {
|
|
207
|
+
return this._previous();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Makes a step forward in model. Moves the {@link #position} to the next position and returns the encountered value.
|
|
213
|
+
*
|
|
214
|
+
* @private
|
|
215
|
+
* @returns {Object}
|
|
216
|
+
* @returns {Boolean} return.done True if iterator is done.
|
|
217
|
+
* @returns {module:engine/model/treewalker~TreeWalkerValue} return.value Information about taken step.
|
|
218
|
+
*/
|
|
219
|
+
_next() {
|
|
220
|
+
const previousPosition = this.position;
|
|
221
|
+
const position = this.position.clone();
|
|
222
|
+
const parent = this._visitedParent;
|
|
223
|
+
|
|
224
|
+
// We are at the end of the root.
|
|
225
|
+
if ( parent.parent === null && position.offset === parent.maxOffset ) {
|
|
226
|
+
return { done: true };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// We reached the walker boundary.
|
|
230
|
+
if ( parent === this._boundaryEndParent && position.offset == this.boundaries.end.offset ) {
|
|
231
|
+
return { done: true };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Get node just after the current position.
|
|
235
|
+
// Use a highly optimized version instead of checking the text node first and then getting the node after. See #6582.
|
|
236
|
+
const positionParent = position.parent;
|
|
237
|
+
const textNodeAtPosition = getTextNodeAtPosition( position, positionParent );
|
|
238
|
+
const node = textNodeAtPosition ? textNodeAtPosition : getNodeAfterPosition( position, positionParent, textNodeAtPosition );
|
|
239
|
+
|
|
240
|
+
if ( node instanceof Element ) {
|
|
241
|
+
if ( !this.shallow ) {
|
|
242
|
+
// Manual operations on path internals for optimization purposes. Here and in the rest of the method.
|
|
243
|
+
position.path.push( 0 );
|
|
244
|
+
this._visitedParent = node;
|
|
245
|
+
} else {
|
|
246
|
+
position.offset++;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
this.position = position;
|
|
250
|
+
|
|
251
|
+
return formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
|
|
252
|
+
} else if ( node instanceof Text ) {
|
|
253
|
+
let charactersCount;
|
|
254
|
+
|
|
255
|
+
if ( this.singleCharacters ) {
|
|
256
|
+
charactersCount = 1;
|
|
257
|
+
} else {
|
|
258
|
+
let offset = node.endOffset;
|
|
259
|
+
|
|
260
|
+
if ( this._boundaryEndParent == parent && this.boundaries.end.offset < offset ) {
|
|
261
|
+
offset = this.boundaries.end.offset;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
charactersCount = offset - position.offset;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const offsetInTextNode = position.offset - node.startOffset;
|
|
268
|
+
const item = new TextProxy( node, offsetInTextNode, charactersCount );
|
|
269
|
+
|
|
270
|
+
position.offset += charactersCount;
|
|
271
|
+
this.position = position;
|
|
272
|
+
|
|
273
|
+
return formatReturnValue( 'text', item, previousPosition, position, charactersCount );
|
|
274
|
+
} else {
|
|
275
|
+
// `node` is not set, we reached the end of current `parent`.
|
|
276
|
+
position.path.pop();
|
|
277
|
+
position.offset++;
|
|
278
|
+
this.position = position;
|
|
279
|
+
this._visitedParent = parent.parent;
|
|
280
|
+
|
|
281
|
+
if ( this.ignoreElementEnd ) {
|
|
282
|
+
return this._next();
|
|
283
|
+
} else {
|
|
284
|
+
return formatReturnValue( 'elementEnd', parent, previousPosition, position );
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Makes a step backward in model. Moves the {@link #position} to the previous position and returns the encountered value.
|
|
291
|
+
*
|
|
292
|
+
* @private
|
|
293
|
+
* @returns {Object}
|
|
294
|
+
* @returns {Boolean} return.done True if iterator is done.
|
|
295
|
+
* @returns {module:engine/model/treewalker~TreeWalkerValue} return.value Information about taken step.
|
|
296
|
+
*/
|
|
297
|
+
_previous() {
|
|
298
|
+
const previousPosition = this.position;
|
|
299
|
+
const position = this.position.clone();
|
|
300
|
+
const parent = this._visitedParent;
|
|
301
|
+
|
|
302
|
+
// We are at the beginning of the root.
|
|
303
|
+
if ( parent.parent === null && position.offset === 0 ) {
|
|
304
|
+
return { done: true };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// We reached the walker boundary.
|
|
308
|
+
if ( parent == this._boundaryStartParent && position.offset == this.boundaries.start.offset ) {
|
|
309
|
+
return { done: true };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Get node just before the current position.
|
|
313
|
+
// Use a highly optimized version instead of checking the text node first and then getting the node before. See #6582.
|
|
314
|
+
const positionParent = position.parent;
|
|
315
|
+
const textNodeAtPosition = getTextNodeAtPosition( position, positionParent );
|
|
316
|
+
const node = textNodeAtPosition ? textNodeAtPosition : getNodeBeforePosition( position, positionParent, textNodeAtPosition );
|
|
317
|
+
|
|
318
|
+
if ( node instanceof Element ) {
|
|
319
|
+
position.offset--;
|
|
320
|
+
|
|
321
|
+
if ( !this.shallow ) {
|
|
322
|
+
position.path.push( node.maxOffset );
|
|
323
|
+
this.position = position;
|
|
324
|
+
this._visitedParent = node;
|
|
325
|
+
|
|
326
|
+
if ( this.ignoreElementEnd ) {
|
|
327
|
+
return this._previous();
|
|
328
|
+
} else {
|
|
329
|
+
return formatReturnValue( 'elementEnd', node, previousPosition, position );
|
|
330
|
+
}
|
|
331
|
+
} else {
|
|
332
|
+
this.position = position;
|
|
333
|
+
|
|
334
|
+
return formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
|
|
335
|
+
}
|
|
336
|
+
} else if ( node instanceof Text ) {
|
|
337
|
+
let charactersCount;
|
|
338
|
+
|
|
339
|
+
if ( this.singleCharacters ) {
|
|
340
|
+
charactersCount = 1;
|
|
341
|
+
} else {
|
|
342
|
+
let offset = node.startOffset;
|
|
343
|
+
|
|
344
|
+
if ( this._boundaryStartParent == parent && this.boundaries.start.offset > offset ) {
|
|
345
|
+
offset = this.boundaries.start.offset;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
charactersCount = position.offset - offset;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const offsetInTextNode = position.offset - node.startOffset;
|
|
352
|
+
const item = new TextProxy( node, offsetInTextNode - charactersCount, charactersCount );
|
|
353
|
+
|
|
354
|
+
position.offset -= charactersCount;
|
|
355
|
+
this.position = position;
|
|
356
|
+
|
|
357
|
+
return formatReturnValue( 'text', item, previousPosition, position, charactersCount );
|
|
358
|
+
} else {
|
|
359
|
+
// `node` is not set, we reached the beginning of current `parent`.
|
|
360
|
+
position.path.pop();
|
|
361
|
+
this.position = position;
|
|
362
|
+
this._visitedParent = parent.parent;
|
|
363
|
+
|
|
364
|
+
return formatReturnValue( 'elementStart', parent, previousPosition, position, 1 );
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function formatReturnValue( type, item, previousPosition, nextPosition, length ) {
|
|
370
|
+
return {
|
|
371
|
+
done: false,
|
|
372
|
+
value: {
|
|
373
|
+
type,
|
|
374
|
+
item,
|
|
375
|
+
previousPosition,
|
|
376
|
+
nextPosition,
|
|
377
|
+
length
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Type of the step made by {@link module:engine/model/treewalker~TreeWalker}.
|
|
384
|
+
* Possible values: `'elementStart'` if walker is at the beginning of a node, `'elementEnd'` if walker is at the end of node,
|
|
385
|
+
* or `'text'` if walker traversed over text.
|
|
386
|
+
*
|
|
387
|
+
* @typedef {'elementStart'|'elementEnd'|'text'} module:engine/model/treewalker~TreeWalkerValueType
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Object returned by {@link module:engine/model/treewalker~TreeWalker} when traversing tree model.
|
|
392
|
+
*
|
|
393
|
+
* @typedef {Object} module:engine/model/treewalker~TreeWalkerValue
|
|
394
|
+
* @property {module:engine/model/treewalker~TreeWalkerValueType} type
|
|
395
|
+
* @property {module:engine/model/item~Item} item Item between old and new positions of {@link module:engine/model/treewalker~TreeWalker}.
|
|
396
|
+
* @property {module:engine/model/position~Position} previousPosition Previous position of the iterator.
|
|
397
|
+
* * Forward iteration: For `'elementEnd'` it is the last position inside the element. For all other types it is the
|
|
398
|
+
* position before the item.
|
|
399
|
+
* * Backward iteration: For `'elementStart'` it is the first position inside the element. For all other types it is
|
|
400
|
+
* the position after item.
|
|
401
|
+
* @property {module:engine/model/position~Position} nextPosition Next position of the iterator.
|
|
402
|
+
* * Forward iteration: For `'elementStart'` it is the first position inside the element. For all other types it is
|
|
403
|
+
* the position after the item.
|
|
404
|
+
* * Backward iteration: For `'elementEnd'` it is last position inside element. For all other types it is the position
|
|
405
|
+
* before the item.
|
|
406
|
+
* @property {Number} [length] Length of the item. For `'elementStart'` it is 1. For `'text'` it is
|
|
407
|
+
* the length of the text. For `'elementEnd'` it is `undefined`.
|
|
408
|
+
*/
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Tree walking directions.
|
|
412
|
+
*
|
|
413
|
+
* @typedef {'forward'|'backward'} module:engine/model/treewalker~TreeWalkerDirection
|
|
414
|
+
*/
|
|
@@ -0,0 +1,77 @@
|
|
|
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/autoparagraphing
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Fixes all empty roots.
|
|
12
|
+
*
|
|
13
|
+
* @protected
|
|
14
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
15
|
+
* @returns {Boolean} `true` if any change has been applied, `false` otherwise.
|
|
16
|
+
*/
|
|
17
|
+
export function autoParagraphEmptyRoots( writer ) {
|
|
18
|
+
const { schema, document } = writer.model;
|
|
19
|
+
|
|
20
|
+
for ( const rootName of document.getRootNames() ) {
|
|
21
|
+
const root = document.getRoot( rootName );
|
|
22
|
+
|
|
23
|
+
if ( root.isEmpty && !schema.checkChild( root, '$text' ) ) {
|
|
24
|
+
// If paragraph element is allowed in the root, create paragraph element.
|
|
25
|
+
if ( schema.checkChild( root, 'paragraph' ) ) {
|
|
26
|
+
writer.insertElement( 'paragraph', root );
|
|
27
|
+
|
|
28
|
+
// Other roots will get fixed in the next post-fixer round. Those will be triggered
|
|
29
|
+
// in the same batch no matter if this method was triggered by the post-fixing or not
|
|
30
|
+
// (the above insertElement call will trigger the post-fixers).
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Checks if the given node wrapped with a paragraph would be accepted by the schema in the given position.
|
|
41
|
+
*
|
|
42
|
+
* @protected
|
|
43
|
+
* @param {module:engine/model/position~Position} position The position at which to check.
|
|
44
|
+
* @param {module:engine/model/node~Node|String} nodeOrType The child node or child type to check.
|
|
45
|
+
* @param {module:engine/model/schema~Schema} schema A schema instance used for element validation.
|
|
46
|
+
*/
|
|
47
|
+
export function isParagraphable( position, nodeOrType, schema ) {
|
|
48
|
+
const context = schema.createContext( position );
|
|
49
|
+
|
|
50
|
+
// When paragraph is allowed in this context...
|
|
51
|
+
if ( !schema.checkChild( context, 'paragraph' ) ) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// And a node would be allowed in this paragraph...
|
|
56
|
+
if ( !schema.checkChild( context.push( 'paragraph' ), nodeOrType ) ) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Inserts a new paragraph at the given position and returns a position inside that paragraph.
|
|
65
|
+
*
|
|
66
|
+
* @protected
|
|
67
|
+
* @param {module:engine/model/position~Position} position The position where a paragraph should be inserted.
|
|
68
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
69
|
+
* @returns {module:engine/model/position~Position} Position inside the created paragraph.
|
|
70
|
+
*/
|
|
71
|
+
export function wrapInParagraph( position, writer ) {
|
|
72
|
+
const paragraph = writer.createElement( 'paragraph' );
|
|
73
|
+
|
|
74
|
+
writer.insert( paragraph, position );
|
|
75
|
+
|
|
76
|
+
return writer.createPositionAt( paragraph, 0 );
|
|
77
|
+
}
|