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