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 var shouldMerge = function (listBlock, sibling) {
274 var targetStyle = editor.dom.getStyle(listBlock, 'list-style-type', true);
275 var style = editor.dom.getStyle(sibling, 'list-style-type', true);
276 return targetStyle === style;
279 function mergeWithAdjacentLists(listBlock) {
282 sibling = listBlock.nextSibling;
283 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName && shouldMerge(listBlock, sibling)) {
284 while ((node = sibling.firstChild)) {
285 listBlock.appendChild(node);
291 sibling = listBlock.previousSibling;
292 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName && shouldMerge(listBlock, sibling)) {
293 while ((node = sibling.firstChild)) {
294 listBlock.insertBefore(node, listBlock.firstChild);
302 * Normalizes the all lists in the specified element.
304 function normalizeList(element) {
305 tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
306 var sibling, parentNode = ul.parentNode;
308 // Move UL/OL to previous LI if it's the only child of a LI
309 if (parentNode.nodeName == 'LI' && parentNode.firstChild == ul) {
310 sibling = parentNode.previousSibling;
311 if (sibling && sibling.nodeName == 'LI') {
312 sibling.appendChild(ul);
314 if (isEmpty(parentNode)) {
315 dom.remove(parentNode);
320 // Append OL/UL to previous LI if it's in a parent OL/UL i.e. old HTML4
321 if (isListNode(parentNode)) {
322 sibling = parentNode.previousSibling;
323 if (sibling && sibling.nodeName == 'LI') {
324 sibling.appendChild(ul);
330 function outdent(li) {
331 var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
333 function removeEmptyLi(li) {
339 if (isEditorBody(ul)) {
343 if (li.nodeName == 'DD') {
344 dom.rename(li, 'DT');
348 if (isFirstChild(li) && isLastChild(li)) {
349 if (ulParent.nodeName == "LI") {
350 dom.insertAfter(li, ulParent);
351 removeEmptyLi(ulParent);
353 } else if (isListNode(ulParent)) {
354 dom.remove(ul, true);
356 ulParent.insertBefore(createNewTextBlock(li), ul);
361 } else if (isFirstChild(li)) {
362 if (ulParent.nodeName == "LI") {
363 dom.insertAfter(li, ulParent);
365 removeEmptyLi(ulParent);
366 } else if (isListNode(ulParent)) {
367 ulParent.insertBefore(li, ul);
369 ulParent.insertBefore(createNewTextBlock(li), ul);
374 } else if (isLastChild(li)) {
375 if (ulParent.nodeName == "LI") {
376 dom.insertAfter(li, ulParent);
377 } else if (isListNode(ulParent)) {
378 dom.insertAfter(li, ul);
380 dom.insertAfter(createNewTextBlock(li), ul);
387 if (ulParent.nodeName == 'LI') {
389 newBlock = createNewTextBlock(li, 'LI');
390 } else if (isListNode(ulParent)) {
391 newBlock = createNewTextBlock(li, 'LI');
393 newBlock = createNewTextBlock(li);
396 splitList(ul, li, newBlock);
397 normalizeList(ul.parentNode);
402 function indent(li) {
403 var sibling, newList, listStyle;
405 function mergeLists(from, to) {
408 if (isListNode(from)) {
409 while ((node = li.lastChild.firstChild)) {
410 to.appendChild(node);
417 if (li.nodeName == 'DT') {
418 dom.rename(li, 'DD');
422 sibling = li.previousSibling;
424 if (sibling && isListNode(sibling)) {
425 sibling.appendChild(li);
429 if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
430 sibling.lastChild.appendChild(li);
431 mergeLists(li.lastChild, sibling.lastChild);
435 sibling = li.nextSibling;
437 if (sibling && isListNode(sibling)) {
438 sibling.insertBefore(li, sibling.firstChild);
442 /*if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
446 sibling = li.previousSibling;
447 if (sibling && sibling.nodeName == 'LI') {
448 newList = dom.create(li.parentNode.nodeName);
449 listStyle = dom.getStyle(li.parentNode, 'listStyleType');
451 dom.setStyle(newList, 'listStyleType', listStyle);
453 sibling.appendChild(newList);
454 newList.appendChild(li);
455 mergeLists(li.lastChild, newList);
462 function indentSelection() {
463 var listElements = getSelectedListItems();
465 if (listElements.length) {
466 var bookmark = createBookmark(selection.getRng(true));
468 for (var i = 0; i < listElements.length; i++) {
469 if (!indent(listElements[i]) && i === 0) {
474 moveToBookmark(bookmark);
475 editor.nodeChanged();
481 function outdentSelection() {
482 var listElements = getSelectedListItems();
484 if (listElements.length) {
485 var bookmark = createBookmark(selection.getRng(true));
486 var i, y, root = editor.getBody();
488 i = listElements.length;
490 var node = listElements[i].parentNode;
492 while (node && node != root) {
493 y = listElements.length;
495 if (listElements[y] === node) {
496 listElements.splice(i, 1);
501 node = node.parentNode;
505 for (i = 0; i < listElements.length; i++) {
506 if (!outdent(listElements[i]) && i === 0) {
511 moveToBookmark(bookmark);
512 editor.nodeChanged();
518 function applyList(listName, detail) {
519 var rng = selection.getRng(true), bookmark, listItemName = 'LI';
521 if (dom.getContentEditable(selection.getNode()) === "false") {
525 listName = listName.toUpperCase();
527 if (listName == 'DL') {
531 function getSelectedTextBlocks() {
532 var textBlocks = [], root = editor.getBody();
534 function getEndPointNode(start) {
535 var container, offset;
537 container = rng[start ? 'startContainer' : 'endContainer'];
538 offset = rng[start ? 'startOffset' : 'endOffset'];
540 // Resolve node index
541 if (container.nodeType == 1) {
542 container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
545 while (container.parentNode != root) {
546 if (isTextBlock(container)) {
550 if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
554 container = container.parentNode;
560 var startNode = getEndPointNode(true);
561 var endNode = getEndPointNode();
562 var block, siblings = [];
564 for (var node = startNode; node; node = node.nextSibling) {
567 if (node == endNode) {
572 tinymce.each(siblings, function(node) {
573 if (isTextBlock(node)) {
574 textBlocks.push(node);
579 if (dom.isBlock(node) || isBr(node)) {
588 var nextSibling = node.nextSibling;
589 if (tinymce.dom.BookmarkManager.isBookmarkNode(node)) {
590 if (isTextBlock(nextSibling) || (!nextSibling && node.parentNode == root)) {
597 block = dom.create('p');
598 node.parentNode.insertBefore(block, node);
599 textBlocks.push(block);
602 block.appendChild(node);
608 bookmark = createBookmark(rng);
610 tinymce.each(getSelectedTextBlocks(), function(block) {
611 var listBlock, sibling;
613 var hasCompatibleStyle = function (sib) {
614 var sibStyle = dom.getStyle(sib, 'list-style-type');
615 var detailStyle = detail ? detail['list-style-type'] : '';
617 detailStyle = detailStyle === null ? '' : detailStyle;
619 return sibStyle === detailStyle;
622 sibling = block.previousSibling;
623 if (sibling && isListNode(sibling) && sibling.nodeName == listName && hasCompatibleStyle(sibling)) {
625 block = dom.rename(block, listItemName);
626 sibling.appendChild(block);
628 listBlock = dom.create(listName);
629 block.parentNode.insertBefore(listBlock, block);
630 listBlock.appendChild(block);
631 block = dom.rename(block, listItemName);
634 updateListStyle(listBlock, detail);
635 mergeWithAdjacentLists(listBlock);
638 moveToBookmark(bookmark);
641 var updateListStyle = function (el, detail) {
642 dom.setStyle(el, 'list-style-type', detail ? detail['list-style-type'] : null);
645 function removeList() {
646 var bookmark = createBookmark(selection.getRng(true)), root = editor.getBody();
648 tinymce.each(getSelectedListItems(), function(li) {
651 if (isEditorBody(li.parentNode)) {
660 for (node = li; node && node != root; node = node.parentNode) {
661 if (isListNode(node)) {
666 splitList(rootList, li);
669 moveToBookmark(bookmark);
672 function toggleList(listName, detail) {
673 var parentList = dom.getParent(selection.getStart(), 'OL,UL,DL');
675 if (isEditorBody(parentList)) {
680 if (parentList.nodeName == listName) {
681 removeList(listName);
683 var bookmark = createBookmark(selection.getRng(true));
684 updateListStyle(parentList, detail);
685 mergeWithAdjacentLists(dom.rename(parentList, listName));
687 moveToBookmark(bookmark);
690 applyList(listName, detail);
694 function queryListCommandState(listName) {
696 var parentList = dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
698 return parentList && parentList.nodeName == listName;
702 function isBogusBr(node) {
707 if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
714 self.backspaceDelete = function(isForward) {
715 function findNextCaretContainer(rng, isForward) {
716 var node = rng.startContainer, offset = rng.startOffset;
717 var nonEmptyBlocks, walker;
719 if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
723 nonEmptyBlocks = editor.schema.getNonEmptyElements();
724 if (node.nodeType == 1) {
725 node = tinymce.dom.RangeUtils.getNode(node, offset);
728 walker = new tinymce.dom.TreeWalker(node, editor.getBody());
730 // Delete at <li>|<br></li> then jump over the bogus br
732 if (isBogusBr(node)) {
737 while ((node = walker[isForward ? 'next' : 'prev2']())) {
738 if (node.nodeName == 'LI' && !node.hasChildNodes()) {
742 if (nonEmptyBlocks[node.nodeName]) {
746 if (node.nodeType == 3 && node.data.length > 0) {
752 function mergeLiElements(fromElm, toElm) {
753 var node, listNode, ul = fromElm.parentNode;
755 if (!isChildOfBody(fromElm) || !isChildOfBody(toElm)) {
759 if (isListNode(toElm.lastChild)) {
760 listNode = toElm.lastChild;
763 if (ul == toElm.lastChild) {
764 if (isBr(ul.previousSibling)) {
765 dom.remove(ul.previousSibling);
769 node = toElm.lastChild;
770 if (node && isBr(node) && fromElm.hasChildNodes()) {
774 if (isEmpty(toElm, true)) {
775 dom.$(toElm).empty();
778 if (!isEmpty(fromElm, true)) {
779 while ((node = fromElm.firstChild)) {
780 toElm.appendChild(node);
785 toElm.appendChild(listNode);
790 if (isEmpty(ul) && !isEditorBody(ul)) {
795 if (selection.isCollapsed()) {
796 var li = dom.getParent(selection.getStart(), 'LI'), ul, rng, otherLi;
800 if (isEditorBody(ul) && dom.isEmpty(ul)) {
804 rng = selection.getRng(true);
805 otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
807 if (otherLi && otherLi != li) {
808 var bookmark = createBookmark(rng);
811 mergeLiElements(otherLi, li);
813 mergeLiElements(li, otherLi);
816 moveToBookmark(bookmark);
819 } else if (!otherLi) {
820 if (!isForward && removeList(ul.nodeName)) {
828 editor.on('BeforeExecCommand', function(e) {
829 var cmd = e.command.toLowerCase(), isHandled;
831 if (cmd == "indent") {
832 if (indentSelection()) {
835 } else if (cmd == "outdent") {
836 if (outdentSelection()) {
842 editor.fire('ExecCommand', {command: e.command});
848 editor.addCommand('InsertUnorderedList', function(ui, detail) {
849 toggleList('UL', detail);
852 editor.addCommand('InsertOrderedList', function(ui, detail) {
853 toggleList('OL', detail);
856 editor.addCommand('InsertDefinitionList', function(ui, detail) {
857 toggleList('DL', detail);
860 editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState('UL'));
861 editor.addQueryStateHandler('InsertOrderedList', queryListCommandState('OL'));
862 editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState('DL'));
864 editor.on('keydown', function(e) {
865 // Check for tab but not ctrl/cmd+tab since it switches browser tabs
866 if (e.keyCode != 9 || tinymce.util.VK.metaKeyPressed(e)) {
870 if (editor.dom.getParent(editor.selection.getStart(), 'LI,DT,DD')) {
882 editor.addButton('indent', {
884 title: 'Increase indent',
886 onPostRender: function() {
889 editor.on('nodechange', function() {
890 var blocks = editor.selection.getSelectedBlocks();
893 for (var i = 0, l = blocks.length; !disable && i < l; i++) {
894 var tag = blocks[i].nodeName;
896 disable = (tag == 'LI' && isFirstChild(blocks[i]) || tag == 'UL' || tag == 'OL' || tag == 'DD');
899 ctrl.disabled(disable);
904 editor.on('keydown', function(e) {
905 if (e.keyCode == tinymce.util.VK.BACKSPACE) {
906 if (self.backspaceDelete()) {
909 } else if (e.keyCode == tinymce.util.VK.DELETE) {
910 if (self.backspaceDelete(true)) {