File: command.js

package info (click to toggle)
ckeditor 4.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 258,804 kB
  • sloc: javascript: 239,590; sh: 184; makefile: 64; python: 37; php: 15; xml: 5
file content (353 lines) | stat: -rw-r--r-- 11,513 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
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
/* bender-tags: editor */
// jscs:disable maximumLineLength
/* bender-ckeditor-plugins: basicstyles,bidi,blockquote,button,clipboard,colorbutton,dialog,div,docprops,elementspath,find,flash,font,format,forms,horizontalrule,iframe,iframedialog,image,indent,justify,link,list,listblock,maximize,newpage,pagebreak,pastefromword,pastetext,placeholder,preview,print,removeformat,save,selectall,showblocks,showborders,smiley,sourcearea,specialchar,stylescombo,table,templates,toolbar,uicolor,undo */
// jscs:enable maximumLineLength

// This list of commands are to be maintained whenever new commands are added.
var READ_ONLY_CMDS = [
	'a11yHelp', 'autogrow', 'about', 'contextMenu', 'copy', 'elementsPathFocus', 'find', 'maximize',
	'preview', 'print', 'showblocks', 'showborders', 'source', 'toolbarCollapse', 'toolbarFocus', 'selectAll', 'exportPdf'
];

function assertCommand( editor, cmd, commandDefinition ) {
	// Register command from command instance.
	editor.addCommand( 'cmd1', cmd );

	// Register command from command definition.
	editor.addCommand( 'cmd2', commandDefinition );

	// Registered command should be same as the command that was passed as definition.
	assert.areSame( editor.getCommand( 'cmd1' ), cmd );

	// Registered command should't be same to another command with same definition.
	assert.areNotSame( editor.getCommand( 'cmd2' ), cmd );
}

bender.editor = true;

