1
YUI.add('editor-tests', function(Y) {
6
fireKey = function(editor, key) {
7
var inst = editor.getInstance();
8
inst.one('body').simulate('keydown', {
12
inst.one('body').simulate('keypress', {
16
inst.one('body').simulate('keyup', {
25
tearDown : function() {
27
test_load: function() {
28
Y.Assert.isObject(Y.Frame, 'EditorBase was not loaded');
29
Y.Assert.isObject(Y.EditorBase, 'EditorBase was not loaded');
31
test_frame: function() {
32
var iframeReady = false;
34
iframe = new Y.Frame({
37
content: 'This is a test.',
38
use: ['node','selector-css3', 'dd-drag', 'dd-ddm']
40
Y.Assert.isInstanceOf(Y.Frame, iframe, 'Iframe instance can not be created');
42
iframe.after('ready', function() {
47
this.wait(function() {
48
Y.Assert.isTrue(iframeReady, 'IFRAME ready event did not fire');
49
var inst = iframe.getInstance();
51
Y.Assert.isInstanceOf(YUI, inst, 'Internal instance not created');
52
Y.Assert.isObject(inst.DD.Drag, 'DD Not loaded inside the frame');
53
Y.Assert.isObject(inst.DD.DDM, 'DD Not loaded inside the frame');
59
test_frame_use: function() {
60
var inst = iframe.getInstance(),
63
iframe.use('slider', function() {
64
test.resume(function() {
65
Y.Assert.isObject(inst.Slider, 'Failed to load Slider inside frame object');
72
test_frame_general: function() {
73
var n = iframe.get('node');
74
var e = Y.one('#editor iframe');
75
Y.Assert.areSame(n, e, 'iframe node getter failed');
77
iframe._fixIECursors();
79
iframe.delegate('click', function() {});
81
var id = iframe.get('id');
82
Y.Assert.isTrue((id.indexOf('iframe-yui') === 0));
85
'test: _DOMPaste': function() {
86
var OT = 'ORIGINAL_TARGET',
89
var inst = iframe.getInstance(),
90
win = inst.config.win;
99
iframe.on('dom:paste', function(e) {
101
Y.Assert.areSame(e.clipboardData.data, 'foobar');
102
Y.Assert.areSame(e.clipboardData.getData(), 'foobar');
110
getData: function() {
117
Y.Assert.isTrue(fired);
119
inst.config.win = win;
122
test_frame_destroy: function() {
125
Y.Assert.isNull(Y.one('#editor iframe'), 'iframe DOM node was not destroyed');
127
test_editor: function() {
129
Y.EditorBase.USE.push('dd');
130
Y.EditorBase.USE.push('node-event-simulate');
131
var iframeReady = false;
133
editor = new Y.EditorBase({
134
content: 'Hello <b>World</b>!!',
135
extracss: 'b { color: red; }'
137
Y.Assert.isInstanceOf(Y.EditorBase, editor, 'EditorBase instance can not be created');
139
editor.after('ready', function() {
142
editor.on('nodeChange', function(e) {
156
Y.Assert.isTrue(events[e.changedType], 'NodeChange working for ' + e.changedType);
157
if (e.changedType !== 'execcommand') {
158
Y.Assert.isTrue(e.changedNode.test('b, body'), 'Changed Node');
162
editor.render('#editor');
167
this.wait(function() {
168
Y.Assert.isTrue(iframeReady, 'IFRAME ready event did not fire');
169
var inst = editor.getInstance();
171
Y.Assert.isInstanceOf(YUI, inst, 'Internal instance not created');
172
Y.Assert.isObject(inst.DD.Drag, 'DD Not loaded inside the frame');
173
Y.Assert.isObject(inst.DD.DDM, 'DD Not loaded inside the frame');
174
Y.Assert.areSame(Y.EditorBase.FILTER_RGB(inst.one('b').getStyle('color')), '#ff0000', 'Extra CSS Failed');
175
inst.one('body').simulate('mousedown', {
179
inst.one('b').simulate('mousedown');
180
inst.one('body').simulate('mouseup');
181
inst.one('b').simulate('mouseup');
187
test_copy_styles: function() {
189
var node = Y.Node.create('<b><u><div style="font-family: Arial; color: purple">Foo</div></u></b>'),
190
node2 = Y.Node.create('<div/>');
192
editor.copyStyles(node.one('div'), node2);
194
Y.Assert.areSame(node.one('div').getStyle('color'), node2.getStyle('color'), 'Style failed to copy');
195
Y.Assert.areSame(node.one('div').getStyle('fontFamily'), node2.getStyle('fontFamily'), 'Style failed to copy');
197
var node = Y.Node.create('<a>'),
198
node2 = Y.Node.create('<div/>');
200
editor.copyStyles(node, node2);
203
test_resolve_node: function() {
204
var inst = editor.getInstance();
205
var node = editor._resolveChangedNode(inst.one('html'));
207
Y.Assert.areNotSame(inst.one('html'), node, 'Failed to resolve HTML node');
209
var node = editor._resolveChangedNode(null);
210
Y.Assert.areSame(inst.one('body'), node, 'Failed to resolve HTML node');
212
test_get_content: function() {
213
var html = editor.getContent(),
214
ex = ((Y.UA.gecko) ? '<br>' : '');
216
html = html.replace(' style=""', '');
218
Y.Assert.areEqual(ex + 'Hello <b>World</b>!!'.toLowerCase(), html.toLowerCase(), 'getContent failed to get the editor content');
220
test_font_size_normalize: function() {
221
var n = Y.Node.create('<span style="font-size: -webkit-xxx-large"></span>');
223
if (Y.UA.webkit) { //Can't apply -webkit styles in something other than webkit, duh..
224
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
225
Y.Assert.areSame('48px', size, 'Failed to parse size');
228
n.setStyle('fontSize', 'xx-large');
229
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
230
Y.Assert.areSame('32px', size, 'Failed to parse size');
232
n.setStyle('fontSize', 'x-large');
233
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
234
Y.Assert.areSame('24px', size, 'Failed to parse size');
236
n.setStyle('fontSize', 'large');
237
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
238
Y.Assert.areSame('18px', size, 'Failed to parse size');
240
n.setStyle('fontSize', 'medium');
241
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
242
Y.Assert.areSame('16px', size, 'Failed to parse size');
244
n.setStyle('fontSize', 'small');
245
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
246
Y.Assert.areSame('13px', size, 'Failed to parse size');
248
n.setStyle('fontSize', 'x-small');
249
var size = Y.EditorBase.NORMALIZE_FONTSIZE(n);
250
Y.Assert.areSame('10px', size, 'Failed to parse size');
253
test_selection_font_removal: function() {
254
var inst = editor.getInstance();
255
var node = inst.Node.create('<font face="" style="foo: bar; font-family: ;"></font>');
256
inst.EditorSelection.removeFontFamily(node);
258
Y.Assert.areSame(node.getAttribute('face'), '', 'Failed to remove font face');
259
if (!Y.UA.ie || (Y.UA.ie && Y.UA.ie > 8)) {
260
//IE 6 doesn't like the getAttribute('style') call, it returns an object
261
Y.Assert.isTrue((node.getAttribute('style').indexOf('foo: bar') > -1), 'Failed to remove font-family ;');
262
Y.Assert.isTrue((node.getAttribute('style').indexOf('font-family') === -1), 'Failed to remove font-family ;');
265
node.setAttribute('style', 'font-family: ');
266
inst.EditorSelection.removeFontFamily(node);
267
Y.Assert.areSame(node.getAttribute('style'), '', 'Failed to remove style attribute');
269
test_gettext: function() {
270
var inst = editor.getInstance();
271
var node = inst.Node.create('<p><font><strong>This is <i>a test</i></strong></font>');
273
var text = inst.EditorSelection.getText(node);
274
Y.Assert.areSame('This is a test', text, 'Failed to filter out HTML');
276
test_selection_general: function() {
277
var inst = editor.getInstance();
279
var count = inst.EditorSelection.hasCursor();
280
Y.Assert.areSame(0, count, 'Cursor object found');
282
inst.EditorSelection.cleanCursor();
284
var count = inst.EditorSelection.hasCursor();
285
Y.Assert.areSame(0, count, 'Cursor object found');
287
test_selection_methods: function() {
288
var inst = editor.getInstance(),
289
sel = new inst.EditorSelection();
291
sel.insertContent('This is a test<br>');
292
editor.execCommand('inserthtml', 'This is another test<br>');
294
editor.execCommand('selectall');
295
editor.execCommand('wrap', 'div');
296
var html = editor.getContent().toLowerCase();
301
Y.Assert.isTrue(editor.getContent().indexOf('This is a test') > -1, 'Failed to insert content');
302
Y.Assert.isTrue(editor.getContent().indexOf('This is another test') > -1, 'Failed to insert content');
303
Y.Assert.isTrue(html.indexOf('<div>') > -1, 'Failed to wrap the content');
306
'test: EditorSelection': function() {
308
var inst = editor.getInstance(),
309
sel = new inst.EditorSelection(),
310
html = '<b>Foobar</b>',
311
node = inst.Node.create(html);
313
var n = sel._wrap(node, 'span');
314
Y.Assert.areSame('foobar', n.innerHTML.toLowerCase());
315
Y.Assert.areSame('span', n.tagName.toLowerCase());
317
var a = sel.anchorNode;
318
sel.anchorNode = node;
320
sel.replace('Foobar', 'davglass');
326
Y.Assert.areSame('EditorSelection Object', sel.toString());
329
test_execCommands: function() {
332
No Asserts here yet, this test is only to
333
show that there are no syntax errors thrown running
336
I still need to develop a way to properly test these commands
338
var inst = editor.getInstance();
339
var cmds = Y.Plugin.ExecCommand.COMMANDS;
341
Y.each(cmds, function(val, cmd) {
342
if (cmd !== 'bidi') {
343
editor.execCommand(cmd, '<b>Foo</b>');
347
var hc = inst.EditorSelection.hasCursor;
348
inst.EditorSelection.hasCursor = function() { return true };
350
Y.each(cmds, function(val, cmd) {
351
if (cmd !== 'bidi' && cmd != 'insertandfocus') {
352
editor.execCommand(cmd, '<b>Foo</b>');
355
inst.EditorSelection.hasCursor = hc;
356
editor.execCommand('insertandfocus', '<b>Foo</b>');
358
editor.frame._execCommand('bold', '');
361
var SEL = inst.EditorSelection;
362
inst.EditorSelection = function() {
364
sel.isCollapsed = false;
369
inst.EditorSelection[i] = SEL[i];
372
editor.execCommand('insertorderedlist', '');
374
inst.EditorSelection = SEL;
377
test_window: function() {
378
Y.Assert.areEqual(Y.Node.getDOMNode(Y.one('#editor iframe').get('contentWindow')), Y.Node.getDOMNode(editor.getInstance().one('win')), 'Window object is not right');
380
test_doc: function() {
381
Y.Assert.areEqual(Y.Node.getDOMNode(Y.one('#editor iframe').get('contentWindow.document')), Y.Node.getDOMNode(editor.getInstance().one('doc')), 'Document object is not right');
383
'test: selection.remove()': function() {
384
var inst = editor.getInstance(),
385
sel = new inst.EditorSelection();
389
test_destroy: function() {
391
Y.Assert.areEqual(Y.one('#editor iframe'), null, 'Frame was not destroyed');
393
test_br_plugin: function() {
394
editor = new Y.EditorBase({
395
content: 'Hello <b>World</b>!!',
396
extracss: 'b { color: red; }'
398
Y.Assert.isInstanceOf(Y.EditorBase, editor, 'Second EditorBase instance can not be created');
399
editor.plug(Y.Plugin.EditorBR);
400
editor.render('#editor');
401
Y.Assert.isInstanceOf(Y.Plugin.EditorBR, editor.editorBR, 'EditorBR was not plugged..');
402
editor.set('content', '<br>');
408
Y.Assert.areEqual(Y.one('#editor iframe'), null, 'Second Frame was not destroyed');
411
test_para_plugin: function() {
412
editor = new Y.EditorBase({
413
content: 'Hello <b>World</b>!!',
414
extracss: 'b { color: red; }'
416
Y.Assert.isInstanceOf(Y.EditorBase, editor, 'Third EditorBase instance can not be created');
417
editor.plug(Y.Plugin.EditorPara);
418
Y.Assert.isInstanceOf(Y.Plugin.EditorPara, editor.editorPara, 'EditorPara was not plugged..');
419
editor.render('#editor');
420
editor.set('content', '<br><b>Test This</b>');
422
var inst = editor.getInstance();
424
var str = '<b>foo</b>';
425
var out = editor.frame.exec._wrapContent(str);
426
Y.Assert.areEqual('<p><b>foo</b></p>', out);
428
var out = editor.frame.exec._wrapContent(str, true);
429
Y.Assert.areEqual('<b>foo</b><br>', out);
433
editor.editorPara._fixFirstPara();
434
editor.editorPara._afterPaste();
435
editor.editorPara._onNodeChange({
437
changedNode: inst.one('b'),
438
changedType: 'enter-up'
440
editor.editorPara._onNodeChange({
442
changedNode: inst.one('br'),
446
Y.Assert.areEqual(Y.one('#editor iframe'), null, 'Third Frame was not destroyed');
449
test_double_plug_setup: function() {
450
editor = new Y.EditorBase({
451
content: 'Hello <b>World</b>!!',
452
extracss: 'b { color: red; }'
454
Y.Assert.isInstanceOf(Y.EditorBase, editor, 'Forth EditorBase instance can not be created');
456
test_double_plug: function() {
457
editor.plug(Y.Plugin.EditorPara);
459
editor.plug(Y.Plugin.EditorBR);
461
test_double_down: function() {
462
Y.Assert.isInstanceOf(Y.Plugin.EditorPara, editor.editorPara, 'EditorPara was not plugged..');
463
editor.render('#editor');
465
Y.Assert.areEqual(Y.one('#editor frame'), null, 'Forth Frame was not destroyed');
467
test_double_plug_setup2: function() {
468
editor = new Y.EditorBase({
469
content: 'Hello <b>World</b>!!',
470
extracss: 'b { color: red; }'
472
Y.Assert.isInstanceOf(Y.EditorBase, editor, 'Fifth EditorBase instance can not be created');
474
test_double_plug2: function() {
475
editor.plug(Y.Plugin.EditorBR);
477
editor.plug(Y.Plugin.EditorPara);
479
test_double_down2: function() {
480
Y.Assert.isInstanceOf(Y.Plugin.EditorBR, editor.editorBR, 'EditorBR was not plugged..');
481
editor.render('#editor');
483
Y.Assert.areEqual(Y.one('#editor frame'), null, 'Fifth Frame was not destroyed');
485
test_bidi_noplug: function() {
486
editor = new Y.EditorBase({
487
content: 'Hello <b>World</b>!!',
488
extracss: 'b { color: red; }'
490
editor.render('#editor');
491
this.wait(function() {
493
editor.execCommand('bidi');
496
test_bidi_plug: function() {
498
editor = new Y.EditorBase({
499
content: 'Hello <b>World</b>!!',
500
extracss: 'b { color: red; }'
502
editor.plug(Y.Plugin.EditorPara);
503
editor.plug(Y.Plugin.EditorBidi);
504
editor.render('#editor');
505
Y.Assert.isInstanceOf(Y.Plugin.EditorBidi, editor.editorBidi, 'EditorBidi plugin failed to load');
508
var inst = editor.getInstance();
509
var sel = new inst.EditorSelection();
510
var b = inst.one('b');
511
Y.Assert.areEqual(b.get('parentNode').get('dir'), '', 'Default direction');
512
sel.selectNode(b, true, true);
513
editor.execCommand('bidi');
514
Y.Assert.areEqual(b.get('parentNode').get('dir'), 'rtl', 'RTL not added to node');
516
sel.selectNode(b, true, true);
517
editor.execCommand('bidi');
518
Y.Assert.areEqual(b.get('parentNode').get('dir'), 'ltr', 'LTR not added to node');
520
sel.selectNode(b, true, true);
521
editor.execCommand('bidi');
522
Y.Assert.areEqual(b.get('parentNode').get('dir'), 'rtl', 'RTL not added BACK to node');
524
editor.editorBidi._afterMouseUp();
525
editor.editorBidi._afterNodeChange({
526
changedType: 'end-up'
529
var out = Y.Plugin.EditorBidi.blockParent(inst.one('body').get('firstChild.firstChild'));
530
Y.Assert.isTrue(out.test('p'));
532
var out = Y.Plugin.EditorBidi.addParents([inst.one('body').get('firstChild')]);
533
Y.Assert.areEqual(1, out.length);
534
Y.Assert.isTrue(out[0].test('p'));
539
'test: EditorSelection': (Y.UA.chrome),
540
test_bidi_plug: (Y.UA.ie && Y.UA.ie === 9),
541
test_selection_methods: ((Y.UA.ie || Y.UA.webkit) ? true : false),
542
test_execCommands: ((Y.UA.webkit || (Y.UA.ie && Y.UA.ie === 9) || Y.UA.chrome) ? true : false)
545
error: { //These tests should error
546
'test: EditorSelection': (Y.UA.chrome),
547
test_selection_methods: (Y.UA.ie || Y.UA.webkit ? true : false),
548
test_execCommands: ((Y.UA.webkit || (Y.UA.ie && Y.UA.ie === 9) || Y.UA.chrome) ? true : false),
549
test_double_plug: true,
550
test_double_plug2: true,
551
test_bidi_noplug: true
556
var suite = new Y.Test.Suite("Editor");
558
suite.add(new Y.Test.Case(template));
559
Y.Test.Runner.add(suite);