@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,482 @@
|
|
|
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/dev-utils/model
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Collection of methods for manipulating the {@link module:engine/model/model model} for testing purposes.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import RootElement from '../model/rootelement';
|
|
15
|
+
import Model from '../model/model';
|
|
16
|
+
import ModelRange from '../model/range';
|
|
17
|
+
import ModelPosition from '../model/position';
|
|
18
|
+
import ModelSelection from '../model/selection';
|
|
19
|
+
import ModelDocumentFragment from '../model/documentfragment';
|
|
20
|
+
import DocumentSelection from '../model/documentselection';
|
|
21
|
+
|
|
22
|
+
import View from '../view/view';
|
|
23
|
+
import ViewContainerElement from '../view/containerelement';
|
|
24
|
+
import ViewRootEditableElement from '../view/rooteditableelement';
|
|
25
|
+
|
|
26
|
+
import { parse as viewParse, stringify as viewStringify } from '../../src/dev-utils/view';
|
|
27
|
+
|
|
28
|
+
import DowncastDispatcher from '../conversion/downcastdispatcher';
|
|
29
|
+
import UpcastDispatcher from '../conversion/upcastdispatcher';
|
|
30
|
+
import Mapper from '../conversion/mapper';
|
|
31
|
+
import {
|
|
32
|
+
convertCollapsedSelection,
|
|
33
|
+
convertRangeSelection,
|
|
34
|
+
insertElement,
|
|
35
|
+
insertText,
|
|
36
|
+
insertUIElement,
|
|
37
|
+
wrap
|
|
38
|
+
} from '../conversion/downcasthelpers';
|
|
39
|
+
|
|
40
|
+
import { isPlainObject } from 'lodash-es';
|
|
41
|
+
import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
|
|
42
|
+
import { StylesProcessor } from '../view/stylesmap';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Writes the content of a model {@link module:engine/model/document~Document document} to an HTML-like string.
|
|
46
|
+
*
|
|
47
|
+
* getData( editor.model ); // -> '<paragraph>Foo![]</paragraph>'
|
|
48
|
+
*
|
|
49
|
+
* **Note:** A {@link module:engine/model/text~Text text} node that contains attributes will be represented as:
|
|
50
|
+
*
|
|
51
|
+
* <$text attribute="value">Text data</$text>
|
|
52
|
+
*
|
|
53
|
+
* **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
|
|
54
|
+
* debugging and testing.
|
|
55
|
+
*
|
|
56
|
+
* @param {module:engine/model/model~Model} model
|
|
57
|
+
* @param {Object} [options]
|
|
58
|
+
* @param {Boolean} [options.withoutSelection=false] Whether to write the selection. When set to `true`, the selection will
|
|
59
|
+
* not be included in the returned string.
|
|
60
|
+
* @param {String} [options.rootName='main'] The name of the root from which the data should be stringified. If not provided,
|
|
61
|
+
* the default `main` name will be used.
|
|
62
|
+
* @param {Boolean} [options.convertMarkers=false] Whether to include markers in the returned string.
|
|
63
|
+
* @returns {String} The stringified data.
|
|
64
|
+
*/
|
|
65
|
+
export function getData( model, options = {} ) {
|
|
66
|
+
if ( !( model instanceof Model ) ) {
|
|
67
|
+
throw new TypeError( 'Model needs to be an instance of module:engine/model/model~Model.' );
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const rootName = options.rootName || 'main';
|
|
71
|
+
const root = model.document.getRoot( rootName );
|
|
72
|
+
|
|
73
|
+
return getData._stringify(
|
|
74
|
+
root,
|
|
75
|
+
options.withoutSelection ? null : model.document.selection,
|
|
76
|
+
options.convertMarkers ? model.markers : null
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Set stringify as getData private method - needed for testing/spying.
|
|
81
|
+
getData._stringify = stringify;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Sets the content of a model {@link module:engine/model/document~Document document} provided as an HTML-like string.
|
|
85
|
+
*
|
|
86
|
+
* setData( editor.model, '<paragraph>Foo![]</paragraph>' );
|
|
87
|
+
*
|
|
88
|
+
* **Note:** Remember to register elements in the {@link module:engine/model/model~Model#schema model's schema} before
|
|
89
|
+
* trying to use them.
|
|
90
|
+
*
|
|
91
|
+
* **Note:** To create a {@link module:engine/model/text~Text text} node that contains attributes use:
|
|
92
|
+
*
|
|
93
|
+
* <$text attribute="value">Text data</$text>
|
|
94
|
+
*
|
|
95
|
+
* **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
|
|
96
|
+
* debugging and testing.
|
|
97
|
+
*
|
|
98
|
+
* @param {module:engine/model/model~Model} model
|
|
99
|
+
* @param {String} data HTML-like string to write into the document.
|
|
100
|
+
* @param {Object} options
|
|
101
|
+
* @param {String} [options.rootName='main'] Root name where parsed data will be stored. If not provided, the default `main`
|
|
102
|
+
* name will be used.
|
|
103
|
+
* @param {Array<Object>} [options.selectionAttributes] A list of attributes which will be passed to the selection.
|
|
104
|
+
* @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward.
|
|
105
|
+
* @param {String} [options.batchType='default'] Batch type used for inserting elements.
|
|
106
|
+
* See {@link module:engine/model/batch~Batch#type}.
|
|
107
|
+
*/
|
|
108
|
+
export function setData( model, data, options = {} ) {
|
|
109
|
+
if ( !( model instanceof Model ) ) {
|
|
110
|
+
throw new TypeError( 'Model needs to be an instance of module:engine/model/model~Model.' );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let modelDocumentFragment, selection;
|
|
114
|
+
const modelRoot = model.document.getRoot( options.rootName || 'main' );
|
|
115
|
+
|
|
116
|
+
// Parse data string to model.
|
|
117
|
+
const parsedResult = setData._parse( data, model.schema, {
|
|
118
|
+
lastRangeBackward: options.lastRangeBackward,
|
|
119
|
+
selectionAttributes: options.selectionAttributes,
|
|
120
|
+
context: [ modelRoot.name ]
|
|
121
|
+
} );
|
|
122
|
+
|
|
123
|
+
// Retrieve DocumentFragment and Selection from parsed model.
|
|
124
|
+
if ( parsedResult.model ) {
|
|
125
|
+
modelDocumentFragment = parsedResult.model;
|
|
126
|
+
selection = parsedResult.selection;
|
|
127
|
+
} else {
|
|
128
|
+
modelDocumentFragment = parsedResult;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if ( typeof options.batchType === 'string' ) {
|
|
132
|
+
model.enqueueChange( options.batchType, writeToModel );
|
|
133
|
+
} else {
|
|
134
|
+
model.change( writeToModel );
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function writeToModel( writer ) {
|
|
138
|
+
// Replace existing model in document by new one.
|
|
139
|
+
writer.remove( writer.createRangeIn( modelRoot ) );
|
|
140
|
+
writer.insert( modelDocumentFragment, modelRoot );
|
|
141
|
+
|
|
142
|
+
// Clean up previous document selection.
|
|
143
|
+
writer.setSelection( null );
|
|
144
|
+
writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );
|
|
145
|
+
|
|
146
|
+
// Update document selection if specified.
|
|
147
|
+
if ( selection ) {
|
|
148
|
+
const ranges = [];
|
|
149
|
+
|
|
150
|
+
for ( const range of selection.getRanges() ) {
|
|
151
|
+
const start = new ModelPosition( modelRoot, range.start.path );
|
|
152
|
+
const end = new ModelPosition( modelRoot, range.end.path );
|
|
153
|
+
|
|
154
|
+
ranges.push( new ModelRange( start, end ) );
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
writer.setSelection( ranges, { backward: selection.isBackward } );
|
|
158
|
+
|
|
159
|
+
if ( options.selectionAttributes ) {
|
|
160
|
+
writer.setSelectionAttribute( selection.getAttributes() );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Set parse as setData private method - needed for testing/spying.
|
|
167
|
+
setData._parse = parse;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Converts model nodes to HTML-like string representation.
|
|
171
|
+
*
|
|
172
|
+
* **Note:** A {@link module:engine/model/text~Text text} node that contains attributes will be represented as:
|
|
173
|
+
*
|
|
174
|
+
* <$text attribute="value">Text data</$text>
|
|
175
|
+
*
|
|
176
|
+
* @param {module:engine/model/rootelement~RootElement|module:engine/model/element~Element|module:engine/model/text~Text|
|
|
177
|
+
* module:engine/model/documentfragment~DocumentFragment} node A node to stringify.
|
|
178
|
+
* @param {module:engine/model/selection~Selection|module:engine/model/position~Position|
|
|
179
|
+
* module:engine/model/range~Range} [selectionOrPositionOrRange=null]
|
|
180
|
+
* A selection instance whose ranges will be included in the returned string data. If a range instance is provided, it will be
|
|
181
|
+
* converted to a selection containing this range. If a position instance is provided, it will be converted to a selection
|
|
182
|
+
* containing one range collapsed at this position.
|
|
183
|
+
* @param {Iterable.<module:engine/model/markercollection~Marker>|null} markers Markers to include.
|
|
184
|
+
* @returns {String} An HTML-like string representing the model.
|
|
185
|
+
*/
|
|
186
|
+
export function stringify( node, selectionOrPositionOrRange = null, markers = null ) {
|
|
187
|
+
const model = new Model();
|
|
188
|
+
const mapper = new Mapper();
|
|
189
|
+
let selection, range;
|
|
190
|
+
|
|
191
|
+
// Create a range witch wraps passed node.
|
|
192
|
+
if ( node instanceof RootElement || node instanceof ModelDocumentFragment ) {
|
|
193
|
+
range = model.createRangeIn( node );
|
|
194
|
+
} else {
|
|
195
|
+
// Node is detached - create new document fragment.
|
|
196
|
+
if ( !node.parent ) {
|
|
197
|
+
const fragment = new ModelDocumentFragment( node );
|
|
198
|
+
range = model.createRangeIn( fragment );
|
|
199
|
+
} else {
|
|
200
|
+
range = new ModelRange(
|
|
201
|
+
model.createPositionBefore( node ),
|
|
202
|
+
model.createPositionAfter( node )
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Get selection from passed selection or position or range if at least one is specified.
|
|
208
|
+
if ( selectionOrPositionOrRange instanceof ModelSelection ) {
|
|
209
|
+
selection = selectionOrPositionOrRange;
|
|
210
|
+
} else if ( selectionOrPositionOrRange instanceof DocumentSelection ) {
|
|
211
|
+
selection = selectionOrPositionOrRange;
|
|
212
|
+
} else if ( selectionOrPositionOrRange instanceof ModelRange ) {
|
|
213
|
+
selection = new ModelSelection( selectionOrPositionOrRange );
|
|
214
|
+
} else if ( selectionOrPositionOrRange instanceof ModelPosition ) {
|
|
215
|
+
selection = new ModelSelection( selectionOrPositionOrRange );
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Set up conversion.
|
|
219
|
+
// Create a temporary view controller.
|
|
220
|
+
const stylesProcessor = new StylesProcessor();
|
|
221
|
+
const view = new View( stylesProcessor );
|
|
222
|
+
const viewDocument = view.document;
|
|
223
|
+
const viewRoot = new ViewRootEditableElement( viewDocument, 'div' );
|
|
224
|
+
|
|
225
|
+
// Create a temporary root element in view document.
|
|
226
|
+
viewRoot.rootName = 'main';
|
|
227
|
+
viewDocument.roots.add( viewRoot );
|
|
228
|
+
|
|
229
|
+
// Create and setup downcast dispatcher.
|
|
230
|
+
const downcastDispatcher = new DowncastDispatcher( { mapper } );
|
|
231
|
+
|
|
232
|
+
// Bind root elements.
|
|
233
|
+
mapper.bindElements( node.root, viewRoot );
|
|
234
|
+
|
|
235
|
+
downcastDispatcher.on( 'insert:$text', insertText() );
|
|
236
|
+
downcastDispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
|
|
237
|
+
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection || data.item.is( '$textProxy' ) ) {
|
|
238
|
+
const converter = wrap( ( modelAttributeValue, { writer } ) => {
|
|
239
|
+
return writer.createAttributeElement(
|
|
240
|
+
'model-text-with-attributes',
|
|
241
|
+
{ [ data.attributeKey ]: stringifyAttributeValue( modelAttributeValue ) }
|
|
242
|
+
);
|
|
243
|
+
} );
|
|
244
|
+
|
|
245
|
+
converter( evt, data, conversionApi );
|
|
246
|
+
}
|
|
247
|
+
} );
|
|
248
|
+
downcastDispatcher.on( 'insert', insertElement( modelItem => {
|
|
249
|
+
// Stringify object types values for properly display as an output string.
|
|
250
|
+
const attributes = convertAttributes( modelItem.getAttributes(), stringifyAttributeValue );
|
|
251
|
+
|
|
252
|
+
return new ViewContainerElement( viewDocument, modelItem.name, attributes );
|
|
253
|
+
} ) );
|
|
254
|
+
|
|
255
|
+
downcastDispatcher.on( 'selection', convertRangeSelection() );
|
|
256
|
+
downcastDispatcher.on( 'selection', convertCollapsedSelection() );
|
|
257
|
+
downcastDispatcher.on( 'addMarker', insertUIElement( ( data, { writer } ) => {
|
|
258
|
+
const name = data.markerName + ':' + ( data.isOpening ? 'start' : 'end' );
|
|
259
|
+
|
|
260
|
+
return writer.createUIElement( name );
|
|
261
|
+
} ) );
|
|
262
|
+
|
|
263
|
+
// Convert model to view.
|
|
264
|
+
const writer = view._writer;
|
|
265
|
+
downcastDispatcher.convertInsert( range, writer );
|
|
266
|
+
|
|
267
|
+
// Convert model selection to view selection.
|
|
268
|
+
if ( selection ) {
|
|
269
|
+
downcastDispatcher.convertSelection( selection, markers || model.markers, writer );
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if ( markers ) {
|
|
273
|
+
// To provide stable results, sort markers by name.
|
|
274
|
+
markers = Array.from( markers ).sort( ( a, b ) => a.name < b.name ? 1 : -1 );
|
|
275
|
+
|
|
276
|
+
for ( const marker of markers ) {
|
|
277
|
+
downcastDispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Parse view to data string.
|
|
282
|
+
let data = viewStringify( viewRoot, viewDocument.selection, { sameSelectionCharacters: true } );
|
|
283
|
+
|
|
284
|
+
// Removing unneccessary <div> and </div> added because `viewRoot` was also stringified alongside input data.
|
|
285
|
+
data = data.substr( 5, data.length - 11 );
|
|
286
|
+
|
|
287
|
+
view.destroy();
|
|
288
|
+
|
|
289
|
+
// Replace valid XML `model-text-with-attributes` element name to `$text`.
|
|
290
|
+
return data.replace( new RegExp( 'model-text-with-attributes', 'g' ), '$text' );
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Parses an HTML-like string and returns the model {@link module:engine/model/rootelement~RootElement rootElement}.
|
|
295
|
+
*
|
|
296
|
+
* **Note:** To create a {@link module:engine/model/text~Text text} node that contains attributes use:
|
|
297
|
+
*
|
|
298
|
+
* <$text attribute="value">Text data</$text>
|
|
299
|
+
*
|
|
300
|
+
* @param {String} data HTML-like string to be parsed.
|
|
301
|
+
* @param {module:engine/model/schema~Schema} schema A schema instance used by converters for element validation.
|
|
302
|
+
* @param {Object} [options={}] Additional configuration.
|
|
303
|
+
* @param {Array<Object>} [options.selectionAttributes] A list of attributes which will be passed to the selection.
|
|
304
|
+
* @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward.
|
|
305
|
+
* @param {module:engine/model/schema~SchemaContextDefinition} [options.context='$root'] The conversion context.
|
|
306
|
+
* If not provided, the default `'$root'` will be used.
|
|
307
|
+
* @returns {module:engine/model/element~Element|module:engine/model/text~Text|
|
|
308
|
+
* module:engine/model/documentfragment~DocumentFragment|Object} Returns the parsed model node or
|
|
309
|
+
* an object with two fields: `model` and `selection`, when selection ranges were included in the data to parse.
|
|
310
|
+
*/
|
|
311
|
+
export function parse( data, schema, options = {} ) {
|
|
312
|
+
const mapper = new Mapper();
|
|
313
|
+
|
|
314
|
+
// Replace not accepted by XML `$text` tag name by valid one `model-text-with-attributes`.
|
|
315
|
+
data = data.replace( new RegExp( '\\$text', 'g' ), 'model-text-with-attributes' );
|
|
316
|
+
|
|
317
|
+
// Parse data to view using view utils.
|
|
318
|
+
const parsedResult = viewParse( data, {
|
|
319
|
+
sameSelectionCharacters: true,
|
|
320
|
+
lastRangeBackward: !!options.lastRangeBackward
|
|
321
|
+
} );
|
|
322
|
+
|
|
323
|
+
// Retrieve DocumentFragment and Selection from parsed view.
|
|
324
|
+
let viewDocumentFragment, viewSelection, selection;
|
|
325
|
+
|
|
326
|
+
if ( parsedResult.view && parsedResult.selection ) {
|
|
327
|
+
viewDocumentFragment = parsedResult.view;
|
|
328
|
+
viewSelection = parsedResult.selection;
|
|
329
|
+
} else {
|
|
330
|
+
viewDocumentFragment = parsedResult;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Set up upcast dispatcher.
|
|
334
|
+
const modelController = new Model();
|
|
335
|
+
const upcastDispatcher = new UpcastDispatcher( { schema, mapper } );
|
|
336
|
+
|
|
337
|
+
upcastDispatcher.on( 'documentFragment', convertToModelFragment() );
|
|
338
|
+
upcastDispatcher.on( 'element:model-text-with-attributes', convertToModelText( true ) );
|
|
339
|
+
upcastDispatcher.on( 'element', convertToModelElement() );
|
|
340
|
+
upcastDispatcher.on( 'text', convertToModelText() );
|
|
341
|
+
|
|
342
|
+
upcastDispatcher.isDebug = true;
|
|
343
|
+
|
|
344
|
+
// Convert view to model.
|
|
345
|
+
let model = modelController.change(
|
|
346
|
+
writer => upcastDispatcher.convert( viewDocumentFragment.root, writer, options.context || '$root' )
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
mapper.bindElements( model, viewDocumentFragment.root );
|
|
350
|
+
|
|
351
|
+
// If root DocumentFragment contains only one element - return that element.
|
|
352
|
+
if ( model.childCount == 1 ) {
|
|
353
|
+
model = model.getChild( 0 );
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Convert view selection to model selection.
|
|
357
|
+
|
|
358
|
+
if ( viewSelection ) {
|
|
359
|
+
const ranges = [];
|
|
360
|
+
|
|
361
|
+
// Convert ranges.
|
|
362
|
+
for ( const viewRange of viewSelection.getRanges() ) {
|
|
363
|
+
ranges.push( mapper.toModelRange( viewRange ) );
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Create new selection.
|
|
367
|
+
selection = new ModelSelection( ranges, { backward: viewSelection.isBackward } );
|
|
368
|
+
|
|
369
|
+
// Set attributes to selection if specified.
|
|
370
|
+
for ( const [ key, value ] of toMap( options.selectionAttributes || [] ) ) {
|
|
371
|
+
selection.setAttribute( key, value );
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Return model end selection when selection was specified.
|
|
376
|
+
if ( selection ) {
|
|
377
|
+
return { model, selection };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Otherwise return model only.
|
|
381
|
+
return model;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// -- Converters view -> model -----------------------------------------------------
|
|
385
|
+
|
|
386
|
+
function convertToModelFragment() {
|
|
387
|
+
return ( evt, data, conversionApi ) => {
|
|
388
|
+
const childrenResult = conversionApi.convertChildren( data.viewItem, data.modelCursor );
|
|
389
|
+
|
|
390
|
+
conversionApi.mapper.bindElements( data.modelCursor.parent, data.viewItem );
|
|
391
|
+
|
|
392
|
+
data = Object.assign( data, childrenResult );
|
|
393
|
+
|
|
394
|
+
evt.stop();
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function convertToModelElement() {
|
|
399
|
+
return ( evt, data, conversionApi ) => {
|
|
400
|
+
const elementName = data.viewItem.name;
|
|
401
|
+
|
|
402
|
+
if ( !conversionApi.schema.checkChild( data.modelCursor, elementName ) ) {
|
|
403
|
+
throw new Error( `Element '${ elementName }' was not allowed in given position.` );
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// View attribute value is a string so we want to typecast it to the original type.
|
|
407
|
+
// E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
|
|
408
|
+
const attributes = convertAttributes( data.viewItem.getAttributes(), parseAttributeValue );
|
|
409
|
+
const element = conversionApi.writer.createElement( data.viewItem.name, attributes );
|
|
410
|
+
|
|
411
|
+
conversionApi.writer.insert( element, data.modelCursor );
|
|
412
|
+
|
|
413
|
+
conversionApi.mapper.bindElements( element, data.viewItem );
|
|
414
|
+
|
|
415
|
+
conversionApi.convertChildren( data.viewItem, element );
|
|
416
|
+
|
|
417
|
+
data.modelRange = ModelRange._createOn( element );
|
|
418
|
+
data.modelCursor = data.modelRange.end;
|
|
419
|
+
|
|
420
|
+
evt.stop();
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function convertToModelText( withAttributes = false ) {
|
|
425
|
+
return ( evt, data, conversionApi ) => {
|
|
426
|
+
if ( !conversionApi.schema.checkChild( data.modelCursor, '$text' ) ) {
|
|
427
|
+
throw new Error( 'Text was not allowed in given position.' );
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let node;
|
|
431
|
+
|
|
432
|
+
if ( withAttributes ) {
|
|
433
|
+
// View attribute value is a string so we want to typecast it to the original type.
|
|
434
|
+
// E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
|
|
435
|
+
const attributes = convertAttributes( data.viewItem.getAttributes(), parseAttributeValue );
|
|
436
|
+
|
|
437
|
+
node = conversionApi.writer.createText( data.viewItem.getChild( 0 ).data, attributes );
|
|
438
|
+
} else {
|
|
439
|
+
node = conversionApi.writer.createText( data.viewItem.data );
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
conversionApi.writer.insert( node, data.modelCursor );
|
|
443
|
+
|
|
444
|
+
data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, node.offsetSize );
|
|
445
|
+
data.modelCursor = data.modelRange.end;
|
|
446
|
+
|
|
447
|
+
evt.stop();
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Tries to get original type of attribute value using JSON parsing:
|
|
452
|
+
//
|
|
453
|
+
// `'true'` => `true`
|
|
454
|
+
// `'1'` => `1`
|
|
455
|
+
// `'{"x":1,"y":2}'` => `{ x: 1, y: 2 }`
|
|
456
|
+
//
|
|
457
|
+
// Parse error means that value should be a string:
|
|
458
|
+
//
|
|
459
|
+
// `'foobar'` => `'foobar'`
|
|
460
|
+
function parseAttributeValue( attribute ) {
|
|
461
|
+
try {
|
|
462
|
+
return JSON.parse( attribute );
|
|
463
|
+
} catch ( e ) {
|
|
464
|
+
return attribute;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// When value is an Object stringify it.
|
|
469
|
+
function stringifyAttributeValue( data ) {
|
|
470
|
+
if ( isPlainObject( data ) ) {
|
|
471
|
+
return JSON.stringify( data );
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return data;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Loop trough attributes map and converts each value by passed converter.
|
|
478
|
+
function* convertAttributes( attributes, converter ) {
|
|
479
|
+
for ( const [ key, value ] of attributes ) {
|
|
480
|
+
yield [ key, converter( value ) ];
|
|
481
|
+
}
|
|
482
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
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/dev-utils/operationreplayer
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/* global setTimeout */
|
|
11
|
+
|
|
12
|
+
import OperationFactory from '../model/operation/operationfactory';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Operation replayer is a development tool created for easy replaying of operations on the document from stringified operations.
|
|
16
|
+
*/
|
|
17
|
+
export default class OperationReplayer {
|
|
18
|
+
/**
|
|
19
|
+
* @param {module:engine/model/model~Model} model Data model.
|
|
20
|
+
* @param {String} logSeparator Separator between operations.
|
|
21
|
+
* @param {String} stringifiedOperations Operations to replay.
|
|
22
|
+
*/
|
|
23
|
+
constructor( model, logSeparator, stringifiedOperations ) {
|
|
24
|
+
this._model = model;
|
|
25
|
+
this._logSeparator = logSeparator;
|
|
26
|
+
this.setStringifiedOperations( stringifiedOperations );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parses the given string containing stringified operations and sets parsed operations as operations to replay.
|
|
31
|
+
*
|
|
32
|
+
* @param {String} stringifiedOperations Stringified operations to replay.
|
|
33
|
+
*/
|
|
34
|
+
setStringifiedOperations( stringifiedOperations ) {
|
|
35
|
+
if ( stringifiedOperations === '' ) {
|
|
36
|
+
this._operationsToReplay = [];
|
|
37
|
+
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this._operationsToReplay = stringifiedOperations
|
|
42
|
+
.split( this._logSeparator )
|
|
43
|
+
.map( stringifiedOperation => JSON.parse( stringifiedOperation ) );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns operations to replay.
|
|
48
|
+
*
|
|
49
|
+
* @returns {Array.<module:engine/model/operation/operation~Operation>}
|
|
50
|
+
*/
|
|
51
|
+
getOperationsToReplay() {
|
|
52
|
+
return this._operationsToReplay;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Applies all operations with a delay between actions.
|
|
57
|
+
*
|
|
58
|
+
* @param {Number} timeInterval Time between applying operations.
|
|
59
|
+
* @returns {Promise}
|
|
60
|
+
*/
|
|
61
|
+
play( timeInterval = 1000 ) {
|
|
62
|
+
const operationReplayer = this; // eslint-disable-line consistent-this
|
|
63
|
+
|
|
64
|
+
return new Promise( ( res, rej ) => {
|
|
65
|
+
play();
|
|
66
|
+
|
|
67
|
+
function play() {
|
|
68
|
+
operationReplayer.applyNextOperation().then( isFinished => {
|
|
69
|
+
if ( isFinished ) {
|
|
70
|
+
return res();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
setTimeout( play, timeInterval );
|
|
74
|
+
} ).catch( err => {
|
|
75
|
+
rej( err );
|
|
76
|
+
} );
|
|
77
|
+
}
|
|
78
|
+
} );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Applies `numberOfOperations` operations, beginning after the last applied operation (or first, if no operations were applied).
|
|
83
|
+
*
|
|
84
|
+
* @param {Number} numberOfOperations The number of operations to apply.
|
|
85
|
+
* @returns {Promise}
|
|
86
|
+
*/
|
|
87
|
+
applyOperations( numberOfOperations ) {
|
|
88
|
+
if ( numberOfOperations <= 0 ) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return this.applyNextOperation()
|
|
93
|
+
.then( isFinished => {
|
|
94
|
+
if ( !isFinished ) {
|
|
95
|
+
return this.applyOperations( numberOfOperations - 1 );
|
|
96
|
+
}
|
|
97
|
+
} );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Applies all operations to replay at once.
|
|
102
|
+
*
|
|
103
|
+
* @returns {Promise}
|
|
104
|
+
*/
|
|
105
|
+
applyAllOperations() {
|
|
106
|
+
return this.applyNextOperation()
|
|
107
|
+
.then( isFinished => {
|
|
108
|
+
if ( !isFinished ) {
|
|
109
|
+
return this.applyAllOperations();
|
|
110
|
+
}
|
|
111
|
+
} );
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Applies the next operation to replay. Returns a promise with the `isFinished` parameter that is `true` if the last
|
|
116
|
+
* operation in the replayer has been applied, `false` otherwise.
|
|
117
|
+
*
|
|
118
|
+
* @returns {Promise.<Boolean>}
|
|
119
|
+
*/
|
|
120
|
+
applyNextOperation() {
|
|
121
|
+
const model = this._model;
|
|
122
|
+
|
|
123
|
+
return new Promise( res => {
|
|
124
|
+
model.enqueueChange( writer => {
|
|
125
|
+
const operationJson = this._operationsToReplay.shift();
|
|
126
|
+
|
|
127
|
+
if ( !operationJson ) {
|
|
128
|
+
return res( true );
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const operation = OperationFactory.fromJSON( operationJson, model.document );
|
|
132
|
+
|
|
133
|
+
writer.batch.addOperation( operation );
|
|
134
|
+
model.applyOperation( operation );
|
|
135
|
+
|
|
136
|
+
res( false );
|
|
137
|
+
} );
|
|
138
|
+
} );
|
|
139
|
+
}
|
|
140
|
+
}
|