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