editor_plugin_src.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /**
  2. * editor_plugin_src.js
  3. *
  4. * Copyright 2011, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function() {
  11. var each = tinymce.each, Event = tinymce.dom.Event, bookmark;
  12. // Skips text nodes that only contain whitespace since they aren't semantically important.
  13. function skipWhitespaceNodes(e, next) {
  14. while (e && (e.nodeType === 8 || (e.nodeType === 3 && /^[ \t\n\r]*$/.test(e.nodeValue)))) {
  15. e = next(e);
  16. }
  17. return e;
  18. }
  19. function skipWhitespaceNodesBackwards(e) {
  20. return skipWhitespaceNodes(e, function(e) {
  21. return e.previousSibling;
  22. });
  23. }
  24. function skipWhitespaceNodesForwards(e) {
  25. return skipWhitespaceNodes(e, function(e) {
  26. return e.nextSibling;
  27. });
  28. }
  29. function hasParentInList(ed, e, list) {
  30. return ed.dom.getParent(e, function(p) {
  31. return tinymce.inArray(list, p) !== -1;
  32. });
  33. }
  34. function isList(e) {
  35. return e && (e.tagName === 'OL' || e.tagName === 'UL');
  36. }
  37. function splitNestedLists(element, dom) {
  38. var tmp, nested, wrapItem;
  39. tmp = skipWhitespaceNodesBackwards(element.lastChild);
  40. while (isList(tmp)) {
  41. nested = tmp;
  42. tmp = skipWhitespaceNodesBackwards(nested.previousSibling);
  43. }
  44. if (nested) {
  45. wrapItem = dom.create('li', { style: 'list-style-type: none;'});
  46. dom.split(element, nested);
  47. dom.insertAfter(wrapItem, nested);
  48. wrapItem.appendChild(nested);
  49. wrapItem.appendChild(nested);
  50. element = wrapItem.previousSibling;
  51. }
  52. return element;
  53. }
  54. function attemptMergeWithAdjacent(e, allowDifferentListStyles, mergeParagraphs) {
  55. e = attemptMergeWithPrevious(e, allowDifferentListStyles, mergeParagraphs);
  56. return attemptMergeWithNext(e, allowDifferentListStyles, mergeParagraphs);
  57. }
  58. function attemptMergeWithPrevious(e, allowDifferentListStyles, mergeParagraphs) {
  59. var prev = skipWhitespaceNodesBackwards(e.previousSibling);
  60. if (prev) {
  61. return attemptMerge(prev, e, allowDifferentListStyles ? prev : false, mergeParagraphs);
  62. } else {
  63. return e;
  64. }
  65. }
  66. function attemptMergeWithNext(e, allowDifferentListStyles, mergeParagraphs) {
  67. var next = skipWhitespaceNodesForwards(e.nextSibling);
  68. if (next) {
  69. return attemptMerge(e, next, allowDifferentListStyles ? next : false, mergeParagraphs);
  70. } else {
  71. return e;
  72. }
  73. }
  74. function attemptMerge(e1, e2, differentStylesMasterElement, mergeParagraphs) {
  75. if (canMerge(e1, e2, !!differentStylesMasterElement, mergeParagraphs)) {
  76. return merge(e1, e2, differentStylesMasterElement);
  77. } else if (e1 && e1.tagName === 'LI' && isList(e2)) {
  78. // Fix invalidly nested lists.
  79. e1.appendChild(e2);
  80. }
  81. return e2;
  82. }
  83. function canMerge(e1, e2, allowDifferentListStyles, mergeParagraphs) {
  84. if (!e1 || !e2) {
  85. return false;
  86. } else if (e1.tagName === 'LI' && e2.tagName === 'LI') {
  87. return e2.style.listStyleType === 'none' || containsOnlyAList(e2);
  88. } else if (isList(e1)) {
  89. return (e1.tagName === e2.tagName && (allowDifferentListStyles || e1.style.listStyleType === e2.style.listStyleType)) || isListForIndent(e2);
  90. } else return mergeParagraphs && e1.tagName === 'P' && e2.tagName === 'P';
  91. }
  92. function isListForIndent(e) {
  93. var firstLI = skipWhitespaceNodesForwards(e.firstChild), lastLI = skipWhitespaceNodesBackwards(e.lastChild);
  94. return firstLI && lastLI && isList(e) && firstLI === lastLI && (isList(firstLI) || firstLI.style.listStyleType === 'none' || containsOnlyAList(firstLI));
  95. }
  96. function containsOnlyAList(e) {
  97. var firstChild = skipWhitespaceNodesForwards(e.firstChild), lastChild = skipWhitespaceNodesBackwards(e.lastChild);
  98. return firstChild && lastChild && firstChild === lastChild && isList(firstChild);
  99. }
  100. function merge(e1, e2, masterElement) {
  101. var lastOriginal = skipWhitespaceNodesBackwards(e1.lastChild), firstNew = skipWhitespaceNodesForwards(e2.firstChild);
  102. if (e1.tagName === 'P') {
  103. e1.appendChild(e1.ownerDocument.createElement('br'));
  104. }
  105. while (e2.firstChild) {
  106. e1.appendChild(e2.firstChild);
  107. }
  108. if (masterElement) {
  109. e1.style.listStyleType = masterElement.style.listStyleType;
  110. }
  111. e2.parentNode.removeChild(e2);
  112. attemptMerge(lastOriginal, firstNew, false);
  113. return e1;
  114. }
  115. function findItemToOperateOn(e, dom) {
  116. var item;
  117. if (!dom.is(e, 'li,ol,ul')) {
  118. item = dom.getParent(e, 'li');
  119. if (item) {
  120. e = item;
  121. }
  122. }
  123. return e;
  124. }
  125. tinymce.create('tinymce.plugins.Lists', {
  126. init: function(ed) {
  127. var LIST_TABBING = 'TABBING';
  128. var LIST_EMPTY_ITEM = 'EMPTY';
  129. var LIST_ESCAPE = 'ESCAPE';
  130. var LIST_PARAGRAPH = 'PARAGRAPH';
  131. var LIST_UNKNOWN = 'UNKNOWN';
  132. var state = LIST_UNKNOWN;
  133. function isTabInList(e) {
  134. // Don't indent on Ctrl+Tab or Alt+Tab
  135. return e.keyCode === tinymce.VK.TAB && !(e.altKey || e.ctrlKey) &&
  136. (ed.queryCommandState('InsertUnorderedList') || ed.queryCommandState('InsertOrderedList'));
  137. }
  138. function isOnLastListItem() {
  139. var li = getLi();
  140. var grandParent = li.parentNode.parentNode;
  141. var isLastItem = li.parentNode.lastChild === li;
  142. return isLastItem && !isNestedList(grandParent) && isEmptyListItem(li);
  143. }
  144. function isNestedList(grandParent) {
  145. if (isList(grandParent)) {
  146. return grandParent.parentNode && grandParent.parentNode.tagName === 'LI';
  147. } else {
  148. return grandParent.tagName === 'LI';
  149. }
  150. }
  151. function isInEmptyListItem() {
  152. return ed.selection.isCollapsed() && isEmptyListItem(getLi());
  153. }
  154. function getLi() {
  155. var n = ed.selection.getStart();
  156. // Get start will return BR if the LI only contains a BR or an empty element as we use these to fix caret position
  157. return ((n.tagName == 'BR' || n.tagName == '') && n.parentNode.tagName == 'LI') ? n.parentNode : n;
  158. }
  159. function isEmptyListItem(li) {
  160. var numChildren = li.childNodes.length;
  161. if (li.tagName === 'LI') {
  162. return numChildren == 0 ? true : numChildren == 1 && (li.firstChild.tagName == '' || li.firstChild.tagName == 'BR' || isEmptyIE9Li(li));
  163. }
  164. return false;
  165. }
  166. function isEmptyIE9Li(li) {
  167. // only consider this to be last item if there is no list item content or that content is nbsp or space since IE9 creates these
  168. var lis = tinymce.grep(li.parentNode.childNodes, function(n) {return n.tagName == 'LI'});
  169. var isLastLi = li == lis[lis.length - 1];
  170. var child = li.firstChild;
  171. return tinymce.isIE9 && isLastLi && (child.nodeValue == String.fromCharCode(160) || child.nodeValue == String.fromCharCode(32));
  172. }
  173. function isEnter(e) {
  174. return e.keyCode === tinymce.VK.ENTER;
  175. }
  176. function isEnterWithoutShift(e) {
  177. return isEnter(e) && !e.shiftKey;
  178. }
  179. function getListKeyState(e) {
  180. if (isTabInList(e)) {
  181. return LIST_TABBING;
  182. } else if (isEnterWithoutShift(e) && isOnLastListItem()) {
  183. // Returns LIST_UNKNOWN since breaking out of lists is handled by the EnterKey.js logic now
  184. //return LIST_ESCAPE;
  185. return LIST_UNKNOWN;
  186. } else if (isEnterWithoutShift(e) && isInEmptyListItem()) {
  187. return LIST_EMPTY_ITEM;
  188. } else {
  189. return LIST_UNKNOWN;
  190. }
  191. }
  192. function cancelDefaultEvents(ed, e) {
  193. // list escape is done manually using outdent as it does not create paragraphs correctly in td's
  194. if (state == LIST_TABBING || state == LIST_EMPTY_ITEM || tinymce.isGecko && state == LIST_ESCAPE) {
  195. Event.cancel(e);
  196. }
  197. }
  198. function isCursorAtEndOfContainer() {
  199. var range = ed.selection.getRng(true);
  200. var startContainer = range.startContainer;
  201. if (startContainer.nodeType == 3) {
  202. var value = startContainer.nodeValue;
  203. if (tinymce.isIE9 && value.length > 1 && value.charCodeAt(value.length-1) == 32) {
  204. // IE9 places a space on the end of the text in some cases so ignore last char
  205. return (range.endOffset == value.length-1);
  206. } else {
  207. return (range.endOffset == value.length);
  208. }
  209. } else if (startContainer.nodeType == 1) {
  210. return range.endOffset == startContainer.childNodes.length;
  211. }
  212. return false;
  213. }
  214. /*
  215. If we are at the end of a list item surrounded with an element, pressing enter should create a
  216. new list item instead without splitting the element e.g. don't want to create new P or H1 tag
  217. */
  218. function isEndOfListItem() {
  219. var node = ed.selection.getNode();
  220. var validElements = 'h1,h2,h3,h4,h5,h6,p,div';
  221. var isLastParagraphOfLi = ed.dom.is(node, validElements) && node.parentNode.tagName === 'LI' && node.parentNode.lastChild === node;
  222. return ed.selection.isCollapsed() && isLastParagraphOfLi && isCursorAtEndOfContainer();
  223. }
  224. // Creates a new list item after the current selection's list item parent
  225. function createNewLi(ed, e) {
  226. if (isEnterWithoutShift(e) && isEndOfListItem()) {
  227. var node = ed.selection.getNode();
  228. var li = ed.dom.create("li");
  229. var parentLi = ed.dom.getParent(node, 'li');
  230. ed.dom.insertAfter(li, parentLi);
  231. // Move caret to new list element.
  232. if (tinymce.isIE6 || tinymce.isIE7 || tinyMCE.isIE8) {
  233. // Removed this line since it would create an odd <&nbsp;> tag and placing the caret inside an empty LI is handled and should be handled by the selection logic
  234. //li.appendChild(ed.dom.create("&nbsp;")); // IE needs an element within the bullet point
  235. ed.selection.setCursorLocation(li, 1);
  236. } else {
  237. ed.selection.setCursorLocation(li, 0);
  238. }
  239. e.preventDefault();
  240. }
  241. }
  242. function imageJoiningListItem(ed, e) {
  243. var prevSibling;
  244. if (!tinymce.isGecko)
  245. return;
  246. var n = ed.selection.getStart();
  247. if (e.keyCode != tinymce.VK.BACKSPACE || n.tagName !== 'IMG')
  248. return;
  249. function lastLI(node) {
  250. var child = node.firstChild;
  251. var li = null;
  252. do {
  253. if (!child)
  254. break;
  255. if (child.tagName === 'LI')
  256. li = child;
  257. } while (child = child.nextSibling);
  258. return li;
  259. }
  260. function addChildren(parentNode, destination) {
  261. while (parentNode.childNodes.length > 0)
  262. destination.appendChild(parentNode.childNodes[0]);
  263. }
  264. // Check if there is a previous sibling
  265. prevSibling = n.parentNode.previousSibling;
  266. if (!prevSibling)
  267. return;
  268. var ul;
  269. if (prevSibling.tagName === 'UL' || prevSibling.tagName === 'OL')
  270. ul = prevSibling;
  271. else if (prevSibling.previousSibling && (prevSibling.previousSibling.tagName === 'UL' || prevSibling.previousSibling.tagName === 'OL'))
  272. ul = prevSibling.previousSibling;
  273. else
  274. return;
  275. var li = lastLI(ul);
  276. // move the caret to the end of the list item
  277. var rng = ed.dom.createRng();
  278. rng.setStart(li, 1);
  279. rng.setEnd(li, 1);
  280. ed.selection.setRng(rng);
  281. ed.selection.collapse(true);
  282. // save a bookmark at the end of the list item
  283. var bookmark = ed.selection.getBookmark();
  284. // copy the image an its text to the list item
  285. var clone = n.parentNode.cloneNode(true);
  286. if (clone.tagName === 'P' || clone.tagName === 'DIV')
  287. addChildren(clone, li);
  288. else
  289. li.appendChild(clone);
  290. // remove the old copy of the image
  291. n.parentNode.parentNode.removeChild(n.parentNode);
  292. // move the caret where we saved the bookmark
  293. ed.selection.moveToBookmark(bookmark);
  294. }
  295. // fix the cursor position to ensure it is correct in IE
  296. function setCursorPositionToOriginalLi(li) {
  297. var list = ed.dom.getParent(li, 'ol,ul');
  298. if (list != null) {
  299. var lastLi = list.lastChild;
  300. // Removed this line since IE9 would report an DOM character error and placing the caret inside an empty LI is handled and should be handled by the selection logic
  301. //lastLi.appendChild(ed.getDoc().createElement(''));
  302. ed.selection.setCursorLocation(lastLi, 0);
  303. }
  304. }
  305. this.ed = ed;
  306. ed.addCommand('Indent', this.indent, this);
  307. ed.addCommand('Outdent', this.outdent, this);
  308. ed.addCommand('InsertUnorderedList', function() {
  309. this.applyList('UL', 'OL');
  310. }, this);
  311. ed.addCommand('InsertOrderedList', function() {
  312. this.applyList('OL', 'UL');
  313. }, this);
  314. ed.onInit.add(function() {
  315. ed.editorCommands.addCommands({
  316. 'outdent': function() {
  317. var sel = ed.selection, dom = ed.dom;
  318. function hasStyleIndent(n) {
  319. n = dom.getParent(n, dom.isBlock);
  320. return n && (parseInt(ed.dom.getStyle(n, 'margin-left') || 0, 10) + parseInt(ed.dom.getStyle(n, 'padding-left') || 0, 10)) > 0;
  321. }
  322. return hasStyleIndent(sel.getStart()) || hasStyleIndent(sel.getEnd()) || ed.queryCommandState('InsertOrderedList') || ed.queryCommandState('InsertUnorderedList');
  323. }
  324. }, 'state');
  325. });
  326. ed.onKeyUp.add(function(ed, e) {
  327. if (state == LIST_TABBING) {
  328. ed.execCommand(e.shiftKey ? 'Outdent' : 'Indent', true, null);
  329. state = LIST_UNKNOWN;
  330. return Event.cancel(e);
  331. } else if (state == LIST_EMPTY_ITEM) {
  332. var li = getLi();
  333. var shouldOutdent = ed.settings.list_outdent_on_enter === true || e.shiftKey;
  334. ed.execCommand(shouldOutdent ? 'Outdent' : 'Indent', true, null);
  335. if (tinymce.isIE) {
  336. setCursorPositionToOriginalLi(li);
  337. }
  338. return Event.cancel(e);
  339. } else if (state == LIST_ESCAPE) {
  340. if (tinymce.isIE6 || tinymce.isIE7 || tinymce.isIE8) {
  341. // append a zero sized nbsp so that caret is positioned correctly in IE after escaping and applying formatting.
  342. // if there is no text then applying formatting for e.g a H1 to the P tag immediately following list after
  343. // escaping from it will cause the caret to be positioned on the last li instead of staying the in P tag.
  344. var n = ed.getDoc().createTextNode('\uFEFF');
  345. ed.selection.getNode().appendChild(n);
  346. } else if (tinymce.isIE9 || tinymce.isGecko) {
  347. // IE9 does not escape the list so we use outdent to do this and cancel the default behaviour
  348. // Gecko does not create a paragraph outdenting inside a TD so default behaviour is cancelled and we outdent ourselves
  349. ed.execCommand('Outdent');
  350. return Event.cancel(e);
  351. }
  352. }
  353. });
  354. function fixListItem(parent, reference) {
  355. // a zero-sized non-breaking space is placed in the empty list item so that the nested list is
  356. // displayed on the below line instead of next to it
  357. var n = ed.getDoc().createTextNode('\uFEFF');
  358. parent.insertBefore(n, reference);
  359. ed.selection.setCursorLocation(n, 0);
  360. // repaint to remove rendering artifact. only visible when creating new list
  361. ed.execCommand('mceRepaint');
  362. }
  363. function fixIndentedListItemForGecko(ed, e) {
  364. if (isEnter(e)) {
  365. var li = getLi();
  366. if (li) {
  367. var parent = li.parentNode;
  368. var grandParent = parent && parent.parentNode;
  369. if (grandParent && grandParent.nodeName == 'LI' && grandParent.firstChild == parent && li == parent.firstChild) {
  370. fixListItem(grandParent, parent);
  371. }
  372. }
  373. }
  374. }
  375. function fixIndentedListItemForIE8(ed, e) {
  376. if (isEnter(e)) {
  377. var li = getLi();
  378. if (ed.dom.select('ul li', li).length === 1) {
  379. var list = li.firstChild;
  380. fixListItem(li, list);
  381. }
  382. }
  383. }
  384. function fixDeletingFirstCharOfList(ed, e) {
  385. function listElements(li) {
  386. var elements = [];
  387. var walker = new tinymce.dom.TreeWalker(li.firstChild, li);
  388. for (var node = walker.current(); node; node = walker.next()) {
  389. if (ed.dom.is(node, 'ol,ul,li')) {
  390. elements.push(node);
  391. }
  392. }
  393. return elements;
  394. }
  395. if (e.keyCode == tinymce.VK.BACKSPACE) {
  396. var li = getLi();
  397. if (li) {
  398. var list = ed.dom.getParent(li, 'ol,ul'),
  399. rng = ed.selection.getRng();
  400. if (list && list.firstChild === li && rng.startOffset == 0) {
  401. var elements = listElements(li);
  402. elements.unshift(li);
  403. ed.execCommand("Outdent", false, elements);
  404. ed.undoManager.add();
  405. return Event.cancel(e);
  406. }
  407. }
  408. }
  409. }
  410. function fixDeletingEmptyLiInWebkit(ed, e) {
  411. var li = getLi();
  412. if (e.keyCode === tinymce.VK.BACKSPACE && ed.dom.is(li, 'li') && li.parentNode.firstChild!==li) {
  413. if (ed.dom.select('ul,ol', li).length === 1) {
  414. var prevLi = li.previousSibling;
  415. ed.dom.remove(ed.dom.select('br', li));
  416. ed.dom.remove(li, true);
  417. var textNodes = tinymce.grep(prevLi.childNodes, function(n){ return n.nodeType === 3 });
  418. if (textNodes.length === 1) {
  419. var textNode = textNodes[0];
  420. ed.selection.setCursorLocation(textNode, textNode.length);
  421. }
  422. ed.undoManager.add();
  423. return Event.cancel(e);
  424. }
  425. }
  426. }
  427. ed.onKeyDown.add(function(_, e) { state = getListKeyState(e); });
  428. ed.onKeyDown.add(cancelDefaultEvents);
  429. ed.onKeyDown.add(imageJoiningListItem);
  430. ed.onKeyDown.add(createNewLi);
  431. if (tinymce.isGecko) {
  432. ed.onKeyUp.add(fixIndentedListItemForGecko);
  433. }
  434. if (tinymce.isIE8) {
  435. ed.onKeyUp.add(fixIndentedListItemForIE8);
  436. }
  437. if (tinymce.isGecko || tinymce.isWebKit) {
  438. ed.onKeyDown.add(fixDeletingFirstCharOfList);
  439. }
  440. if (tinymce.isWebKit) {
  441. ed.onKeyDown.add(fixDeletingEmptyLiInWebkit);
  442. }
  443. },
  444. applyList: function(targetListType, oppositeListType) {
  445. var t = this, ed = t.ed, dom = ed.dom, applied = [], hasSameType = false, hasOppositeType = false, hasNonList = false, actions,
  446. selectedBlocks = ed.selection.getSelectedBlocks();
  447. function cleanupBr(e) {
  448. if (e && e.tagName === 'BR') {
  449. dom.remove(e);
  450. }
  451. }
  452. function makeList(element) {
  453. var list = dom.create(targetListType), li;
  454. function adjustIndentForNewList(element) {
  455. // If there's a margin-left, outdent one level to account for the extra list margin.
  456. if (element.style.marginLeft || element.style.paddingLeft) {
  457. t.adjustPaddingFunction(false)(element);
  458. }
  459. }
  460. if (element.tagName === 'LI') {
  461. // No change required.
  462. } else if (element.tagName === 'P' || element.tagName === 'DIV' || element.tagName === 'BODY') {
  463. processBrs(element, function(startSection, br) {
  464. doWrapList(startSection, br, element.tagName === 'BODY' ? null : startSection.parentNode);
  465. li = startSection.parentNode;
  466. adjustIndentForNewList(li);
  467. cleanupBr(br);
  468. });
  469. if (li) {
  470. if (li.tagName === 'LI' && (element.tagName === 'P' || selectedBlocks.length > 1)) {
  471. dom.split(li.parentNode.parentNode, li.parentNode);
  472. }
  473. attemptMergeWithAdjacent(li.parentNode, true);
  474. }
  475. return;
  476. } else {
  477. // Put the list around the element.
  478. li = dom.create('li');
  479. dom.insertAfter(li, element);
  480. li.appendChild(element);
  481. adjustIndentForNewList(element);
  482. element = li;
  483. }
  484. dom.insertAfter(list, element);
  485. list.appendChild(element);
  486. attemptMergeWithAdjacent(list, true);
  487. applied.push(element);
  488. }
  489. function doWrapList(start, end, template) {
  490. var li, n = start, tmp;
  491. while (!dom.isBlock(start.parentNode) && start.parentNode !== dom.getRoot()) {
  492. start = dom.split(start.parentNode, start.previousSibling);
  493. start = start.nextSibling;
  494. n = start;
  495. }
  496. if (template) {
  497. li = template.cloneNode(true);
  498. start.parentNode.insertBefore(li, start);
  499. while (li.firstChild) dom.remove(li.firstChild);
  500. li = dom.rename(li, 'li');
  501. } else {
  502. li = dom.create('li');
  503. start.parentNode.insertBefore(li, start);
  504. }
  505. while (n && n != end) {
  506. tmp = n.nextSibling;
  507. li.appendChild(n);
  508. n = tmp;
  509. }
  510. if (li.childNodes.length === 0) {
  511. li.innerHTML = '<br _mce_bogus="1" />';
  512. }
  513. makeList(li);
  514. }
  515. function processBrs(element, callback) {
  516. var startSection, previousBR, END_TO_START = 3, START_TO_END = 1,
  517. breakElements = 'br,ul,ol,p,div,h1,h2,h3,h4,h5,h6,table,blockquote,address,pre,form,center,dl';
  518. function isAnyPartSelected(start, end) {
  519. var r = dom.createRng(), sel;
  520. bookmark.keep = true;
  521. ed.selection.moveToBookmark(bookmark);
  522. bookmark.keep = false;
  523. sel = ed.selection.getRng(true);
  524. if (!end) {
  525. end = start.parentNode.lastChild;
  526. }
  527. r.setStartBefore(start);
  528. r.setEndAfter(end);
  529. return !(r.compareBoundaryPoints(END_TO_START, sel) > 0 || r.compareBoundaryPoints(START_TO_END, sel) <= 0);
  530. }
  531. function nextLeaf(br) {
  532. if (br.nextSibling)
  533. return br.nextSibling;
  534. if (!dom.isBlock(br.parentNode) && br.parentNode !== dom.getRoot())
  535. return nextLeaf(br.parentNode);
  536. }
  537. // Split on BRs within the range and process those.
  538. startSection = element.firstChild;
  539. // First mark the BRs that have any part of the previous section selected.
  540. var trailingContentSelected = false;
  541. each(dom.select(breakElements, element), function(br) {
  542. if (br.hasAttribute && br.hasAttribute('_mce_bogus')) {
  543. return true; // Skip the bogus Brs that are put in to appease Firefox and Safari.
  544. }
  545. if (isAnyPartSelected(startSection, br)) {
  546. dom.addClass(br, '_mce_tagged_br');
  547. startSection = nextLeaf(br);
  548. }
  549. });
  550. trailingContentSelected = (startSection && isAnyPartSelected(startSection, undefined));
  551. startSection = element.firstChild;
  552. each(dom.select(breakElements, element), function(br) {
  553. // Got a section from start to br.
  554. var tmp = nextLeaf(br);
  555. if (br.hasAttribute && br.hasAttribute('_mce_bogus')) {
  556. return true; // Skip the bogus Brs that are put in to appease Firefox and Safari.
  557. }
  558. if (dom.hasClass(br, '_mce_tagged_br')) {
  559. callback(startSection, br, previousBR);
  560. previousBR = null;
  561. } else {
  562. previousBR = br;
  563. }
  564. startSection = tmp;
  565. });
  566. if (trailingContentSelected) {
  567. callback(startSection, undefined, previousBR);
  568. }
  569. }
  570. function wrapList(element) {
  571. processBrs(element, function(startSection, br, previousBR) {
  572. // Need to indent this part
  573. doWrapList(startSection, br);
  574. cleanupBr(br);
  575. cleanupBr(previousBR);
  576. });
  577. }
  578. function changeList(element) {
  579. if (tinymce.inArray(applied, element) !== -1) {
  580. return;
  581. }
  582. if (element.parentNode.tagName === oppositeListType) {
  583. dom.split(element.parentNode, element);
  584. makeList(element);
  585. attemptMergeWithNext(element.parentNode, false);
  586. }
  587. applied.push(element);
  588. }
  589. function convertListItemToParagraph(element) {
  590. var child, nextChild, mergedElement, splitLast;
  591. if (tinymce.inArray(applied, element) !== -1) {
  592. return;
  593. }
  594. element = splitNestedLists(element, dom);
  595. while (dom.is(element.parentNode, 'ol,ul,li')) {
  596. dom.split(element.parentNode, element);
  597. }
  598. // Push the original element we have from the selection, not the renamed one.
  599. applied.push(element);
  600. element = dom.rename(element, 'p');
  601. mergedElement = attemptMergeWithAdjacent(element, false, ed.settings.force_br_newlines);
  602. if (mergedElement === element) {
  603. // Now split out any block elements that can't be contained within a P.
  604. // Manually iterate to ensure we handle modifications correctly (doesn't work with tinymce.each)
  605. child = element.firstChild;
  606. while (child) {
  607. if (dom.isBlock(child)) {
  608. child = dom.split(child.parentNode, child);
  609. splitLast = true;
  610. nextChild = child.nextSibling && child.nextSibling.firstChild;
  611. } else {
  612. nextChild = child.nextSibling;
  613. if (splitLast && child.tagName === 'BR') {
  614. dom.remove(child);
  615. }
  616. splitLast = false;
  617. }
  618. child = nextChild;
  619. }
  620. }
  621. }
  622. each(selectedBlocks, function(e) {
  623. e = findItemToOperateOn(e, dom);
  624. if (e.tagName === oppositeListType || (e.tagName === 'LI' && e.parentNode.tagName === oppositeListType)) {
  625. hasOppositeType = true;
  626. } else if (e.tagName === targetListType || (e.tagName === 'LI' && e.parentNode.tagName === targetListType)) {
  627. hasSameType = true;
  628. } else {
  629. hasNonList = true;
  630. }
  631. });
  632. if (hasNonList &&!hasSameType || hasOppositeType || selectedBlocks.length === 0) {
  633. actions = {
  634. 'LI': changeList,
  635. 'H1': makeList,
  636. 'H2': makeList,
  637. 'H3': makeList,
  638. 'H4': makeList,
  639. 'H5': makeList,
  640. 'H6': makeList,
  641. 'P': makeList,
  642. 'BODY': makeList,
  643. 'DIV': selectedBlocks.length > 1 ? makeList : wrapList,
  644. defaultAction: wrapList,
  645. elements: this.selectedBlocks()
  646. };
  647. } else {
  648. actions = {
  649. defaultAction: convertListItemToParagraph,
  650. elements: this.selectedBlocks(),
  651. processEvenIfEmpty: true
  652. };
  653. }
  654. this.process(actions);
  655. },
  656. indent: function() {
  657. var ed = this.ed, dom = ed.dom, indented = [];
  658. function createWrapItem(element) {
  659. var wrapItem = dom.create('li', { style: 'list-style-type: none;'});
  660. dom.insertAfter(wrapItem, element);
  661. return wrapItem;
  662. }
  663. function createWrapList(element) {
  664. var wrapItem = createWrapItem(element),
  665. list = dom.getParent(element, 'ol,ul'),
  666. listType = list.tagName,
  667. listStyle = dom.getStyle(list, 'list-style-type'),
  668. attrs = {},
  669. wrapList;
  670. if (listStyle !== '') {
  671. attrs.style = 'list-style-type: ' + listStyle + ';';
  672. }
  673. wrapList = dom.create(listType, attrs);
  674. wrapItem.appendChild(wrapList);
  675. return wrapList;
  676. }
  677. function indentLI(element) {
  678. if (!hasParentInList(ed, element, indented)) {
  679. element = splitNestedLists(element, dom);
  680. var wrapList = createWrapList(element);
  681. wrapList.appendChild(element);
  682. attemptMergeWithAdjacent(wrapList.parentNode, false);
  683. attemptMergeWithAdjacent(wrapList, false);
  684. indented.push(element);
  685. }
  686. }
  687. this.process({
  688. 'LI': indentLI,
  689. defaultAction: this.adjustPaddingFunction(true),
  690. elements: this.selectedBlocks()
  691. });
  692. },
  693. outdent: function(ui, elements) {
  694. var t = this, ed = t.ed, dom = ed.dom, outdented = [];
  695. function outdentLI(element) {
  696. var listElement, targetParent, align;
  697. if (!hasParentInList(ed, element, outdented)) {
  698. if (dom.getStyle(element, 'margin-left') !== '' || dom.getStyle(element, 'padding-left') !== '') {
  699. return t.adjustPaddingFunction(false)(element);
  700. }
  701. align = dom.getStyle(element, 'text-align', true);
  702. if (align === 'center' || align === 'right') {
  703. dom.setStyle(element, 'text-align', 'left');
  704. return;
  705. }
  706. element = splitNestedLists(element, dom);
  707. listElement = element.parentNode;
  708. targetParent = element.parentNode.parentNode;
  709. if (targetParent.tagName === 'P') {
  710. dom.split(targetParent, element.parentNode);
  711. } else {
  712. dom.split(listElement, element);
  713. if (targetParent.tagName === 'LI') {
  714. // Nested list, need to split the LI and go back out to the OL/UL element.
  715. dom.split(targetParent, element);
  716. } else if (!dom.is(targetParent, 'ol,ul')) {
  717. dom.rename(element, 'p');
  718. }
  719. }
  720. outdented.push(element);
  721. }
  722. }
  723. var listElements = elements && tinymce.is(elements, 'array') ? elements : this.selectedBlocks();
  724. this.process({
  725. 'LI': outdentLI,
  726. defaultAction: this.adjustPaddingFunction(false),
  727. elements: listElements
  728. });
  729. each(outdented, attemptMergeWithAdjacent);
  730. },
  731. process: function(actions) {
  732. var t = this, sel = t.ed.selection, dom = t.ed.dom, selectedBlocks, r;
  733. function isEmptyElement(element) {
  734. var excludeBrsAndBookmarks = tinymce.grep(element.childNodes, function(n) {
  735. return !(n.nodeName === 'BR' || n.nodeName === 'SPAN' && dom.getAttrib(n, 'data-mce-type') == 'bookmark'
  736. || n.nodeType == 3 && (n.nodeValue == String.fromCharCode(160) || n.nodeValue == ''));
  737. });
  738. return excludeBrsAndBookmarks.length === 0;
  739. }
  740. function processElement(element) {
  741. dom.removeClass(element, '_mce_act_on');
  742. if (!element || element.nodeType !== 1 || ! actions.processEvenIfEmpty && selectedBlocks.length > 1 && isEmptyElement(element)) {
  743. return;
  744. }
  745. element = findItemToOperateOn(element, dom);
  746. var action = actions[element.tagName];
  747. if (!action) {
  748. action = actions.defaultAction;
  749. }
  750. action(element);
  751. }
  752. function recurse(element) {
  753. t.splitSafeEach(element.childNodes, processElement, true);
  754. }
  755. function brAtEdgeOfSelection(container, offset) {
  756. return offset >= 0 && container.hasChildNodes() && offset < container.childNodes.length &&
  757. container.childNodes[offset].tagName === 'BR';
  758. }
  759. function isInTable() {
  760. var n = sel.getNode();
  761. var p = dom.getParent(n, 'td');
  762. return p !== null;
  763. }
  764. selectedBlocks = actions.elements;
  765. r = sel.getRng(true);
  766. if (!r.collapsed) {
  767. if (brAtEdgeOfSelection(r.endContainer, r.endOffset - 1)) {
  768. r.setEnd(r.endContainer, r.endOffset - 1);
  769. sel.setRng(r);
  770. }
  771. if (brAtEdgeOfSelection(r.startContainer, r.startOffset)) {
  772. r.setStart(r.startContainer, r.startOffset + 1);
  773. sel.setRng(r);
  774. }
  775. }
  776. if (tinymce.isIE8) {
  777. // append a zero sized nbsp so that caret is restored correctly using bookmark
  778. var s = t.ed.selection.getNode();
  779. if (s.tagName === 'LI' && !(s.parentNode.lastChild === s)) {
  780. var i = t.ed.getDoc().createTextNode('\uFEFF');
  781. s.appendChild(i);
  782. }
  783. }
  784. bookmark = sel.getBookmark();
  785. actions.OL = actions.UL = recurse;
  786. t.splitSafeEach(selectedBlocks, processElement);
  787. sel.moveToBookmark(bookmark);
  788. bookmark = null;
  789. // we avoid doing repaint in a table as this will move the caret out of the table in Firefox 3.6
  790. if (!isInTable()) {
  791. // Avoids table or image handles being left behind in Firefox.
  792. t.ed.execCommand('mceRepaint');
  793. }
  794. },
  795. splitSafeEach: function(elements, f, forceClassBase) {
  796. if (forceClassBase ||
  797. (tinymce.isGecko &&
  798. (/Firefox\/[12]\.[0-9]/.test(navigator.userAgent) ||
  799. /Firefox\/3\.[0-4]/.test(navigator.userAgent)))) {
  800. this.classBasedEach(elements, f);
  801. } else {
  802. each(elements, f);
  803. }
  804. },
  805. classBasedEach: function(elements, f) {
  806. var dom = this.ed.dom, nodes, element;
  807. // Mark nodes
  808. each(elements, function(element) {
  809. dom.addClass(element, '_mce_act_on');
  810. });
  811. nodes = dom.select('._mce_act_on');
  812. while (nodes.length > 0) {
  813. element = nodes.shift();
  814. dom.removeClass(element, '_mce_act_on');
  815. f(element);
  816. nodes = dom.select('._mce_act_on');
  817. }
  818. },
  819. adjustPaddingFunction: function(isIndent) {
  820. var indentAmount, indentUnits, ed = this.ed;
  821. indentAmount = ed.settings.indentation;
  822. indentUnits = /[a-z%]+/i.exec(indentAmount);
  823. indentAmount = parseInt(indentAmount, 10);
  824. return function(element) {
  825. var currentIndent, newIndentAmount;
  826. currentIndent = parseInt(ed.dom.getStyle(element, 'margin-left') || 0, 10) + parseInt(ed.dom.getStyle(element, 'padding-left') || 0, 10);
  827. if (isIndent) {
  828. newIndentAmount = currentIndent + indentAmount;
  829. } else {
  830. newIndentAmount = currentIndent - indentAmount;
  831. }
  832. ed.dom.setStyle(element, 'padding-left', '');
  833. ed.dom.setStyle(element, 'margin-left', newIndentAmount > 0 ? newIndentAmount + indentUnits : '');
  834. };
  835. },
  836. selectedBlocks: function() {
  837. var ed = this.ed, selectedBlocks = ed.selection.getSelectedBlocks();
  838. return selectedBlocks.length == 0 ? [ ed.dom.getRoot() ] : selectedBlocks;
  839. },
  840. getInfo: function() {
  841. return {
  842. longname : 'Lists',
  843. author : 'Moxiecode Systems AB',
  844. authorurl : 'http://tinymce.moxiecode.com',
  845. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/lists',
  846. version : tinymce.majorVersion + "." + tinymce.minorVersion
  847. };
  848. }
  849. });
  850. tinymce.PluginManager.add("lists", tinymce.plugins.Lists);
  851. }());