]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/lists/plugin.js
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / lists / plugin.js
1 /**
2  * plugin.js
3  *
4  * Released under LGPL License.
5  * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /*global tinymce:true */
12 /*eslint consistent-this:0 */
13
14 tinymce.PluginManager.add('lists', function(editor) {
15         var self = this;
16
17         function isChildOfBody(elm) {
18                 return editor.$.contains(editor.getBody(), elm);
19         }
20
21         function isBr(node) {
22                 return node && node.nodeName == 'BR';
23         }
24
25         function isListNode(node) {
26                 return node && (/^(OL|UL|DL)$/).test(node.nodeName) && isChildOfBody(node);
27         }
28
29         function isFirstChild(node) {
30                 return node.parentNode.firstChild == node;
31         }
32
33         function isLastChild(node) {
34                 return node.parentNode.lastChild == node;
35         }
36
37         function isTextBlock(node) {
38                 return node && !!editor.schema.getTextBlockElements()[node.nodeName];
39         }
40
41         function isEditorBody(elm) {
42                 return elm === editor.getBody();
43         }
44
45         editor.on('init', function() {
46                 var dom = editor.dom, selection = editor.selection;
47
48                 function isEmpty(elm, keepBookmarks) {
49                         var empty = dom.isEmpty(elm);
50
51                         if (keepBookmarks && dom.select('span[data-mce-type=bookmark]').length > 0) {
52                                 return false;
53                         }
54
55                         return empty;
56                 }
57
58                 /**
59                  * Returns a range bookmark. This will convert indexed bookmarks into temporary span elements with
60                  * index 0 so that they can be restored properly after the DOM has been modified. Text bookmarks will not have spans
61                  * added to them since they can be restored after a dom operation.
62                  *
63                  * So this: <p><b>|</b><b>|</b></p>
64                  * becomes: <p><b><span data-mce-type="bookmark">|</span></b><b data-mce-type="bookmark">|</span></b></p>
65                  *
66                  * @param  {DOMRange} rng DOM Range to get bookmark on.
67                  * @return {Object} Bookmark object.
68                  */
69                 function createBookmark(rng) {
70                         var bookmark = {};
71
72                         function setupEndPoint(start) {
73                                 var offsetNode, container, offset;
74
75                                 container = rng[start ? 'startContainer' : 'endContainer'];
76                                 offset = rng[start ? 'startOffset' : 'endOffset'];
77
78                                 if (container.nodeType == 1) {
79                                         offsetNode = dom.create('span', {'data-mce-type': 'bookmark'});
80
81                                         if (container.hasChildNodes()) {
82                                                 offset = Math.min(offset, container.childNodes.length - 1);
83
84                                                 if (start) {
85                                                         container.insertBefore(offsetNode, container.childNodes[offset]);
86                                                 } else {
87                                                         dom.insertAfter(offsetNode, container.childNodes[offset]);
88                                                 }
89                                         } else {
90                                                 container.appendChild(offsetNode);
91                                         }
92
93                                         container = offsetNode;
94                                         offset = 0;
95                                 }
96
97                                 bookmark[start ? 'startContainer' : 'endContainer'] = container;
98                                 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
99                         }
100
101                         setupEndPoint(true);
102
103                         if (!rng.collapsed) {
104                                 setupEndPoint();
105                         }
106
107                         return bookmark;
108                 }
109
110                 /**
111                  * Moves the selection to the current bookmark and removes any selection container wrappers.
112                  *
113                  * @param {Object} bookmark Bookmark object to move selection to.
114                  */
115                 function moveToBookmark(bookmark) {
116                         function restoreEndPoint(start) {
117                                 var container, offset, node;
118
119                                 function nodeIndex(container) {
120                                         var node = container.parentNode.firstChild, idx = 0;
121
122                                         while (node) {
123                                                 if (node == container) {
124                                                         return idx;
125                                                 }
126
127                                                 // Skip data-mce-type=bookmark nodes
128                                                 if (node.nodeType != 1 || node.getAttribute('data-mce-type') != 'bookmark') {
129                                                         idx++;
130                                                 }
131
132                                                 node = node.nextSibling;
133                                         }
134
135                                         return -1;
136                                 }
137
138                                 container = node = bookmark[start ? 'startContainer' : 'endContainer'];
139                                 offset = bookmark[start ? 'startOffset' : 'endOffset'];
140
141                                 if (!container) {
142                                         return;
143                                 }
144
145                                 if (container.nodeType == 1) {
146                                         offset = nodeIndex(container);
147                                         container = container.parentNode;
148                                         dom.remove(node);
149                                 }
150
151                                 bookmark[start ? 'startContainer' : 'endContainer'] = container;
152                                 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
153                         }
154
155                         restoreEndPoint(true);
156                         restoreEndPoint();
157
158                         var rng = dom.createRng();
159
160                         rng.setStart(bookmark.startContainer, bookmark.startOffset);
161
162                         if (bookmark.endContainer) {
163                                 rng.setEnd(bookmark.endContainer, bookmark.endOffset);
164                         }
165
166                         selection.setRng(rng);
167                 }
168
169                 function createNewTextBlock(contentNode, blockName) {
170                         var node, textBlock, fragment = dom.createFragment(), hasContentNode;
171                         var blockElements = editor.schema.getBlockElements();
172
173                         if (editor.settings.forced_root_block) {
174                                 blockName = blockName || editor.settings.forced_root_block;
175                         }
176
177                         if (blockName) {
178                                 textBlock = dom.create(blockName);
179
180                                 if (textBlock.tagName === editor.settings.forced_root_block) {
181                                         dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
182                                 }
183
184                                 fragment.appendChild(textBlock);
185                         }
186
187                         if (contentNode) {
188                                 while ((node = contentNode.firstChild)) {
189                                         var nodeName = node.nodeName;
190
191                                         if (!hasContentNode && (nodeName != 'SPAN' || node.getAttribute('data-mce-type') != 'bookmark')) {
192                                                 hasContentNode = true;
193                                         }
194
195                                         if (blockElements[nodeName]) {
196                                                 fragment.appendChild(node);
197                                                 textBlock = null;
198                                         } else {
199                                                 if (blockName) {
200                                                         if (!textBlock) {
201                                                                 textBlock = dom.create(blockName);
202                                                                 fragment.appendChild(textBlock);
203                                                         }
204
205                                                         textBlock.appendChild(node);
206                                                 } else {
207                                                         fragment.appendChild(node);
208                                                 }
209                                         }
210                                 }
211                         }
212
213                         if (!editor.settings.forced_root_block) {
214                                 fragment.appendChild(dom.create('br'));
215                         } else {
216                                 // BR is needed in empty blocks on non IE browsers
217                                 if (!hasContentNode && (!tinymce.Env.ie || tinymce.Env.ie > 10)) {
218                                         textBlock.appendChild(dom.create('br', {'data-mce-bogus': '1'}));
219                                 }
220                         }
221
222                         return fragment;
223                 }
224
225                 function getSelectedListItems() {
226                         return tinymce.grep(selection.getSelectedBlocks(), function(block) {
227                                 return /^(LI|DT|DD)$/.test(block.nodeName);
228                         });
229                 }
230
231                 function splitList(ul, li, newBlock) {
232                         var tmpRng, fragment, bookmarks, node;
233
234                         function removeAndKeepBookmarks(targetNode) {
235                                 tinymce.each(bookmarks, function(node) {
236                                         targetNode.parentNode.insertBefore(node, li.parentNode);
237                                 });
238
239                                 dom.remove(targetNode);
240                         }
241
242                         bookmarks = dom.select('span[data-mce-type="bookmark"]', ul);
243                         newBlock = newBlock || createNewTextBlock(li);
244                         tmpRng = dom.createRng();
245                         tmpRng.setStartAfter(li);
246                         tmpRng.setEndAfter(ul);
247                         fragment = tmpRng.extractContents();
248
249                         for (node = fragment.firstChild; node; node = node.firstChild) {
250                                 if (node.nodeName == 'LI' && dom.isEmpty(node)) {
251                                         dom.remove(node);
252                                         break;
253                                 }
254                         }
255
256                         if (!dom.isEmpty(fragment)) {
257                                 dom.insertAfter(fragment, ul);
258                         }
259
260                         dom.insertAfter(newBlock, ul);
261
262                         if (isEmpty(li.parentNode)) {
263                                 removeAndKeepBookmarks(li.parentNode);
264                         }
265
266                         dom.remove(li);
267
268                         if (isEmpty(ul)) {
269                                 dom.remove(ul);
270                         }
271                 }
272
273                 function mergeWithAdjacentLists(listBlock) {
274                         var sibling, node;
275
276                         sibling = listBlock.nextSibling;
277                         if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
278                                 while ((node = sibling.firstChild)) {
279                                         listBlock.appendChild(node);
280                                 }
281
282                                 dom.remove(sibling);
283                         }
284
285                         sibling = listBlock.previousSibling;
286                         if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
287                                 while ((node = sibling.firstChild)) {
288                                         listBlock.insertBefore(node, listBlock.firstChild);
289                                 }
290
291                                 dom.remove(sibling);
292                         }
293                 }
294
295                 /**
296                  * Normalizes the all lists in the specified element.
297                  */
298                 function normalizeList(element) {
299                         tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
300                                 var sibling, parentNode = ul.parentNode;
301
302                                 // Move UL/OL to previous LI if it's the only child of a LI
303                                 if (parentNode.nodeName == 'LI' && parentNode.firstChild == ul) {
304                                         sibling = parentNode.previousSibling;
305                                         if (sibling && sibling.nodeName == 'LI') {
306                                                 sibling.appendChild(ul);
307
308                                                 if (isEmpty(parentNode)) {
309                                                         dom.remove(parentNode);
310                                                 }
311                                         }
312                                 }
313
314                                 // Append OL/UL to previous LI if it's in a parent OL/UL i.e. old HTML4
315                                 if (isListNode(parentNode)) {
316                                         sibling = parentNode.previousSibling;
317                                         if (sibling && sibling.nodeName == 'LI') {
318                                                 sibling.appendChild(ul);
319                                         }
320                                 }
321                         });
322                 }
323
324                 function outdent(li) {
325                         var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
326
327                         function removeEmptyLi(li) {
328                                 if (isEmpty(li)) {
329                                         dom.remove(li);
330                                 }
331                         }
332
333                         if (isEditorBody(ul)) {
334                                 return true;
335                         }
336
337                         if (li.nodeName == 'DD') {
338                                 dom.rename(li, 'DT');
339                                 return true;
340                         }
341
342                         if (isFirstChild(li) && isLastChild(li)) {
343                                 if (ulParent.nodeName == "LI") {
344                                         dom.insertAfter(li, ulParent);
345                                         removeEmptyLi(ulParent);
346                                         dom.remove(ul);
347                                 } else if (isListNode(ulParent)) {
348                                         dom.remove(ul, true);
349                                 } else {
350                                         ulParent.insertBefore(createNewTextBlock(li), ul);
351                                         dom.remove(ul);
352                                 }
353
354                                 return true;
355                         } else if (isFirstChild(li)) {
356                                 if (ulParent.nodeName == "LI") {
357                                         dom.insertAfter(li, ulParent);
358                                         li.appendChild(ul);
359                                         removeEmptyLi(ulParent);
360                                 } else if (isListNode(ulParent)) {
361                                         ulParent.insertBefore(li, ul);
362                                 } else {
363                                         ulParent.insertBefore(createNewTextBlock(li), ul);
364                                         dom.remove(li);
365                                 }
366
367                                 return true;
368                         } else if (isLastChild(li)) {
369                                 if (ulParent.nodeName == "LI") {
370                                         dom.insertAfter(li, ulParent);
371                                 } else if (isListNode(ulParent)) {
372                                         dom.insertAfter(li, ul);
373                                 } else {
374                                         dom.insertAfter(createNewTextBlock(li), ul);
375                                         dom.remove(li);
376                                 }
377
378                                 return true;
379                         }
380
381                         if (ulParent.nodeName == 'LI') {
382                                 ul = ulParent;
383                                 newBlock = createNewTextBlock(li, 'LI');
384                         } else if (isListNode(ulParent)) {
385                                 newBlock = createNewTextBlock(li, 'LI');
386                         } else {
387                                 newBlock = createNewTextBlock(li);
388                         }
389
390                         splitList(ul, li, newBlock);
391                         normalizeList(ul.parentNode);
392
393                         return true;
394                 }
395
396                 function indent(li) {
397                         var sibling, newList;
398
399                         function mergeLists(from, to) {
400                                 var node;
401
402                                 if (isListNode(from)) {
403                                         while ((node = li.lastChild.firstChild)) {
404                                                 to.appendChild(node);
405                                         }
406
407                                         dom.remove(from);
408                                 }
409                         }
410
411                         if (li.nodeName == 'DT') {
412                                 dom.rename(li, 'DD');
413                                 return true;
414                         }
415
416                         sibling = li.previousSibling;
417
418                         if (sibling && isListNode(sibling)) {
419                                 sibling.appendChild(li);
420                                 return true;
421                         }
422
423                         if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
424                                 sibling.lastChild.appendChild(li);
425                                 mergeLists(li.lastChild, sibling.lastChild);
426                                 return true;
427                         }
428
429                         sibling = li.nextSibling;
430
431                         if (sibling && isListNode(sibling)) {
432                                 sibling.insertBefore(li, sibling.firstChild);
433                                 return true;
434                         }
435
436                         /*if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
437                                 return false;
438                         }*/
439
440                         sibling = li.previousSibling;
441                         if (sibling && sibling.nodeName == 'LI') {
442                                 newList = dom.create(li.parentNode.nodeName);
443                                 sibling.appendChild(newList);
444                                 newList.appendChild(li);
445                                 mergeLists(li.lastChild, newList);
446                                 return true;
447                         }
448
449                         return false;
450                 }
451
452                 function indentSelection() {
453                         var listElements = getSelectedListItems();
454
455                         if (listElements.length) {
456                                 var bookmark = createBookmark(selection.getRng(true));
457
458                                 for (var i = 0; i < listElements.length; i++) {
459                                         if (!indent(listElements[i]) && i === 0) {
460                                                 break;
461                                         }
462                                 }
463
464                                 moveToBookmark(bookmark);
465                                 editor.nodeChanged();
466
467                                 return true;
468                         }
469                 }
470
471                 function outdentSelection() {
472                         var listElements = getSelectedListItems();
473
474                         if (listElements.length) {
475                                 var bookmark = createBookmark(selection.getRng(true));
476                                 var i, y, root = editor.getBody();
477
478                                 i = listElements.length;
479                                 while (i--) {
480                                         var node = listElements[i].parentNode;
481
482                                         while (node && node != root) {
483                                                 y = listElements.length;
484                                                 while (y--) {
485                                                         if (listElements[y] === node) {
486                                                                 listElements.splice(i, 1);
487                                                                 break;
488                                                         }
489                                                 }
490
491                                                 node = node.parentNode;
492                                         }
493                                 }
494
495                                 for (i = 0; i < listElements.length; i++) {
496                                         if (!outdent(listElements[i]) && i === 0) {
497                                                 break;
498                                         }
499                                 }
500
501                                 moveToBookmark(bookmark);
502                                 editor.nodeChanged();
503
504                                 return true;
505                         }
506                 }
507
508                 function applyList(listName) {
509                         var rng = selection.getRng(true), bookmark, listItemName = 'LI';
510
511                         if (dom.getContentEditable(selection.getNode()) === "false") {
512                                 return;
513                         }
514
515                         listName = listName.toUpperCase();
516
517                         if (listName == 'DL') {
518                                 listItemName = 'DT';
519                         }
520
521                         function getSelectedTextBlocks() {
522                                 var textBlocks = [], root = editor.getBody();
523
524                                 function getEndPointNode(start) {
525                                         var container, offset;
526
527                                         container = rng[start ? 'startContainer' : 'endContainer'];
528                                         offset = rng[start ? 'startOffset' : 'endOffset'];
529
530                                         // Resolve node index
531                                         if (container.nodeType == 1) {
532                                                 container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
533                                         }
534
535                                         while (container.parentNode != root) {
536                                                 if (isTextBlock(container)) {
537                                                         return container;
538                                                 }
539
540                                                 if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
541                                                         return container;
542                                                 }
543
544                                                 container = container.parentNode;
545                                         }
546
547                                         return container;
548                                 }
549
550                                 var startNode = getEndPointNode(true);
551                                 var endNode = getEndPointNode();
552                                 var block, siblings = [];
553
554                                 for (var node = startNode; node; node = node.nextSibling) {
555                                         siblings.push(node);
556
557                                         if (node == endNode) {
558                                                 break;
559                                         }
560                                 }
561
562                                 tinymce.each(siblings, function(node) {
563                                         if (isTextBlock(node)) {
564                                                 textBlocks.push(node);
565                                                 block = null;
566                                                 return;
567                                         }
568
569                                         if (dom.isBlock(node) || isBr(node)) {
570                                                 if (isBr(node)) {
571                                                         dom.remove(node);
572                                                 }
573
574                                                 block = null;
575                                                 return;
576                                         }
577
578                                         var nextSibling = node.nextSibling;
579                                         if (tinymce.dom.BookmarkManager.isBookmarkNode(node)) {
580                                                 if (isTextBlock(nextSibling) || (!nextSibling && node.parentNode == root)) {
581                                                         block = null;
582                                                         return;
583                                                 }
584                                         }
585
586                                         if (!block) {
587                                                 block = dom.create('p');
588                                                 node.parentNode.insertBefore(block, node);
589                                                 textBlocks.push(block);
590                                         }
591
592                                         block.appendChild(node);
593                                 });
594
595                                 return textBlocks;
596                         }
597
598                         bookmark = createBookmark(rng);
599
600                         tinymce.each(getSelectedTextBlocks(), function(block) {
601                                 var listBlock, sibling;
602
603                                 sibling = block.previousSibling;
604                                 if (sibling && isListNode(sibling) && sibling.nodeName == listName) {
605                                         listBlock = sibling;
606                                         block = dom.rename(block, listItemName);
607                                         sibling.appendChild(block);
608                                 } else {
609                                         listBlock = dom.create(listName);
610                                         block.parentNode.insertBefore(listBlock, block);
611                                         listBlock.appendChild(block);
612                                         block = dom.rename(block, listItemName);
613                                 }
614
615                                 mergeWithAdjacentLists(listBlock);
616                         });
617
618                         moveToBookmark(bookmark);
619                 }
620
621                 function removeList() {
622                         var bookmark = createBookmark(selection.getRng(true)), root = editor.getBody();
623
624                         tinymce.each(getSelectedListItems(), function(li) {
625                                 var node, rootList;
626
627                                 if (isEditorBody(li.parentNode)) {
628                                         return;
629                                 }
630
631                                 if (isEmpty(li)) {
632                                         outdent(li);
633                                         return;
634                                 }
635
636                                 for (node = li; node && node != root; node = node.parentNode) {
637                                         if (isListNode(node)) {
638                                                 rootList = node;
639                                         }
640                                 }
641
642                                 splitList(rootList, li);
643                         });
644
645                         moveToBookmark(bookmark);
646                 }
647
648                 function toggleList(listName) {
649                         var parentList = dom.getParent(selection.getStart(), 'OL,UL,DL');
650
651                         if (isEditorBody(parentList)) {
652                                 return;
653                         }
654
655                         if (parentList) {
656                                 if (parentList.nodeName == listName) {
657                                         removeList(listName);
658                                 } else {
659                                         var bookmark = createBookmark(selection.getRng(true));
660                                         mergeWithAdjacentLists(dom.rename(parentList, listName));
661                                         moveToBookmark(bookmark);
662                                 }
663                         } else {
664                                 applyList(listName);
665                         }
666                 }
667
668                 function queryListCommandState(listName) {
669                         return function() {
670                                 var parentList = dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
671
672                                 return parentList && parentList.nodeName == listName;
673                         };
674                 }
675
676                 function isBogusBr(node) {
677                         if (!isBr(node)) {
678                                 return false;
679                         }
680
681                         if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
682                                 return true;
683                         }
684
685                         return false;
686                 }
687
688                 self.backspaceDelete = function(isForward) {
689                         function findNextCaretContainer(rng, isForward) {
690                                 var node = rng.startContainer, offset = rng.startOffset;
691                                 var nonEmptyBlocks, walker;
692
693                                 if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
694                                         return node;
695                                 }
696
697                                 nonEmptyBlocks = editor.schema.getNonEmptyElements();
698                                 if (node.nodeType == 1) {
699                                         node = tinymce.dom.RangeUtils.getNode(node, offset);
700                                 }
701
702                                 walker = new tinymce.dom.TreeWalker(node, editor.getBody());
703
704                                 // Delete at <li>|<br></li> then jump over the bogus br
705                                 if (isForward) {
706                                         if (isBogusBr(node)) {
707                                                 walker.next();
708                                         }
709                                 }
710
711                                 while ((node = walker[isForward ? 'next' : 'prev2']())) {
712                                         if (node.nodeName == 'LI' && !node.hasChildNodes()) {
713                                                 return node;
714                                         }
715
716                                         if (nonEmptyBlocks[node.nodeName]) {
717                                                 return node;
718                                         }
719
720                                         if (node.nodeType == 3 && node.data.length > 0) {
721                                                 return node;
722                                         }
723                                 }
724                         }
725
726                         function mergeLiElements(fromElm, toElm) {
727                                 var node, listNode, ul = fromElm.parentNode;
728
729                                 if (!isChildOfBody(fromElm) || !isChildOfBody(toElm)) {
730                                         return;
731                                 }
732
733                                 if (isListNode(toElm.lastChild)) {
734                                         listNode = toElm.lastChild;
735                                 }
736
737                                 if (ul == toElm.lastChild) {
738                                         if (isBr(ul.previousSibling)) {
739                                                 dom.remove(ul.previousSibling);
740                                         }
741                                 }
742
743                                 node = toElm.lastChild;
744                                 if (node && isBr(node) && fromElm.hasChildNodes()) {
745                                         dom.remove(node);
746                                 }
747
748                                 if (isEmpty(toElm, true)) {
749                                         dom.$(toElm).empty();
750                                 }
751
752                                 if (!isEmpty(fromElm, true)) {
753                                         while ((node = fromElm.firstChild)) {
754                                                 toElm.appendChild(node);
755                                         }
756                                 }
757
758                                 if (listNode) {
759                                         toElm.appendChild(listNode);
760                                 }
761
762                                 dom.remove(fromElm);
763
764                                 if (isEmpty(ul) && !isEditorBody(ul)) {
765                                         dom.remove(ul);
766                                 }
767                         }
768
769                         if (selection.isCollapsed()) {
770                                 var li = dom.getParent(selection.getStart(), 'LI'), ul, rng, otherLi;
771
772                                 if (li) {
773                                         ul = li.parentNode;
774                                         if (isEditorBody(ul) && dom.isEmpty(ul)) {
775                                                 return true;
776                                         }
777
778                                         rng = selection.getRng(true);
779                                         otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
780
781                                         if (otherLi && otherLi != li) {
782                                                 var bookmark = createBookmark(rng);
783
784                                                 if (isForward) {
785                                                         mergeLiElements(otherLi, li);
786                                                 } else {
787                                                         mergeLiElements(li, otherLi);
788                                                 }
789
790                                                 moveToBookmark(bookmark);
791
792                                                 return true;
793                                         } else if (!otherLi) {
794                                                 if (!isForward && removeList(ul.nodeName)) {
795                                                         return true;
796                                                 }
797                                         }
798                                 }
799                         }
800                 };
801
802                 editor.on('BeforeExecCommand', function(e) {
803                         var cmd = e.command.toLowerCase(), isHandled;
804
805                         if (cmd == "indent") {
806                                 if (indentSelection()) {
807                                         isHandled = true;
808                                 }
809                         } else if (cmd == "outdent") {
810                                 if (outdentSelection()) {
811                                         isHandled = true;
812                                 }
813                         }
814
815                         if (isHandled) {
816                                 editor.fire('ExecCommand', {command: e.command});
817                                 e.preventDefault();
818                                 return true;
819                         }
820                 });
821
822                 editor.addCommand('InsertUnorderedList', function() {
823                         toggleList('UL');
824                 });
825
826                 editor.addCommand('InsertOrderedList', function() {
827                         toggleList('OL');
828                 });
829
830                 editor.addCommand('InsertDefinitionList', function() {
831                         toggleList('DL');
832                 });
833
834                 editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState('UL'));
835                 editor.addQueryStateHandler('InsertOrderedList', queryListCommandState('OL'));
836                 editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState('DL'));
837
838                 editor.on('keydown', function(e) {
839                         // Check for tab but not ctrl/cmd+tab since it switches browser tabs
840                         if (e.keyCode != 9 || tinymce.util.VK.metaKeyPressed(e)) {
841                                 return;
842                         }
843
844                         if (editor.dom.getParent(editor.selection.getStart(), 'LI,DT,DD')) {
845                                 e.preventDefault();
846
847                                 if (e.shiftKey) {
848                                         outdentSelection();
849                                 } else {
850                                         indentSelection();
851                                 }
852                         }
853                 });
854         });
855
856         editor.addButton('indent', {
857                 icon: 'indent',
858                 title: 'Increase indent',
859                 cmd: 'Indent',
860                 onPostRender: function() {
861                         var ctrl = this;
862
863                         editor.on('nodechange', function() {
864                                 var blocks = editor.selection.getSelectedBlocks();
865                                 var disable = false;
866
867                                 for (var i = 0, l = blocks.length; !disable && i < l; i++) {
868                                         var tag = blocks[i].nodeName;
869
870                                         disable = (tag == 'LI' && isFirstChild(blocks[i]) || tag == 'UL' || tag == 'OL' || tag == 'DD');
871                                 }
872
873                                 ctrl.disabled(disable);
874                         });
875                 }
876         });
877
878         editor.on('keydown', function(e) {
879                 if (e.keyCode == tinymce.util.VK.BACKSPACE) {
880                         if (self.backspaceDelete()) {
881                                 e.preventDefault();
882                         }
883                 } else if (e.keyCode == tinymce.util.VK.DELETE) {
884                         if (self.backspaceDelete(true)) {
885                                 e.preventDefault();
886                         }
887                 }
888         });
889 });