1 if (wysihtml5.browser.supported()) {
2 module("wysihtml5.Editor.commands", {
5 this.editableArea1 = document.createElement("div");
6 this.editableArea1.id = "wysihtml5-test-editable1";
7 this.editableArea1.className = "wysihtml5-test-class1";
8 this.editableArea1.title = "Please enter your foo";
9 this.editableArea1.innerHTML = "hey tiff, what's up?";
11 document.body.appendChild(this.editableArea1);
15 setCaretInsideNode: function(editor, el) {
16 var r1 = editor.composer.selection.createRange(),
17 e1 = el.childNodes[0];
20 editor.composer.selection.setSelection(r1);
23 teardown: function() {
25 this.editableArea1.parentNode.removeChild(this.editableArea1);
26 while (leftover = document.querySelector("div.wysihtml5-test-class1, iframe.wysihtml5-sandbox, div.wysihtml5-sandbox")) {
27 leftover.parentNode.removeChild(leftover);
29 document.body.className = this.originalBodyClassName;
32 equal: function(actual, expected, message) {
33 return QUnit.assert.htmlEqual(actual, expected, message);
38 // bold, italic, underline
39 asyncTest("Basic formating tests", function() {
42 text = "once upon a time there was an unformated text.",
50 editor = new wysihtml5.Editor(this.editableArea1, {
51 parserRules: parserRules
54 editor.on("load", function() {
55 var editableElement = that.editableArea1;
57 editor.setValue(text, true);
58 editor.composer.selection.selectNode(editor.editableElement);
59 editor.composer.commands.exec('bold');
60 equal(editableElement.innerHTML.toLowerCase(), "<b>" + text + "</b>", "Command bold sets text as bold correctly");
62 editor.composer.selection.getSelection().collapseToEnd();
64 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
66 editor.composer.commands.exec('bold');
67 editor.composer.selection.getSelection().collapseToEnd();
68 editor.composer.commands.exec('insertHtml', 'test');
69 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
70 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "<b>" + text + "</b>test", "With caret at last position bold is not removed but set to notbold at caret");
73 that.setCaretInsideNode(editor, editableElement.querySelector('b'));
74 editor.composer.commands.exec('bold');
76 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Bold is correctly removed when text caret is inside bold");
77 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
80 editor.setValue(text, true);
81 editor.composer.selection.selectNode(editor.editableElement);
82 editor.composer.commands.exec('italic');
83 equal(editableElement.innerHTML.toLowerCase(), "<i>" + text + "</i>", "Command italic sets text as italic correctly");
85 editor.composer.selection.getSelection().collapseToEnd();
86 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
88 editor.composer.commands.exec('italic');
89 editor.composer.selection.getSelection().collapseToEnd();
90 editor.composer.commands.exec('insertHtml', 'test');
92 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "<i>" + text + "</i>test", "With caret at last position italic is not removed but set to notitalic at caret");
93 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
95 that.setCaretInsideNode(editor, editableElement.querySelector('i'));
96 editor.composer.commands.exec('italic');
98 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Italic is correctly removed when text caret is inside italic");
99 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
102 editor.setValue(text, true);
103 editor.composer.selection.selectNode(editor.editableElement);
104 editor.composer.commands.exec('underline');
105 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "<u>" + text + "</u>", "Command underline sets text as underline correctly");
107 editor.composer.selection.getSelection().collapseToEnd();
108 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret is collapsed");
110 editor.composer.commands.exec('underline');
111 editor.composer.selection.getSelection().collapseToEnd();
112 editor.composer.commands.exec('insertHtml', 'test');
114 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), "<u>" + text + "</u>test", "With caret at last position underline is not removed but set to notunderline at caret");
115 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
117 that.setCaretInsideNode(editor, editableElement.querySelector('u'));
118 editor.composer.commands.exec('underline');
120 equal(editableElement.innerHTML.toLowerCase().replace(/\uFEFF/g, ''), text + "test", "Underline is correctly removed when text caret is inside underline");
121 ok(editor.composer.selection.getSelection().isCollapsed, "Text caret did remain collapsed");
127 // formatblock (alignment, headings, paragraph, pre, blockquote)
128 asyncTest("Format block", function() {
131 editor = new wysihtml5.Editor(this.editableArea1),
132 text = "once upon a time<br>there was an unformated text<br>spanning many lines.";
134 editor.on("load", function() {
135 var editableElement = that.editableArea1;
136 editor.setValue(text, true);
137 editor.composer.selection.selectNode(editor.editableElement);
138 editor.composer.commands.exec('justifyRight');
139 equal(editableElement.innerHTML.toLowerCase(), '<div class="wysiwyg-text-align-right">' + text + '</div>', "Text corectly wrapped in one aligning div");
141 editor.composer.commands.exec('justifyRight');
142 equal(editableElement.innerHTML.toLowerCase(), text, "Aligning div correctly removed");
144 editor.composer.selection.selectNode(editor.editableElement);
145 editor.composer.selection.getSelection().collapseToStart();
147 editor.composer.commands.exec('justifyRight');
148 editor.composer.selection.getSelection().collapseToStart();
149 equal(editableElement.innerHTML.toLowerCase(), '<div class="wysiwyg-text-align-right">once upon a time</div>there was an unformated text<br>spanning many lines.', "Only first line correctly wrapped in aligning div");
151 var node = editor.editableElement.querySelectorAll('.wysiwyg-text-align-right');
152 editor.composer.selection.selectNode(node[0].childNodes[0]);
153 editor.composer.commands.exec('justifyLeft');
154 equal(editableElement.innerHTML.toLowerCase(), '<div class="wysiwyg-text-align-left">once upon a time</div>there was an unformated text<br>spanning many lines.', "First line wrapper class changed correctly");
156 editor.composer.commands.exec('formatBlock', "h1");
157 equal(editableElement.innerHTML.toLowerCase(), '<h1 class="wysiwyg-text-align-left">once upon a time</h1>there was an unformated text<br>spanning many lines.', "Alignment div changed to heading ok");
159 editor.composer.commands.exec('formatBlock', "h1");
160 equal(editableElement.innerHTML.toLowerCase(), '<div class="wysiwyg-text-align-left">once upon a time</div>there was an unformated text<br>spanning many lines.', "heading back to div ok");
162 editor.composer.commands.exec('justifyRight');
163 editor.composer.commands.exec('formatBlock', "h1");
164 editor.composer.commands.exec('justifyRight');
165 equal(editableElement.innerHTML.toLowerCase(), '<h1>once upon a time</h1>there was an unformated text<br>spanning many lines.', "heading alignment removed sucessfully");
167 editor.composer.commands.exec('justifyRight');
168 editor.composer.commands.exec('formatBlock', "p");
169 editor.composer.commands.exec('justifyRight');
170 equal(editableElement.innerHTML.toLowerCase(), '<p>once upon a time</p>there was an unformated text<br>spanning many lines.', "heading alignment removed sucessfully");
172 editor.setValue(text, true);
173 editor.composer.selection.selectNode(editor.editableElement);
174 editor.composer.commands.exec('alignRightStyle');
175 equal(editableElement.innerHTML.toLowerCase(), '<div style="text-align: right;">' + text + '</div>', "Text corectly wrapped in one aligning div with style");
177 editor.composer.commands.exec('alignCenterStyle');
178 equal(editableElement.innerHTML.toLowerCase(), '<div style="text-align: center;">' + text + '</div>', "Alignment (style) changed correctly to center");
180 editor.composer.commands.exec('alignLeftStyle');
181 equal(editableElement.innerHTML.toLowerCase(), '<div style="text-align: left;">' + text + '</div>', "Alignment (style) changed correctly to left");
183 editor.composer.commands.exec('alignLeftStyle');
184 equal(editableElement.innerHTML.toLowerCase(), text, "Alignment (style) correctly removed");
191 asyncTest("Format code", function() {
194 editor = new wysihtml5.Editor(this.editableArea1),
195 text = "once upon a time there was an unformated text.";
197 editor.on("load", function() {
198 var editableElement = that.editableArea1;
199 editor.setValue(text, true);
201 editor.composer.selection.selectNode(editor.editableElement);
202 editor.composer.commands.exec('formatCode', 'language-html');
203 equal(editableElement.innerHTML.toLowerCase(), '<pre><code class="language-html">' + text + '</code></pre>', "Text corectly wrapped in pre and code and classname addded");
205 editor.composer.commands.exec('formatCode', 'language-html');
206 equal(editableElement.innerHTML.toLowerCase(), text, "Code block correctly removed");
212 // createLink/removeLink
213 asyncTest("Create/remove link", function() {
217 editor = new wysihtml5.Editor(this.editableArea1),
220 editor.on("load", function() {
221 var editableElement = that.editableArea1;
222 editor.setValue(text, true);
223 editor.composer.selection.selectNode(editor.editableElement);
226 editor.composer.commands.exec('createLink', {
227 "href": "http://test.com", "title": "test"
229 that.equal(editableElement.innerHTML.toLowerCase(), '<a href="http://test.com" title="test">' + text + '</a>', "Link added correctly");
232 editor.composer.selection.selectNode(editor.editableElement);
233 editor.composer.commands.exec('createLink', {
234 "href": "http://changed.com"
236 that.equal(editableElement.innerHTML.toLowerCase(), '<a href="http://changed.com">' + text + '</a>', "Link attributes changed correctly when createLink is executed on existing link");
239 editor.composer.selection.selectNode(editor.editableElement);
240 editor.composer.commands.exec('removeLink');
241 that.equal(editableElement.innerHTML, text, "Link remove correctly");
244 editor.composer.selection.selectNode(editor.editableElement);
245 editor.composer.selection.getSelection().collapseToStart();
246 editor.composer.commands.exec('createLink', {
247 "href": "http://test.com", "title": "test"
249 that.equal(editableElement.innerHTML.toLowerCase(), '<a href="http://test.com" title="test">http://test.com/</a> ' + text + '', "Link with caret added correctly");
256 asyncTest("Create table", function() {
259 editor = new wysihtml5.Editor(this.editableArea1),
262 editor.on("load", function() {
263 var editableElement = that.editableArea1,
264 expectText = '<table style="width: 100%;">' +
276 editor.setValue(text, true);
277 editor.composer.selection.selectNode(editor.editableElement);
278 editor.composer.commands.exec('createTable', {
281 tableStyle: "width: 100%;"
283 equal(editableElement.innerHTML.toLowerCase(), expectText, "Text corectly wrapped in one aligning div");
289 asyncTest("Create lists", function() {
292 editor = new wysihtml5.Editor(this.editableArea1),
295 editor.on("load", function() {
296 var editableElement = that.editableArea1,
297 expectText = '<ul><li></li></ul>',
298 expectTextBr = '<ul><li><br></li></ul>',
299 expectTextWithContents = '<ul><li>text</li></ul>',
300 expectTextWithContentsBr = '<ul><li>text<br></li></ul>',
301 expectOrdText = '<ol><li></li></ol>',
302 expectOrdTextBr = '<ol><li><br></li></ol>',
303 expectOrdTextWithContents = '<ol><li>text</li></ol>',
304 expectOrdTextWithContentsBr = '<ol><li>text<br></li></ol>';
306 editor.setValue(text, true);
307 editor.composer.selection.selectNode(editor.editableElement);
308 editor.composer.commands.exec('insertUnorderedList');
309 ok(editableElement.innerHTML.toLowerCase() == expectText || editableElement.innerHTML.toLowerCase() == expectTextBr, "Unordered list created");
311 editor.composer.commands.exec('insertHTML', 'text');
312 ok(editableElement.innerHTML.toLowerCase() == expectTextWithContents || editableElement.innerHTML.toLowerCase() == expectTextWithContentsBr , "In unordered list placed caret correctly");
314 editor.setValue(text, true);
315 editor.composer.selection.selectNode(editor.editableElement);
316 editor.composer.commands.exec('insertOrderedList');
317 ok(editableElement.innerHTML.toLowerCase() == expectOrdText || editableElement.innerHTML.toLowerCase() == expectOrdTextBr, "Ordered list created");
319 editor.composer.commands.exec('insertHTML', 'text');
320 ok(editableElement.innerHTML.toLowerCase() == expectOrdTextWithContents || editableElement.innerHTML.toLowerCase() == expectOrdTextWithContentsBr, "In ordered list placed caret correctly");
322 editableElement.innerHTML = '<ul><li>test</li><li class="second">test</li><li>test</li></ul>';
323 editor.composer.selection.selectNode(editor.editableElement.querySelector('.second'));
324 editor.composer.commands.exec('indentList');
325 equal(editableElement.innerHTML.toLowerCase(), '<ul><li>test<ul><li class="second">test</li></ul></li><li>test</li></ul>', "List indent increases level correctly");
327 editor.composer.commands.exec('outdentList');
328 equal(editableElement.innerHTML.toLowerCase(), '<ul><li>test</li><li class="second">test</li><li>test</li></ul>', "List outdent decreases level correctly");
330 editor.composer.commands.exec('outdentList');
331 equal(editableElement.innerHTML.toLowerCase(), '<ul><li>test</li></ul><br>test<ul><li>test</li></ul>', "List outdent escapes current list item correctly out of list");
340 asyncTest("Create blockquote", function() {
343 editor = new wysihtml5.Editor(this.editableArea1, {
352 text = "<h1>heading</h1><p>text</p>",
353 text2 = "test<h1>heading</h1>test";
355 editor.on("load", function() {
356 var editableElement = that.editableArea1;
358 editor.setValue(text, true);
360 editor.composer.selection.selectNode(editor.editableElement);
361 editor.composer.commands.exec('insertBlockQuote');
362 equal(editableElement.innerHTML.toLowerCase(), "<blockquote>" + text + "</blockquote>" , "Blockquote created with headings and paragraphs preserved.");
364 editor.composer.commands.exec('insertBlockQuote');
365 equal(editableElement.innerHTML.toLowerCase(), text, "Blockquote removed with headings and paragraphs preserved.");
368 editor.setValue(text2, true);
369 editor.composer.selection.selectNode(editor.editableElement.querySelector('h1'));
370 editor.composer.commands.exec('insertBlockQuote');
371 equal(editableElement.innerHTML.toLowerCase(), "test<blockquote><h1>heading</h1></blockquote>test" , "Blockquote created.");
373 editor.composer.commands.exec('insertBlockQuote');
374 equal(editableElement.innerHTML.toLowerCase(), "test<br><h1>heading</h1><br>test" , "Blockquote removed and line breaks added.");