File: editor.js

package info (click to toggle)
ckeditor 4.4.4%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,632 kB
  • ctags: 2,419
  • sloc: sh: 190; python: 37; makefile: 29; php: 15; xml: 5
file content (314 lines) | stat: -rw-r--r-- 10,707 bytes parent folder | download
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
/* bender-tags: editor,unit */

CKEDITOR.replaceClass = 'ckeditor';
bender.editor = true;

bender.test(
{

	test_name : function() {
		assert.areSame( 'editor1', CKEDITOR.instances.editor1.name );
	},

	test_element : function() {
		assert.areSame( document.getElementById( 'editor1' ), CKEDITOR.instances.editor1.element.$ );
	},

	'ignore:test_config' : function() {
		// The instance default config must match the CKEDITOR.config.

		var config = CKEDITOR.instances.editor1.config;

		for ( var prop in CKEDITOR.config )
			assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
	},

	'ignore:test_config_inpage' : function() {
		var self = this;

		CKEDITOR.replace( 'editor2',
			{
				// The custom setting to be checked.
				test1 : 'ball',
				baseHref : 'test',

				on :
				{
					instanceReady : function() {
						self.resume( function() {
							var config = CKEDITOR.instances.editor2.config;

							assert.areSame( 'ball', config.test1, '"test1" doesn\'t match' );
							assert.areSame( 'test', config.baseHref, '"baseHref" doesn\'t match' );

							// All other settings must match CKEDITOR.config.
							for ( var prop in CKEDITOR.config ) {
								if ( prop != 'test1' && prop != 'baseHref' )
									assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
							}
						} );
					}
				}
			} );

		this.wait();
	},

	test_config_customConfig : function() {
		var tc = this;
		// Pass in-page settings to the instance.
		CKEDITOR.replace( 'editor3', { customConfig : '%TEST_DIR%_assets/custom_config_1.js', test1 : 'ball', baseHref : 'test',
			on :
			{
				configLoaded : function( event ) {
					tc.resume( function() {
						var config = event.editor.config;

						assert.areSame( 'Ok', config.test_custom1, '"test_custom1" doesn\'t match' );
						assert.areSame( 'Ok', config.test_custom2, '"test_custom1" doesn\'t match' );
						assert.areSame( 'ball', config.test1, '"test1" doesn\'t match' );
						assert.areSame( 'test', config.baseHref, '"baseHref" doesn\'t match' );

						// All other settings must match CKEDITOR.config.
						for ( var prop in CKEDITOR.config ) {
							if ( !Object.hasOwnProperty( CKEDITOR.config, prop ) )
								continue;

							// Creators might add required plugin to core.
							if ( prop != 'plugins'
									 && prop != 'customConfig' && prop != 'test_custom1' && prop != 'test_custom2' && prop != 'test1' && prop != 'baseHref' )
								assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
						}
					} );
				}
			}
		} );

		this.wait();
	},

	'test getData/setData() - events and arguments': function() {
		var events = [],
			editor = new CKEDITOR.editor( {}, CKEDITOR.document.getById( 'editor4' ), CKEDITOR.ELEMENT_MODE_REPLACE );

		function checkEventData( value ) {
			return function( evt  ) {
				events.push( evt.name );
				// Check data value.
				if ( value )
					assert.areSame( value, evt.data.dataValue, 'check data on event: ' + evt.name );
				// Alter the data value.
				if ( evt.name == 'setData' )
					evt.data.dataValue = 'bar';
			};
		}

		// This function allows to call either older API or new object based setData().
		// It takes setData() params in new format (as editor#setData()).
		function callSetData( data, params, legacyInterface ) {
			if ( legacyInterface )
				editor.setData( data, params.callback, params.internal );
			else
				editor.setData( data, params );
		}

		var listeners = [],
			allEvents = [ 'setData', 'afterSetData', 'beforeGetData', 'getData', 'saveSnapshot' ],
			listener = checkEventData();

		for ( var i = 0; i < allEvents.length; i++ )
			listeners.push( editor.on( allEvents[ i ], listener ) );

		for ( i = 0; i <= 1; i++ ) {
			var useOldAPI = Boolean( i ),
				// Helper var with human-readable info what interface version is called.
				interfaceName = useOldAPI ? 'old API params inline' :'new API params as object';

			events = [];

			// Test setData/getData internal.
			callSetData( 'foo', { internal: true }, useOldAPI );
			assert.areSame( 'foo', editor.getData( true ), 'setData internally - ' + interfaceName );
			// No events should be fired.
			assert.areSame( '', events.join( ',' ), 'Events fired - ' + interfaceName );

			events = [];
			// Test non-internal setData() - snapshot is expected.
			callSetData( 'foo', {}, useOldAPI );

			assert.areSame( 'saveSnapshot,setData,afterSetData', events.join( ',' ),
				'Invalid events for setData() - ' + interfaceName );

			// Test non-internal getData().
			events = [];

			assert.areSame( 'bar', editor.getData(), 'setData listener change data value - ' + interfaceName );
			assert.areSame( 'beforeGetData,getData', events.join( ',' ) );
		}

		// New API provides params.noSnapshot, which should prevent saveSnapshot event.
		events = [];
		callSetData( 'foo', { noSnapshot: true } );

		assert.areSame( 'setData,afterSetData', events.join( ',' ), 'Invalid events with params.noSnapshot = true' );
	},

	'test setData() callback - new API': function() {
		var callbackCalledTimes = 0,
			callback = function() {
				callbackCalledTimes += 1;
			},
			listener;

		listener = this.editor.on( 'dataReady', function() {
			listener.removeListener();

			resume( function() {
				assert.areEqual( 1, callbackCalledTimes, 'callback called once' );
			} );
		} );

		this.editor.setData( '<p>setData</p>', { callback: callback } );
		wait();
	},

	'test setData() callback - old API': function() {
		var callbackCalledTimes = 0,
			callback = function() {
				callbackCalledTimes += 1;
			},
			listener;

		listener = this.editor.on( 'dataReady', function() {
			listener.removeListener();

			resume( function() {
				assert.areEqual( 1, callbackCalledTimes, 'callback called once' );
			} );
		} );

		this.editor.setData( '<p>setData</p>', callback );
		wait();
	},

	updateElement: function( element, mode ) {
		var editor = new CKEDITOR.editor( {}, element, mode );
		editor.setData( 'foo' );

		// Test update element explicit call.
		if ( editor.updateElement() )
			assert.areSame( 'foo', element.is( 'textarea' ) ? element.getValue() : element.getHtml(), 'editor data update to element' );

		// Test update element implicitly on editor destroy.
		editor.setData( 'bar' );
		editor.destroy();

		assert.areSame( 'bar', element.is( 'textarea' ) ? element.getValue() : element.getHtml(), 'editor destroy should trigger data update' );
		element.remove();
	},

	'test blockless editor' : function() {
		var tc = this;
		var el = CKEDITOR.dom.element.createFromHtml( '<h1>heading</h1>' );
		CKEDITOR.document.getBody().append( el );

		var editor = new CKEDITOR.editor( {}, el, CKEDITOR.ELEMENT_MODE_INLINE );
		editor.on( 'loaded', function( evt ) {
			tc.resume( function() {
				   evt.removeListener();
				   assert.isTrue( editor.blockless );
			   } );
		} );
		this.wait();
	},

	test_updateElement : function() {
		var body = CKEDITOR.document.getBody();
		this.updateElement( body.append( new CKEDITOR.dom.element( 'textarea' ) ),
								 CKEDITOR.ELEMENT_MODE_REPLACE );
		this.updateElement( body.append( CKEDITOR.dom.element.createFromHtml( '<div contenteditable="true"></div>' ) ),
								 CKEDITOR.ELEMENT_MODE_INLINE );
	},

	'test editor.getResizable' : function() {
		var editor = CKEDITOR.instances.editor1,
			resizeable = editor.getResizable(),
			innerResizeable = editor.getResizable( true );

		assert.isTrue( editor.container.equals( resizeable ) );
		assert.isTrue( editor.ui.space( 'contents' ).equals( innerResizeable ) );
	},

	'test editor.activeEnterMode': function() {
		var editor = this.editor,
			activeEnterModeChanged = 0;

		assert.areSame( CKEDITOR.ENTER_P, editor.activeEnterMode, 'initial activeEnterMode' );
		assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, 'initial activeShiftEnterMode' );

		editor.on( 'activeEnterModeChange', function() {
			activeEnterModeChanged += 1;
		} );


		editor.setActiveEnterMode( CKEDITOR.ENTER_BR );

		assert.areSame( CKEDITOR.ENTER_BR, editor.activeEnterMode, '1st activeEnterMode' );
		assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, '1st activeShiftEnterMode' );
		assert.areSame( 1, activeEnterModeChanged, 'activeEnterModeChange was fired once' );


		editor.setActiveEnterMode( CKEDITOR.ENTER_DIV, CKEDITOR.ENTER_P );

		assert.areSame( CKEDITOR.ENTER_DIV, editor.activeEnterMode, '2nd activeEnterMode' );
		assert.areSame( CKEDITOR.ENTER_P, editor.activeShiftEnterMode, '2nd activeShiftEnterMode' );
		assert.areSame( 2, activeEnterModeChanged, 'activeEnterModeChange was fired once' );


		editor.setActiveEnterMode( CKEDITOR.ENTER_DIV, CKEDITOR.ENTER_P );

		assert.areSame( CKEDITOR.ENTER_DIV, editor.activeEnterMode, '2nd activeEnterMode - no change' );
		assert.areSame( CKEDITOR.ENTER_P, editor.activeShiftEnterMode, '2nd activeShiftEnterMode - no change' );
		assert.areSame( 2, activeEnterModeChanged, 'activeEnterModeChange was not fired' );


		editor.setActiveEnterMode( null, CKEDITOR.ENTER_DIV );

		assert.areSame( CKEDITOR.ENTER_P, editor.activeEnterMode, '3rd activeEnterMode - reseted' );
		assert.areSame( CKEDITOR.ENTER_DIV, editor.activeShiftEnterMode, '3rd activeShiftEnterMode' );
		assert.areSame( 3, activeEnterModeChanged, 'activeEnterModeChange was fired once' );


		editor.setActiveEnterMode( null, null );

		assert.areSame( CKEDITOR.ENTER_P, editor.activeEnterMode, '4th activeEnterMode' );
		assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, '4th activeShiftEnterMode' );
		assert.areSame( 4, activeEnterModeChanged, 'activeEnterModeChange was fired once' );
	},

	'test blockless editor always works in BR mode': function() {
		bender.editorBot.create( {
			name: 'test_blockless_enter',
			creator: 'inline',
			config: {
				enterMode: CKEDITOR.ENTER_DIV,
				shiftEnterMode: CKEDITOR.ENTER_P
			}
		}, function( bot ) {
			var editor = bot.editor;

			assert.isTrue( editor.blockless, 'it is a blockless editor' );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeEnterMode, 'initial activeEnterMode' );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, 'initial activeShiftEnterMode' );

			editor.setActiveEnterMode( CKEDITOR.ENTER_P, CKEDITOR.ENTER_DIV );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeEnterMode, '1st activeEnterMode' );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, '1st activeShiftEnterMode' );

			editor.setActiveEnterMode( null, null );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeEnterMode, '2nd activeEnterMode' );
			assert.areSame( CKEDITOR.ENTER_BR, editor.activeShiftEnterMode, '2nd activeShiftEnterMode' );
		} );
	}

} );