4 * Released under LGPL License.
5 * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 /*global tinymce:true */
12 /*eslint consistent-this:0 */
14 tinymce.PluginManager.add('lists', function(editor) {
17 function isChildOfBody(elm) {
18 return editor.$.contains(editor.getBody(), elm);
22 return node && node.nodeName == 'BR';
25 function isListNode(node) {
26 return node && (/^(OL|UL|DL)$/).test(node.nodeName) && isChildOfBody(node);
29 function isFirstChild(node) {
30 return node.parentNode.firstChild == node;
33 function isLastChild(node) {
34 return node.parentNode.lastChild == node;
37 function isTextBlock(node) {
38 return node && !!editor.schema.getTextBlockElements()[node.nodeName];
41 function isEditorBody(elm) {
42 return elm === editor.getBody();
45 editor.on('init', function() {
46 var dom = editor.dom, selection = editor.selection;
48 function isEmpty(elm, keepBookmarks) {
49 var empty = dom.isEmpty(elm);
51 if (keepBookmarks && dom.select('span[data-mce-type=bookmark]').length > 0) {
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.
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>
66 * @param {DOMRange} rng DOM Range to get bookmark on.
67 * @return {Object} Bookmark object.
69 function createBookmark(rng) {
72 function setupEndPoint(start) {
73 var offsetNode, container, offset;
75 container = rng[start ? 'startContainer' : 'endContainer'];
76 offset = rng[start ? 'startOffset' : 'endOffset'];
78 if (container.nodeType == 1) {
79 offsetNode = dom.create('span', {'data-mce-type': 'bookmark'});
81 if (container.hasChildNodes()) {
82 offset = Math.min(offset, container.childNodes.length - 1);
85 container.insertBefore(offsetNode, container.childNodes[offset]);
87 dom.insertAfter(offsetNode, container.childNodes[offset]);
90 container.appendChild(offsetNode);
93 container = offsetNode;
97 bookmark[start ? 'startContainer' : 'endContainer'] = container;
98 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
103 if (!rng.collapsed) {
111 * Moves the selection to the current bookmark and removes any selection container wrappers.
113 * @param {Object} bookmark Bookmark object to move selection to.
115 function moveToBookmark(bookmark) {
116 function restoreEndPoint(start) {
117 var container, offset, node;
119 function nodeIndex(container) {
120 var node = container.parentNode.firstChild, idx = 0;
123 if (node == container) {
127 // Skip data-mce-type=bookmark nodes
128 if (node.nodeType != 1 || node.getAttribute('data-mce-type') != 'bookmark') {
132 node = node.nextSibling;
138 container = node = bookmark[start ? 'startContainer' : 'endContainer'];
139 offset = bookmark[start ? 'startOffset' : 'endOffset'];
145 if (container.nodeType == 1) {
146 offset = nodeIndex(container);
147 container = container.parentNode;
151 bookmark[start ? 'startContainer' : 'endContainer'] = container;
152 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
155 restoreEndPoint(true);
158 var rng = dom.createRng();
160 rng.setStart(bookmark.startContainer, bookmark.startOffset);
162 if (bookmark.endContainer) {
163 rng.setEnd(bookmark.endContainer, bookmark.endOffset);
166 selection.setRng(rng);
169 function createNewTextBlock(contentNode, blockName) {
170 var node, textBlock, fragment = dom.createFragment(), hasContentNode;
171 var blockElements = editor.schema.getBlockElements();
173 if (editor.settings.forced_root_block) {
174 blockName = blockName || editor.settings.forced_root_block;
178 textBlock = dom.create(blockName);
180 if (textBlock.tagName === editor.settings.forced_root_block) {
181 dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
184 fragment.appendChild(textBlock);
188 while ((node = contentNode.firstChild)) {
189 var nodeName = node.nodeName;
191 if (!hasContentNode && (nodeName != 'SPAN' || node.getAttribute('data-mce-type') != 'bookmark')) {
192 hasContentNode = true;
195 if (blockElements[nodeName]) {
196 fragment.appendChild(node);
201 textBlock = dom.create(blockName);
202 fragment.appendChild(textBlock);
205 textBlock.appendChild(node);
207 fragment.appendChild(node);
213 if (!editor.settings.forced_root_block) {
214 fragment.appendChild(dom.create('br'));
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'}));
225 function getSelectedListItems() {
226 return tinymce.grep(selection.getSelectedBlocks(), function(block) {
227 return /^(LI|DT|DD)$/.test(block.nodeName);
231 function splitList(ul, li, newBlock) {
232 var tmpRng, fragment, bookmarks, node;
234 function removeAndKeepBookmarks(targetNode) {
235 tinymce.each(bookmarks, function(node) {
236 targetNode.parentNode.insertBefore(node, li.parentNode);
239 dom.remove(targetNode);
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();
249 for (node = fragment.firstChild; node; node = node.firstChild) {
250 if (node.nodeName == 'LI' && dom.isEmpty(node)) {
256 if (!dom.isEmpty(fragment)) {
257 dom.insertAfter(fragment, ul);
260 dom.insertAfter(newBlock, ul);
262 if (isEmpty(li.parentNode)) {
263 removeAndKeepBookmarks(li.parentNode);
273 function mergeWithAdjacentLists(listBlock) {
276 sibling = listBlock.nextSibling;
277 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
278 while ((node = sibling.firstChild)) {
279 listBlock.appendChild(node);
285 sibling = listBlock.previousSibling;
286 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
287 while ((node = sibling.firstChild)) {
288 listBlock.insertBefore(node, listBlock.firstChild);
296 * Normalizes the all lists in the specified element.
298 function normalizeList(element) {
299 tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
300 var sibling, parentNode = ul.parentNode;
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);
308 if (isEmpty(parentNode)) {
309 dom.remove(parentNode);
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);
324 function outdent(li) {
325 var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
327 function removeEmptyLi(li) {
333 if (isEditorBody(ul)) {
337 if (li.nodeName == 'DD') {
338 dom.rename(li, 'DT');
342 if (isFirstChild(li) && isLastChild(li)) {
343 if (ulParent.nodeName == "LI") {
344 dom.insertAfter(li, ulParent);
345 removeEmptyLi(ulParent);
347 } else if (isListNode(ulParent)) {
348 dom.remove(ul, true);
350 ulParent.insertBefore(createNewTextBlock(li), ul);
355 } else if (isFirstChild(li)) {
356 if (ulParent.nodeName == "LI") {
357 dom.insertAfter(li, ulParent);
359 removeEmptyLi(ulParent);
360 } else if (isListNode(ulParent)) {
361 ulParent.insertBefore(li, ul);
363 ulParent.insertBefore(createNewTextBlock(li), ul);
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);
374 dom.insertAfter(createNewTextBlock(li), ul);
381 if (ulParent.nodeName == 'LI') {
383 newBlock = createNewTextBlock(li, 'LI');
384 } else if (isListNode(ulParent)) {
385 newBlock = createNewTextBlock(li, 'LI');
387 newBlock = createNewTextBlock(li);
390 splitList(ul, li, newBlock);
391 normalizeList(ul.parentNode);
396 function indent(li) {
397 var sibling, newList;
399 function mergeLists(from, to) {
402 if (isListNode(from)) {
403 while ((node = li.lastChild.firstChild)) {
404 to.appendChild(node);
411 if (li.nodeName == 'DT') {
412 dom.rename(li, 'DD');
416 sibling = li.previousSibling;
418 if (sibling && isListNode(sibling)) {
419 sibling.appendChild(li);
423 if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
424 sibling.lastChild.appendChild(li);
425 mergeLists(li.lastChild, sibling.lastChild);
429 sibling = li.nextSibling;
431 if (sibling && isListNode(sibling)) {
432 sibling.insertBefore(li, sibling.firstChild);
436 /*if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
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);
452 function indentSelection() {
453 var listElements = getSelectedListItems();
455 if (listElements.length) {
456 var bookmark = createBookmark(selection.getRng(true));
458 for (var i = 0; i < listElements.length; i++) {
459 if (!indent(listElements[i]) && i === 0) {
464 moveToBookmark(bookmark);
465 editor.nodeChanged();
471 function outdentSelection() {
472 var listElements = getSelectedListItems();
474 if (listElements.length) {
475 var bookmark = createBookmark(selection.getRng(true));
476 var i, y, root = editor.getBody();
478 i = listElements.length;
480 var node = listElements[i].parentNode;
482 while (node && node != root) {
483 y = listElements.length;
485 if (listElements[y] === node) {
486 listElements.splice(i, 1);
491 node = node.parentNode;
495 for (i = 0; i < listElements.length; i++) {
496 if (!outdent(listElements[i]) && i === 0) {
501 moveToBookmark(bookmark);
502 editor.nodeChanged();
508 function applyList(listName) {
509 var rng = selection.getRng(true), bookmark, listItemName = 'LI';
511 if (dom.getContentEditable(selection.getNode()) === "false") {
515 listName = listName.toUpperCase();
517 if (listName == 'DL') {
521 function getSelectedTextBlocks() {
522 var textBlocks = [], root = editor.getBody();
524 function getEndPointNode(start) {
525 var container, offset;
527 container = rng[start ? 'startContainer' : 'endContainer'];
528 offset = rng[start ? 'startOffset' : 'endOffset'];
530 // Resolve node index
531 if (container.nodeType == 1) {
532 container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
535 while (container.parentNode != root) {
536 if (isTextBlock(container)) {
540 if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
544 container = container.parentNode;
550 var startNode = getEndPointNode(true);
551 var endNode = getEndPointNode();
552 var block, siblings = [];
554 for (var node = startNode; node; node = node.nextSibling) {
557 if (node == endNode) {
562 tinymce.each(siblings, function(node) {
563 if (isTextBlock(node)) {
564 textBlocks.push(node);
569 if (dom.isBlock(node) || isBr(node)) {
578 var nextSibling = node.nextSibling;
579 if (tinymce.dom.BookmarkManager.isBookmarkNode(node)) {
580 if (isTextBlock(nextSibling) || (!nextSibling && node.parentNode == root)) {
587 block = dom.create('p');
588 node.parentNode.insertBefore(block, node);
589 textBlocks.push(block);
592 block.appendChild(node);
598 bookmark = createBookmark(rng);
600 tinymce.each(getSelectedTextBlocks(), function(block) {
601 var listBlock, sibling;
603 sibling = block.previousSibling;
604 if (sibling && isListNode(sibling) && sibling.nodeName == listName) {
606 block = dom.rename(block, listItemName);
607 sibling.appendChild(block);
609 listBlock = dom.create(listName);
610 block.parentNode.insertBefore(listBlock, block);
611 listBlock.appendChild(block);
612 block = dom.rename(block, listItemName);
615 mergeWithAdjacentLists(listBlock);
618 moveToBookmark(bookmark);
621 function removeList() {
622 var bookmark = createBookmark(selection.getRng(true)), root = editor.getBody();
624 tinymce.each(getSelectedListItems(), function(li) {
627 if (isEditorBody(li.parentNode)) {
636 for (node = li; node && node != root; node = node.parentNode) {
637 if (isListNode(node)) {
642 splitList(rootList, li);
645 moveToBookmark(bookmark);
648 function toggleList(listName) {
649 var parentList = dom.getParent(selection.getStart(), 'OL,UL,DL');
651 if (isEditorBody(parentList)) {
656 if (parentList.nodeName == listName) {
657 removeList(listName);
659 var bookmark = createBookmark(selection.getRng(true));
660 mergeWithAdjacentLists(dom.rename(parentList, listName));
661 moveToBookmark(bookmark);
668 function queryListCommandState(listName) {
670 var parentList = dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
672 return parentList && parentList.nodeName == listName;
676 function isBogusBr(node) {
681 if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
688 self.backspaceDelete = function(isForward) {
689 function findNextCaretContainer(rng, isForward) {
690 var node = rng.startContainer, offset = rng.startOffset;
691 var nonEmptyBlocks, walker;
693 if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
697 nonEmptyBlocks = editor.schema.getNonEmptyElements();
698 if (node.nodeType == 1) {
699 node = tinymce.dom.RangeUtils.getNode(node, offset);
702 walker = new tinymce.dom.TreeWalker(node, editor.getBody());
704 // Delete at <li>|<br></li> then jump over the bogus br
706 if (isBogusBr(node)) {
711 while ((node = walker[isForward ? 'next' : 'prev2']())) {
712 if (node.nodeName == 'LI' && !node.hasChildNodes()) {
716 if (nonEmptyBlocks[node.nodeName]) {
720 if (node.nodeType == 3 && node.data.length > 0) {
726 function mergeLiElements(fromElm, toElm) {
727 var node, listNode, ul = fromElm.parentNode;
729 if (!isChildOfBody(fromElm) || !isChildOfBody(toElm)) {
733 if (isListNode(toElm.lastChild)) {
734 listNode = toElm.lastChild;
737 if (ul == toElm.lastChild) {
738 if (isBr(ul.previousSibling)) {
739 dom.remove(ul.previousSibling);
743 node = toElm.lastChild;
744 if (node && isBr(node) && fromElm.hasChildNodes()) {
748 if (isEmpty(toElm, true)) {
749 dom.$(toElm).empty();
752 if (!isEmpty(fromElm, true)) {
753 while ((node = fromElm.firstChild)) {
754 toElm.appendChild(node);
759 toElm.appendChild(listNode);
764 if (isEmpty(ul) && !isEditorBody(ul)) {
769 if (selection.isCollapsed()) {
770 var li = dom.getParent(selection.getStart(), 'LI'), ul, rng, otherLi;
774 if (isEditorBody(ul) && dom.isEmpty(ul)) {
778 rng = selection.getRng(true);
779 otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
781 if (otherLi && otherLi != li) {
782 var bookmark = createBookmark(rng);
785 mergeLiElements(otherLi, li);
787 mergeLiElements(li, otherLi);
790 moveToBookmark(bookmark);
793 } else if (!otherLi) {
794 if (!isForward && removeList(ul.nodeName)) {
802 editor.on('BeforeExecCommand', function(e) {
803 var cmd = e.command.toLowerCase(), isHandled;
805 if (cmd == "indent") {
806 if (indentSelection()) {
809 } else if (cmd == "outdent") {
810 if (outdentSelection()) {
816 editor.fire('ExecCommand', {command: e.command});
822 editor.addCommand('InsertUnorderedList', function() {
826 editor.addCommand('InsertOrderedList', function() {
830 editor.addCommand('InsertDefinitionList', function() {
834 editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState('UL'));
835 editor.addQueryStateHandler('InsertOrderedList', queryListCommandState('OL'));
836 editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState('DL'));
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)) {
844 if (editor.dom.getParent(editor.selection.getStart(), 'LI,DT,DD')) {
856 editor.addButton('indent', {
858 title: 'Increase indent',
860 onPostRender: function() {
863 editor.on('nodechange', function() {
864 var blocks = editor.selection.getSelectedBlocks();
867 for (var i = 0, l = blocks.length; !disable && i < l; i++) {
868 var tag = blocks[i].nodeName;
870 disable = (tag == 'LI' && isFirstChild(blocks[i]) || tag == 'UL' || tag == 'OL' || tag == 'DD');
873 ctrl.disabled(disable);
878 editor.on('keydown', function(e) {
879 if (e.keyCode == tinymce.util.VK.BACKSPACE) {
880 if (self.backspaceDelete()) {
883 } else if (e.keyCode == tinymce.util.VK.DELETE) {
884 if (self.backspaceDelete(true)) {