Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit e20e133

Browse files
authored
Merge pull request #1413 from ckeditor/t/1408
Feature: Introduce `ElementDefinition#priority` property which allows specifying the priority of created element during the downcast conversion. Closes #1408.
2 parents 86521df + cdd9c92 commit e20e133

File tree

7 files changed

+93
-64
lines changed

7 files changed

+93
-64
lines changed

src/conversion/conversion.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class Conversion {
164164
* conversion.elementToElement( { model: 'paragraph', view: 'p' } );
165165
*
166166
* // Override other converters by specifying converter definition with higher priority.
167-
* conversion.elementToElement( { model: 'paragraph', view: 'div', priority: 'high' } );
167+
* conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
168168
*
169169
* // View specified as an object instead of a string.
170170
* conversion.elementToElement( {
@@ -237,7 +237,7 @@ export default class Conversion {
237237
upcastElementToElement( {
238238
model,
239239
view,
240-
priority: definition.priority
240+
converterPriority: definition.converterPriority
241241
} )
242242
);
243243
}
@@ -251,7 +251,7 @@ export default class Conversion {
251251
* conversion.attributeToElement( { model: 'bold', view: 'strong' } );
252252
*
253253
* // Override other converters by specifying converter definition with higher priority.
254-
* conversion.attributeToElement( { model: 'bold', view: 'b', priority: 'high' } );
254+
* conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
255255
*
256256
* // View specified as an object instead of a string.
257257
* conversion.attributeToElement( {
@@ -579,7 +579,7 @@ export default class Conversion {
579579
* Any view element matching `upcastAlso` will also be converted to model. If `model` describes multiple values, `upcastAlso`
580580
* is an object that assigns those values (`upcastAlso` object keys) to {@link module:engine/view/matcher~MatcherPattern}s
581581
* (`upcastAlso` object values).
582-
* @property {module:utils/priorities~PriorityString} [priority] Conversion priority.
582+
* @property {module:utils/priorities~PriorityString} [converterPriority] Converter priority.
583583
*/
584584

585585
// Helper function for `Conversion` `.add()` method.

src/conversion/downcast-converters.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import cloneDeep from '@ckeditor/ckeditor5-utils/src/lib/lodash/cloneDeep';
2626
*
2727
* downcastElementToElement( { model: 'paragraph', view: 'p' } );
2828
*
29-
* downcastElementToElement( { model: 'paragraph', view: 'div', priority: 'high' } );
29+
* downcastElementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
3030
*
3131
* downcastElementToElement( {
3232
* model: 'fancyParagraph',
@@ -55,7 +55,7 @@ export function downcastElementToElement( config ) {
5555
config.view = _normalizeToElementConfig( config.view, 'container' );
5656

5757
return dispatcher => {
58-
dispatcher.on( 'insert:' + config.model, insertElement( config.view ), { priority: config.priority || 'normal' } );
58+
dispatcher.on( 'insert:' + config.model, insertElement( config.view ), { priority: config.converterPriority || 'normal' } );
5959
};
6060
}
6161

@@ -67,7 +67,7 @@ export function downcastElementToElement( config ) {
6767
*
6868
* downcastAttributeToElement( { model: 'bold', view: 'strong' } );
6969
*
70-
* downcastAttributeToElement( { model: 'bold', view: 'b', priority: 'high' } );
70+
* downcastAttributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
7171
*
7272
* downcastAttributeToElement( {
7373
* model: 'invert',
@@ -123,7 +123,7 @@ export function downcastElementToElement( config ) {
123123
* @param {module:engine/view/elementdefinition~ElementDefinition|Function|Object} config.view View element definition or a function
124124
* that takes model attribute value and view writer as parameters and returns a view attribute element. If `config.model.values` is
125125
* given, `config.view` should be an object assigning values from `config.model.values` to view element definitions or functions.
126-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
126+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
127127
* @returns {Function} Conversion helper.
128128
*/
129129
export function downcastAttributeToElement( config ) {
@@ -147,7 +147,7 @@ export function downcastAttributeToElement( config ) {
147147
const elementCreator = _getFromAttributeCreator( config );
148148

149149
return dispatcher => {
150-
dispatcher.on( eventName, wrap( elementCreator ), { priority: config.priority || 'normal' } );
150+
dispatcher.on( eventName, wrap( elementCreator ), { priority: config.converterPriority || 'normal' } );
151151
};
152152
}
153153

@@ -159,7 +159,7 @@ export function downcastAttributeToElement( config ) {
159159
*
160160
* downcastAttributeToAttribute( { model: 'source', view: 'src' } );
161161
*
162-
* downcastAttributeToAttribute( { model: 'source', view: 'href', priority: 'high' } );
162+
* downcastAttributeToAttribute( { model: 'source', view: 'href', converterPriority: 'high' } );
163163
*
164164
* downcastAttributeToAttribute( {
165165
* model: {
@@ -201,7 +201,7 @@ export function downcastAttributeToElement( config ) {
201201
* array of `String`s. If `key` is `'style'`, `value` is an object with key-value pairs. In other cases, `value` is a `String`.
202202
* If `config.model.values` is set, `config.view` should be an object assigning values from `config.model.values` to
203203
* `{ key, value }` objects or a functions.
204-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
204+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
205205
* @returns {Function} Conversion helper.
206206
*/
207207
export function downcastAttributeToAttribute( config ) {
@@ -225,7 +225,7 @@ export function downcastAttributeToAttribute( config ) {
225225
const elementCreator = _getFromAttributeCreator( config );
226226

227227
return dispatcher => {
228-
dispatcher.on( eventName, changeAttribute( elementCreator ), { priority: config.priority || 'normal' } );
228+
dispatcher.on( eventName, changeAttribute( elementCreator ), { priority: config.converterPriority || 'normal' } );
229229
};
230230
}
231231

@@ -238,7 +238,7 @@ export function downcastAttributeToAttribute( config ) {
238238
*
239239
* downcastMarkerToElement( { model: 'search', view: 'marker-search' } );
240240
*
241-
* downcastMarkerToElement( { model: 'search', view: 'search-result', priority: 'high' } );
241+
* downcastMarkerToElement( { model: 'search', view: 'search-result', converterPriority: 'high' } );
242242
*
243243
* downcastMarkerToElement( {
244244
* model: 'search',
@@ -272,7 +272,7 @@ export function downcastAttributeToAttribute( config ) {
272272
* @param {String} config.model Name of the model marker (or model marker group) to convert.
273273
* @param {module:engine/view/elementdefinition~ElementDefinition|Function} config.view View element definition or a function
274274
* that takes model marker data as a parameter and returns view ui element.
275-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
275+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
276276
* @returns {Function} Conversion helper.
277277
*/
278278
export function downcastMarkerToElement( config ) {
@@ -281,8 +281,8 @@ export function downcastMarkerToElement( config ) {
281281
config.view = _normalizeToElementConfig( config.view, 'ui' );
282282

283283
return dispatcher => {
284-
dispatcher.on( 'addMarker:' + config.model, insertUIElement( config.view ), { priority: config.priority || 'normal' } );
285-
dispatcher.on( 'removeMarker:' + config.model, removeUIElement( config.view ), { priority: config.priority || 'normal' } );
284+
dispatcher.on( 'addMarker:' + config.model, insertUIElement( config.view ), { priority: config.converterPriority || 'normal' } );
285+
dispatcher.on( 'removeMarker:' + config.model, removeUIElement( config.view ), { priority: config.converterPriority || 'normal' } );
286286
};
287287
}
288288

@@ -307,7 +307,7 @@ export function downcastMarkerToElement( config ) {
307307
*
308308
* downcastMarkerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
309309
*
310-
* downcastMarkerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, priority: 'high' } );
310+
* downcastMarkerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
311311
*
312312
* downcastMarkerToHighlight( {
313313
* model: 'comment',
@@ -331,14 +331,14 @@ export function downcastMarkerToElement( config ) {
331331
* @param {String} config.model Name of the model marker (or model marker group) to convert.
332332
* @param {module:engine/conversion/downcast-converters~HighlightDescriptor|Function} config.view Highlight descriptor
333333
* which will be used for highlighting or a function that takes model marker data as a parameter and returns a highlight descriptor.
334-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
334+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
335335
* @returns {Function} Conversion helper.
336336
*/
337337
export function downcastMarkerToHighlight( config ) {
338338
return dispatcher => {
339-
dispatcher.on( 'addMarker:' + config.model, highlightText( config.view ), { priority: config.priority || 'normal' } );
340-
dispatcher.on( 'addMarker:' + config.model, highlightElement( config.view ), { priority: config.priority || 'normal' } );
341-
dispatcher.on( 'removeMarker:' + config.model, removeHighlight( config.view ), { priority: config.priority || 'normal' } );
339+
dispatcher.on( 'addMarker:' + config.model, highlightText( config.view ), { priority: config.converterPriority || 'normal' } );
340+
dispatcher.on( 'addMarker:' + config.model, highlightElement( config.view ), { priority: config.converterPriority || 'normal' } );
341+
dispatcher.on( 'removeMarker:' + config.model, removeHighlight( config.view ), { priority: config.converterPriority || 'normal' } );
342342
};
343343
}
344344

@@ -370,14 +370,19 @@ function _createViewElementFromDefinition( viewElementDefinition, viewWriter, vi
370370
}
371371

372372
let element;
373+
const attributes = Object.assign( {}, viewElementDefinition.attributes );
373374

374375
if ( viewElementType == 'container' ) {
375-
element = viewWriter.createContainerElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
376+
element = viewWriter.createContainerElement( viewElementDefinition.name, attributes );
376377
} else if ( viewElementType == 'attribute' ) {
377-
element = viewWriter.createAttributeElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
378+
const options = {
379+
priority: viewElementDefinition.priority || ViewAttributeElement.DEFAULT_PRIORITY
380+
};
381+
382+
element = viewWriter.createAttributeElement( viewElementDefinition.name, attributes, options );
378383
} else {
379384
// 'ui'.
380-
element = viewWriter.createUIElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
385+
element = viewWriter.createUIElement( viewElementDefinition.name, attributes );
381386
}
382387

383388
if ( viewElementDefinition.styles ) {

src/conversion/upcast-converters.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import cloneDeep from '@ckeditor/ckeditor5-utils/src/lib/lodash/cloneDeep';
2626
*
2727
* upcastElementToElement( { view: 'p', model: 'paragraph' } );
2828
*
29-
* upcastElementToElement( { view: 'p', model: 'paragraph', priority: 'high' } );
29+
* upcastElementToElement( { view: 'p', model: 'paragraph', converterPriority: 'high' } );
3030
*
3131
* upcastElementToElement( {
3232
* view: {
@@ -52,7 +52,7 @@ import cloneDeep from '@ckeditor/ckeditor5-utils/src/lib/lodash/cloneDeep';
5252
* @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
5353
* @param {String|module:engine/model/element~Element|Function} config.model Name of the model element, a model element
5454
* instance or a function that takes a view element and returns a model element. The model element will be inserted in the model.
55-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
55+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
5656
* @returns {Function} Conversion helper.
5757
*/
5858
export function upcastElementToElement( config ) {
@@ -64,7 +64,7 @@ export function upcastElementToElement( config ) {
6464
const eventName = elementName ? 'element:' + elementName : 'element';
6565

6666
return dispatcher => {
67-
dispatcher.on( eventName, converter, { priority: config.priority || 'normal' } );
67+
dispatcher.on( eventName, converter, { priority: config.converterPriority || 'normal' } );
6868
};
6969
}
7070

@@ -78,7 +78,7 @@ export function upcastElementToElement( config ) {
7878
*
7979
* upcastElementToAttribute( { view: 'strong', model: 'bold' } );
8080
*
81-
* upcastElementToAttribute( { view: 'strong', model: 'bold', priority: 'high' } );
81+
* upcastElementToAttribute( { view: 'strong', model: 'bold', converterPriority: 'high' } );
8282
*
8383
* upcastElementToAttribute( {
8484
* view: {
@@ -130,7 +130,7 @@ export function upcastElementToElement( config ) {
130130
* @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
131131
* the model attribute. `value` property may be set as a function that takes a view element and returns the value.
132132
* If `String` is given, the model attribute value will be set to `true`.
133-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
133+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
134134
* @returns {Function} Conversion helper.
135135
*/
136136
export function upcastElementToAttribute( config ) {
@@ -144,7 +144,7 @@ export function upcastElementToAttribute( config ) {
144144
const eventName = elementName ? 'element:' + elementName : 'element';
145145

146146
return dispatcher => {
147-
dispatcher.on( eventName, converter, { priority: config.priority || 'normal' } );
147+
dispatcher.on( eventName, converter, { priority: config.converterPriority || 'normal' } );
148148
};
149149
}
150150

@@ -160,7 +160,7 @@ export function upcastElementToAttribute( config ) {
160160
*
161161
* upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source' } );
162162
*
163-
* upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source', priority: 'normal' } );
163+
* upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source', converterPriority: 'normal' } );
164164
*
165165
* upcastAttributeToAttribute( {
166166
* view: {
@@ -209,7 +209,7 @@ export function upcastElementToAttribute( config ) {
209209
* @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
210210
* the model attribute. `value` property may be set as a function that takes a view element and returns the value.
211211
* If `String` is given, the model attribute value will be same as view attribute value.
212-
* @param {module:utils/priorities~PriorityString} [config.priority='low'] Converter priority.
212+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
213213
* @returns {Function} Conversion helper.
214214
*/
215215
export function upcastAttributeToAttribute( config ) {
@@ -226,7 +226,7 @@ export function upcastAttributeToAttribute( config ) {
226226
const converter = _prepareToAttributeConverter( config );
227227

228228
return dispatcher => {
229-
dispatcher.on( 'element', converter, { priority: config.priority || 'low' } );
229+
dispatcher.on( 'element', converter, { priority: config.converterPriority || 'low' } );
230230
};
231231
}
232232

@@ -240,7 +240,7 @@ export function upcastAttributeToAttribute( config ) {
240240
*
241241
* upcastElementToMarker( { view: 'marker-search', model: 'search' } );
242242
*
243-
* upcastElementToMarker( { view: 'marker-search', model: 'search', priority: 'high' } );
243+
* upcastElementToMarker( { view: 'marker-search', model: 'search', converterPriority: 'high' } );
244244
*
245245
* upcastElementToMarker( {
246246
* view: 'marker-search',
@@ -263,7 +263,7 @@ export function upcastAttributeToAttribute( config ) {
263263
* @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
264264
* @param {String|Function} config.model Name of the model marker, or a function that takes a view element and returns
265265
* a model marker name.
266-
* @param {module:utils/priorities~PriorityString} [config.priority='normal'] Converter priority.
266+
* @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
267267
* @returns {Function} Conversion helper.
268268
*/
269269
export function upcastElementToMarker( config ) {

src/view/elementdefinition.jsdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@
5555
* Value under that key must be a string.
5656
* @property {Object} [attributes] Object with key-value pairs representing attributes. Each object key represents
5757
* attribute name. Value under that key must be a string.
58+
* @property {Number} [priority] Element's {@link module:engine/view/attributeelement~AttributeElement#priority priority}.
5859
*/

tests/conversion/conversion.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ describe( 'Conversion', () => {
125125
test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
126126
} );
127127

128-
it( 'config.priority is defined', () => {
128+
it( 'config.converterPriority is defined', () => {
129129
conversion.elementToElement( { model: 'paragraph', view: 'p' } );
130-
conversion.elementToElement( { model: 'paragraph', view: 'div', priority: 'high' } );
130+
conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
131131

132132
test( '<div>Foo</div>', '<paragraph>Foo</paragraph>' );
133133
test( '<p>Foo</p>', '<paragraph>Foo</paragraph>', '<div>Foo</div>' );
@@ -224,9 +224,9 @@ describe( 'Conversion', () => {
224224
test( '<p><strong>Foo</strong> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
225225
} );
226226

227-
it( 'config.priority is defined', () => {
227+
it( 'config.converterPriority is defined', () => {
228228
conversion.attributeToElement( { model: 'bold', view: 'strong' } );
229-
conversion.attributeToElement( { model: 'bold', view: 'b', priority: 'high' } );
229+
conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
230230

231231
test( '<p><b>Foo</b></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>' );
232232
test( '<p><strong>Foo</strong></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>', '<p><b>Foo</b></p>' );

0 commit comments

Comments
 (0)