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 */
13 tinymce.PluginManager.add('lists', function(editor) {
16 editor.on('init', function() {
17 var dom = editor.dom, selection = editor.selection;
20 * Returns a range bookmark. This will convert indexed bookmarks into temporary span elements with
21 * index 0 so that they can be restored properly after the DOM has been modified. Text bookmarks will not have spans
22 * added to them since they can be restored after a dom operation.
24 * So this: <p><b>|</b><b>|</b></p>
25 * becomes: <p><b><span data-mce-type="bookmark">|</span></b><b data-mce-type="bookmark">|</span></b></p>
27 * @param {DOMRange} rng DOM Range to get bookmark on.
28 * @return {Object} Bookmark object.
30 function createBookmark(rng) {
33 function setupEndPoint(start) {
34 var offsetNode, container, offset;
36 container = rng[start ? 'startContainer' : 'endContainer'];
37 offset = rng[start ? 'startOffset' : 'endOffset'];
39 if (container.nodeType == 1) {
40 offsetNode = dom.create('span', {'data-mce-type': 'bookmark'});
42 if (container.hasChildNodes()) {
43 offset = Math.min(offset, container.childNodes.length - 1);
44 container.insertBefore(offsetNode, container.childNodes[offset]);
46 container.appendChild(offsetNode);
49 container = offsetNode;
53 bookmark[start ? 'startContainer' : 'endContainer'] = container;
54 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
67 * Moves the selection to the current bookmark and removes any selection container wrappers.
69 * @param {Object} bookmark Bookmark object to move selection to.
71 function moveToBookmark(bookmark) {
72 function restoreEndPoint(start) {
73 var container, offset, node;
75 function nodeIndex(container) {
76 var node = container.parentNode.firstChild, idx = 0;
79 if (node == container) {
83 // Skip data-mce-type=bookmark nodes
84 if (node.nodeType != 1 || node.getAttribute('data-mce-type') != 'bookmark') {
88 node = node.nextSibling;
94 container = node = bookmark[start ? 'startContainer' : 'endContainer'];
95 offset = bookmark[start ? 'startOffset' : 'endOffset'];
101 if (container.nodeType == 1) {
102 if (container.parentNode == editor.getBody()) {
103 // TODO: Move the create block or br to some global class possible ForceBlocks.js
104 var block, forcedRootBlock = editor.settings.forced_root_block;
106 if (forcedRootBlock) {
107 block = dom.create(forcedRootBlock);
108 if (!tinymce.Env.ie || tinymce.Env.ie > 10) {
109 block.appendChild(dom.create('br', {'data-mce-bogus': 'true'}));
112 container.parentNode.insertBefore(block, container);
116 block = dom.create('br');
117 container.parentNode.insertBefore(block, container);
118 container = container.parentNode;
119 offset = nodeIndex(block);
123 offset = nodeIndex(container);
124 container = container.parentNode;
126 offset = nodeIndex(container);
127 container = container.parentNode;
134 bookmark[start ? 'startContainer' : 'endContainer'] = container;
135 bookmark[start ? 'startOffset' : 'endOffset'] = offset;
138 restoreEndPoint(true);
141 var rng = dom.createRng();
143 rng.setStart(bookmark.startContainer, bookmark.startOffset);
145 if (bookmark.endContainer) {
146 rng.setEnd(bookmark.endContainer, bookmark.endOffset);
149 selection.setRng(rng);
152 function isListNode(node) {
153 return node && (/^(OL|UL)$/).test(node.nodeName);
156 function isFirstChild(node) {
157 return node.parentNode.firstChild == node;
160 function isLastChild(node) {
161 return node.parentNode.lastChild == node;
164 function isTextBlock(node) {
165 return node && !!editor.schema.getTextBlockElements()[node.nodeName];
168 function createNewTextBlock(contentNode, blockName) {
171 if (editor.settings.forced_root_block) {
172 blockName = blockName || editor.settings.forced_root_block;
176 textBlock = dom.create(blockName);
178 textBlock = dom.createFragment();
182 while ((node = contentNode.firstChild)) {
183 textBlock.appendChild(node);
187 if (!editor.settings.forced_root_block) {
188 textBlock.appendChild(dom.create('br'));
191 // BR is needed in empty blocks on non IE browsers
192 if (!textBlock.hasChildNodes() && (!tinymce.Env.ie || tinymce.Env.ie > 10)) {
193 textBlock.innerHTML = '<br data-mce-bogus="1">';
199 function getSelectedListItems() {
200 return tinymce.grep(selection.getSelectedBlocks(), function(block) {
201 return block.nodeName == 'LI';
205 function getSelectedTextBlocks() {
206 return tinymce.grep(selection.getSelectedBlocks(), isTextBlock);
209 function splitList(ul, li, newBlock) {
210 var tmpRng, fragment;
212 var bookmarks = dom.select('span[data-mce-type="bookmark"]', ul);
214 newBlock = newBlock || createNewTextBlock(li);
215 tmpRng = dom.createRng();
216 tmpRng.setStartAfter(li);
217 tmpRng.setEndAfter(ul);
218 fragment = tmpRng.extractContents();
220 if (!dom.isEmpty(fragment)) {
221 dom.insertAfter(fragment, ul);
224 if (!dom.isEmpty(newBlock)) {
225 dom.insertAfter(newBlock, ul);
228 if (dom.isEmpty(li.parentNode)) {
229 tinymce.each(bookmarks, function(node) {
230 li.parentNode.parentNode.insertBefore(node, li.parentNode);
233 dom.remove(li.parentNode);
239 function mergeWithAdjacentLists(listBlock) {
242 sibling = listBlock.nextSibling;
243 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
244 while ((node = sibling.firstChild)) {
245 listBlock.appendChild(node);
251 sibling = listBlock.previousSibling;
252 if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName) {
253 while ((node = sibling.firstChild)) {
254 listBlock.insertBefore(node, listBlock.firstChild);
262 * Normalizes the all lists in the specified element.
264 function normalizeList(element) {
265 tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
266 var sibling, parentNode = ul.parentNode;
268 // Move UL/OL to previous LI if it's the only child of a LI
269 if (parentNode.nodeName == 'LI' && parentNode.firstChild == ul) {
270 sibling = parentNode.previousSibling;
271 if (sibling && sibling.nodeName == 'LI') {
272 sibling.appendChild(ul);
274 if (dom.isEmpty(parentNode)) {
275 dom.remove(parentNode);
280 // Append OL/UL to previous LI if it's in a parent OL/UL i.e. old HTML4
281 if (isListNode(parentNode)) {
282 sibling = parentNode.previousSibling;
283 if (sibling && sibling.nodeName == 'LI') {
284 sibling.appendChild(ul);
291 var state, bookmark = createBookmark(selection.getRng(true));
293 tinymce.each(getSelectedListItems(), function(li) {
294 var sibling, newList;
296 sibling = li.previousSibling;
298 if (sibling && sibling.nodeName == 'UL') {
299 sibling.appendChild(li);
303 if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
304 sibling.lastChild.appendChild(li);
308 sibling = li.nextSibling;
310 if (sibling && sibling.nodeName == 'UL') {
311 sibling.insertBefore(li, sibling.firstChild);
315 if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
319 sibling = li.previousSibling;
320 if (sibling && sibling.nodeName == 'LI') {
321 newList = dom.create(li.parentNode.nodeName);
322 sibling.appendChild(newList);
323 newList.appendChild(li);
326 /*sibling = li.nextSibling;
327 if (sibling && sibling.nodeName == 'LI') {
328 newList = dom.create(li.parentNode.nodeName);
329 sibling.insertBefore(newList, sibling.firstChild);
330 newList.appendChild(li);
336 moveToBookmark(bookmark);
342 var state, bookmark = createBookmark(selection.getRng(true));
344 function removeEmptyLi(li) {
345 if (dom.isEmpty(li)) {
350 tinymce.each(getSelectedListItems(), function(li) {
351 var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
353 if (isFirstChild(li) && isLastChild(li)) {
354 if (ulParent.nodeName == "LI") {
355 dom.insertAfter(li, ulParent);
356 removeEmptyLi(ulParent);
357 } else if (isListNode(ulParent)) {
358 dom.remove(ul, true);
362 } else if (isFirstChild(li)) {
363 if (ulParent.nodeName == "LI") {
364 dom.insertAfter(li, ulParent);
365 newBlock = dom.create("LI");
366 newBlock.appendChild(ul);
367 dom.insertAfter(newBlock, li);
368 removeEmptyLi(ulParent);
369 } else if (isListNode(ulParent)) {
370 ulParent.insertBefore(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);
383 if (ulParent.nodeName == 'LI') {
385 newBlock = createNewTextBlock(li, 'LI');
386 } else if (isListNode(ulParent)) {
387 newBlock = createNewTextBlock(li, 'LI');
392 splitList(ul, li, newBlock);
393 normalizeList(ul.parentNode);
399 moveToBookmark(bookmark);
404 function applyList(listName) {
405 var rng = selection.getRng(true), bookmark = createBookmark(rng), textBlocks = getSelectedTextBlocks();
407 function convertNonBlockLines() {
408 function getEndPointNode(start) {
409 var container, offset, root = editor.getBody();
411 container = rng[start ? 'startContainer' : 'endContainer'];
412 offset = rng[start ? 'startOffset' : 'endOffset'];
414 // Resolve node index
415 if (container.nodeType == 1) {
416 container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
419 while (container.parentNode != root) {
420 if (isTextBlock(container)) {
424 if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
428 container = container.parentNode;
434 function getAllSiblings(node, isForward) {
435 var sibling, siblings = [];
437 if (!isTextBlock(node)) {
438 // Walk to start/end of line
440 sibling = node[isForward ? 'previousSibling' : 'nextSibling'];
441 if (dom.isBlock(sibling) || !sibling) {
450 node = node[isForward ? 'nextSibling' : 'previousSibling'];
457 var startNode = getEndPointNode(true);
458 var endNode = getEndPointNode();
461 siblings = getAllSiblings(startNode, true);
463 if (startNode != endNode) {
464 siblings = siblings.concat(getAllSiblings(endNode).reverse());
467 tinymce.each(siblings, function(node) {
468 if (dom.isBlock(node) && node.nodeName != 'BR') {
472 if (!block || node.nodeName == 'BR') {
473 if (node.nodeName == 'BR') {
474 if (!node.nextSibling || (dom.isBlock(node.nextSibling) && node.nextSibling.nodeName != "BR")) {
480 block = dom.create('p');
481 textBlocks.push(block);
482 node.parentNode.insertBefore(block, node);
485 if (node.nodeName != 'BR') {
486 block.appendChild(node);
491 if (node == endNode) {
497 convertNonBlockLines();
499 tinymce.each(textBlocks, function(block) {
500 var listBlock, sibling;
502 sibling = block.previousSibling;
503 if (sibling && isListNode(sibling) && sibling.nodeName == listName) {
505 block = dom.rename(block, 'LI');
506 sibling.appendChild(block);
508 listBlock = dom.create(listName);
509 block.parentNode.insertBefore(listBlock, block);
510 listBlock.appendChild(block);
511 block = dom.rename(block, 'LI');
514 mergeWithAdjacentLists(listBlock);
517 moveToBookmark(bookmark);
520 function removeList() {
521 var bookmark = createBookmark(selection.getRng(true));
523 tinymce.each(getSelectedListItems(), function(li) {
526 for (node = li; node; node = node.parentNode) {
527 if (isListNode(node)) {
532 splitList(rootList, li);
535 moveToBookmark(bookmark);
538 function toggleList(listName) {
539 var parentList = dom.getParent(selection.getStart(), 'OL,UL');
542 if (parentList.nodeName == listName) {
543 removeList(listName);
545 var bookmark = createBookmark(selection.getRng(true));
546 mergeWithAdjacentLists(dom.rename(parentList, listName));
547 moveToBookmark(bookmark);
554 plugin.backspaceDelete = function(isForward) {
555 function findNextCaretContainer(rng, isForward) {
556 var node = rng.startContainer, offset = rng.startOffset;
558 if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
562 var walker = new tinymce.dom.TreeWalker(rng.startContainer);
563 while ((node = walker[isForward ? 'next' : 'prev']())) {
564 if (node.nodeType == 3 && node.data.length > 0) {
570 function mergeLiElements(fromElm, toElm) {
571 var node, listNode, ul = fromElm.parentNode;
573 if (isListNode(toElm.lastChild)) {
574 listNode = toElm.lastChild;
577 node = toElm.lastChild;
578 if (node && node.nodeName == 'BR' && fromElm.hasChildNodes()) {
582 while ((node = fromElm.firstChild)) {
583 toElm.appendChild(node);
587 toElm.appendChild(listNode);
592 if (dom.isEmpty(ul)) {
597 if (selection.isCollapsed()) {
598 var li = dom.getParent(selection.getStart(), 'LI');
601 var rng = selection.getRng(true);
602 var otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
604 if (otherLi && otherLi != li) {
605 var bookmark = createBookmark(rng);
608 mergeLiElements(otherLi, li);
610 mergeLiElements(li, otherLi);
613 moveToBookmark(bookmark);
616 } else if (!otherLi) {
617 if (!isForward && removeList(li.parentNode.nodeName)) {
625 editor.addCommand('Indent', function() {
631 editor.addCommand('Outdent', function() {
637 editor.addCommand('InsertUnorderedList', function() {
641 editor.addCommand('InsertOrderedList', function() {
646 editor.on('keydown', function(e) {
647 if (e.keyCode == tinymce.util.VK.BACKSPACE) {
648 if (plugin.backspaceDelete()) {
651 } else if (e.keyCode == tinymce.util.VK.DELETE) {
652 if (plugin.backspaceDelete(true)) {