4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
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 isListNode(node) {
18 return node && (/^(OL|UL|DL)$/).test(node.nodeName);
21 function isFirstChild(node) {
22 return node.parentNode.firstChild == node;
25 function isLastChild(node) {
26 return node.parentNode.lastChild == node;
29 function isTextBlock(node) {
30 return node && !!editor.schema.getTextBlockElements()[node.nodeName];
33 editor.on('init', function() {
34 var dom = editor.dom, selection = editor.selection;
37 * Returns a range bookmark. This will convert indexed bookmarks into temporary span elements with
38 * index 0 so that they can be restored properly after the DOM has been modified. Text bookmarks will not have spans
39 * added to them since they can be restored after a dom operation.
41 * So this: <p><b>|</b><b>|</b></p>
42 * becomes: <p><b><span data-mce-type="bookmark">|</span></b><b data-mce-type="bookmark">|</span></b></p>
44 * @param {DOMRange} rng DOM Range to get bookmark on.
45 * @return {Object} Bookmark object.
47 function createBookmark(rng) {
50 function setupEndPoint(start) {
51 var offsetNode, container, offset;
53 container = rng[start ? 'startContainer' : 'endContainer'];
54 offset = rng[start ? 'startOffset' : 'endOffset'];
56 if (container.nodeType == 1) {
57 offsetNode = dom.create('span', {'data-mce-type': 'bookmark'});
59 if (container.hasChildNodes()) {
60 offset = Math.min(offset, container.childNodes.length - 1);
63 container.insertBefore(offsetNode, container.childNodes[offset]);
65 dom.insertAfter(offsetNode, container.childNodes[offset]);
68 container.appendChild(offsetNode);
71 container = offsetNode;
75 bookmark[start ? 'startContainer' : 'endContainer'] = container;
76 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
89 * Moves the selection to the current bookmark and removes any selection container wrappers.
91 * @param {Object} bookmark Bookmark object to move selection to.
93 function moveToBookmark(bookmark) {
94 function restoreEndPoint(start) {
95 var container, offset, node;
97 function nodeIndex(container) {
98 var node = container.parentNode.firstChild, idx = 0;
101 if (node == container) {
105 // Skip data-mce-type=bookmark nodes
106 if (node.nodeType != 1 || node.getAttribute('data-mce-type') != 'bookmark') {
110 node = node.nextSibling;
116 container = node = bookmark[start ? 'startContainer' : 'endContainer'];
117 offset = bookmark[start ? 'startOffset' : 'endOffset'];
123 if (container.nodeType == 1) {
124 offset = nodeIndex(container);
125 container = container.parentNode;
129 bookmark[start ? 'startContainer' : 'endContainer'] = container;
130 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
133 restoreEndPoint(true);
136 var rng = dom.createRng();
138 rng.setStart(bookmark.startContainer, bookmark.startOffset);
140 if (bookmark.endContainer) {
141 rng.setEnd(bookmark.endContainer, bookmark.endOffset);
144 selection.setRng(rng);
147 function createNewTextBlock(contentNode, blockName) {
148 var node, textBlock, fragment = dom.createFragment(), hasContentNode;
149 var blockElements = editor.schema.getBlockElements();
151 if (editor.settings.forced_root_block) {
152 blockName = blockName || editor.settings.forced_root_block;
156 textBlock = dom.create(blockName);
158 if (textBlock.tagName === editor.settings.forced_root_block) {
159 dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
162 fragment.appendChild(textBlock);
166 while ((node = contentNode.firstChild)) {
167 var nodeName = node.nodeName;
169 if (!hasContentNode && (nodeName != 'SPAN' || node.getAttribute('data-mce-type') != 'bookmark')) {
170 hasContentNode = true;
173 if (blockElements[nodeName]) {
174 fragment.appendChild(node);
179 textBlock = dom.create(blockName);
180 fragment.appendChild(textBlock);
183 textBlock.appendChild(node);
185 fragment.appendChild(node);
191 if (!editor.settings.forced_root_block) {
192 fragment.appendChild(dom.create('br'));
194 // BR is needed in empty blocks on non IE browsers
195 if (!hasContentNode && (!tinymce.Env.ie || tinymce.Env.ie > 10)) {
196 textBlock.appendChild(dom.create('br', {'data-mce-bogus': '1'}));
203 function getSelectedListItems() {
204 return tinymce.grep(selection.getSelectedBlocks(), function(block) {
205 return /^(LI|DT|DD)$/.test(block.nodeName);
209 function splitList(ul, li, newBlock) {
210 var tmpRng, fragment, bookmarks, node;
212 function removeAndKeepBookmarks(targetNode) {
213 tinymce.each(bookmarks, function(node) {
214 targetNode.parentNode.insertBefore(node, li.parentNode);
217 dom.remove(targetNode);
220 bookmarks = dom.select('span[data-mce-type="bookmark"]', ul);
221 newBlock = newBlock || createNewTextBlock(li);
222 tmpRng = dom.createRng();
223 tmpRng.setStartAfter(li);
224 tmpRng.setEndAfter(ul);
225 fragment = tmpRng.extractContents();
227 for (node = fragment.firstChild; node; node = node.firstChild) {
228 if (node.nodeName == 'LI' && dom.isEmpty(node)) {
234 if (!dom.isEmpty(fragment)) {
235 dom.insertAfter(fragment, ul);
238 dom.insertAfter(newBlock, ul);
240 if (dom.isEmpty(li.parentNode)) {
241 removeAndKeepBookmarks(li.parentNode);
246 if (dom.isEmpty(ul)) {
251 function mergeWithAdjacentLists(listBlock) {
254 sibling = listBlock.nextSibling;
255 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
256 while ((node = sibling.firstChild)) {
257 listBlock.appendChild(node);
263 sibling = listBlock.previousSibling;
264 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
265 while ((node = sibling.firstChild)) {
266 listBlock.insertBefore(node, listBlock.firstChild);
274 * Normalizes the all lists in the specified element.
276 function normalizeList(element) {
277 tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
278 var sibling, parentNode = ul.parentNode;
280 // Move UL/OL to previous LI if it's the only child of a LI
281 if (parentNode.nodeName == 'LI' && parentNode.firstChild == ul) {
282 sibling = parentNode.previousSibling;
283 if (sibling && sibling.nodeName == 'LI') {
284 sibling.appendChild(ul);
286 if (dom.isEmpty(parentNode)) {
287 dom.remove(parentNode);
292 // Append OL/UL to previous LI if it's in a parent OL/UL i.e. old HTML4
293 if (isListNode(parentNode)) {
294 sibling = parentNode.previousSibling;
295 if (sibling && sibling.nodeName == 'LI') {
296 sibling.appendChild(ul);
302 function outdent(li) {
303 var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
305 function removeEmptyLi(li) {
306 if (dom.isEmpty(li)) {
311 if (li.nodeName == 'DD') {
312 dom.rename(li, 'DT');
316 if (isFirstChild(li) && isLastChild(li)) {
317 if (ulParent.nodeName == "LI") {
318 dom.insertAfter(li, ulParent);
319 removeEmptyLi(ulParent);
321 } else if (isListNode(ulParent)) {
322 dom.remove(ul, true);
324 ulParent.insertBefore(createNewTextBlock(li), ul);
329 } else if (isFirstChild(li)) {
330 if (ulParent.nodeName == "LI") {
331 dom.insertAfter(li, ulParent);
333 removeEmptyLi(ulParent);
334 } else if (isListNode(ulParent)) {
335 ulParent.insertBefore(li, ul);
337 ulParent.insertBefore(createNewTextBlock(li), ul);
342 } else if (isLastChild(li)) {
343 if (ulParent.nodeName == "LI") {
344 dom.insertAfter(li, ulParent);
345 } else if (isListNode(ulParent)) {
346 dom.insertAfter(li, ul);
348 dom.insertAfter(createNewTextBlock(li), ul);
354 if (ulParent.nodeName == 'LI') {
356 newBlock = createNewTextBlock(li, 'LI');
357 } else if (isListNode(ulParent)) {
358 newBlock = createNewTextBlock(li, 'LI');
360 newBlock = createNewTextBlock(li);
363 splitList(ul, li, newBlock);
364 normalizeList(ul.parentNode);
372 function indent(li) {
373 var sibling, newList;
375 function mergeLists(from, to) {
378 if (isListNode(from)) {
379 while ((node = li.lastChild.firstChild)) {
380 to.appendChild(node);
387 if (li.nodeName == 'DT') {
388 dom.rename(li, 'DD');
392 sibling = li.previousSibling;
394 if (sibling && isListNode(sibling)) {
395 sibling.appendChild(li);
399 if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
400 sibling.lastChild.appendChild(li);
401 mergeLists(li.lastChild, sibling.lastChild);
405 sibling = li.nextSibling;
407 if (sibling && isListNode(sibling)) {
408 sibling.insertBefore(li, sibling.firstChild);
412 if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
416 sibling = li.previousSibling;
417 if (sibling && sibling.nodeName == 'LI') {
418 newList = dom.create(li.parentNode.nodeName);
419 sibling.appendChild(newList);
420 newList.appendChild(li);
421 mergeLists(li.lastChild, newList);
428 function indentSelection() {
429 var listElements = getSelectedListItems();
431 if (listElements.length) {
432 var bookmark = createBookmark(selection.getRng(true));
434 for (var i = 0; i < listElements.length; i++) {
435 if (!indent(listElements[i]) && i === 0) {
440 moveToBookmark(bookmark);
441 editor.nodeChanged();
447 function outdentSelection() {
448 var listElements = getSelectedListItems();
450 if (listElements.length) {
451 var bookmark = createBookmark(selection.getRng(true));
452 var i, y, root = editor.getBody();
454 i = listElements.length;
456 var node = listElements[i].parentNode;
458 while (node && node != root) {
459 y = listElements.length;
461 if (listElements[y] === node) {
462 listElements.splice(i, 1);
467 node = node.parentNode;
471 for (i = 0; i < listElements.length; i++) {
472 if (!outdent(listElements[i]) && i === 0) {
477 moveToBookmark(bookmark);
478 editor.nodeChanged();
484 function applyList(listName) {
485 var rng = selection.getRng(true), bookmark = createBookmark(rng), listItemName = 'LI';
487 listName = listName.toUpperCase();
489 if (listName == 'DL') {
493 function getSelectedTextBlocks() {
494 var textBlocks = [], root = editor.getBody();
496 function getEndPointNode(start) {
497 var container, offset;
499 container = rng[start ? 'startContainer' : 'endContainer'];
500 offset = rng[start ? 'startOffset' : 'endOffset'];
502 // Resolve node index
503 if (container.nodeType == 1) {
504 container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
507 while (container.parentNode != root) {
508 if (isTextBlock(container)) {
512 if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
516 container = container.parentNode;
522 var startNode = getEndPointNode(true);
523 var endNode = getEndPointNode();
524 var block, siblings = [];
526 for (var node = startNode; node; node = node.nextSibling) {
529 if (node == endNode) {
534 tinymce.each(siblings, function(node) {
535 if (isTextBlock(node)) {
536 textBlocks.push(node);
541 if (dom.isBlock(node) || node.nodeName == 'BR') {
542 if (node.nodeName == 'BR') {
550 var nextSibling = node.nextSibling;
551 if (tinymce.dom.BookmarkManager.isBookmarkNode(node)) {
552 if (isTextBlock(nextSibling) || (!nextSibling && node.parentNode == root)) {
559 block = dom.create('p');
560 node.parentNode.insertBefore(block, node);
561 textBlocks.push(block);
564 block.appendChild(node);
570 tinymce.each(getSelectedTextBlocks(), function(block) {
571 var listBlock, sibling;
573 sibling = block.previousSibling;
574 if (sibling && isListNode(sibling) && sibling.nodeName == listName) {
576 block = dom.rename(block, listItemName);
577 sibling.appendChild(block);
579 listBlock = dom.create(listName);
580 block.parentNode.insertBefore(listBlock, block);
581 listBlock.appendChild(block);
582 block = dom.rename(block, listItemName);
585 mergeWithAdjacentLists(listBlock);
588 moveToBookmark(bookmark);
591 function removeList() {
592 var bookmark = createBookmark(selection.getRng(true)), root = editor.getBody();
594 tinymce.each(getSelectedListItems(), function(li) {
597 if (dom.isEmpty(li)) {
602 for (node = li; node && node != root; node = node.parentNode) {
603 if (isListNode(node)) {
608 splitList(rootList, li);
611 moveToBookmark(bookmark);
614 function toggleList(listName) {
615 var parentList = dom.getParent(selection.getStart(), 'OL,UL,DL');
618 if (parentList.nodeName == listName) {
619 removeList(listName);
621 var bookmark = createBookmark(selection.getRng(true));
622 mergeWithAdjacentLists(dom.rename(parentList, listName));
623 moveToBookmark(bookmark);
630 function queryListCommandState(listName) {
632 var parentList = dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
634 return parentList && parentList.nodeName == listName;
638 self.backspaceDelete = function(isForward) {
639 function findNextCaretContainer(rng, isForward) {
640 var node = rng.startContainer, offset = rng.startOffset;
641 var nonEmptyBlocks, walker;
643 if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
647 nonEmptyBlocks = editor.schema.getNonEmptyElements();
648 walker = new tinymce.dom.TreeWalker(rng.startContainer);
650 while ((node = walker[isForward ? 'next' : 'prev']())) {
651 if (node.nodeName == 'LI' && !node.hasChildNodes()) {
655 if (nonEmptyBlocks[node.nodeName]) {
659 if (node.nodeType == 3 && node.data.length > 0) {
665 function mergeLiElements(fromElm, toElm) {
666 var node, listNode, ul = fromElm.parentNode;
668 if (isListNode(toElm.lastChild)) {
669 listNode = toElm.lastChild;
672 node = toElm.lastChild;
673 if (node && node.nodeName == 'BR' && fromElm.hasChildNodes()) {
677 if (dom.isEmpty(toElm)) {
678 dom.$(toElm).empty();
681 if (!dom.isEmpty(fromElm)) {
682 while ((node = fromElm.firstChild)) {
683 toElm.appendChild(node);
688 toElm.appendChild(listNode);
693 if (dom.isEmpty(ul)) {
698 if (selection.isCollapsed()) {
699 var li = dom.getParent(selection.getStart(), 'LI');
702 var rng = selection.getRng(true);
703 var otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
705 if (otherLi && otherLi != li) {
706 var bookmark = createBookmark(rng);
709 mergeLiElements(otherLi, li);
711 mergeLiElements(li, otherLi);
714 moveToBookmark(bookmark);
717 } else if (!otherLi) {
718 if (!isForward && removeList(li.parentNode.nodeName)) {
726 editor.on('BeforeExecCommand', function(e) {
727 var cmd = e.command.toLowerCase(), isHandled;
729 if (cmd == "indent") {
730 if (indentSelection()) {
733 } else if (cmd == "outdent") {
734 if (outdentSelection()) {
740 editor.fire('ExecCommand', {command: e.command});
746 editor.addCommand('InsertUnorderedList', function() {
750 editor.addCommand('InsertOrderedList', function() {
754 editor.addCommand('InsertDefinitionList', function() {
758 editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState('UL'));
759 editor.addQueryStateHandler('InsertOrderedList', queryListCommandState('OL'));
760 editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState('DL'));
762 editor.on('keydown', function(e) {
763 // Check for tab but not ctrl/cmd+tab since it switches browser tabs
764 if (e.keyCode != 9 || tinymce.util.VK.metaKeyPressed(e)) {
768 if (editor.dom.getParent(editor.selection.getStart(), 'LI,DT,DD')) {
780 editor.addButton('indent', {
782 title: 'Increase indent',
784 onPostRender: function() {
787 editor.on('nodechange', function() {
788 var blocks = editor.selection.getSelectedBlocks();
791 for (var i = 0, l = blocks.length; !disable && i < l; i++) {
792 var tag = blocks[i].nodeName;
794 disable = (tag == 'LI' && isFirstChild(blocks[i]) || tag == 'UL' || tag == 'OL' || tag == 'DD');
797 ctrl.disabled(disable);
802 editor.on('keydown', function(e) {
803 if (e.keyCode == tinymce.util.VK.BACKSPACE) {
804 if (self.backspaceDelete()) {
807 } else if (e.keyCode == tinymce.util.VK.DELETE) {
808 if (self.backspaceDelete(true)) {