bender.test( {
	checkAllCmds: function( fn ) {
		var cmd, cmds = this.editor.commands;
		for ( var name in cmds ) {
			cmd = cmds[ name ];
			fn( cmd, name );
		}
	},

	'test "contextSensitive" property': function() {
		var ed = this.editor;
		var cmd = ed.addCommand( 'test_context_sensitive', {
			contextSensitive: true,
			refresh: function( editor, path ) {
				assert.isTrue( path.lastElement.is( 'strong' ) );
				this.setState( CKEDITOR.TRISTATE_ON );
			}
		} );

		var bot = this.editorBot;
		bot.setHtmlWithSelection( '<p><strong>^</strong></p>' );
		assert.areSame( cmd.state, CKEDITOR.TRISTATE_ON );

		// We don't want this to be executed in successive tests
		// since they use the same editor (https://dev.ckeditor.com/ticket/9848).
		delete ed.commands.test_context_sensitive;
	},

	// https://dev.ckeditor.com/ticket/8342
	'test command states with readonly editor': function() {
		var bot = this.editorBot, editor = bot.editor;
		editor.setReadOnly( true );
		bot.setHtmlWithSelection( '<p>[foo]</p>' );

		this.checkAllCmds( function( cmd, name ) {
			if ( cmd.state != CKEDITOR.TRISTATE_DISABLED && CKEDITOR.tools.indexOf( READ_ONLY_CMDS, name ) == -1 )
				assert.fail( 'command: ' + name + ' should be disabled in readonly' );
		} );

		assert.isTrue( true );
		editor.setReadOnly( false );
	},

	'test command states in details>summary': function() {
		// <summary> is blocklimit (can't be split) and doesn't accept block content.
		var bot = this.editorBot;
		bot.setHtmlWithSelection( '<details><summary>foo^bar</summary></details>' );

		this.checkAllCmds( function( cmd, name ) {
			if ( cmd.state != CKEDITOR.TRISTATE_DISABLED && cmd.context in CKEDITOR.dtd.$block )
				assert.fail( 'command: ' + name + ' should be disabled inside of summary' );
		} );

		assert.isTrue( true );
	},

	'test command state integration with ACF - custom config': function() {
		var cmd1, cmd2, cmd3, cmd4,
			exec1 = 0,
			exec2 = 0,
			changedState1 = [];

		bender.editorBot.create( {
			name: 'test_editor_acf',
			config: {
				allowedContent: 'p b i',
				on: {
					loaded: function( evt ) {
						var editor = evt.editor;

						resume( function() {
							cmd1 = editor.addCommand( 'acftest1', {
								exec: function() {
									exec1++;
								},
								requiredContent: 'b'
							} );
							cmd2 = editor.addCommand( 'acftest2', {
								exec: function() {
									exec2++;
								},
								requiredContent: 'x'
							} );
							cmd3 = editor.addCommand( 'acftest3', {
								exec: function() {},
								requiredContent: 'i'
							} );

							// Overwrite the enable, disable, setState methods, to count how many times command was updated.
							var oldEnable = cmd1.enable,
								oldDisable = cmd1.disable,
								oldSetState = cmd1.setState;
							cmd1.enable = function() {
								changedState1.push( 'enable' );
								oldEnable.call( this );
							};
							cmd1.disable = function() {
								changedState1.push( 'disable' );
								oldDisable.call( this );
							};
							cmd1.setState = function( newState ) {
								changedState1.push( 'setState' );
								oldSetState.call( this, newState );
							};

							assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd1.state, 'initial cmd1.state' );
							assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd2.state, 'initial cmd2.state' );
							assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd3.state, 'initial cmd3.state' );

							cmd3.enable();
							assert.areSame( CKEDITOR.TRISTATE_OFF, cmd3.state, 'after enable() cmd3.state' );

							wait();
						} );

						// Add with low priority, so mode listener which refreshes commands
						// is executed first.
						editor.on( 'mode', function() {
							// Cache states now, because resume() executes listener with a delay ;|.
							var st1 = cmd1.state,
								st2 = cmd2.state,
								st3 = cmd3.state;

							// https://dev.ckeditor.com/ticket/10103 Before this test was created commands were refreshed on #mode, but not on #instanceReady.
							// So cmd4 wouldn't be refreshed because this listener will be executed after that
							// refreshing commands.
							cmd4 = editor.addCommand( 'acftest4', {
								exec: function() {}
							} );

							resume( function() {
								// https://dev.ckeditor.com/ticket/10249 Commands should be updated on first 'mode' event, so they are ready
								// on 'instanceReady'.
								assert.areSame( CKEDITOR.TRISTATE_OFF, st1, 'first "mode" cmd1.state' );
								assert.areSame( CKEDITOR.TRISTATE_DISABLED, st2, 'first "mode" cmd2.state' );
								assert.areSame( CKEDITOR.TRISTATE_OFF, st3, 'first "mode" cmd3.state' );

								wait();
							} );
						}, null, null, 100 );
					}
				}
			}
		}, function( bot ) {
			var editor = bot.editor;

			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'ready cmd1.state' );
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd2.state, 'ready cmd2.state' );
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd3.state, 'ready cmd3.state' );
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd4.state, 'ready cmd4.state' );
			// Note: enable() executes setState(), so both should be logged.
			assert.areSame( 'enable,setState', changedState1.join( ',' ), 'cmd1.state was changed once during initialization' );

			cmd1.refresh( editor, editor.elementPath() );
			cmd2.refresh( editor, editor.elementPath() );

			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'refreshed cmd1.state' );
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd2.state, 'refreshed cmd2.state' );

			assert.isTrue( cmd1.exec() );
			assert.isFalse( cmd2.exec() );

			assert.areSame( 1, exec1 );
			assert.areSame( 0, exec2 );

			cmd2.enable();
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd2.state, 'after enable() cmd2.state' );
			cmd2.disable();
			cmd2.enable();
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd2.state, 'after disable()&enable() cmd2.state' );

			cmd1.disable();
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd1.state, 'after disable() cmd1.state' );
			cmd1.enable();
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'after enable() cmd1.state' );

			var cmd5 = editor.addCommand( 'acftest5', {
				exec: function() {}
			} );
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd5.state, 'command added after instanceReady is refreshed automatically' );
		} );
	},

	'test command state integration with ACF - default config': function() {
		bender.editorBot.create( {
			name: 'test_editor_acf2'
		}, function( bot ) {
			var editor = bot.editor,
				cmd1, cmd2,
				exec1 = 0,
				exec2 = 0;

			cmd1 = editor.addCommand( 'acftest1', {
				exec: function() {
					exec1++;
				},
				requiredContent: 'p',
				allowedContent: 'b'
			} );
			cmd2 = editor.addCommand( 'acftest2', {
				exec: function() {
					exec2++;
				},
				requiredContent: 'i',
				allowedContent: 'i'
			} );

			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'initial cmd1.state' );
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd2.state, 'initial cmd2.state' );

			cmd1.refresh( editor, editor.elementPath() );
			cmd2.refresh( editor, editor.elementPath() );

			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'refreshed cmd1.state' );
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd2.state, 'refreshed cmd2.state' );

			assert.isTrue( cmd1.exec() );
			assert.isTrue( cmd2.exec() );

			assert.areSame( 1, exec1 );
			assert.areSame( 1, exec2 );

			assert.isTrue( editor.filter.check( 'b' ) );
			assert.isTrue( editor.filter.check( 'i' ) );

			cmd1.disable();
			assert.areSame( CKEDITOR.TRISTATE_DISABLED, cmd1.state, 'after disable() cmd1.state' );
			cmd1.enable();
			assert.areSame( CKEDITOR.TRISTATE_OFF, cmd1.state, 'after enable() cmd1.state' );
		} );
	},

	'test checkAllowed': function() {
		var editor = this.editor,
			cmd = editor.addCommand( 'testCheckAllowed', {
				exec: function() {},
				requiredContent: 'foo[tca]'
			} );

		assert.isFalse( cmd.checkAllowed(), 'is not allowed' );

		editor.filter.allow( 'foo[tca]' );

		assert.isFalse( cmd.checkAllowed(), 'is not allowed - cache' );
		assert.isTrue( cmd.checkAllowed( true ), 'is allowed - no cache' );
	},

	// https://dev.ckeditor.com/ticket/13548
	'test copy command not disabled after clicking on elements path': function() {
		if ( !CKEDITOR.env.ie ) {
			assert.ignore();
			return;
		}

		var editor = this.editor,
			cmd = editor.getCommand( 'copy' );

		var bot = this.editorBot;
		bot.setHtmlWithSelection( '<p><strong>test^</strong></p>' );

		editor.once( 'selectionChange', function() {
			resume( function() {
				assert.areNotSame( cmd.state, CKEDITOR.TRISTATE_DISABLED );
			} );
		} );
		editor._.elementsPath.onClick( 0 );

		wait();
	},

	// https://dev.ckeditor.com/ticket/13548
	'test cut command not disabled after clicking on elements path': function() {
		if ( !CKEDITOR.env.ie ) {
			assert.ignore();
			return;
		}

		var editor = this.editor,
			cmd = editor.getCommand( 'cut' );

		var bot = this.editorBot;
		bot.setHtmlWithSelection( '<p><strong>test^</strong></p>' );

		editor.once( 'selectionChange', function() {
			resume( function() {
				assert.areNotSame( cmd.state, CKEDITOR.TRISTATE_DISABLED );
			} );
		} );
		editor._.elementsPath.onClick( 0 );

		wait();
	},

	// (#1582)
	'test addCommand from command instance': function() {
		var editor = this.editor,
			styleDefinition = {
				element: 'span',
				attributes: {
					bar: 'foo'
				}
			},
			style = new CKEDITOR.style( styleDefinition ),
			commandDefinition = new CKEDITOR.styleCommand( style ),
			cmd = new CKEDITOR.command( editor, commandDefinition );

		assertCommand( editor, cmd, commandDefinition );
	},

	// (#1582)
	'test addCommand from command subclass': function() {
		var editor = this.editor,
			styleDefinition = {
				element: 'span',
				attributes: {
					bar: 'foo'
				}
			},
			subCommand = CKEDITOR.tools.createClass( { base: CKEDITOR.command } ),
			style = new CKEDITOR.style( styleDefinition ),
			commandDefinition = new CKEDITOR.styleCommand( style ),
			cmd = new subCommand( editor, commandDefinition );

		assertCommand( editor, cmd, commandDefinition );
	}
} );