File: selection.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 (540 lines) | stat: -rw-r--r-- 19,544 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
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
/* bender-tags: editor,unit */

bender.editor = {
	config: {
		allowedContent: true
	}
};

bender.test(
{
	test_contructor : function() {
		// Make the DOM selection at the beginning of the document.
		var newRange = new CKEDITOR.dom.range( doc );
		newRange.moveToPosition( doc.getBody(), CKEDITOR.POSITION_AFTER_START );
		var domSel = rangy.getSelection();
		domSel.removeAllRanges();
		domSel.addRange( convertRange( newRange ) );

		// create new selection scoped in the entire document .
		var sel1 = new CKEDITOR.dom.selection( doc );
		assert.isFalse( !!sel1.isLocked, 'selection.isLock' );
		assert.areSame( doc.$, sel1.document.$, 'selection.document' );
		assert.areSame( doc.getBody().$, sel1.root.$, 'selection.root' );
		assert.isTypeOf( 'number', sel1.rev, 'selection.rev' );


		// create new selection scoped in the editable element.
		var editable = doc.getById( 'sandbox' );

		var sel2 = new CKEDITOR.dom.selection( editable );
		assert.isFalse( !!sel2.isLocked, 'selection.isLock' );
		assert.areSame( sel2.document.$, doc.$, 'selection.document' );
		assert.areSame( sel2.root.$, editable.$, 'selection.root' );
		assert.isTrue( sel2.rev > sel1.rev, 'sel2\'s revision is greater than sel1\'s' );

		// Check the selection fields should be empty.
		assert.areSame( CKEDITOR.SELECTION_NONE, sel2.getType(), 'selection.getType()' );
		assert.areSame( null, sel2.getStartElement(), 'selection.getStartElement()' );
		assert.areSame( null, sel2.getSelectedElement(), 'selection.getSelectedElement()' );
		assert.areSame( '', sel2.getSelectedText(), 'selection.getSelectedText()' );
		assert.areSame( 0, sel2.getRanges().length, 'selection.getRanges()' );
	},

	test_getSelection : function() {
		var sel = doc.getSelection();
		assert.isFalse( !!sel.isLocked, 'selection.isLock' );
		assert.areSame( sel.document.$, doc.$, 'selection.document' );
		assert.areSame( sel.root.$, doc.getBody().$, 'selection.boundary' );
	},

	// Test getRanges/setRanges with various selection source.
	test_ranges_manipulation : function() {
		// Text selection.
		testSelection( '<div>[foo]</div>' );
		testSelection( '<p><strong>[foo</strong>]bar</p>' );
		testSelection( '<p><strong>foo[</strong>bar]</p>' );

		// Collapsed selection
		testSelection( '<div>fo^o</div>' );
		testSelection( '<div>foo^</div>' );
		testSelection( '<div>^foo</div>' );
		testSelection( '<p><strong>foo</strong>^bar</p>' );
		testSelection( '<p><strong>foo^</strong>bar</p>' );
		testSelection( '<p><strong>^foo</strong>bar</p>' );
		testSelection( '<p>^<strong>foo</strong>bar</p>' );

		// Collapsed selection inside of empty inline is not testable.
		testSelection( '<p><b><i>^</i></b></p>' );
		testSelection( '<p>foo<b><i>^</i></b></p>' );

		testSelection( '<p><br />^bar</p>' );
		testSelection( '<p>foo<br />^bar</p>' );
		testSelection( '<ul><li>bullet line 1</li><li>bullet line 2</li></ul>^second line' );

		// Element selection.
		testSelection( '<p>[<img />]</p>' );
		testSelection( '<p>[<input />]</p>' );

		// Entire block selection (FF only).
		if ( CKEDITOR.env.gecko ) {
			testSelection( '[<p style="float:left">foo</p>]' );
			testSelection( '<div>[<p>foo</p>]</div>' );
			testSelection( '<div>[<p>foo</p><p>bar</p>]</div>' );
			testSelection( '<div>[<h1>foo</h1>]</div>' );
			testSelection( '<div>[<pre>foo</pre>]</div>' );
		}

		// Table selection.
		testSelection( '<div><table><tr><td>[foo</td><td>bar]</td></tr></table></div>' );
		testSelection( '<div><table><tr><td>[foo</td></tr><tr><td>bar]</td></tr></table></div>' );

		// Multiple selection (FF only)
		if ( CKEDITOR.env.gecko ) {
			testSelection( '<div><table><tr><td>[foo]</td><td>[bar]</td></tr></table></div>' );
			testSelection( '<div><table><tr><td>[foo]</td></tr><tr><td>[bar]</td></tr></table></div>' );
		}
	},

	'test selectRanges - range containing table cell': function() {
		var editor = this.editor,
			range = bender.tools.range.setWithHtml( editor.editable(), '<table><tr><td>x</td>[<td>foo</td>]<td>x</td></tr></table>' );

		editor.getSelection().selectRanges( [ range ] );

		assert.areSame( 'foo', CKEDITOR.tools.trim( editor.getSelection().getSelectedText() ) );
	},

	'test selectRanges - range containing empty table cell': function() {
		var editor = this.editor,
			data = '<table><tbody><tr><td>x</td><td id="cell"></td><td>x</td></tr></tbody></table>';

		this.editorBot.setData( data, function() {
			var range = editor.createRange(),
				cell = editor.document.getById( 'cell' );

			range.setStartAt( cell, CKEDITOR.POSITION_BEFORE_START );
			range.setEndAt( cell, CKEDITOR.POSITION_AFTER_END );

			editor.getSelection().selectRanges( [ range ] );

			var sel = editor.getSelection();

			// IE8 && Webkit (precisely - excluding Blink) can't select cell from outside.
			if ( ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 ) || CKEDITOR.env.safari ) {
				var selected = sel.getStartElement();
				assert.isTrue( cell.equals( selected ) || cell.contains( selected ), 'selection is inside the cell' );
			} else
				assert.areSame( cell, sel.getSelectedElement(), 'cell is selected' );

			// I saw some strange span being left by IE8. Let's check data to be safe.
			assert.areSame( data, editor.getData().replace( /&nbsp;|\u00a0/, '' ), 'data is ok' );
		} );
	},

	test_getSelectedElement : function() {
		testSelectedElement( '[<img />]', 'img' );
		testSelectedElement( '[<hr />]', 'hr' );
		testSelectedElement( '[<b><i><img /></i>]</b>', 'img' );
	},

	// Issue noticed during works on #9764.
	'test getSelectedElement does not modify ranges': function() {
		var editor = this.editor;

		this.editorBot.setData( '<div>foo</div><p>bar</p>', function() {
			var elP = editor.editable().findOne( 'p' ),
				elDiv = editor.editable().findOne( 'div' ),
				range = editor.createRange();

			range.setStartAt( elDiv, CKEDITOR.POSITION_BEFORE_END );
			range.setEndAt( elP, CKEDITOR.POSITION_BEFORE_END );

			editor.focus();
			range.select();

			var sel = editor.getSelection(),
				// Cache range before getting selected element.
				range = sel.getRanges()[ 0 ].clone();

			// Verify that test case works correctly. It might happen that
			// getSelectedElement() has been already executed.
			assert.isTrue( elDiv.contains( range.startContainer ) || elDiv.equals( range.startContainer ),
				'range was not already modified' );

			sel.getSelectedElement();

			var range2 = sel.getRanges()[ 0 ];

			assert.isTrue( range.startContainer.equals( range2.startContainer ) && range.startOffset == range2.startOffset,
				'range was not modified by calling getSelectedElement' );
		} );
	},

	// #11493
	'test getRanges(true) does not modify cached ranges': function() {
		var editor = this.editor;

		bender.tools.setHtmlWithSelection( editor, '<div>te[xt<span contenteditable="false">foo</span>te]xt</div>' );

		var selection = editor.getSelection(),
			editableRanges = selection.getRanges( true ),
			allRanges = selection.getRanges();

		assert.areNotSame( editableRanges, allRanges, 'both method calls returned different arrays' );
		assert.areSame( 2, editableRanges.length, '2 editable ranges returned by getRanges( true )' );
		assert.areSame( 1, allRanges.length, 'only 1 range returned by getRanges()' );
	},

	// #11493
	'test getRanges(true) called after getRanges() does not modify cached ranges': function() {
		var editor = this.editor;

		bender.tools.setHtmlWithSelection( editor, '<div>te[xt<span contenteditable="false">foo</span>te]xt</div>' );

		var selection = editor.getSelection(),
			allRanges = selection.getRanges(),
			editableRanges = selection.getRanges( true );

		assert.areNotSame( editableRanges, allRanges, 'both method calls returned different arrays' );
		assert.areSame( 2, editableRanges.length, '2 editable ranges returned by getRanges( true )' );
		assert.areSame( 1, allRanges.length, 'only 1 range returned by getRanges()' );
	},

	test_getSelectedText : function() {
		testSelectedText( '[<b>foo</b>bar]', 'foobar' );
		testSelectedText( '[<b>foo<img /></b>bar]', 'foobar' );
	},

	test_getStartElement : function() {
		testStartElement( '<b>^foo</b>', 'b' );
		testStartElement( '<b>foo^</b>', 'b' );
		testStartElement( '<i><b>foo[</b>bar]</i>', 'i' );
		testStartElement( '<p>foo[</p><div>bar]</div>', 'p' );
		testStartElement( '[<img />]', 'img' );
	},

	test_lock_unlock : function() {
		// Make the first selection.
		var sourceRange = makeSelection( '<strong id="start">foo</strong>bar[<img />]' )[ 0 ];
		var sel = doc.getSelection(),
			initialRev = sel.rev;

		sel.lock();

		// Make a fresh selection to drop the previous one.
		var newRange = new CKEDITOR.dom.range( doc );
		newRange.selectNodeContents( doc.getById( 'start' ) );
		var domSel = rangy.getSelection();
		domSel.removeAllRanges();
		domSel.addRange( convertRange( newRange ) );

		var resultRange = sel.getRanges()[ 0 ];

		assert.isTrue( !!sel.isLocked, 'selection should be marked as locked.' );
		assert.isTrue( checkRangeEqual( resultRange, sourceRange ), 'get ranges result from locked selection doesn\'t match the original.' );
		assert.isTrue( sel.getStartElement().is( 'img' ), 'start element result from locked selection doesn\'t match the original.' );
		assert.isTrue( sel.getSelectedElement().is( 'img' ), 'selected element result from locked selection doesn\'t match the original.' );
		assert.areSame( initialRev, sel.rev, 'selection\'s rev has not been modified' );

		sel.unlock();

		var resultRange = sel.getRanges()[ 0 ];
		assert.isFalse( !!sel.isLocked, 'selection should not be marked as locked.' );
		assert.isTrue( checkRangeEqual( resultRange, newRange ), 'get ranges result from locked selection doesn\'t match the original.' );
		assert.isTrue( sel.getStartElement().is( 'strong' ), 'start element result from locked selection doesn\'t match the original.' );
		assert.isTrue( sel.rev > initialRev, 'unlocked selection gets new rev' );
	},

	'test unlock outdated selection 1' : function() {
		makeSelection( '<p>a[b<b id="bold">c]d</b></p>' );

		var sel = doc.getSelection(),
			initialRev = sel.rev;

		sel.lock();

		// Remove node in which one selection's end is anchored.
		doc.getById( 'bold' ).remove();

		sel.selectRanges = function() {
			assert.fail( 'selectRanges should not be called.' );
		};

		sel.unlock( true );

		assert.isTrue( true, 'No error was thrown.' );
		assert.isTrue( sel.rev > initialRev, 'New revision' );
	},

	'test unlock outdated selection 2' : function() {
		makeSelection( '<p>a<b id="bold">c[d<i>e]f</i></b></p>' );

		var sel = doc.getSelection(),
			initialRev = sel.rev;

		sel.lock();

		// Remove node in which both selection's ends are anchored.
		doc.getById( 'bold' ).remove();

		sel.selectRanges = function() {
			assert.fail( 'selectRanges should not be called.' );
		};

		sel.unlock( true );

		assert.isTrue( true, 'No error was thrown.' );
		assert.isTrue( sel.rev > initialRev, 'New revision' );
	},

	test_selectRanges_after_locked : function() {
		// Make the first selection.
		var sourceRange = makeSelection( '<strong id="start">foo</strong>bar[<img />]' )[ 0 ];
		var sel = doc.getSelection(),
			initialRev = sel.rev;

		sel.lock();

		// Blur the editable.
		var input = doc.getById( 'input_1' );
		input.focus();

		// Select a new range on locked selection.
		var newRange = new CKEDITOR.dom.range( doc );
		var el = doc.getById( 'start' );
		newRange.selectNodeContents( el );
		sel.selectRanges( [ newRange ] );

		// Check focus remains after selecting ranges.
		assert.areSame( input.$, document.activeElement, 'focus should remains in the text input' );
		// Check the new (refreshed) locked selection.
		assert.isTrue( !!sel.isLocked, 'selection should be locked still' );
		assert.areSame( el, sel.getStartElement(), 'start element of locked should match' );
		assert.areSame( CKEDITOR.SELECTION_TEXT, sel.getType(), 'selection type of locked should match' );
		assert.areSame( 'foo', CKEDITOR.tools.trim( sel.getSelectedText() ), 'selected text of locked should match' );
		assert.isTrue( sel.rev > initialRev, 'New revision after selectRanges on locked selection' );

		// Re-focus the editable.
		doc.getById( 'sandbox' ).focus();

		// Check dom selection range takes effect.
		var domSel = rangy.getSelection();
		var domRange = convertRange( domSel.getRangeAt( 0 ) );
		checkRangeEqual( domRange, newRange, 'actual selection range should be the same' );
	},

	test_removeAllRanges: function() {
		var range = new CKEDITOR.dom.range( doc );
		range.selectNodeContents( doc.getBody() );
		range.select();

		var sel = doc.getSelection(),
			initialRev = sel.rev;

		sel.removeAllRanges();

		var domSel = rangy.getSelection();

		// Various ways of detecting empty selection among browsers.
		var nativeSel = sel.getNative(),
			msSelection = typeof window.getSelection != 'function';

		msSelection ?
			assert.areSame( 'None', nativeSel.type ) :
			assert.areSame( 0, domSel.rangeCount );

		assert.isTrue( sel.rev > initialRev, 'Next revision' );

		// MS selection will remain a (empty) dom range collapsed at the beginning
		// at the document even after the removal.
		if ( msSelection ) {
			var startContainer = doc.getBody();
			var emptyRange = new CKEDITOR.dom.range( doc );
			emptyRange.moveToPosition( startContainer, CKEDITOR.POSITION_AFTER_START );
			checkSelection.call( sel, CKEDITOR.SELECTION_TEXT, startContainer, null, '', [ emptyRange ] );
		}
		else
			checkSelection.call( sel, CKEDITOR.SELECTION_NONE, null, null, '', 0 );
	},

	// #11500
	'test removeAllRanges is limited to its root': function() {
		var editable1 = doc.getById( 'sandbox' ),
			editable2 = doc.getById( 'sandbox2' );

		tools.setHtmlWithSelection( editable1, '<p>[foo]</p>' );

		var sel2 = new CKEDITOR.dom.selection( editable2 );
		sel2.removeAllRanges();

		assert.areSame( 'foo', new CKEDITOR.dom.selection( editable1 ).getSelectedText(),
			'selection in editable1 was not cleared' );
	},

	// Check ranges return from selection is properly scoped.
	'check selection ranges\' scope' : function() {
		var editable = doc.getById( 'sandbox' );
		tools.setHtmlWithSelection( editable, '<p>[foo]</p>' );
		var sel = new CKEDITOR.dom.selection( editable ),
		ranges = sel.getRanges();
		for ( var i = 0; i < ranges.length; i++ )
			assert.areSame( ranges[ i ].root, editable );
	},

	'test get only editable ranges' : function() {
		var editable = doc.getById( 'sandbox' ),
			sel, ranges;

		makeSelection( 'f[oo<span contenteditable="false">bar</span>bo]m' );
		sel = new CKEDITOR.dom.selection( editable );
		// Get only editable ranges.
		ranges = sel.getRanges( true );

		assert.areEqual( 2, ranges.length );
		assert.areSame( 'oo', ranges[ 0 ].getEnclosedNode().getText() );
		assert.areSame( 'bo', ranges[ 1 ].getEnclosedNode().getText() );
	},

	'test get only editable ranges 2' : function() {
		if ( CKEDITOR.env.ie )
			assert.ignore();

		var editable = doc.getById( 'sandbox' ),
			sel, ranges;

		makeSelection( 'x<span contenteditable="false">fo[o b]ar</span>x' );
		sel = new CKEDITOR.dom.selection( editable );
		// Get only editable ranges.
		ranges = sel.getRanges( true );

		assert.areEqual( 0, ranges.length );
	},

	'selection scrolls into view' : function() {

		function assertElementInViewport( el ) {
			var view = doc.getWindow().getViewPaneSize();
			var rect = marker.getClientRect();
			assert.isTrue( rect.top > 0 && rect.top < view.height );
		}

		var editable = doc.getById( 'sandbox' );
		var linebreaks = CKEDITOR.tools.repeat( '<br />', 100 );
		editable.setHtml( '<p>' + linebreaks + 'foo<span id="scroll_marker">bar</span></p>' );

		// MUST not using setHtmlWithSelection which will split text nodes.
		var marker = doc.getById( 'scroll_marker' );
		var range = new CKEDITOR.dom.range( editable );
		range.setStart( marker.getPrevious(), 0 );
		range.setEnd( marker.getFirst(), 3 );
		range.select();

		var sel = new CKEDITOR.dom.selection( editable );
		sel.scrollIntoView();

		// Check the selection is really scrolled into view.
		assertElementInViewport( marker );

		// Make sure the scrollInto view doesn't destroy the selection.
		assert.areSame( 'foobar', sel.getSelectedText() );
	},

	'test cloning - standalone selection': function() {
		makeSelection( 'foo[bar]bom' );

		var editable = doc.getById( 'sandbox' ),
			sel = new CKEDITOR.dom.selection( editable ),
			initialRev = sel.rev,
			clone = new CKEDITOR.dom.selection( sel );

		assert.areSame( sel.document.$, clone.document.$, 'document' );
		assert.areSame( sel.root, clone.root, 'root' );
		assert.areSame( sel.isLocked, clone.isLocked, 'isLocked' );
		assert.areSame( sel.isFake, clone.isFake, 'isFake' );
		assert.areNotSame( sel._.cache, clone._.cache, 'cache' );
		assert.areSame( sel.rev, clone.rev, 'revision' );
		assertSelectionsAreEqual( clone, sel );

		var range = new CKEDITOR.dom.range( editable );
		range.setStart( editable, 0 );
		range.setEndAt( editable, CKEDITOR.POSITION_BEFORE_END );
		sel.selectRanges( [ range ] );

		assert.areNotSame( sel.getSelectedText(), clone.getSelectedText(), 'getSelectedText()' );
		assert.areSame( initialRev, clone.rev, 'Clone\'s revision has not been modified' );
	},

	'test cloning - editor selection': function() {
		bender.tools.setHtmlWithSelection( this.editor, '<p>foo[bar]bom</p>' );

		var sel = this.editor.getSelection(),
			clone = new CKEDITOR.dom.selection( sel );

		assert.areSame( sel.document.$, clone.document.$, 'document' );
		assert.areSame( sel.root, clone.root, 'root' );
		assert.areSame( sel.isLocked, clone.isLocked, 'isLocked' );
		assert.areSame( sel.isFake, clone.isFake, 'isFake' );
		assert.areNotSame( sel._.cache, clone._.cache, 'cache' );
		assert.areSame( sel.rev, clone.rev, 'revision' );
		assertSelectionsAreEqual( clone, sel );
	},

	'test cloning - locked selection': function() {
		makeSelection( 'foo[bar]bom' );

		var editable = doc.getById( 'sandbox' ),
			sel = new CKEDITOR.dom.selection( editable );

		sel.lock();

		var clone = new CKEDITOR.dom.selection( sel );

		assert.areSame( sel.document.$, clone.document.$, 'document' );
		assert.areSame( sel.root, clone.root, 'root' );
		assert.areSame( sel.isLocked, clone.isLocked, 'isLocked' );
		assert.areSame( sel.isFake, clone.isFake, 'isFake' );
		assert.areNotSame( sel._.cache, clone._.cache, 'cache' );
		assert.areSame( sel.rev, clone.rev, 'revision' );
		assertSelectionsAreEqual( clone, sel );
	},

	'test cloning - fake selection': function() {
		var editor = this.editor;

		bender.tools.setHtmlWithSelection( editor, '<p>[foo] <span id="bar">bar</span></p>' );

		var el = editor.document.getById( 'bar' );

		var sel = editor.getSelection();
		sel.fake( el );

		var clone = new CKEDITOR.dom.selection( sel );

		assert.areSame( sel.document.$, clone.document.$, 'document' );
		assert.areSame( sel.root, clone.root, 'root' );
		assert.areSame( sel.isLocked, clone.isLocked, 'isLocked' );
		assert.areSame( sel.isFake, clone.isFake, 'isFake' );
		assert.areNotSame( sel._.cache, clone._.cache, 'cache' );
		assert.areSame( sel.rev, clone.rev, 'revision' );
		assertSelectionsAreEqual( clone, sel );
	},

	'test revision after executing getters': function() {
		makeSelection( 'foo[bar]bom' );

		var sel = new CKEDITOR.dom.selection( doc ),
			initialRev = sel.rev;

		sel.getCommonAncestor();
		sel.getNative();
		sel.getRanges();
		sel.getRanges( true );
		sel.getSelectedElement();
		sel.getSelectedText();
		sel.getStartElement();
		sel.getType();

		assert.areSame( initialRev, sel.rev, 'Revision has not been modified' );
	}
} );