9dee4b27b1cce1b44964fd554def07d6d6c9b915.svn-base
20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* @fileOverview Defines the {@link CKEDITOR.tools} object, which contains
* utility functions.
*/
(function()
{
var functions = [];
CKEDITOR.on( 'reset', function()
{
functions = [];
});
/**
* Utility functions.
* @namespace
* @example
*/
CKEDITOR.tools =
{
/**
* Compare the elements of two arrays.
* @param {Array} arrayA An array to be compared.
* @param {Array} arrayB The other array to be compared.
* @returns {Boolean} "true" is the arrays have the same lenght and
* their elements match.
* @example
* 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 ) ); // false
*/
arrayCompare : function( arrayA, arrayB )
{
if ( !arrayA && !arrayB )
return true;
if ( !arrayA || !arrayB || arrayA.length != arrayB.length )
return false;
for ( var i = 0 ; i < arrayA.length ; i++ )
{
if ( arrayA[ i ] != arrayB[ i ] )
return false;
}
return true;
},
/**
* Creates a deep copy of an object.
* Attention: there is no support for recursive references.
* @param {Object} object The object to be cloned.
* @returns {Object} The object clone.
* @example
* 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
*/
clone : function( obj )
{
var clone;
// Array.
if ( obj && ( obj instanceof Array ) )
{
clone = [];
for ( var i = 0 ; i < obj.length ; i++ )
clone[ i ] = this.clone( obj[ i ] );
return clone;
}
// "Static" types.
if ( obj === null
|| ( typeof( obj ) != 'object' )
|| ( obj instanceof String )
|| ( obj instanceof Number )
|| ( obj instanceof Boolean )
|| ( obj instanceof Date )
|| ( obj instanceof RegExp) )
{
return obj;
}
// Objects.
clone = new obj.constructor();
for ( var propertyName in obj )
{
var property = obj[ propertyName ];
clone[ propertyName ] = this.clone( property );
}
return clone;
},
/**
* Turn the first letter of string to upper-case.
* @param {String} str
*/
capitalize: function( str )
{
return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase();
},
/**
* Copy the properties from one object to another. By default, properties
* already present in the target object <strong>are not</strong> overwritten.
* @param {Object} target The object to be extended.
* @param {Object} source[,souce(n)] The objects from which copy
* properties. Any number of objects can be passed to this function.
* @param {Boolean} [overwrite] If 'true' is specified it indicates that
* properties already present in the target object could be
* overwritten by subsequent objects.
* @param {Object} [properties] Only properties within the specified names
* list will be received from the source object.
* @returns {Object} the extended object (target).
* @example
* // 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 );
*/
extend : function( target )
{
var argsLength = arguments.length,
overwrite, propertiesList;
if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
argsLength--;
else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
{
propertiesList = arguments [ argsLength -1 ];
argsLength-=2;
}
for ( var i = 1 ; i < argsLength ; i++ )
{
var source = arguments[ i ];
for ( var propertyName in source )
{
// Only copy existed fields if in overwrite mode.
if ( overwrite === true || target[ propertyName ] == undefined )
{
// Only copy specified fields if list is provided.
if ( !propertiesList || ( propertyName in propertiesList ) )
target[ propertyName ] = source[ propertyName ];
}
}
}
return target;
},
/**
* Creates an object which is an instance of a class which prototype is a
* predefined object. All properties defined in the source object are
* automatically inherited by the resulting object, including future
* changes to it.
* @param {Object} source The source object to be used as the prototype for
* the final object.
* @returns {Object} The resulting copy.
*/
prototypedCopy : function( source )
{
var copy = function()
{};
copy.prototype = source;
return new copy();
},
/**
* Checks if an object is an Array.
* @param {Object} object The object to be checked.
* @type Boolean
* @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
* @example
* alert( CKEDITOR.tools.isArray( [] ) ); // "true"
* alert( CKEDITOR.tools.isArray( 'Test' ) ); // "false"
*/
isArray : function( object )
{
return ( !!object && object instanceof Array );
},
/**
* Whether the object contains no properties of it's own.
* @param object
*/
isEmpty : function ( object )
{
for ( var i in object )
{
if ( object.hasOwnProperty( i ) )
return false;
}
return true;
},
/**
* Transforms a CSS property name to its relative DOM style name.
* @param {String} cssName The CSS property name.
* @returns {String} The transformed name.
* @example
* alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // "backgroundColor"
* alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // "cssFloat"
*/
cssStyleToDomStyle : ( function()
{
var test = document.createElement( 'div' ).style;
var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
: ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
: 'float';
return function( cssName )
{
if ( cssName == 'float' )
return cssFloat;
else
{
return cssName.replace( /-./g, function( match )
{
return match.substr( 1 ).toUpperCase();
});
}
};
} )(),
/**
* Build the HTML snippet of a set of <style>/<link>.
* @param css {String|Array} Each of which are url (absolute) of a CSS file or
* a trunk of style text.
*/
buildStyleHtml : function ( css )
{
css = [].concat( css );
var item, retval = [];
for ( var i = 0; i < css.length; i++ )
{
item = css[ i ];
// Is CSS style text ?
if ( /@import|[{}]/.test(item) )
retval.push('<style>' + item + '</style>');
else
retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');
}
return retval.join( '' );
},
/**
* Replace special HTML characters in a string with their relative HTML
* entity values.
* @param {String} text The string to be encoded.
* @returns {String} The encode string.
* @example
* alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // "A &gt; B &amp; C &lt; D"
*/
htmlEncode : function( text )
{
var standard = function( text )
{
var span = new CKEDITOR.dom.element( 'span' );
span.setText( text );
return span.getHtml();
};
var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
function( text )
{
// #3874 IE and Safari encode line-break into <br>
return standard( text ).replace( /<br>/gi, '\n' );
} :
standard;
var fix2 = ( standard( '>' ) == '>' ) ?
function( text )
{
// WebKit does't encode the ">" character, which makes sense, but
// it's different than other browsers.
return fix1( text ).replace( />/g, '>' );
} :
fix1;
var fix3 = ( standard( ' ' ) == ' ' ) ?
function( text )
{
// #3785 IE8 changes spaces (>= 2) to
return fix2( text ).replace( / /g, ' ' );
} :
fix2;
this.htmlEncode = fix3;
return this.htmlEncode( text );
},
/**
* Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values.
* @param {String} The attribute's value to be encoded.
* @returns {String} The encode value.
* @example
* element.setAttribute( 'title', '<a " b >' );
* alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) ); // ">a " b <"
*/
htmlEncodeAttr : function( text )
{
return text.replace( /"/g, '"' ).replace( /</g, '<' ).replace( />/g, '>' );
},
/**
* Gets a unique number for this CKEDITOR execution session. It returns
* progressive numbers starting at 1.
* @function
* @returns {Number} A unique number.
* @example
* alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "1" (e.g.)
* alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "2"
*/
getNextNumber : (function()
{
var last = 0;
return function()
{
return ++last;
};
})(),
/**
* Gets a unique ID for CKEditor's interface elements. It returns a
* string with the "cke_" prefix and a progressive number.
* @function
* @returns {String} A unique ID.
* @example
* alert( CKEDITOR.tools.<b>getNextId()</b> ); // "cke_1" (e.g.)
* alert( CKEDITOR.tools.<b>getNextId()</b> ); // "cke_2"
*/
getNextId : function()
{
return 'cke_' + this.getNextNumber();
},
/**
* Creates a function override.
* @param {Function} originalFunction The function to be overridden.
* @param {Function} functionBuilder A function that returns the new
* function. The original function reference will be passed to this
* function.
* @returns {Function} The new function.
* @example
* var example =
* {
* myFunction : function( name )
* {
* alert( 'Name: ' + name );
* }
* };
*
* example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
* {
* return function( name )
* {
* alert( 'Override Name: ' + name );
* myFunctionOriginal.call( this, name );
* };
* });
*/
override : function( originalFunction, functionBuilder )
{
return functionBuilder( originalFunction );
},
/**
* Executes a function after specified delay.
* @param {Function} func The function to be executed.
* @param {Number} [milliseconds] The amount of time (millisecods) to wait
* to fire the function execution. Defaults to zero.
* @param {Object} [scope] The object to hold the function execution scope
* (the "this" object). By default the "window" object.
* @param {Object|Array} [args] A single object, or an array of objects, to
* pass as arguments to the function.
* @param {Object} [ownerWindow] The window that will be used to set the
* timeout. By default the current "window".
* @returns {Object} A value that can be used to cancel the function execution.
* @example
* CKEDITOR.tools.<b>setTimeout(
* function()
* {
* alert( 'Executed after 2 seconds' );
* },
* 2000 )</b>;
*/
setTimeout : function( func, milliseconds, scope, args, ownerWindow )
{
if ( !ownerWindow )
ownerWindow = window;
if ( !scope )
scope = ownerWindow;
return ownerWindow.setTimeout(
function()
{
if ( args )
func.apply( scope, [].concat( args ) ) ;
else
func.apply( scope ) ;
},
milliseconds || 0 );
},
/**
* Remove spaces from the start and the end of a string. The following
* characters are removed: space, tab, line break, line feed.
* @function
* @param {String} str The text from which remove the spaces.
* @returns {String} The modified string without the boundary spaces.
* @example
* alert( CKEDITOR.tools.trim( ' example ' ); // "example"
*/
trim : (function()
{
// We are not using \s because we don't want "non-breaking spaces" to be caught.
var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
return function( str )
{
return str.replace( trimRegex, '' ) ;
};
})(),
/**
* Remove spaces from the start (left) of a string. The following
* characters are removed: space, tab, line break, line feed.
* @function
* @param {String} str The text from which remove the spaces.
* @returns {String} The modified string excluding the removed spaces.
* @example
* alert( CKEDITOR.tools.ltrim( ' example ' ); // "example "
*/
ltrim : (function()
{
// We are not using \s because we don't want "non-breaking spaces" to be caught.
var trimRegex = /^[ \t\n\r]+/g;
return function( str )
{
return str.replace( trimRegex, '' ) ;
};
})(),
/**
* Remove spaces from the end (right) of a string. The following
* characters are removed: space, tab, line break, line feed.
* @function
* @param {String} str The text from which remove the spaces.
* @returns {String} The modified string excluding the removed spaces.
* @example
* alert( CKEDITOR.tools.ltrim( ' example ' ); // " example"
*/
rtrim : (function()
{
// We are not using \s because we don't want "non-breaking spaces" to be caught.
var trimRegex = /[ \t\n\r]+$/g;
return function( str )
{
return str.replace( trimRegex, '' ) ;
};
})(),
/**
* Returns the index of an element in an array.
* @param {Array} array The array to be searched.
* @param {Object} entry The element to be found.
* @returns {Number} The (zero based) index of the first entry that matches
* the entry, or -1 if not found.
* @example
* 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 !== false
*/
indexOf :
// #2514: We should try to use Array.indexOf if it does exist.
( Array.prototype.indexOf ) ?
function( array, entry )
{
return array.indexOf( entry );
}
:
function( array, entry )
{
for ( var i = 0, len = array.length ; i < len ; i++ )
{
if ( array[ i ] === entry )
return i;
}
return -1;
},
/**
* Creates a function that will always execute in the context of a
* specified object.
* @param {Function} func The function to be executed.
* @param {Object} obj The object to which bind the execution context.
* @returns {Function} The function that can be used to execute the
* "func" function in the context of "obj".
* @example
* var obj = { text : 'My Object' };
*
* function alertText()
* {
* alert( this.text );
* }
*
* var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;
* newFunc(); // Alerts "My Object".
*/
bind : function( func, obj )
{
return function() { return func.apply( obj, arguments ); };
},
/**
* Class creation based on prototype inheritance, with supports of the
* following features:
* <ul>
* <li> Static fields </li>
* <li> Private fields </li>
* <li> Public (prototype) fields </li>
* <li> Chainable base class constructor </li>
* </ul>
* @param {Object} definition The class definition object.
* @returns {Function} A class-like JavaScript function.
*/
createClass : function( definition )
{
var $ = definition.$,
baseClass = definition.base,
privates = definition.privates || definition._,
proto = definition.proto,
statics = definition.statics;
if ( privates )
{
var originalConstructor = $;
$ = function()
{
// Create (and get) the private namespace.
var _ = this._ || ( this._ = {} );
// Make some magic so "this" will refer to the main
// instance when coding private functions.
for ( var privateName in privates )
{
var priv = privates[ privateName ];
_[ privateName ] =
( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
}
originalConstructor.apply( this, arguments );
};
}
if ( baseClass )
{
$.prototype = this.prototypedCopy( baseClass.prototype );
$.prototype.constructor = $;
$.prototype.base = function()
{
this.base = baseClass.prototype.base;
baseClass.apply( this, arguments );
this.base = arguments.callee;
};
}
if ( proto )
this.extend( $.prototype, proto, true );
if ( statics )
this.extend( $, statics, true );
return $;
},
/**
* Creates a function reference that can be called later using
* CKEDITOR.tools.callFunction. This approach is specially useful to
* make DOM attribute function calls to JavaScript defined functions.
* @param {Function} fn The function to be executed on call.
* @param {Object} [scope] The object to have the context on "fn" execution.
* @returns {Number} A unique reference to be used in conjuction with
* CKEDITOR.tools.callFunction.
* @example
* var ref = <b>CKEDITOR.tools.addFunction</b>(
* function()
* {
* alert( 'Hello!');
* });
* CKEDITOR.tools.callFunction( ref ); // Hello!
*/
addFunction : function( fn, scope )
{
return functions.push( function()
{
return fn.apply( scope || this, arguments );
}) - 1;
},
/**
* Removes the function reference created with {@see CKEDITOR.tools.addFunction}.
* @param {Number} ref The function reference created with
* CKEDITOR.tools.addFunction.
*/
removeFunction : function( ref )
{
functions[ ref ] = null;
},
/**
* Executes a function based on the reference created with
* CKEDITOR.tools.addFunction.
* @param {Number} ref The function reference created with
* CKEDITOR.tools.addFunction.
* @param {[Any,[Any,...]} params Any number of parameters to be passed
* to the executed function.
* @returns {Any} The return value of the function.
* @example
* var ref = CKEDITOR.tools.addFunction(
* function()
* {
* alert( 'Hello!');
* });
* <b>CKEDITOR.tools.callFunction( ref )</b>; // Hello!
*/
callFunction : function( ref )
{
var fn = functions[ ref ];
return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
},
/**
* Append the 'px' length unit to the size if it's missing.
* @param length
*/
cssLength : (function()
{
return function( length )
{
return length + ( !length || isNaN( Number( length ) ) ? '' : 'px' );
};
})(),
/**
* Convert the specified CSS length value to the calculated pixel length inside this page.
* <strong>Note:</strong> Percentage based value is left intact.
* @param {String} cssLength CSS length value.
*/
convertToPx : ( function ()
{
var calculator;
return function( cssLength )
{
if ( !calculator )
{
calculator = CKEDITOR.dom.element.createFromHtml(
'<div style="position:absolute;left:-9999px;' +
'top:-9999px;margin:0px;padding:0px;border:0px;"' +
'></div>', CKEDITOR.document );
CKEDITOR.document.getBody().append( calculator );
}
if ( !(/%$/).test( cssLength ) )
{
calculator.setStyle( 'width', cssLength );
return calculator.$.clientWidth;
}
return cssLength;
};
} )(),
/**
* String specified by {@param str} repeats {@param times} times.
* @param str
* @param times
*/
repeat : function( str, times )
{
return new Array( times + 1 ).join( str );
},
/**
* Return the first successfully executed function's return value that
* doesn't throw any exception.
*/
tryThese : function()
{
var returnValue;
for ( var i = 0, length = arguments.length; i < length; i++ )
{
var lambda = arguments[i];
try
{
returnValue = lambda();
break;
}
catch (e) {}
}
return returnValue;
},
/**
* Generate a combined key from a series of params.
* @param {String} subKey One or more string used as sub keys.
* @example
* var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' );
* alert( key ); // "key1-key2-key3".
*/
genKey : function()
{
return Array.prototype.slice.call( arguments ).join( '-' );
}
};
})();
// PACKAGER_RENAME( CKEDITOR.tools )