Skip to content

Commit

Permalink
Merge pull request #3125 from ckeditor/t/3123
Browse files Browse the repository at this point in the history
`CKEDITOR.tools` improvements
  • Loading branch information
f1ames authored Jun 15, 2019
2 parents b3f6b49 + e95b968 commit 4d72da5
Show file tree
Hide file tree
Showing 97 changed files with 326 additions and 188 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ Fixed Issues:
* [#1469](https://github.com/ckeditor/ckeditor-dev/issues/1469): Fixed: Trying to get data from nested editable inside freshly pasted widget throws an error.
* [#2923](https://github.com/ckeditor/ckeditor-dev/issues/2923): Fixed: CSS `windowtext` color is not correctly recognized by [`CKEDITOR.tools.style.parse`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_style_parse.html) functions.
* [#2235](https://github.com/ckeditor/ckeditor-dev/issues/2235): Fixed: [Image](https://ckeditor.com/cke4/addon/image) in table cell has an empty URL field when edited from context menu opened by right-click when [Table Selection](https://ckeditor.com/cke4/addon/tableselection) plugin is in use.
* [#3120](https://github.com/ckeditor/ckeditor-dev/issues/3120): [IE8] Fixed: [`CKEDITOR.tools.extend`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tool.html#method-extend) function doesn't work with [`DontEnum`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Properties) object property attribute.

API Changes:

* [#1496](https://github.com/ckeditor/ckeditor-dev/issues/1496): [Balloon Toolbar](https://ckeditor.com/cke4/addon/balloontoolbar) exposed methods [`CKEDITOR.ui.balloonToolbar.reposition`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_ui_balloonToolbar.html#reposition) and [`CKEDITOR.ui.balloonToolbarView.reposition`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_ui_balloonToolbarView.html#reposition).
* [#2021](https://github.com/ckeditor/ckeditor-dev/issues/2021): Add [`CKEDITOR.dom.documentFragment.find`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dom_documentFragment.html#method-find) and [`CKEDITOR.dom.documentFragment.findOne`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dom_documentFragment.html#method-findOne) methods.
* [#2700](https://github.com/ckeditor/ckeditor-dev/issues/2700): Added the [`CKEDITOR.tools.array.find`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_array.html#method-find) function.
* [#3123](https://github.com/ckeditor/ckeditor-dev/issues/3123): Added the [`CKEDITOR.tools.object.keys`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_object.html#method-keys) function.
* [#3123](https://github.com/ckeditor/ckeditor-dev/issues/3123): Added the [`CKEDITOR.tools.object.entries`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_object.html#method-entries) function.
* [#3123](https://github.com/ckeditor/ckeditor-dev/issues/3123): Added the [`CKEDITOR.tools.object.values`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_object.html#method-values) function.
* [#2821](https://github.com/ckeditor/ckeditor-dev/issues/2821): The [`CKEDITOR.template#source`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_template.html#property-source) property can now be a function, meaning that it could return changed template values during the runtime. Thanks to [Jacek Pulit](https://github.com/jacek-pulit)!
* [#2598](https://github.com/ckeditor/ckeditor-dev/issues/2598): Added [`CKEDITOR.plugins.pagebreak.createElement`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_plugins_pagebreak.html#method-createElement) method allowing to create [Page Break](https://ckeditor.com/cke4/addon/pagebreak) plugin [`CKEDITOR.dom.element`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dom_element.html) instance.
* [#2748](https://github.com/ckeditor/ckeditor-dev/issues/2748): Enhance errors thrown while creating editor on a nonexistent element or while trying to instantiate second editor on the same element. Thanks to [Byran Zaugg](https://github.com/blzaugg)!
Expand All @@ -37,6 +41,7 @@ Other Changes:

* [#2741](https://github.com/ckeditor/ckeditor-dev/issues/2721): Replaced deprecated `arguments.callee` calls with named function expressions.
* [#2924](https://github.com/ckeditor/ckeditor-dev/issues/2924): Marked [`CKEDITOR.tools.style.parse.border`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_style_border.html) as deprecated in favor of [`CKEDITOR.tools.style.border.fromCssRule`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_style_border.html#static-method-fromCssRule) function.
* [#3132](https://github.com/ckeditor/ckeditor-dev/issues/2924): Marked [`CKEDITOR.tools.objectKeys`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tool.html#method-objectKeys) as deprecated in favor of [`CKEDITOR.tools.object.keys`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_object.html#method-keys).

## CKEditor 4.11.4

Expand Down
2 changes: 1 addition & 1 deletion core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@
}

// Destroy filters attached to the editor (#1722).
CKEDITOR.tools.array.forEach( CKEDITOR.tools.objectKeys( filters ), function( id ) {
CKEDITOR.tools.array.forEach( CKEDITOR.tools.object.keys( filters ), function( id ) {
var filter = filters[ id ];
if ( self === filter.editor ) {
filter.destroy();
Expand Down
6 changes: 3 additions & 3 deletions core/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
// NOTE: data-cke-* assigned elements are preserved only when filter is used with
// htmlDataProcessor.toHtml because we don't want to protect them when outputting data
// (toDataFormat).
if ( toHtml && el.name == 'span' && ~CKEDITOR.tools.objectKeys( el.attributes ).join( '|' ).indexOf( 'data-cke-' ) )
if ( toHtml && el.name == 'span' && ~CKEDITOR.tools.object.keys( el.attributes ).join( '|' ).indexOf( 'data-cke-' ) )
return;

processRetVal = processElement( that, el, toBeRemoved, filterOpts );
Expand Down Expand Up @@ -1061,7 +1061,7 @@

rules[ styleDef.element ] = rule = {
styles: styleDef.styles,
requiredStyles: styleDef.styles && CKEDITOR.tools.objectKeys( styleDef.styles )
requiredStyles: styleDef.styles && CKEDITOR.tools.object.keys( styleDef.styles )
};

if ( attrs ) {
Expand All @@ -1070,7 +1070,7 @@
rule.requiredClasses = rule.classes;
delete attrs[ 'class' ];
rule.attributes = attrs;
rule.requiredAttributes = attrs && CKEDITOR.tools.objectKeys( attrs );
rule.requiredAttributes = attrs && CKEDITOR.tools.object.keys( attrs );
}

return rules;
Expand Down
122 changes: 104 additions & 18 deletions core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,19 @@
propertiesList = arguments[ argsLength - 1 ];
argsLength -= 2;
}

for ( var i = 1; i < argsLength; i++ ) {
var source = arguments[ i ];
for ( var propertyName in source ) {
var source = arguments[ i ] || {};

CKEDITOR.tools.array.forEach( CKEDITOR.tools.object.keys( source ), function( propertyName ) {
// Only copy existed fields if in overwrite mode.
if ( overwrite === true || target[ propertyName ] == null ) {
// Only copy specified fields if list is provided.
// Only copy specified fields if list is provided.
if ( !propertiesList || ( propertyName in propertiesList ) )
target[ propertyName ] = source[ propertyName ];

}
}

} );
}

return target;
Expand Down Expand Up @@ -1129,21 +1131,12 @@
},

/**
* Returns an array of passed object's keys.
*
* console.log( CKEDITOR.tools.objectKeys( { foo: 1, bar: false } );
* // -> [ 'foo', 'bar' ]
*
* @inheritdoc CKEDITOR.tools.object#keys
* @since 4.1
* @param {Object} obj
* @returns {Array} Object's keys.
* @deprecated 4.12.0 Use {@link CKEDITOR.tools.object#keys} instead.
*/
objectKeys: function( obj ) {
var keys = [];
for ( var i in obj )
keys.push( i );

return keys;
return CKEDITOR.tools.object.keys( obj );
},

/**
Expand Down Expand Up @@ -2086,6 +2079,99 @@
* @member CKEDITOR.tools
*/
object: {

/**
* List of ECMA3 object properties with obsolete
* [DontEnum](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Properties)
* attribute.
*
* @member CKEDITOR.tools.object
* @private
* @since 4.12.0
*/
DONT_ENUMS: [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],

/**
* Returns an array of key-value pairs using enumerable string-keyed
* object properties.
*
* ```javascript
* console.log( CKEDITOR.tools.object.entries( { foo: 1, bar: false } );
* // -> [ [ 'foo', 1 ], [ 'bar', false ] ]
* ```
*
* @since 4.12.0
* @member CKEDITOR.tools.object
* @param {Object} obj
* @returns {Array} Object's key-value pairs.
*/
entries: function( obj ) {
return CKEDITOR.tools.array.map( CKEDITOR.tools.object.keys( obj ), function( key ) {
return [ key, obj[ key ] ];
} );
},

/**
* Returns an array of passed object enumerable values.
*
* ```javascript
* console.log( CKEDITOR.tools.object.values( { foo: 1, bar: false } );
* // -> [ 1, false ]
* ```
*
* @since 4.12.0
* @member CKEDITOR.tools.object
* @param {Object} obj
* @returns {Array} Object's values.
*/
values: function( obj ) {
return CKEDITOR.tools.array.map( CKEDITOR.tools.object.keys( obj ), function( key ) {
return obj[ key ];
} );
},

/**
* Returns an array of passed object keys.
*
* ```javascript
* console.log( CKEDITOR.tools.object.keys( { foo: 1, bar: false } );
* // -> [ 'foo', 'bar' ]
* ```
*
* @since 4.12.0
* @member CKEDITOR.tools.object
* @param {Object} obj
* @returns {Array} Object's keys.
*/
keys: function( obj ) {
var hasOwnProperty = Object.prototype.hasOwnProperty,
keys = [],
dontEnums = CKEDITOR.tools.object.DONT_ENUMS;

for ( var prop in obj ) {
keys.push( prop );
}

// Fix don't enum bug for IE < 9 browsers (#3120).
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 ) {
for ( var i = 0; i < dontEnums.length; i++ ) {
if ( hasOwnProperty.call( obj, dontEnums[ i ] ) ) {
keys.push( dontEnums[ i ] );
}
}
}

return keys;
},

/**
* Returns the first key from `obj` which has a given `value`.
*
Expand Down Expand Up @@ -2153,7 +2239,7 @@
copy1 = tools.clone( obj1 ),
copy2 = tools.clone( obj2 );

tools.array.forEach( tools.objectKeys( copy2 ), function( key ) {
tools.array.forEach( tools.object.keys( copy2 ), function( key ) {
if ( typeof copy2[ key ] === 'object' && typeof copy1[ key ] === 'object' ) {
copy1[ key ] = tools.object.merge( copy1[ key ], copy2[ key ] );
} else {
Expand Down
2 changes: 1 addition & 1 deletion plugins/autocomplete/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@
}

function encodeItem( item ) {
return CKEDITOR.tools.array.reduce( CKEDITOR.tools.objectKeys( item ), function( cur, key ) {
return CKEDITOR.tools.array.reduce( CKEDITOR.tools.object.keys( item ), function( cur, key ) {
cur[ key ] = CKEDITOR.tools.htmlEncode( item[ key ] );
return cur;
}, {} );
Expand Down
2 changes: 1 addition & 1 deletion plugins/balloontoolbar/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@
*/
CKEDITOR.ui.balloonToolbarView.prototype.renderItems = function( items ) {
var output = [],
keys = CKEDITOR.tools.objectKeys( items ),
keys = CKEDITOR.tools.object.keys( items ),
groupStarted = false;

// When we rerender toolbar we want to clear focusable in case of removing some items.
Expand Down
4 changes: 2 additions & 2 deletions plugins/clipboard/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2659,7 +2659,7 @@
return false;
}

CKEDITOR.tools.array.forEach( CKEDITOR.tools.objectKeys( this._.data ), function( type ) {
CKEDITOR.tools.array.forEach( CKEDITOR.tools.object.keys( this._.data ), function( type ) {
typesToCheck[ type ] = 1;
} );

Expand Down Expand Up @@ -3073,7 +3073,7 @@
*/
_applyDataComment: function( content, data ) {
var customData = '';
if ( data && CKEDITOR.tools.objectKeys( data ).length ) {
if ( data && CKEDITOR.tools.object.keys( data ).length ) {
customData = '<!--cke-data:' + encodeURIComponent( JSON.stringify( data ) ) + '-->';
}
return customData + ( content && content.length ? content : '' );
Expand Down
2 changes: 1 addition & 1 deletion plugins/codesnippet/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// We might escape special regex chars below, but we expect that there
// should be no crazy values used as lang keys.
editor._.codesnippet.langsRegex = new RegExp( '(?:^|\\s)language-(' +
CKEDITOR.tools.objectKeys( langs ).join( '|' ) + ')(?:\\s|$)' );
CKEDITOR.tools.object.keys( langs ).join( '|' ) + ')(?:\\s|$)' );
};

editor.once( 'pluginsLoaded', function() {
Expand Down
2 changes: 1 addition & 1 deletion plugins/colorbutton/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CKEDITOR.plugins.add( 'colorbutton', {
var background = tools.style.parse.background( element.styles.background );

// We return true only if background specifies **only** color property, and there's only one background directive.
return background.color && tools.objectKeys( background ).length === 1;
return background.color && tools.object.keys( background ).length === 1;
},
right: function( element ) {
var style = new CKEDITOR.style( editor.config.colorButton_backStyle, {
Expand Down
4 changes: 2 additions & 2 deletions plugins/imagebase/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}

function getWidgetsWithFeature( widgets, feature ) {
return CKEDITOR.tools.array.reduce( CKEDITOR.tools.objectKeys( widgets ), function( featuredWidgets, id ) {
return CKEDITOR.tools.array.reduce( CKEDITOR.tools.object.keys( widgets ), function( featuredWidgets, id ) {
var widget = widgets[ id ];
if ( hasWidgetFeature( widget, feature ) ) {
featuredWidgets.push( widget );
Expand Down Expand Up @@ -702,7 +702,7 @@
* @member CKEDITOR.plugins.imagebase.imageWidgetDefinition
* @property {String}
*/
definition.upcast = CKEDITOR.tools.objectKeys( definition.upcasts ).join( ',' );
definition.upcast = CKEDITOR.tools.object.keys( definition.upcasts ).join( ',' );

return definition;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/link/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@

return {
set: set,
removed: CKEDITOR.tools.objectKeys( removed )
removed: CKEDITOR.tools.object.keys( removed )
};
},

Expand Down
8 changes: 4 additions & 4 deletions plugins/pastefromword/filter/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
// Create style stack for td/th > font if only class
// and style attributes are present. Such markup is produced by Excel.
if ( CKEDITOR.dtd.tr[ element.parent.name ] &&
CKEDITOR.tools.arrayCompare( CKEDITOR.tools.objectKeys( element.attributes ), [ 'class', 'style' ] ) ) {
CKEDITOR.tools.arrayCompare( CKEDITOR.tools.object.keys( element.attributes ), [ 'class', 'style' ] ) ) {

Style.createStyleStack( element, filter, editor );
} else {
Expand Down Expand Up @@ -319,7 +319,7 @@

// In case parent div has only align attr, move it to the table element (https://dev.ckeditor.com/ticket/16811).
if ( parent.name && parent.name === 'div' && parent.attributes.align &&
tools.objectKeys( parent.attributes ).length === 1 && parent.children.length === 1 ) {
tools.object.keys( parent.attributes ).length === 1 && parent.children.length === 1 ) {

// If align is the only attribute of parent.
element.attributes.align = parent.attributes.align;
Expand Down Expand Up @@ -669,7 +669,7 @@
delete styles[ 'text-indent' ];
}

var keys = tools.objectKeys( styles );
var keys = tools.object.keys( styles );

for ( var i = 0; i < keys.length; i++ ) {
var styleName = keys[ i ].toLowerCase(),
Expand Down Expand Up @@ -787,7 +787,7 @@
'background'
],
style = tools.parseCssText( element.attributes.style ),
keys = tools.objectKeys( style ),
keys = tools.object.keys( style ),
sortedKeys = [],
nonSortedKeys = [];

Expand Down
4 changes: 2 additions & 2 deletions plugins/widget/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3925,8 +3925,8 @@
};

function deepCompare( left, right ) {
var leftKeys = CKEDITOR.tools.objectKeys( left ),
rightKeys = CKEDITOR.tools.objectKeys( right );
var leftKeys = CKEDITOR.tools.object.keys( left ),
rightKeys = CKEDITOR.tools.object.keys( right );

if ( leftKeys.length !== rightKeys.length ) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion tests/_benderjs/ckeditor/static/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
*/
objToArray: function( obj ) {
var tools = CKEDITOR.tools;
return tools.array.map( tools.objectKeys( obj ), function( key ) {
return tools.array.map( tools.object.keys( obj ), function( key ) {
return obj[ key ];
} );
},
Expand Down
2 changes: 1 addition & 1 deletion tests/adapters/jquery/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bender.test( {
},

assertEditorInstances: function( expected ) {
arrayAssert.itemsAreEqual( expected, CKEDITOR.tools.objectKeys( CKEDITOR.instances ), 'Editors instances list should match with expected.' );
arrayAssert.itemsAreEqual( expected, CKEDITOR.tools.object.keys( CKEDITOR.instances ), 'Editors instances list should match with expected.' );
},

destroyAll: function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/config/manual/extraplugins.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extraPlugins: 'basicstyles, toolbar',
on: {
instanceReady: function() {
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.objectKeys( this.plugins ) + '</p>' );
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.object.keys( this.plugins ) + '</p>' );
}
}
} );
Expand Down
2 changes: 1 addition & 1 deletion tests/core/config/manual/plugins.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
plugins: 'wysiwygarea,basicstyles, toolbar ',
on: {
instanceReady: function() {
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.objectKeys( this.plugins ) + '</p>' );
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.object.keys( this.plugins ) + '</p>' );
}
}
} );
Expand Down
2 changes: 1 addition & 1 deletion tests/core/config/manual/pluginsarray.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h2>removePlugins</h2>
plugins: 'wysiwygarea,toolbar',
on: {
instanceReady: function() {
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.objectKeys( this.plugins ) + '</p>' );
this.setData( '<p>I have following plugins loaded: ' + CKEDITOR.tools.object.keys( this.plugins ) + '</p>' );
}
}
}
Expand Down
Loading

0 comments on commit 4d72da5

Please sign in to comment.