File: editable.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 (178 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (4)
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
/* bender-tags: editor */

var testEditable = CKEDITOR.tools.createClass( {
	base: CKEDITOR.editable,
	_: {},
	proto: {
		insertHtml: function( data ) {
			this._.html = data;
		},

		insertElement: function( element ) {
			this._.element = element;
		},

		insertText: function( text ) {
			this._.text = text;
		},

		setData: function( data, isSnapshot ) {
			this._[ isSnapshot ? 'snapshot' : 'data' ] = data;
		},

		getData: function( isSnapshot ) {
			return this._[ isSnapshot ? 'snapshot' : 'data' ] || '';
		},

		focus: function() {
			this._.focus = true;
		},
		detach: function() {
			var _ = this._;
			_.html = _.element = _.text = _.snapshot = _.data = _.focus = undefined;
			testEditable.baseProto.detach.call( this );
		}
	}
} );

var doc = CKEDITOR.document;

bender.test(
{
	// Initialize the editor instance.
	'async:init': function() {
		var tc = this;
		var editor = new CKEDITOR.editor();
		editor.on( 'loaded', function() {
			editor.editable( new testEditable( editor, doc.getBody() ) );
			tc.editor = editor;
			tc.callback();
		} );
	},

	// Test all editable APIs.
	testEditable: function() {
		var editable = this.editor.editable();
		assert.areSame( editable.$, doc.getBody().$ );
		assert.areSame( this.editor, editable.editor );

		this.editor.focus();
		assert.isTrue( editable._.focus );

		this.editor.setData( 'foo' );
		assert.areSame( 'foo', editable._.data, 'set data' );
		assert.areSame( 'foo', this.editor.getData(), 'retrieve data' );

		this.editor.insertElement( 'foo' );
		this.editor.insertText( 'foo' );
		this.editor.insertHtml( 'foo' );

		assert.areSame( 'foo', editable._.element, 'insert element' );
		assert.areSame( 'foo', editable._.text, 'insert text' );
		assert.areSame( 'foo', editable._.text, 'insert html' );
	},

	// Test editable destruction.
	testDetach: function() {
		var editable = this.editor.editable();
		this.editor.editable( null );

		this.editor.focus();
		this.editor.setData( 'foo' );
		this.editor.insertElement( 'foo' );
		this.editor.insertText( 'bar' );
		this.editor.insertHtml( 'bar' );

		// Check all delegations has ceased after destroy.
		assert.isUndefined( editable._.focus );
		assert.isUndefined( editable._.data );
		assert.isUndefined( editable._.element );
		assert.isUndefined( editable._.text );
	},

	'test listeners attaching/detaching': function() {
		var editor = this.editor,
			editable = new testEditable( editor, doc.getBody() ),
			obj = {},
			fired = '';

		editor.editable( editable );

		CKEDITOR.event.implementOn( obj );

		editable.attachListener( obj, 'testEvent', function() {
			fired += '1';
		} );
		editable.attachListener( obj, 'testEvent', function() {
			fired += '2';
		}, null, null, 1 );
		obj.on( 'testEvent', function() {
			fired += '3';
		} );

		obj.fire( 'testEvent' );
		assert.areEqual( '213', fired, 'All 3 fired, but in priorities order' );

		fired = '';
		editor.editable( null ); // Detach editable.

		obj.fire( 'testEvent' );
		assert.areEqual( '3', fired, 'Both listeners were removed, but directly attached remained' );
	},

	'test manual listener detaching': function() {
		var editor = this.editor,
			editable = new testEditable( editor, doc.getBody() ),
			obj = {},
			fired = 0;

		editor.editable( editable );

		CKEDITOR.event.implementOn( obj );

		var listener = editable.attachListener( obj, 'testEvent', function() {
			fired++;
		} );

		editable.attachListener( obj, 'testEvent2', function() {
			fired++;
		} );

		obj.fire( 'testEvent' );
		assert.areEqual( 1, fired, 'Caught testEvent' );

		listener.removeListener();

		obj.fire( 'testEvent' );
		assert.areEqual( 1, fired, 'Have not caught testEvent' );

		editor.editable( null ); // Detach editable.

		// Test whether all listeners were unbound correctly.
		obj.fire( 'testEvent2' );
		assert.areEqual( 1, fired, 'Have not caught testEvent2' );
	},

	'test dblclick is forwarded to editor#doubleclick': function() {
		var editor = this.editor,
			editable = new testEditable( editor, doc.getBody() ),
			fired,
			target;

		editor.editable( editable );

		editor.on( 'doubleclick', function( evt ) {
			fired = true;
			target = evt.data.element;

			// We need to cancel it because in this mocked editable there's no selection,
			// some some listening plugins may fail.
			evt.cancel();
		}, null, null, -999 );

		editor.editable().fire( 'dblclick', new CKEDITOR.dom.event( { target: editor.editable().$ } ) );

		assert.isTrue( fired, 'editor#doubleclick was fired' );
		assert.areSame( editor.editable(), target, 'data.element was set' );
	}
} );