Skip to content

Commit 35f64d7

Browse files
committed
Merge branch 't/12416' into major
2 parents 0dfdca2 + eb4ce93 commit 35f64d7

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ New Features:
1212
* [#12169](http://dev.ckeditor.com/ticket/12169): Introduce [`editor.drop`](http://docs.ckeditor.com/#!/api/CKEDITOR.editor-event-drop), [`editor.dragstart`](http://docs.ckeditor.com/#!/api/CKEDITOR.editor-event-dragstart) and [`editor.dragend`](http://docs.ckeditor.com/#!/api/CKEDITOR.editor-event-dragend) events.
1313
* [#11583](http://dev.ckeditor.com/ticket/11583): Added support for HTML5 `required` attribute in various form elements. Thanks to [Steven Busse](https://github.com/sbusse)!
1414
* [#12143](http://dev.ckeditor.com/ticket/12143): Added [`config.floatSpacePreferRight`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-floatSpacePreferRight) configuration option that switches the alignment of the floating toolbar. Thanks to [InvisibleBacon](http://github.com/InvisibleBacon)!
15+
* [#12416](http://dev.ckeditor.com/ticket/12416): Added [`widgetDefinition.upcastPriority`](http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget.definition-property-upcastPriority) property which gives more control over widgets upcasting order to the widget author.
1516

1617
Fixed Issues:
1718

core/tools.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
return true;
6969
},
7070

71+
/**
72+
* Finds index of the first element in array for which the `compareFunction` returns `true`.
73+
*
74+
* CKEDITOR.tools.getIndex( [ 1, 2, 4, 3, 5 ], function( el ) {
75+
* return el >= 3;
76+
* } ); // 2
77+
*
78+
* @since 4.5
79+
* @param {Array} array Array to search in.
80+
* @param {Function} compareFunction Compare function.
81+
* @returns {Number} The index of the first matching element or `-1` if none matches.
82+
*/
83+
getIndex: function( arr, compareFunction ) {
84+
for ( var i = 0; i < arr.length; ++i ) {
85+
if ( compareFunction( arr[ i ] ) )
86+
return i;
87+
}
88+
return -1;
89+
},
90+
7191
/**
7292
* Creates a deep copy of an object.
7393
*

plugins/widget/plugin.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,20 +1736,36 @@
17361736

17371737
function addWidgetProcessors( widgetsRepo, widgetDef ) {
17381738
var upcast = widgetDef.upcast,
1739-
upcasts;
1739+
upcasts,
1740+
priority = widgetDef.upcastPriority || 10;
17401741

17411742
if ( !upcast )
17421743
return;
17431744

17441745
// Multiple upcasts defined in string.
17451746
if ( typeof upcast == 'string' ) {
17461747
upcasts = upcast.split( ',' );
1747-
while ( upcasts.length )
1748-
widgetsRepo._.upcasts.push( [ widgetDef.upcasts[ upcasts.pop() ], widgetDef.name ] );
1748+
while ( upcasts.length ) {
1749+
addUpcast( widgetDef.upcasts[ upcasts.pop() ], widgetDef.name, priority );
1750+
}
17491751
}
17501752
// Single rule which is automatically activated.
1751-
else
1752-
widgetsRepo._.upcasts.push( [ upcast, widgetDef.name ] );
1753+
else {
1754+
addUpcast( upcast, widgetDef.name, priority );
1755+
}
1756+
1757+
function addUpcast( upcast, name, priority ) {
1758+
// Find index of the first higher (in terms of value) priority upcast.
1759+
var index = CKEDITOR.tools.getIndex( widgetsRepo._.upcasts, function( element ) {
1760+
return element[ 2 ] > priority;
1761+
} );
1762+
// Add at the end if it is the highest priority so far.
1763+
if ( index < 0 ) {
1764+
index = widgetsRepo._.upcasts.length;
1765+
}
1766+
1767+
widgetsRepo._.upcasts.splice( index, 0, [ upcast, name, priority ] );
1768+
}
17531769
}
17541770

17551771
function blurWidget( widgetsRepo, widget ) {
@@ -3453,6 +3469,14 @@
34533469
* @property {Object} upcasts
34543470
*/
34553471

3472+
/**
3473+
* The {@link #upcast} method(s) priority. The upcast with lower priority number will be called before
3474+
* the one with higher number. The default priority is `10`.
3475+
*
3476+
* @since 4.5
3477+
* @property {Number} [upcastPriority=10]
3478+
*/
3479+
34563480
/**
34573481
* The function to be used to downcast this widget or
34583482
* a name of the downcast option from the {@link #downcasts} object.

tests/core/tools.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ bender.test(
5858
assert.isFalse( CKEDITOR.tools.isArray( window.x ) );
5959
},
6060

61+
'test getIndex - not found': function() {
62+
assert.areSame( -1, CKEDITOR.tools.getIndex( [ 1, 2, 3 ], function( el ) { return el == 4; } ) );
63+
},
64+
65+
'test getIndex - found first': function() {
66+
assert.areSame( 2, CKEDITOR.tools.getIndex( [ 0, 1, 2, 2, 2, 3, 2, 2 ], function( el ) { return el == 2; } ) );
67+
},
68+
69+
'test getIndex - found on last position': function() {
70+
assert.areSame( 2, CKEDITOR.tools.getIndex( [ 0, 1, 2 ], function( el ) { return el == 2; } ) );
71+
},
72+
6173
test_htmlEncode1: function() {
6274
assert.areSame( '&lt;b&gt;Test&lt;/b&gt;', CKEDITOR.tools.htmlEncode( '<b>Test</b>' ) );
6375
},

tests/plugins/widget/widgetsrepoapi.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,49 @@
157157
assert.isTrue( true );
158158
},
159159

160+
'test widgets.add - upcast priorities': function() {
161+
var editor = this.editor,
162+
bot = this.editorBot,
163+
upcastCalled = '';
164+
165+
editor.widgets.add( 'prioritiesTest1', {
166+
upcast: function() {
167+
upcastCalled += '1';
168+
}
169+
} );
170+
171+
editor.widgets.add( 'prioritiesTest2', {
172+
upcastPriority: 5,
173+
upcast: function() {
174+
upcastCalled += '2';
175+
}
176+
} );
177+
178+
editor.widgets.add( 'prioritiesTest3', {
179+
upcastPriority: 5,
180+
upcast: function() {
181+
upcastCalled += '3';
182+
}
183+
} );
184+
185+
editor.widgets.add( 'prioritiesTest4', {
186+
upcastPriority: 15,
187+
upcast: function() {
188+
upcastCalled += '4';
189+
}
190+
} );
191+
192+
editor.widgets.add( 'prioritiesTest5', {
193+
upcast: function() {
194+
upcastCalled += '5';
195+
}
196+
} );
197+
198+
bot.setData( '<p>foo</p>', function() {
199+
assert.areSame( '23154', upcastCalled );
200+
} );
201+
},
202+
160203
'test widgets.wrapElement - inline': function() {
161204
var editor = this.editor;
162205

0 commit comments

Comments
 (0)