/
insertion.js
149 lines (121 loc) · 4.41 KB
/
insertion.js
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
/* bender-tags: editor,unit,insertion */
( function() {
'use strict';
var doc = CKEDITOR.document,
tools = bender.tools;
bender.editor = {
config: {
autoParagraph: false,
allowedContent: true
}
};
bender.test( {
testInsertElement: function() {
var editor = this.editor;
// When editor has focus.
var ins = CKEDITOR.dom.element.createFromHtml( '<strong>baz</strong>', editor.document );
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
editor.insertElement( ins );
assert.areSame( 'foo<strong>baz</strong>bar', tools.compatHtml( editor.getData() ), 'insert element with existing selection, editor focused' );
// When editor loose focus.
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
ins = CKEDITOR.dom.element.createFromHtml( '<strong>baz</strong>', editor.document );
doc.getById( 'text_input' ).focus();
editor.insertElement( ins );
assert.areSame( 'foo<strong>baz</strong>bar', tools.compatHtml( editor.getData() ), 'insert element with existing selection, editor blurred' );
},
testInsertHtml: function() {
var editor = this.editor;
// When editor has focus.
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
editor.insertHtml( 'baz' );
assert.areSame( 'foobazbar', tools.compatHtml( editor.getData() ), 'insert html with existing selection, editor focused' );
// When editor loose focus.
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
doc.getById( 'text_input' ).focus();
editor.insertHtml( 'baz' );
assert.areSame( 'foobazbar', tools.compatHtml( editor.getData() ), 'insert html with existing selection, editor blurred' );
},
testInsertText: function() {
var editor = this.editor;
// When editor has focus.
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
editor.insertText( 'baz' );
assert.areSame( 'foobazbar', tools.compatHtml( editor.getData() ), 'insert text with existing selection, editor focused' );
// When editor loose focus.
bender.tools.setHtmlWithSelection( editor, 'foo^bar' );
doc.getById( 'text_input' ).focus();
editor.insertText( 'baz' );
assert.areSame( 'foobazbar', tools.compatHtml( editor.getData() ), 'insert text with existing selection, editor blurred' );
},
'test insertHtml without filter': function() {
bender.editorBot.create( {
name: 'test_inserthtml_no_acf',
config: {
removePlugins: 'basicstyles'
}
}, function( bot ) {
var editor = bot.editor;
editor.focus();
editor.insertHtml( '<em>A</em>B' );
assert.areSame( '<p>AB</p>', bot.getData(), '<em> has been filtered out' );
editor.insertHtml( '<em>C</em>D', 'unfiltered_html' );
assert.areSame( '<p>AB<em>C</em>D</p>', bot.getData(), '<em> has been inserted' );
} );
},
'test insertHtml uses editor.activeEnterMode': function() {
bender.editorBot.create( {
name: 'test_inserthtml_entermode',
config: {
allowedContent: true
}
}, function( bot ) {
var editor = bot.editor,
toHtml = 0,
mode;
editor.focus();
editor.on( 'toHtml', function( evt ) {
toHtml += 1;
mode = evt.data.enterMode;
} );
editor.setActiveEnterMode( CKEDITOR.ENTER_BR );
editor.insertHtml( 'foo' );
assert.areSame( CKEDITOR.ENTER_BR, mode, 'dynamic enter mode was used - BR' );
editor.setActiveEnterMode( null );
editor.insertHtml( 'foo' );
assert.areSame( CKEDITOR.ENTER_P, mode, 'dynamic enter mode was used - P' );
// Just to be sure that test is correct.
assert.areSame( 2, toHtml, 'toHtml was fired twice' );
} );
},
'test insertHtml without focus': function() {
bender.editorBot.create( {
name: 'test_inserthtml_no_focus',
config: {
allowedContent: true
}
}, function( bot ) {
bot.editor.insertHtml( '<p>foo</p>' );
assert.areSame( '<p>foo</p>', bot.editor.getData(), 'HTML was inserted' );
} );
},
'test insertText without focus': function() {
bender.editorBot.create( {
name: 'test_inserttext_no_focus',
config: {
allowedContent: true
}
}, function( bot ) {
bot.editor.insertText( 'bar' );
assert.areSame( '<p>bar</p>', bot.editor.getData(), 'text was inserted' );
} );
},
'test insertHtml sets options.protectedWhitespaces of dP.toHtml': function() {
var editor = this.editor;
editor.once( 'toHtml', function( evt ) {
assert.isTrue( evt.data.protectedWhitespaces );
} );
editor.insertHtml( ' foo ' );
}
} );
} )();