if (wysihtml5.browser.supported()) {
module("wysihtml5.Editor.commands", {
setup: function() {
this.editableArea1 = document.createElement("div");
this.editableArea1.id = "wysihtml5-test-editable1";
this.editableArea1.className = "wysihtml5-test-class1";
this.editableArea1.title = "Please enter your foo";
this.editableArea1.innerHTML = "hey tiff, what's up?";
document.body.appendChild(this.editableArea1);
},
setCaretInsideNode: function(editor, el) {
var r1 = editor.composer.selection.createRange(),
e1 = el.childNodes[0];
r1.setEnd(e1, 1);
r1.setStart(e1, 1);
editor.composer.selection.setSelection(r1);
},
teardown: function() {
var leftover;
this.editableArea1.parentNode.removeChild(this.editableArea1);
while (leftover = document.querySelector("div.wysihtml5-test-class1, iframe.wysihtml5-sandbox, div.wysihtml5-sandbox")) {
leftover.parentNode.removeChild(leftover);
}
document.body.className = this.originalBodyClassName;
},
equal: function(actual, expected, message) {
return QUnit.assert.htmlEqual(actual, expected, message);
},
});
// bold, italic, underline
asyncTest("Basic formating tests", function() {
expect(18);
var that = this,
text = "once upon a time there was an unformated text.",
parserRules = {
tags: {
b: true,
i: true,
u: true
}
},
editor = new wysihtml5.Editor(this.editableArea1, {
parserRules: parserRules
});
editor.on("load", function() {
var editableElement = that.editableArea1;
// basic bold
editor.setValue(text, true);
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('bold');
equal(editableElement.innerHTML.toLowerCase(), "" + text + "", "Command bold sets text as bold correctly");
editor.composer.selection.getSelection().collapseToEnd();
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
editor.composer.commands.exec('bold');
editor.composer.selection.getSelection().collapseToEnd();
editor.composer.commands.exec('insertHtml', 'test');
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "" + text + "test", "With caret at last position bold is not removed but set to notbold at caret");
that.setCaretInsideNode(editor, editableElement.querySelector('b'));
editor.composer.commands.exec('bold');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Bold is correctly removed when text caret is inside bold");
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
// basic italic
editor.setValue(text, true);
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('italic');
equal(editableElement.innerHTML.toLowerCase(), "" + text + "", "Command italic sets text as italic correctly");
editor.composer.selection.getSelection().collapseToEnd();
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
editor.composer.commands.exec('italic');
editor.composer.selection.getSelection().collapseToEnd();
editor.composer.commands.exec('insertHtml', 'test');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "" + text + "test", "With caret at last position italic is not removed but set to notitalic at caret");
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
that.setCaretInsideNode(editor, editableElement.querySelector('i'));
editor.composer.commands.exec('italic');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Italic is correctly removed when text caret is inside italic");
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
// basic underline
editor.setValue(text, true);
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('underline');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "" + text + "", "Command underline sets text as underline correctly");
editor.composer.selection.getSelection().collapseToEnd();
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
editor.composer.commands.exec('underline');
editor.composer.selection.getSelection().collapseToEnd();
editor.composer.commands.exec('insertHtml', 'test');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "" + text + "test", "With caret at last position underline is not removed but set to notunderline at caret");
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
that.setCaretInsideNode(editor, editableElement.querySelector('u'));
editor.composer.commands.exec('underline');
equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Underline is correctly removed when text caret is inside underline");
ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
start();
});
});
// formatblock (alignment, headings, paragraph, pre, blockquote)
asyncTest("Format block", function() {
expect(12);
var that = this,
editor = new wysihtml5.Editor(this.editableArea1),
text = "once upon a time
there was an unformated text
spanning many lines.";
editor.on("load", function() {
var editableElement = that.editableArea1;
editor.setValue(text, true);
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('justifyRight');
equal(editableElement.innerHTML.toLowerCase(), '
once upon a time
there was an unformated text' + text + '', "Text corectly wrapped in pre and code and classname addded");
editor.composer.commands.exec('formatCode', 'language-html');
equal(editableElement.innerHTML.toLowerCase(), text, "Code block correctly removed");
start();
});
});
// createLink/removeLink
asyncTest("Create/remove link", function() {
expect(4);
var that = this,
editor = new wysihtml5.Editor(this.editableArea1),
text = "text";
editor.on("load", function() {
var editableElement = that.editableArea1;
editor.setValue(text, true);
editor.composer.selection.selectNode(editor.editableElement);
// Create
editor.composer.commands.exec('createLink', {
"href": "http://test.com", "title": "test"
});
that.equal(editableElement.innerHTML.toLowerCase(), '' + text + '', "Link added correctly");
// Change
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('createLink', {
"href": "http://changed.com"
});
that.equal(editableElement.innerHTML.toLowerCase(), '' + text + '', "Link attributes changed correctly when createLink is executed on existing link");
//Remove
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.commands.exec('removeLink');
that.equal(editableElement.innerHTML, text, "Link remove correctly");
// Create with caret
editor.composer.selection.selectNode(editor.editableElement);
editor.composer.selection.getSelection().collapseToStart();
editor.composer.commands.exec('createLink', {
"href": "http://test.com", "title": "test"
});
that.equal(editableElement.innerHTML.toLowerCase(), 'http://test.com/ ' + text + '', "Link with caret added correctly");
start();
});
});
// create table
asyncTest("Create table", function() {
expect(1);
var that = this,
editor = new wysihtml5.Editor(this.editableArea1),
text = "test";
editor.on("load", function() {
var editableElement = that.editableArea1,
expectText = '| ' + ' | ' + ' |
| ' + ' | ' + ' |
text
", text2 = "test" + text + "" , "Blockquote created with headings and paragraphs preserved."); editor.composer.commands.exec('insertBlockQuote'); equal(editableElement.innerHTML.toLowerCase(), text, "Blockquote removed with headings and paragraphs preserved."); editor.setValue(text2, true); editor.composer.selection.selectNode(editor.editableElement.querySelector('h1')); editor.composer.commands.exec('insertBlockQuote'); equal(editableElement.innerHTML.toLowerCase(), "test
test" , "Blockquote created."); editor.composer.commands.exec('insertBlockQuote'); equal(editableElement.innerHTML.toLowerCase(), "testheading