CKEDITOR.tools
Utility functions.
Filtering
Properties
array : arrayCKEDITOR.tools#arrayA set of array helpers.
object : objectCKEDITOR.tools#objectA set of object helpers.
style : styleCKEDITOR.tools#styleA set of functions for operations on styles.
-
The data URI of a transparent image. May be used e.g. in HTML as an image source or in CSS in
url().Defaults to
'data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==' -
Note: This regexp is deprecated, use CKEDITOR.tools.color to handle colors.
Defaults to
/hsla?\(\s*[0-9.]+\s*,\s*\d+%\s*,\s*\d+%\s*(?:,\s*[0-9.]+\s*)?\)/gi -
Note: This regexp is deprecated, use CKEDITOR.tools.color to handle colors.
Defaults to
/rgba?\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*(?:,\s*[0-9.]+\s*)?\)/gi
Methods
addFunction( fn, [ scope ] ) → NumberCKEDITOR.tools#addFunctionCreates a function reference that can be called later using callFunction. This approach is especially useful to make DOM attribute function calls to JavaScript-defined functions.
var ref = CKEDITOR.tools.addFunction( function() { alert( 'Hello!'); } ); CKEDITOR.tools.callFunction( ref ); // 'Hello!'Parameters
fn : FunctionThe function to be executed on call.
[ scope ] : ObjectThe object to have the context on
fnexecution.Returns
NumberA unique reference to be used in conjuction with callFunction.
arrayCompare( arrayA, arrayB ) → BooleanCKEDITOR.tools#arrayCompareCompares the elements of two arrays.
var a = [ 1, 'a', 3 ]; var b = [ 1, 3, 'a' ]; var c = [ 1, 'a', 3 ]; var d = [ 1, 'a', 3, 4 ]; alert( CKEDITOR.tools.arrayCompare( a, b ) ); // false alert( CKEDITOR.tools.arrayCompare( a, c ) ); // true alert( CKEDITOR.tools.arrayCompare( a, d ) ); // falseParameters
arrayA : ArrayAn array to be compared.
arrayB : ArrayThe other array to be compared.
Returns
Booleantrueif the arrays have the same length and their elements match.bind( func, obj, [ args ] ) → FunctionCKEDITOR.tools#bindCreates a function that will always execute in the context of a specified object.
var obj = { text: 'My Object' }; function alertText() { alert( this.text ); } var newFunc = CKEDITOR.tools.bind( alertText, obj ); newFunc(); // Alerts 'My Object'.Since 4.13.0 additional arguments can be bound to a function.
function logData( text, number1, number2 ) { console.log( text, number1, number2 ); } var newFunc = CKEDITOR.tools.bind( logData, null, 'Foo', 1 ); newFunc(); // Logs: 'Foo', 1, undefined. newFunc( 2 ); // Logs: 'Foo', 1, 2.Parameters
func : FunctionThe function to be executed.
obj : ObjectThe object to which the execution context will be bound.
[ args ] : *Arguments provided to the bound function when invoking the target function. Available since 4.13.0.
Returns
FunctionThe function that can be used to execute the
funcfunction in the context of the specifiedobjobject.buildStyleHtml( css ) → StringCKEDITOR.tools#buildStyleHtmlBuilds a HTML snippet from a set of
<style>/<link>.Parameters
css : String | ArrayEach of which are URLs (absolute) of a CSS file or a trunk of style text.
Returns
String
buildTableMap( table, startRow, startCell, endRow, endCell )CKEDITOR.tools#buildTableMapCreates a two-dimension array that reflects the actual layout of table cells, with cell spans, with mappings to the original
tdelements.It could also create a map for the specified fragment of the table.
Parameters
table : elementstartRow : NumberRow index from which the map should be created.
startCell : NumberCell index from which the map should be created.
endRow : NumberRow index to which the map should be created.
endCell : NumberCell index to which the map should be created.
callFunction( ref, params ) → MixedCKEDITOR.tools#callFunctionExecutes a function based on the reference created with addFunction.
var ref = CKEDITOR.tools.addFunction( function() { alert( 'Hello!'); } ); CKEDITOR.tools.callFunction( ref ); // 'Hello!'Parameters
ref : NumberThe function reference created with addFunction.
params : MixedAny number of parameters to be passed to the executed function.
Returns
MixedThe return value of the function.
capitalize( str, [ keepCase ] ) → StringCKEDITOR.tools#capitalizeTurns the first letter of a string to upper-case.
Parameters
str : String[ keepCase ] : BooleanKeep the case of 2nd to last letter.
Returns
String
since 4.4.0
checkIfAnyArrayItemMatches( arr, regexp ) → BooleanCKEDITOR.tools#checkIfAnyArrayItemMatchesChecks if any of the
arritems match the provided regular expression.Parameters
arr : ArrayThe array whose items will be checked.
regexp : RegExpThe regular expression.
Returns
BooleanReturns
truefor the first occurrence of the search pattern.since 4.4.0
checkIfAnyObjectPropertyMatches( obj, regexp ) → BooleanCKEDITOR.tools#checkIfAnyObjectPropertyMatchesChecks if any of the
objproperties match the provided regular expression.Parameters
obj : ObjectThe object whose properties will be checked.
regexp : RegExpThe regular expression.
Returns
BooleanReturns
truefor the first occurrence of the search pattern.clone( object ) → ObjectCKEDITOR.tools#cloneCreates a deep copy of an object.
Note: Recursive references are not supported.
var obj = { name: 'John', cars: { Mercedes: { color: 'blue' }, Porsche: { color: 'red' } } }; var clone = CKEDITOR.tools.clone( obj ); clone.name = 'Paul'; clone.cars.Porsche.color = 'silver'; alert( obj.name ); // 'John' alert( clone.name ); // 'Paul' alert( obj.cars.Porsche.color ); // 'red' alert( clone.cars.Porsche.color ); // 'silver'Parameters
object : ObjectThe object to be cloned.
Returns
ObjectThe object clone.
Converts an array to an object by rewriting array items to object properties.
var arr = [ 'foo', 'bar', 'foo' ]; console.log( CKEDITOR.tools.convertArrayToObject( arr ) ); // -> { foo: true, bar: true } console.log( CKEDITOR.tools.convertArrayToObject( arr, 1 ) ); // -> { foo: 1, bar: 1 }Parameters
arr : ArrayThe array to be converted to an object.
[ fillWith ] : ObjectSet each property of an object to
fillWithvalue.Defaults to
trueConverts a bytes array into a a Base64-encoded string.
Parameters
bytesArray : Number[]An array that stores 1 byte in each cell as an Integer number.
Returns
StringBase64-encoded string that represents input bytes.
Converts a hex string to an array containing 1 byte in each cell. Bytes are represented as Integer numbers.
Parameters
hexString : StringContains an input string which represents bytes, e.g.
"08A11D8ADA2B".Returns
Number[]Bytes stored in a form of Integer numbers, e.g.
[ 8, 161, 29, 138, 218, 43 ].convertRgbToHex( styleText ) → StringCKEDITOR.tools#convertRgbToHexFinds and converts
rgb(x,x,x)color definition into a given string to hexadecimal notation.Note: For handling RGB string only (not within text) it is recommended to use CKEDITOR.tools.color to create color instance and CKEDITOR.tools.color.getHex method to get its hexadecimal representation:
var color = new CKEDITOR.tools.color( 'rgb( 225, 225, 225 )' ); // Create color instance. console.log( color.getHex() ); // #FFFFFFParameters
styleText : StringThe style data (or just a string containing RGB colors) to be converted.
Returns
StringThe style data with RGB colors converted to hexadecimal equivalents.
convertToPx( cssLength ) → Number | StringCKEDITOR.tools#convertToPxConverts the specified CSS length value to the calculated pixel length inside this page.
Since 4.11.0 it also returns negative values.
Note: Percentage-based value is left intact.
Parameters
cssLength : StringCSS length value.
Returns
Number | StringA number representing the length in pixels or a string with a percentage value.
Makes fast (shallow) copy of an object. This method is faster than clone which does a deep copy of an object (including arrays).
Parameters
source : ObjectThe object to be copied.
Returns
ObjectCopy of
source.createClass( definition ) → FunctionCKEDITOR.tools#createClassClass creation based on prototype inheritance which supports the following features:
- Static fields
- Private fields
- Public (prototype) fields
- Chainable base class constructor
Parameters
definition : ObjectThe class definition object.
Returns
FunctionA class-like JavaScript function.
cssLength( length )CKEDITOR.tools#cssLengthAppends the
pxlength unit to the size value if it is missing.var cssLength = CKEDITOR.tools.cssLength; cssLength( 42 ); // '42px' cssLength( '42' ); // '42px' cssLength( '42px' ); // '42px' cssLength( '42%' ); // '42%' cssLength( 'bold' ); // 'bold' cssLength( false ); // '' cssLength( NaN ); // ''Parameters
length : Number | String | Boolean
cssStyleToDomStyle( cssName ) → StringCKEDITOR.tools#cssStyleToDomStyleTransforms a CSS property name to its relative DOM style name.
alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // 'backgroundColor' alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // 'cssFloat'Parameters
cssName : StringThe CSS property name.
Returns
StringThe transformed name.
cssVendorPrefix( property, value, [ asString ] ) → Object | StringCKEDITOR.tools#cssVendorPrefixGenerates an object or a string containing vendor-specific and vendor-free CSS properties.
CKEDITOR.tools.cssVendorPrefix( 'border-radius', '0', true ); // On Firefox: '-moz-border-radius:0;border-radius:0' // On Chrome: '-webkit-border-radius:0;border-radius:0'Parameters
property : StringThe CSS property name.
value : StringThe CSS value.
[ asString ] : BooleanIf
true, then the returned value will be a CSS string.Defaults to
falseReturns
Object | StringThe object containing CSS properties or its stringified version.
Returns a new debounced version of the passed function that will postpone its execution until the given milliseconds have elapsed since the last time it was invoked.
Parameters
func : FunctionThe function to be executed.
[ milliseconds ] : NumberThe amount of time (in milliseconds) to wait to fire the function execution.
Defaults to
0Returns
Function
defer( fn ) → FunctionCKEDITOR.tools#deferCreates a "deferred" function which will not run immediately, but rather runs as soon as the interpreter’s call stack is empty. Behaves much like
window.setTimeoutwith a delay.Note: The return value of the original function will be lost.
Parameters
fn : FunctionThe callee function.
Returns
FunctionThe new deferred function.
Enables HTML5 elements for older browsers (IE8) in the passed document.
In IE8 this method can also be executed on a document fragment.
Note: This method has to be used in the
<head>section of the document.Parameters
doc : ObjectNative
DocumentorDocumentFragmentin which the elements will be enabled.[ withAppend ] : BooleanWhether to append created elements to the
doc.Returns an escaped CSS selector.
CSS.escape()is used if defined, leading digit is escaped otherwise.Parameters
selector : StringA CSS selector to escape.
Returns
StringAn escaped selector.
Creates an events buffer instance.
See the input method's documentation for example code listings.
Parameters
minInterval : NumberThe minimum interval between
outputcalls in milliseconds.output : FunctionThe function that will be executed as
output.[ contextObj ] : ObjectThe object used as context to the listener call (the
thisobject).Returns
event
extend( target, source, [ overwrite ], [ properties ] ) → ObjectCKEDITOR.tools#extendCopies the properties from one object to another. By default, properties already present in the target object are not overwritten.
// Create the sample object. var myObject = { prop1: true }; // Extend the above object with two properties. CKEDITOR.tools.extend( myObject, { prop2: true, prop3: true } ); // Alert 'prop1', 'prop2' and 'prop3'. for ( var p in myObject ) alert( p );Parameters
target : ObjectThe object to be extended.
source : ObjectThe object(s) from properties will be copied. Any number of objects can be passed to this function.
[ overwrite ] : BooleanIf
trueis specified, it indicates that properties already present in the target object could be overwritten by subsequent objects.[ properties ] : ObjectOnly properties within the specified names list will be received from the source object.
Returns
ObjectThe extended object (target).
Tries to fix the
document.domainof the current document to match the parent window domain, avoiding "Same Origin" policy issues. This is an Internet Explorer only requirement.Returns
Booleantrueif the current domain is already good or if it has been fixed successfully.genKey( subKey ) → StringCKEDITOR.tools#genKeyGenerates a combined key from a series of params.
var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' ); alert( key ); // 'key1-key2-key3'.Parameters
subKey : StringOne or more strings used as subkeys.
Returns
String
since 4.10.0
getAbsoluteRectPosition( window, rect ) → CKEDITORCKEDITOR.tools#getAbsoluteRectPositionConverts relative positions inside a DOM rectangle into absolute ones using the given window as context. "Absolute" here means in relation to the upper-left corner of the topmost viewport.
Parameters
window : CKEDITORThe window containing an element for which the rectangle is passed.
rect : CKEDITORA rectangle with a relative position.
Returns
CKEDITORA rectangle with an absolute position.
Returns the value of the cookie with a given name or
nullif the cookie is not found.Parameters
name : String
Returns
String
Returns the CSRF token value. The value is a hash stored in
document.cookieunder theckCsrfTokenkey. The CSRF token can be used to secure the communication between the web browser and the server, i.e. for the file upload feature in the editor.Returns
String
Finds the index of the first element in an array for which the
compareFunctionreturnstrue.CKEDITOR.tools.getIndex( [ 1, 2, 4, 3, 5 ], function( el ) { return el >= 3; } ); // 2Parameters
array : ArrayArray to search in.
compareFunction : FunctionCompare function.
Returns
NumberThe index of the first matching element or
-1if none matches.Detects which mouse button generated a given DOM event.
Parameters
evt : event | EventDOM event. Since 4.11.3 a native
MouseEventinstance can be passed.Returns
BooleanReturns a number indicating the mouse button or
falseif the mouse button cannot be determined.getNextId() → StringCKEDITOR.tools#getNextIdGets a unique ID for CKEditor interface elements. It returns a string with the "cke_" prefix and a consecutive number.
alert( CKEDITOR.tools.getNextId() ); // (e.g.) 'cke_1' alert( CKEDITOR.tools.getNextId() ); // 'cke_2'Returns
StringA unique ID.
getNextNumber() → NumberCKEDITOR.tools#getNextNumberGets a unique number for this CKEDITOR execution session. It returns consecutive numbers starting from 1.
alert( CKEDITOR.tools.getNextNumber() ); // (e.g.) 1 alert( CKEDITOR.tools.getNextNumber() ); // 2Returns
NumberA unique number.
Finds all span elements styled with the given property in the editor contents.
Parameters
property : StringCSS property which will be used in query.
source : elementThe element to be searched.
Returns
ArrayReturns an array of CKEDITOR.dom.elements.
getUniqueId() → StringCKEDITOR.tools#getUniqueIdGets a universally unique ID. It returns a random string compliant with ISO/IEC 11578:1996, without dashes, with the "e" prefix to make sure that the ID does not start with a number.
Returns
StringA global unique ID.
htmlDecode( The ) → StringCKEDITOR.tools#htmlDecodeDecodes HTML entities that browsers tend to encode when used in text nodes.
alert( CKEDITOR.tools.htmlDecode( '<a & b >' ) ); // '<a & b >'Read more about chosen entities in the research.
Parameters
The : Stringstring to be decoded.
Returns
StringThe decoded string.
htmlDecodeAttr( text ) → StringCKEDITOR.tools#htmlDecodeAttrDecodes HTML entities that browsers tend to encode when used in attributes.
alert( CKEDITOR.tools.htmlDecodeAttr( '<a " b>' ) ); // '<a " b>'Since CKEditor 4.5.0 this method simply executes htmlDecode which covers all necessary entities.
Parameters
text : StringThe text to be decoded.
Returns
StringThe decoded text.
htmlEncode( text ) → StringCKEDITOR.tools#htmlEncodeReplaces special HTML characters in a string with their relative HTML entity values.
alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // 'A > B & C < D'Parameters
text : StringThe string to be encoded.
Returns
StringThe encoded string.
htmlEncodeAttr( The ) → StringCKEDITOR.tools#htmlEncodeAttrReplaces special HTML characters in HTMLElement attribute with their relative HTML entity values.
alert( CKEDITOR.tools.htmlEncodeAttr( '<a " b >' ) ); // '<a " b >'Parameters
The : Stringattribute value to be encoded.
Returns
StringThe encoded value.
indexOf( array, value ) → NumberCKEDITOR.tools#indexOfReturns the index of an element in an array.
var letters = [ 'a', 'b', 0, 'c', false ]; alert( CKEDITOR.tools.indexOf( letters, '0' ) ); // -1 because 0 !== '0' alert( CKEDITOR.tools.indexOf( letters, false ) ); // 4 because 0 !== falseParameters
array : ArrayThe array to be searched.
value : Object | FunctionThe element to be found. This can be an evaluation function which receives a single parameter call for each entry in the array, returning
trueif the entry matches.Returns
NumberThe (zero-based) index of the first entry that matches the entry, or
-1if not found.