@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,997 @@
|
|
|
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
|
+
import Matcher from '../view/matcher';
|
|
7
|
+
import ConversionHelpers from './conversionhelpers';
|
|
8
|
+
|
|
9
|
+
import { cloneDeep } from 'lodash-es';
|
|
10
|
+
|
|
11
|
+
import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
|
|
12
|
+
import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Contains {@link module:engine/view/view view} to {@link module:engine/model/model model} converters for
|
|
16
|
+
* {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}.
|
|
17
|
+
*
|
|
18
|
+
* @module engine/conversion/upcasthelpers
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Upcast conversion helper functions.
|
|
23
|
+
*
|
|
24
|
+
* @extends module:engine/conversion/conversionhelpers~ConversionHelpers
|
|
25
|
+
*/
|
|
26
|
+
export default class UpcastHelpers extends ConversionHelpers {
|
|
27
|
+
/**
|
|
28
|
+
* View element to model element conversion helper.
|
|
29
|
+
*
|
|
30
|
+
* This conversion results in creating a model element. For example,
|
|
31
|
+
* view `<p>Foo</p>` becomes `<paragraph>Foo</paragraph>` in the model.
|
|
32
|
+
*
|
|
33
|
+
* Keep in mind that the element will be inserted only if it is allowed
|
|
34
|
+
* by {@link module:engine/model/schema~Schema schema} configuration.
|
|
35
|
+
*
|
|
36
|
+
* editor.conversion.for( 'upcast' ).elementToElement( {
|
|
37
|
+
* view: 'p',
|
|
38
|
+
* model: 'paragraph'
|
|
39
|
+
* } );
|
|
40
|
+
*
|
|
41
|
+
* editor.conversion.for( 'upcast' ).elementToElement( {
|
|
42
|
+
* view: 'p',
|
|
43
|
+
* model: 'paragraph',
|
|
44
|
+
* converterPriority: 'high'
|
|
45
|
+
* } );
|
|
46
|
+
*
|
|
47
|
+
* editor.conversion.for( 'upcast' ).elementToElement( {
|
|
48
|
+
* view: {
|
|
49
|
+
* name: 'p',
|
|
50
|
+
* classes: 'fancy'
|
|
51
|
+
* },
|
|
52
|
+
* model: 'fancyParagraph'
|
|
53
|
+
* } );
|
|
54
|
+
*
|
|
55
|
+
* editor.conversion.for( 'upcast' ).elementToElement( {
|
|
56
|
+
* view: {
|
|
57
|
+
* name: 'p',
|
|
58
|
+
* classes: 'heading'
|
|
59
|
+
* },
|
|
60
|
+
* model: ( viewElement, conversionApi ) => {
|
|
61
|
+
* const modelWriter = conversionApi.writer;
|
|
62
|
+
*
|
|
63
|
+
* return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
|
|
64
|
+
* }
|
|
65
|
+
* } );
|
|
66
|
+
*
|
|
67
|
+
* See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
|
|
68
|
+
* to the conversion process.
|
|
69
|
+
*
|
|
70
|
+
* @method #elementToElement
|
|
71
|
+
* @param {Object} config Conversion configuration.
|
|
72
|
+
* @param {module:engine/view/matcher~MatcherPattern} [config.view] Pattern matching all view elements which should be converted. If not
|
|
73
|
+
* set, the converter will fire for every view element.
|
|
74
|
+
* @param {String|module:engine/model/element~Element|Function} config.model Name of the model element, a model element instance or a
|
|
75
|
+
* function that takes a view element and {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API}
|
|
76
|
+
* and returns a model element. The model element will be inserted in the model.
|
|
77
|
+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
|
|
78
|
+
* @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
|
|
79
|
+
*/
|
|
80
|
+
elementToElement( config ) {
|
|
81
|
+
return this.add( upcastElementToElement( config ) );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* View element to model attribute conversion helper.
|
|
86
|
+
*
|
|
87
|
+
* This conversion results in setting an attribute on a model node. For example, view `<strong>Foo</strong>` becomes
|
|
88
|
+
* `Foo` {@link module:engine/model/text~Text model text node} with `bold` attribute set to `true`.
|
|
89
|
+
*
|
|
90
|
+
* This helper is meant to set a model attribute on all the elements that are inside the converted element:
|
|
91
|
+
*
|
|
92
|
+
* <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
|
|
93
|
+
*
|
|
94
|
+
* Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
|
|
95
|
+
* Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text. See
|
|
96
|
+
* {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute} for comparison.
|
|
97
|
+
*
|
|
98
|
+
* Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
|
|
99
|
+
*
|
|
100
|
+
* editor.conversion.for( 'upcast' ).elementToAttribute( {
|
|
101
|
+
* view: 'strong',
|
|
102
|
+
* model: 'bold'
|
|
103
|
+
* } );
|
|
104
|
+
*
|
|
105
|
+
* editor.conversion.for( 'upcast' ).elementToAttribute( {
|
|
106
|
+
* view: 'strong',
|
|
107
|
+
* model: 'bold',
|
|
108
|
+
* converterPriority: 'high'
|
|
109
|
+
* } );
|
|
110
|
+
*
|
|
111
|
+
* editor.conversion.for( 'upcast' ).elementToAttribute( {
|
|
112
|
+
* view: {
|
|
113
|
+
* name: 'span',
|
|
114
|
+
* classes: 'bold'
|
|
115
|
+
* },
|
|
116
|
+
* model: 'bold'
|
|
117
|
+
* } );
|
|
118
|
+
*
|
|
119
|
+
* editor.conversion.for( 'upcast' ).elementToAttribute( {
|
|
120
|
+
* view: {
|
|
121
|
+
* name: 'span',
|
|
122
|
+
* classes: [ 'styled', 'styled-dark' ]
|
|
123
|
+
* },
|
|
124
|
+
* model: {
|
|
125
|
+
* key: 'styled',
|
|
126
|
+
* value: 'dark'
|
|
127
|
+
* }
|
|
128
|
+
* } );
|
|
129
|
+
*
|
|
130
|
+
* editor.conversion.for( 'upcast' ).elementToAttribute( {
|
|
131
|
+
* view: {
|
|
132
|
+
* name: 'span',
|
|
133
|
+
* styles: {
|
|
134
|
+
* 'font-size': /[\s\S]+/
|
|
135
|
+
* }
|
|
136
|
+
* },
|
|
137
|
+
* model: {
|
|
138
|
+
* key: 'fontSize',
|
|
139
|
+
* value: ( viewElement, conversionApi ) => {
|
|
140
|
+
* const fontSize = viewElement.getStyle( 'font-size' );
|
|
141
|
+
* const value = fontSize.substr( 0, fontSize.length - 2 );
|
|
142
|
+
*
|
|
143
|
+
* if ( value <= 10 ) {
|
|
144
|
+
* return 'small';
|
|
145
|
+
* } else if ( value > 12 ) {
|
|
146
|
+
* return 'big';
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* return null;
|
|
150
|
+
* }
|
|
151
|
+
* }
|
|
152
|
+
* } );
|
|
153
|
+
*
|
|
154
|
+
* See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
|
|
155
|
+
* to the conversion process.
|
|
156
|
+
*
|
|
157
|
+
* @method #elementToAttribute
|
|
158
|
+
* @param {Object} config Conversion configuration.
|
|
159
|
+
* @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
|
|
160
|
+
* @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
|
|
161
|
+
* the model attribute. `value` property may be set as a function that takes a view element and
|
|
162
|
+
* {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
|
|
163
|
+
* If `String` is given, the model attribute value will be set to `true`.
|
|
164
|
+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
|
|
165
|
+
* @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
|
|
166
|
+
*/
|
|
167
|
+
elementToAttribute( config ) {
|
|
168
|
+
return this.add( upcastElementToAttribute( config ) );
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* View attribute to model attribute conversion helper.
|
|
173
|
+
*
|
|
174
|
+
* This conversion results in setting an attribute on a model node. For example, view `<img src="foo.jpg"></img>` becomes
|
|
175
|
+
* `<imageBlock source="foo.jpg"></imageBlock>` in the model.
|
|
176
|
+
*
|
|
177
|
+
* This helper is meant to convert view attributes from view elements which got converted to the model, so the view attribute
|
|
178
|
+
* is set only on the corresponding model node:
|
|
179
|
+
*
|
|
180
|
+
* <div class="dark"><div>foo</div></div> --> <div dark="true"><div>foo</div></div>
|
|
181
|
+
*
|
|
182
|
+
* Above, `class="dark"` attribute is added only to the `<div>` elements that has it. This is in contrary to
|
|
183
|
+
* {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute} which sets attributes for
|
|
184
|
+
* all the children in the model:
|
|
185
|
+
*
|
|
186
|
+
* <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
|
|
187
|
+
*
|
|
188
|
+
* Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
|
|
189
|
+
* Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text.
|
|
190
|
+
*
|
|
191
|
+
* Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
|
|
192
|
+
*
|
|
193
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
194
|
+
* view: 'src',
|
|
195
|
+
* model: 'source'
|
|
196
|
+
* } );
|
|
197
|
+
*
|
|
198
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
199
|
+
* view: { key: 'src' },
|
|
200
|
+
* model: 'source'
|
|
201
|
+
* } );
|
|
202
|
+
*
|
|
203
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
204
|
+
* view: { key: 'src' },
|
|
205
|
+
* model: 'source',
|
|
206
|
+
* converterPriority: 'normal'
|
|
207
|
+
* } );
|
|
208
|
+
*
|
|
209
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
210
|
+
* view: {
|
|
211
|
+
* key: 'data-style',
|
|
212
|
+
* value: /[\s\S]+/
|
|
213
|
+
* },
|
|
214
|
+
* model: 'styled'
|
|
215
|
+
* } );
|
|
216
|
+
*
|
|
217
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
218
|
+
* view: {
|
|
219
|
+
* name: 'img',
|
|
220
|
+
* key: 'class',
|
|
221
|
+
* value: 'styled-dark'
|
|
222
|
+
* },
|
|
223
|
+
* model: {
|
|
224
|
+
* key: 'styled',
|
|
225
|
+
* value: 'dark'
|
|
226
|
+
* }
|
|
227
|
+
* } );
|
|
228
|
+
*
|
|
229
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
230
|
+
* view: {
|
|
231
|
+
* key: 'class',
|
|
232
|
+
* value: /styled-[\S]+/
|
|
233
|
+
* },
|
|
234
|
+
* model: {
|
|
235
|
+
* key: 'styled'
|
|
236
|
+
* value: ( viewElement, conversionApi ) => {
|
|
237
|
+
* const regexp = /styled-([\S]+)/;
|
|
238
|
+
* const match = viewElement.getAttribute( 'class' ).match( regexp );
|
|
239
|
+
*
|
|
240
|
+
* return match[ 1 ];
|
|
241
|
+
* }
|
|
242
|
+
* }
|
|
243
|
+
* } );
|
|
244
|
+
*
|
|
245
|
+
* Converting styles works a bit differently as it requires `view.styles` to be an object and by default
|
|
246
|
+
* a model attribute will be set to `true` by such a converter. You can set the model attribute to any value by providing the `value`
|
|
247
|
+
* callback that returns the desired value.
|
|
248
|
+
*
|
|
249
|
+
* // Default conversion of font-weight style will result in setting bold attribute to true.
|
|
250
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
251
|
+
* view: {
|
|
252
|
+
* styles: {
|
|
253
|
+
* 'font-weight': 'bold'
|
|
254
|
+
* }
|
|
255
|
+
* },
|
|
256
|
+
* model: 'bold'
|
|
257
|
+
* } );
|
|
258
|
+
*
|
|
259
|
+
* // This converter will pass any style value to the `lineHeight` model attribute.
|
|
260
|
+
* editor.conversion.for( 'upcast' ).attributeToAttribute( {
|
|
261
|
+
* view: {
|
|
262
|
+
* styles: {
|
|
263
|
+
* 'line-height': /[\s\S]+/
|
|
264
|
+
* }
|
|
265
|
+
* },
|
|
266
|
+
* model: {
|
|
267
|
+
* key: 'lineHeight',
|
|
268
|
+
* value: ( viewElement, conversionApi ) => viewElement.getStyle( 'line-height' )
|
|
269
|
+
* }
|
|
270
|
+
* } );
|
|
271
|
+
*
|
|
272
|
+
* See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
|
|
273
|
+
* to the conversion process.
|
|
274
|
+
*
|
|
275
|
+
* @method #attributeToAttribute
|
|
276
|
+
* @param {Object} config Conversion configuration.
|
|
277
|
+
* @param {String|Object} config.view Specifies which view attribute will be converted. If a `String` is passed,
|
|
278
|
+
* attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
|
|
279
|
+
* specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
|
|
280
|
+
* property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
|
|
281
|
+
* a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
|
|
282
|
+
* @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
|
|
283
|
+
* the model attribute. `value` property may be set as a function that takes a view element and
|
|
284
|
+
* {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
|
|
285
|
+
* If `String` is given, the model attribute value will be same as view attribute value.
|
|
286
|
+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
|
|
287
|
+
* @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
|
|
288
|
+
*/
|
|
289
|
+
attributeToAttribute( config ) {
|
|
290
|
+
return this.add( upcastAttributeToAttribute( config ) );
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* View element to model marker conversion helper.
|
|
295
|
+
*
|
|
296
|
+
* This conversion results in creating a model marker. For example, if the marker was stored in a view as an element:
|
|
297
|
+
* `<p>Fo<span data-marker="comment" data-comment-id="7"></span>o</p><p>B<span data-marker="comment" data-comment-id="7"></span>ar</p>`,
|
|
298
|
+
* after the conversion is done, the marker will be available in
|
|
299
|
+
* {@link module:engine/model/model~Model#markers model document markers}.
|
|
300
|
+
*
|
|
301
|
+
* **Note**: When this helper is used in the data upcast in combination with
|
|
302
|
+
* {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} in the data downcast,
|
|
303
|
+
* then invalid HTML code (e.g. a span between table cells) may be produced by the latter converter.
|
|
304
|
+
*
|
|
305
|
+
* In most of the cases, the {@link #dataToMarker} should be used instead.
|
|
306
|
+
*
|
|
307
|
+
* editor.conversion.for( 'upcast' ).elementToMarker( {
|
|
308
|
+
* view: 'marker-search',
|
|
309
|
+
* model: 'search'
|
|
310
|
+
* } );
|
|
311
|
+
*
|
|
312
|
+
* editor.conversion.for( 'upcast' ).elementToMarker( {
|
|
313
|
+
* view: 'marker-search',
|
|
314
|
+
* model: 'search',
|
|
315
|
+
* converterPriority: 'high'
|
|
316
|
+
* } );
|
|
317
|
+
*
|
|
318
|
+
* editor.conversion.for( 'upcast' ).elementToMarker( {
|
|
319
|
+
* view: 'marker-search',
|
|
320
|
+
* model: ( viewElement, conversionApi ) => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
|
|
321
|
+
* } );
|
|
322
|
+
*
|
|
323
|
+
* editor.conversion.for( 'upcast' ).elementToMarker( {
|
|
324
|
+
* view: {
|
|
325
|
+
* name: 'span',
|
|
326
|
+
* attributes: {
|
|
327
|
+
* 'data-marker': 'search'
|
|
328
|
+
* }
|
|
329
|
+
* },
|
|
330
|
+
* model: 'search'
|
|
331
|
+
* } );
|
|
332
|
+
*
|
|
333
|
+
* See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
|
|
334
|
+
* to the conversion process.
|
|
335
|
+
*
|
|
336
|
+
* @method #elementToMarker
|
|
337
|
+
* @param {Object} config Conversion configuration.
|
|
338
|
+
* @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
|
|
339
|
+
* @param {String|Function} config.model Name of the model marker, or a function that takes a view element and returns
|
|
340
|
+
* a model marker name.
|
|
341
|
+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
|
|
342
|
+
* @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
|
|
343
|
+
*/
|
|
344
|
+
elementToMarker( config ) {
|
|
345
|
+
return this.add( upcastElementToMarker( config ) );
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* View-to-model marker conversion helper.
|
|
350
|
+
*
|
|
351
|
+
* Converts view data created by {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`}
|
|
352
|
+
* back to a model marker.
|
|
353
|
+
*
|
|
354
|
+
* This converter looks for specific view elements and view attributes that mark marker boundaries. See
|
|
355
|
+
* {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} to learn what view data
|
|
356
|
+
* is expected by this converter.
|
|
357
|
+
*
|
|
358
|
+
* The `config.view` property is equal to the marker group name to convert.
|
|
359
|
+
*
|
|
360
|
+
* By default, this converter creates markers with the `group:name` name convention (to match the default `markerToData` conversion).
|
|
361
|
+
*
|
|
362
|
+
* The conversion configuration can take a function that will generate a marker name.
|
|
363
|
+
* If such function is set as the `config.model` parameter, it is passed the `name` part from the view element or attribute and it is
|
|
364
|
+
* expected to return a string with the marker name.
|
|
365
|
+
*
|
|
366
|
+
* Basic usage:
|
|
367
|
+
*
|
|
368
|
+
* // Using the default conversion.
|
|
369
|
+
* // In this case, all markers from the `comment` group will be converted.
|
|
370
|
+
* // The conversion will look for `<comment-start>` and `<comment-end>` tags and
|
|
371
|
+
* // `data-comment-start-before`, `data-comment-start-after`,
|
|
372
|
+
* // `data-comment-end-before` and `data-comment-end-after` attributes.
|
|
373
|
+
* editor.conversion.for( 'upcast' ).dataToMarker( {
|
|
374
|
+
* view: 'comment'
|
|
375
|
+
* } );
|
|
376
|
+
*
|
|
377
|
+
* An example of a model that may be generated by this conversion:
|
|
378
|
+
*
|
|
379
|
+
* // View:
|
|
380
|
+
* <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p>
|
|
381
|
+
* <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure>
|
|
382
|
+
*
|
|
383
|
+
* // Model:
|
|
384
|
+
* <paragraph>Foo[bar</paragraph>
|
|
385
|
+
* <imageBlock src="abc.jpg"></imageBlock>]
|
|
386
|
+
*
|
|
387
|
+
* Where `[]` are boundaries of a marker that will receive the `comment:commentId:uid` name.
|
|
388
|
+
*
|
|
389
|
+
* Other examples of usage:
|
|
390
|
+
*
|
|
391
|
+
* // Using a custom function which is the same as the default conversion:
|
|
392
|
+
* editor.conversion.for( 'upcast' ).dataToMarker( {
|
|
393
|
+
* view: 'comment',
|
|
394
|
+
* model: ( name, conversionApi ) => 'comment:' + name,
|
|
395
|
+
* } );
|
|
396
|
+
*
|
|
397
|
+
* // Using the converter priority:
|
|
398
|
+
* editor.conversion.for( 'upcast' ).dataToMarker( {
|
|
399
|
+
* view: 'comment',
|
|
400
|
+
* model: ( name, conversionApi ) => 'comment:' + name,
|
|
401
|
+
* converterPriority: 'high'
|
|
402
|
+
* } );
|
|
403
|
+
*
|
|
404
|
+
* See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
|
|
405
|
+
* to the conversion process.
|
|
406
|
+
*
|
|
407
|
+
* @method #dataToMarker
|
|
408
|
+
* @param {Object} config Conversion configuration.
|
|
409
|
+
* @param {String} config.view The marker group name to convert.
|
|
410
|
+
* @param {Function} [config.model] A function that takes the `name` part from the view element or attribute and
|
|
411
|
+
* {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the marker name.
|
|
412
|
+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
|
|
413
|
+
* @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
|
|
414
|
+
*/
|
|
415
|
+
dataToMarker( config ) {
|
|
416
|
+
return this.add( upcastDataToMarker( config ) );
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Function factory, creates a converter that converts {@link module:engine/view/documentfragment~DocumentFragment view document fragment}
|
|
422
|
+
* or all children of {@link module:engine/view/element~Element} into
|
|
423
|
+
* {@link module:engine/model/documentfragment~DocumentFragment model document fragment}.
|
|
424
|
+
* This is the "entry-point" converter for upcast (view to model conversion). This converter starts the conversion of all children
|
|
425
|
+
* of passed view document fragment. Those children {@link module:engine/view/node~Node view nodes} are then handled by other converters.
|
|
426
|
+
*
|
|
427
|
+
* This also a "default", last resort converter for all view elements that has not been converted by other converters.
|
|
428
|
+
* When a view element is being converted to the model but it does not have converter specified, that view element
|
|
429
|
+
* will be converted to {@link module:engine/model/documentfragment~DocumentFragment model document fragment} and returned.
|
|
430
|
+
*
|
|
431
|
+
* @returns {Function} Universal converter for view {@link module:engine/view/documentfragment~DocumentFragment fragments} and
|
|
432
|
+
* {@link module:engine/view/element~Element elements} that returns
|
|
433
|
+
* {@link module:engine/model/documentfragment~DocumentFragment model fragment} with children of converted view item.
|
|
434
|
+
*/
|
|
435
|
+
export function convertToModelFragment() {
|
|
436
|
+
return ( evt, data, conversionApi ) => {
|
|
437
|
+
// Second argument in `consumable.consume` is discarded for ViewDocumentFragment but is needed for ViewElement.
|
|
438
|
+
if ( !data.modelRange && conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
|
|
439
|
+
const { modelRange, modelCursor } = conversionApi.convertChildren( data.viewItem, data.modelCursor );
|
|
440
|
+
|
|
441
|
+
data.modelRange = modelRange;
|
|
442
|
+
data.modelCursor = modelCursor;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Function factory, creates a converter that converts {@link module:engine/view/text~Text} to {@link module:engine/model/text~Text}.
|
|
449
|
+
*
|
|
450
|
+
* @returns {Function} {@link module:engine/view/text~Text View text} converter.
|
|
451
|
+
*/
|
|
452
|
+
export function convertText() {
|
|
453
|
+
return ( evt, data, { schema, consumable, writer } ) => {
|
|
454
|
+
let position = data.modelCursor;
|
|
455
|
+
|
|
456
|
+
// When node is already converted then do nothing.
|
|
457
|
+
if ( !consumable.test( data.viewItem ) ) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if ( !schema.checkChild( position, '$text' ) ) {
|
|
462
|
+
if ( !isParagraphable( position, '$text', schema ) ) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
position = wrapInParagraph( position, writer );
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
consumable.consume( data.viewItem );
|
|
470
|
+
|
|
471
|
+
const text = writer.createText( data.viewItem.data );
|
|
472
|
+
|
|
473
|
+
writer.insert( text, position );
|
|
474
|
+
|
|
475
|
+
data.modelRange = writer.createRange(
|
|
476
|
+
position,
|
|
477
|
+
position.getShiftedBy( text.offsetSize )
|
|
478
|
+
);
|
|
479
|
+
data.modelCursor = data.modelRange.end;
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Function factory, creates a callback function which converts a {@link module:engine/view/selection~Selection
|
|
485
|
+
* view selection} taken from the {@link module:engine/view/document~Document#event:selectionChange} event
|
|
486
|
+
* and sets in on the {@link module:engine/model/document~Document#selection model}.
|
|
487
|
+
*
|
|
488
|
+
* **Note**: because there is no view selection change dispatcher nor any other advanced view selection to model
|
|
489
|
+
* conversion mechanism, the callback should be set directly on view document.
|
|
490
|
+
*
|
|
491
|
+
* view.document.on( 'selectionChange', convertSelectionChange( modelDocument, mapper ) );
|
|
492
|
+
*
|
|
493
|
+
* @param {module:engine/model/model~Model} model Data model.
|
|
494
|
+
* @param {module:engine/conversion/mapper~Mapper} mapper Conversion mapper.
|
|
495
|
+
* @returns {Function} {@link module:engine/view/document~Document#event:selectionChange} callback function.
|
|
496
|
+
*/
|
|
497
|
+
export function convertSelectionChange( model, mapper ) {
|
|
498
|
+
return ( evt, data ) => {
|
|
499
|
+
const viewSelection = data.newSelection;
|
|
500
|
+
|
|
501
|
+
const ranges = [];
|
|
502
|
+
|
|
503
|
+
for ( const viewRange of viewSelection.getRanges() ) {
|
|
504
|
+
ranges.push( mapper.toModelRange( viewRange ) );
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const modelSelection = model.createSelection( ranges, { backward: viewSelection.isBackward } );
|
|
508
|
+
|
|
509
|
+
if ( !modelSelection.isEqual( model.document.selection ) ) {
|
|
510
|
+
model.change( writer => {
|
|
511
|
+
writer.setSelection( modelSelection );
|
|
512
|
+
} );
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// View element to model element conversion helper.
|
|
518
|
+
//
|
|
519
|
+
// See {@link ~UpcastHelpers#elementToElement `.elementToElement()` upcast helper} for examples.
|
|
520
|
+
//
|
|
521
|
+
// @param {Object} config Conversion configuration.
|
|
522
|
+
// @param {module:engine/view/matcher~MatcherPattern} [config.view] Pattern matching all view elements which should be converted. If not
|
|
523
|
+
// set, the converter will fire for every view element.
|
|
524
|
+
// @param {String|module:engine/model/element~Element|Function} config.model Name of the model element, a model element
|
|
525
|
+
// instance or a function that takes a view element and returns a model element. The model element will be inserted in the model.
|
|
526
|
+
// @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
|
|
527
|
+
// @returns {Function} Conversion helper.
|
|
528
|
+
function upcastElementToElement( config ) {
|
|
529
|
+
config = cloneDeep( config );
|
|
530
|
+
|
|
531
|
+
const converter = prepareToElementConverter( config );
|
|
532
|
+
|
|
533
|
+
const elementName = getViewElementNameFromConfig( config.view );
|
|
534
|
+
const eventName = elementName ? 'element:' + elementName : 'element';
|
|
535
|
+
|
|
536
|
+
return dispatcher => {
|
|
537
|
+
dispatcher.on( eventName, converter, { priority: config.converterPriority || 'normal' } );
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// View element to model attribute conversion helper.
|
|
542
|
+
//
|
|
543
|
+
// See {@link ~UpcastHelpers#elementToAttribute `.elementToAttribute()` upcast helper} for examples.
|
|
544
|
+
//
|
|
545
|
+
// @param {Object} config Conversion configuration.
|
|
546
|
+
// @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
|
|
547
|
+
// @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
|
|
548
|
+
// the model attribute. `value` property may be set as a function that takes a view element and returns the value.
|
|
549
|
+
// If `String` is given, the model attribute value will be set to `true`.
|
|
550
|
+
// @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
|
|
551
|
+
// @returns {Function} Conversion helper.
|
|
552
|
+
function upcastElementToAttribute( config ) {
|
|
553
|
+
config = cloneDeep( config );
|
|
554
|
+
|
|
555
|
+
normalizeModelAttributeConfig( config );
|
|
556
|
+
|
|
557
|
+
const converter = prepareToAttributeConverter( config, false );
|
|
558
|
+
|
|
559
|
+
const elementName = getViewElementNameFromConfig( config.view );
|
|
560
|
+
const eventName = elementName ? 'element:' + elementName : 'element';
|
|
561
|
+
|
|
562
|
+
return dispatcher => {
|
|
563
|
+
dispatcher.on( eventName, converter, { priority: config.converterPriority || 'low' } );
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// View attribute to model attribute conversion helper.
|
|
568
|
+
//
|
|
569
|
+
// See {@link ~UpcastHelpers#attributeToAttribute `.attributeToAttribute()` upcast helper} for examples.
|
|
570
|
+
//
|
|
571
|
+
// @param {Object} config Conversion configuration.
|
|
572
|
+
// @param {String|Object} config.view Specifies which view attribute will be converted. If a `String` is passed,
|
|
573
|
+
// attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
|
|
574
|
+
// specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
|
|
575
|
+
// property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
|
|
576
|
+
// a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
|
|
577
|
+
// @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
|
|
578
|
+
// the model attribute. `value` property may be set as a function that takes a view element and returns the value.
|
|
579
|
+
// If `String` is given, the model attribute value will be same as view attribute value.
|
|
580
|
+
// @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
|
|
581
|
+
// @returns {Function} Conversion helper.
|
|
582
|
+
function upcastAttributeToAttribute( config ) {
|
|
583
|
+
config = cloneDeep( config );
|
|
584
|
+
|
|
585
|
+
let viewKey = null;
|
|
586
|
+
|
|
587
|
+
if ( typeof config.view == 'string' || config.view.key ) {
|
|
588
|
+
viewKey = normalizeViewAttributeKeyValueConfig( config );
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
normalizeModelAttributeConfig( config, viewKey );
|
|
592
|
+
|
|
593
|
+
const converter = prepareToAttributeConverter( config, true );
|
|
594
|
+
|
|
595
|
+
return dispatcher => {
|
|
596
|
+
dispatcher.on( 'element', converter, { priority: config.converterPriority || 'low' } );
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// View element to model marker conversion helper.
|
|
601
|
+
//
|
|
602
|
+
// See {@link ~UpcastHelpers#elementToMarker `.elementToMarker()` upcast helper} for examples.
|
|
603
|
+
//
|
|
604
|
+
// @param {Object} config Conversion configuration.
|
|
605
|
+
// @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
|
|
606
|
+
// @param {String|Function} config.model Name of the model marker, or a function that takes a view element and returns
|
|
607
|
+
// a model marker name.
|
|
608
|
+
// @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
|
|
609
|
+
// @returns {Function} Conversion helper.
|
|
610
|
+
function upcastElementToMarker( config ) {
|
|
611
|
+
config = cloneDeep( config );
|
|
612
|
+
|
|
613
|
+
normalizeElementToMarkerConfig( config );
|
|
614
|
+
|
|
615
|
+
return upcastElementToElement( config );
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// View data to model marker conversion helper.
|
|
619
|
+
//
|
|
620
|
+
// See {@link ~UpcastHelpers#dataToMarker} to learn more.
|
|
621
|
+
//
|
|
622
|
+
// @param {Object} config
|
|
623
|
+
// @param {String} config.view
|
|
624
|
+
// @param {Function} [config.model]
|
|
625
|
+
// @param {module:utils/priorities~PriorityString} [config.converterPriority='normal']
|
|
626
|
+
// @returns {Function} Conversion helper.
|
|
627
|
+
function upcastDataToMarker( config ) {
|
|
628
|
+
config = cloneDeep( config );
|
|
629
|
+
|
|
630
|
+
// Default conversion.
|
|
631
|
+
if ( !config.model ) {
|
|
632
|
+
config.model = name => {
|
|
633
|
+
return name ? config.view + ':' + name : config.view;
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const converterStart = prepareToElementConverter( normalizeDataToMarkerConfig( config, 'start' ) );
|
|
638
|
+
const converterEnd = prepareToElementConverter( normalizeDataToMarkerConfig( config, 'end' ) );
|
|
639
|
+
|
|
640
|
+
return dispatcher => {
|
|
641
|
+
dispatcher.on( 'element:' + config.view + '-start', converterStart, { priority: config.converterPriority || 'normal' } );
|
|
642
|
+
dispatcher.on( 'element:' + config.view + '-end', converterEnd, { priority: config.converterPriority || 'normal' } );
|
|
643
|
+
|
|
644
|
+
// Below is a hack that is needed to properly handle `converterPriority` for both elements and attributes.
|
|
645
|
+
// Attribute conversion needs to be performed *after* element conversion.
|
|
646
|
+
// This converter handles both element conversion and attribute conversion, which means that if a single
|
|
647
|
+
// `config.converterPriority` is used, it will lead to problems. For example, if the `'high'` priority is used,
|
|
648
|
+
// the attribute conversion will be performed before a lot of element upcast converters.
|
|
649
|
+
// On the other hand, we want to support `config.converterPriority` and converter overwriting.
|
|
650
|
+
//
|
|
651
|
+
// To make it work, we need to do some extra processing for priority for attribute converter.
|
|
652
|
+
// Priority `'low'` value should be the base value and then we will change it depending on `config.converterPriority` value.
|
|
653
|
+
//
|
|
654
|
+
// This hack probably would not be needed if attributes are upcasted separately.
|
|
655
|
+
//
|
|
656
|
+
const basePriority = priorities.get( 'low' );
|
|
657
|
+
const maxPriority = priorities.get( 'highest' );
|
|
658
|
+
const priorityFactor = priorities.get( config.converterPriority ) / maxPriority; // Number in range [ -1, 1 ].
|
|
659
|
+
|
|
660
|
+
dispatcher.on( 'element', upcastAttributeToMarker( config ), { priority: basePriority + priorityFactor } );
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// Function factory, returns a callback function which converts view attributes to a model marker.
|
|
665
|
+
//
|
|
666
|
+
// The converter looks for elements with `data-group-start-before`, `data-group-start-after`, `data-group-end-before`
|
|
667
|
+
// and `data-group-end-after` attributes and inserts `$marker` model elements before/after those elements.
|
|
668
|
+
// `group` part is specified in `config.view`.
|
|
669
|
+
//
|
|
670
|
+
// @param {Object} config
|
|
671
|
+
// @param {String} config.view
|
|
672
|
+
// @param {Function} [config.model]
|
|
673
|
+
// @returns {Function} Marker converter.
|
|
674
|
+
function upcastAttributeToMarker( config ) {
|
|
675
|
+
return ( evt, data, conversionApi ) => {
|
|
676
|
+
const attrName = `data-${ config.view }`;
|
|
677
|
+
|
|
678
|
+
// Check if any attribute for the given view item can be consumed before changing the conversion data
|
|
679
|
+
// and consuming view items with these attributes.
|
|
680
|
+
if (
|
|
681
|
+
!conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-end-after' } ) &&
|
|
682
|
+
!conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-start-after' } ) &&
|
|
683
|
+
!conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-end-before' } ) &&
|
|
684
|
+
!conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-start-before' } )
|
|
685
|
+
) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// This converter wants to add a model element, marking a marker, before/after an element (or maybe even group of elements).
|
|
690
|
+
// To do that, we can use `data.modelRange` which is set on an element (or a group of elements) that has been upcasted.
|
|
691
|
+
// But, if the processed view element has not been upcasted yet (it does not have been converted), we need to
|
|
692
|
+
// fire conversion for its children first, then we will have `data.modelRange` available.
|
|
693
|
+
if ( !data.modelRange ) {
|
|
694
|
+
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-end-after' } ) ) {
|
|
698
|
+
addMarkerElements( data.modelRange.end, data.viewItem.getAttribute( attrName + '-end-after' ).split( ',' ) );
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-start-after' } ) ) {
|
|
702
|
+
addMarkerElements( data.modelRange.end, data.viewItem.getAttribute( attrName + '-start-after' ).split( ',' ) );
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-end-before' } ) ) {
|
|
706
|
+
addMarkerElements( data.modelRange.start, data.viewItem.getAttribute( attrName + '-end-before' ).split( ',' ) );
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-start-before' } ) ) {
|
|
710
|
+
addMarkerElements( data.modelRange.start, data.viewItem.getAttribute( attrName + '-start-before' ).split( ',' ) );
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function addMarkerElements( position, markerViewNames ) {
|
|
714
|
+
for ( const markerViewName of markerViewNames ) {
|
|
715
|
+
const markerName = config.model( markerViewName, conversionApi );
|
|
716
|
+
const element = conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
|
|
717
|
+
|
|
718
|
+
conversionApi.writer.insert( element, position );
|
|
719
|
+
|
|
720
|
+
if ( data.modelCursor.isEqual( position ) ) {
|
|
721
|
+
data.modelCursor = data.modelCursor.getShiftedBy( 1 );
|
|
722
|
+
} else {
|
|
723
|
+
data.modelCursor = data.modelCursor._getTransformedByInsertion( position, 1 );
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
data.modelRange = data.modelRange._getTransformedByInsertion( position, 1 )[ 0 ];
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// Helper function for from-view-element conversion. Checks if `config.view` directly specifies converted view element's name
|
|
733
|
+
// and if so, returns it.
|
|
734
|
+
//
|
|
735
|
+
// @param {Object} config Conversion view config.
|
|
736
|
+
// @returns {String|null} View element name or `null` if name is not directly set.
|
|
737
|
+
function getViewElementNameFromConfig( viewConfig ) {
|
|
738
|
+
if ( typeof viewConfig == 'string' ) {
|
|
739
|
+
return viewConfig;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
if ( typeof viewConfig == 'object' && typeof viewConfig.name == 'string' ) {
|
|
743
|
+
return viewConfig.name;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
return null;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// Helper for to-model-element conversion. Takes a config object and returns a proper converter function.
|
|
750
|
+
//
|
|
751
|
+
// @param {Object} config Conversion configuration.
|
|
752
|
+
// @returns {Function} View to model converter.
|
|
753
|
+
function prepareToElementConverter( config ) {
|
|
754
|
+
const matcher = new Matcher( config.view );
|
|
755
|
+
|
|
756
|
+
return ( evt, data, conversionApi ) => {
|
|
757
|
+
const matcherResult = matcher.match( data.viewItem );
|
|
758
|
+
|
|
759
|
+
if ( !matcherResult ) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
const match = matcherResult.match;
|
|
764
|
+
|
|
765
|
+
// Force consuming element's name.
|
|
766
|
+
match.name = true;
|
|
767
|
+
|
|
768
|
+
if ( !conversionApi.consumable.test( data.viewItem, match ) ) {
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const modelElement = getModelElement( config.model, data.viewItem, conversionApi );
|
|
773
|
+
|
|
774
|
+
if ( !modelElement ) {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
if ( !conversionApi.safeInsert( modelElement, data.modelCursor ) ) {
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
conversionApi.consumable.consume( data.viewItem, match );
|
|
783
|
+
conversionApi.convertChildren( data.viewItem, modelElement );
|
|
784
|
+
conversionApi.updateConversionResult( modelElement, data );
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// Helper function for upcasting-to-element converter. Takes the model configuration, the converted view element
|
|
789
|
+
// and a writer instance and returns a model element instance to be inserted in the model.
|
|
790
|
+
//
|
|
791
|
+
// @param {String|Function|module:engine/model/element~Element} model Model conversion configuration.
|
|
792
|
+
// @param {module:engine/view/node~Node} input The converted view node.
|
|
793
|
+
// @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi The upcast conversion API.
|
|
794
|
+
function getModelElement( model, input, conversionApi ) {
|
|
795
|
+
if ( model instanceof Function ) {
|
|
796
|
+
return model( input, conversionApi );
|
|
797
|
+
} else {
|
|
798
|
+
return conversionApi.writer.createElement( model );
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// Helper function view-attribute-to-model-attribute helper. Normalizes `config.view` which was set as `String` or
|
|
803
|
+
// as an `Object` with `key`, `value` and `name` properties. Normalized `config.view` has is compatible with
|
|
804
|
+
// {@link module:engine/view/matcher~MatcherPattern}.
|
|
805
|
+
//
|
|
806
|
+
// @param {Object} config Conversion config.
|
|
807
|
+
// @returns {String} Key of the converted view attribute.
|
|
808
|
+
function normalizeViewAttributeKeyValueConfig( config ) {
|
|
809
|
+
if ( typeof config.view == 'string' ) {
|
|
810
|
+
config.view = { key: config.view };
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
const key = config.view.key;
|
|
814
|
+
let normalized;
|
|
815
|
+
|
|
816
|
+
if ( key == 'class' || key == 'style' ) {
|
|
817
|
+
const keyName = key == 'class' ? 'classes' : 'styles';
|
|
818
|
+
|
|
819
|
+
normalized = {
|
|
820
|
+
[ keyName ]: config.view.value
|
|
821
|
+
};
|
|
822
|
+
} else {
|
|
823
|
+
const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
|
|
824
|
+
|
|
825
|
+
normalized = {
|
|
826
|
+
attributes: {
|
|
827
|
+
[ key ]: value
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
if ( config.view.name ) {
|
|
833
|
+
normalized.name = config.view.name;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
config.view = normalized;
|
|
837
|
+
|
|
838
|
+
return key;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// Helper function that normalizes `config.model` in from-model-attribute conversion. `config.model` can be set
|
|
842
|
+
// as a `String`, an `Object` with only `key` property or an `Object` with `key` and `value` properties. Normalized
|
|
843
|
+
// `config.model` is an `Object` with `key` and `value` properties.
|
|
844
|
+
//
|
|
845
|
+
// @param {Object} config Conversion config.
|
|
846
|
+
// @param {String} viewAttributeKeyToCopy Key of the converted view attribute. If it is set, model attribute value
|
|
847
|
+
// will be equal to view attribute value.
|
|
848
|
+
function normalizeModelAttributeConfig( config, viewAttributeKeyToCopy = null ) {
|
|
849
|
+
const defaultModelValue = viewAttributeKeyToCopy === null ? true : viewElement => viewElement.getAttribute( viewAttributeKeyToCopy );
|
|
850
|
+
|
|
851
|
+
const key = typeof config.model != 'object' ? config.model : config.model.key;
|
|
852
|
+
const value = typeof config.model != 'object' || typeof config.model.value == 'undefined' ? defaultModelValue : config.model.value;
|
|
853
|
+
|
|
854
|
+
config.model = { key, value };
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// Helper for to-model-attribute conversion. Takes the model attribute name and conversion configuration and returns
|
|
858
|
+
// a proper converter function.
|
|
859
|
+
//
|
|
860
|
+
// @param {String} modelAttributeKey The key of the model attribute to set on a model node.
|
|
861
|
+
// @param {Object|Array.<Object>} config Conversion configuration. It is possible to provide multiple configurations in an array.
|
|
862
|
+
// @param {Boolean} shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
|
|
863
|
+
// on all elements in the range.
|
|
864
|
+
function prepareToAttributeConverter( config, shallow ) {
|
|
865
|
+
const matcher = new Matcher( config.view );
|
|
866
|
+
|
|
867
|
+
return ( evt, data, conversionApi ) => {
|
|
868
|
+
const match = matcher.match( data.viewItem );
|
|
869
|
+
|
|
870
|
+
// If there is no match, this callback should not do anything.
|
|
871
|
+
if ( !match ) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if ( onlyViewNameIsDefined( config.view, data.viewItem ) ) {
|
|
876
|
+
match.match.name = true;
|
|
877
|
+
} else {
|
|
878
|
+
// Do not test or consume `name` consumable.
|
|
879
|
+
delete match.match.name;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// Try to consume appropriate values from consumable values list.
|
|
883
|
+
if ( !conversionApi.consumable.test( data.viewItem, match.match ) ) {
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const modelKey = config.model.key;
|
|
888
|
+
const modelValue = typeof config.model.value == 'function' ?
|
|
889
|
+
config.model.value( data.viewItem, conversionApi ) : config.model.value;
|
|
890
|
+
|
|
891
|
+
// Do not convert if attribute building function returned falsy value.
|
|
892
|
+
if ( modelValue === null ) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// Since we are converting to attribute we need a range on which we will set the attribute.
|
|
897
|
+
// If the range is not created yet, let's create it by converting children of the current node first.
|
|
898
|
+
if ( !data.modelRange ) {
|
|
899
|
+
// Convert children and set conversion result as a current data.
|
|
900
|
+
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// Set attribute on current `output`. `Schema` is checked inside this helper function.
|
|
904
|
+
const attributeWasSet = setAttributeOn( data.modelRange, { key: modelKey, value: modelValue }, shallow, conversionApi );
|
|
905
|
+
|
|
906
|
+
// It may happen that a converter will try to set an attribute that is not allowed in the given context.
|
|
907
|
+
// In such a situation we cannot consume the attribute. See: https://github.com/ckeditor/ckeditor5/pull/9249#issuecomment-815658459.
|
|
908
|
+
if ( attributeWasSet ) {
|
|
909
|
+
conversionApi.consumable.consume( data.viewItem, match.match );
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// Helper function that checks if element name should be consumed in attribute converters.
|
|
915
|
+
//
|
|
916
|
+
// @param {Object} config Conversion view config.
|
|
917
|
+
// @returns {Boolean}
|
|
918
|
+
function onlyViewNameIsDefined( viewConfig, viewItem ) {
|
|
919
|
+
// https://github.com/ckeditor/ckeditor5-engine/issues/1786
|
|
920
|
+
const configToTest = typeof viewConfig == 'function' ? viewConfig( viewItem ) : viewConfig;
|
|
921
|
+
|
|
922
|
+
if ( typeof configToTest == 'object' && !getViewElementNameFromConfig( configToTest ) ) {
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
return !configToTest.classes && !configToTest.attributes && !configToTest.styles;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
// Helper function for to-model-attribute converter. Sets model attribute on given range. Checks {@link module:engine/model/schema~Schema}
|
|
930
|
+
// to ensure proper model structure.
|
|
931
|
+
//
|
|
932
|
+
// If any node on the given range has already defined an attribute with the same name, its value will not be updated.
|
|
933
|
+
//
|
|
934
|
+
// @param {module:engine/model/range~Range} modelRange Model range on which attribute should be set.
|
|
935
|
+
// @param {Object} modelAttribute Model attribute to set.
|
|
936
|
+
// @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi Conversion API.
|
|
937
|
+
// @param {Boolean} shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
|
|
938
|
+
// on all elements in the range.
|
|
939
|
+
// @returns {Boolean} `true` if attribute was set on at least one node from given `modelRange`.
|
|
940
|
+
function setAttributeOn( modelRange, modelAttribute, shallow, conversionApi ) {
|
|
941
|
+
let result = false;
|
|
942
|
+
|
|
943
|
+
// Set attribute on each item in range according to Schema.
|
|
944
|
+
for ( const node of Array.from( modelRange.getItems( { shallow } ) ) ) {
|
|
945
|
+
// Skip if not allowed.
|
|
946
|
+
if ( !conversionApi.schema.checkAttribute( node, modelAttribute.key ) ) {
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// Mark the node as consumed even if the attribute will not be updated because it's in a valid context (schema)
|
|
951
|
+
// and would be converted if the attribute wouldn't be present. See #8921.
|
|
952
|
+
result = true;
|
|
953
|
+
|
|
954
|
+
// Do not override the attribute if it's already present.
|
|
955
|
+
if ( node.hasAttribute( modelAttribute.key ) ) {
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
conversionApi.writer.setAttribute( modelAttribute.key, modelAttribute.value, node );
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
return result;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastElementToMarker()`
|
|
966
|
+
// function and converts it to a format that is supported by `upcastElementToElement()` function.
|
|
967
|
+
//
|
|
968
|
+
// @param {Object} config Conversion configuration.
|
|
969
|
+
function normalizeElementToMarkerConfig( config ) {
|
|
970
|
+
const oldModel = config.model;
|
|
971
|
+
|
|
972
|
+
config.model = ( viewElement, conversionApi ) => {
|
|
973
|
+
const markerName = typeof oldModel == 'string' ? oldModel : oldModel( viewElement, conversionApi );
|
|
974
|
+
|
|
975
|
+
return conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastDataToMarker()`
|
|
980
|
+
// function and converts it to a format that is supported by `upcastElementToElement()` function.
|
|
981
|
+
//
|
|
982
|
+
// @param {Object} config Conversion configuration.
|
|
983
|
+
function normalizeDataToMarkerConfig( config, type ) {
|
|
984
|
+
const configForElements = {};
|
|
985
|
+
|
|
986
|
+
// Upcast <markerGroup-start> and <markerGroup-end> elements.
|
|
987
|
+
configForElements.view = config.view + '-' + type;
|
|
988
|
+
|
|
989
|
+
configForElements.model = ( viewElement, conversionApi ) => {
|
|
990
|
+
const viewName = viewElement.getAttribute( 'name' );
|
|
991
|
+
const markerName = config.model( viewName, conversionApi );
|
|
992
|
+
|
|
993
|
+
return conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
return configForElements;
|
|
997
|
+
}
|