Till Krüss
2014-02-10 334475a50bcc97a8c326aadff0dcbb61fad40a4f
program/js/tiny_mce/plugins/table/editor_plugin_src.js
@@ -1,1202 +1,1449 @@
/**
 * $Id: editor_plugin_src.js 1209 2009-08-20 12:35:10Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
 */
(function() {
   var each = tinymce.each;
   // Checks if the selection/caret is at the start of the specified block element
   function isAtStart(rng, par) {
      var doc = par.ownerDocument, rng2 = doc.createRange(), elm;
      rng2.setStartBefore(par);
      rng2.setEnd(rng.endContainer, rng.endOffset);
      elm = doc.createElement('body');
      elm.appendChild(rng2.cloneContents());
      // Check for text characters of other elements that should be treated as content
      return elm.innerHTML.replace(/<(br|img|object|embed|input|textarea)[^>]*>/gi, '-').replace(/<[^>]+>/g, '').length == 0;
   };
   tinymce.create('tinymce.plugins.TablePlugin', {
      init : function(ed, url) {
         var t = this;
         t.editor = ed;
         t.url = url;
         // Register buttons
         each([
            ['table', 'table.desc', 'mceInsertTable', true],
            ['delete_table', 'table.del', 'mceTableDelete'],
            ['delete_col', 'table.delete_col_desc', 'mceTableDeleteCol'],
            ['delete_row', 'table.delete_row_desc', 'mceTableDeleteRow'],
            ['col_after', 'table.col_after_desc', 'mceTableInsertColAfter'],
            ['col_before', 'table.col_before_desc', 'mceTableInsertColBefore'],
            ['row_after', 'table.row_after_desc', 'mceTableInsertRowAfter'],
            ['row_before', 'table.row_before_desc', 'mceTableInsertRowBefore'],
            ['row_props', 'table.row_desc', 'mceTableRowProps', true],
            ['cell_props', 'table.cell_desc', 'mceTableCellProps', true],
            ['split_cells', 'table.split_cells_desc', 'mceTableSplitCells', true],
            ['merge_cells', 'table.merge_cells_desc', 'mceTableMergeCells', true]
         ], function(c) {
            ed.addButton(c[0], {title : c[1], cmd : c[2], ui : c[3]});
         });
         if (ed.getParam('inline_styles')) {
            // Force move of attribs to styles in strict mode
            ed.onPreProcess.add(function(ed, o) {
               var dom = ed.dom;
               each(dom.select('table', o.node), function(n) {
                  var v;
                  if (v = dom.getAttrib(n, 'width')) {
                     dom.setStyle(n, 'width', v);
                     dom.setAttrib(n, 'width');
                  }
                  if (v = dom.getAttrib(n, 'height')) {
                     dom.setStyle(n, 'height', v);
                     dom.setAttrib(n, 'height');
                  }
               });
            });
         }
         ed.onInit.add(function() {
            // Fixes an issue on Gecko where it's impossible to place the caret behind a table
            // This fix will force a paragraph element after the table but only when the forced_root_block setting is enabled
            if (!tinymce.isIE && ed.getParam('forced_root_block')) {
               function fixTableCaretPos() {
                  var last = ed.getBody().lastChild;
                  if (last && last.nodeName == 'TABLE')
                     ed.dom.add(ed.getBody(), 'p', null, '<br mce_bogus="1" />');
               };
               // Fixes an bug where it's impossible to place the caret before a table in Gecko
               // this fix solves it by detecting when the caret is at the beginning of such a table
               // and then manually moves the caret infront of the table
               if (tinymce.isGecko) {
                  ed.onKeyDown.add(function(ed, e) {
                     var rng, table, dom = ed.dom;
                     // On gecko it's not possible to place the caret before a table
                     if (e.keyCode == 37 || e.keyCode == 38) {
                        rng = ed.selection.getRng();
                        table = dom.getParent(rng.startContainer, 'table');
                        if (table && ed.getBody().firstChild == table) {
                           if (isAtStart(rng, table)) {
                              rng = dom.createRng();
                              rng.setStartBefore(table);
                              rng.setEndBefore(table);
                              ed.selection.setRng(rng);
                              e.preventDefault();
                           }
                        }
                     }
                  });
               }
               ed.onKeyUp.add(fixTableCaretPos);
               ed.onSetContent.add(fixTableCaretPos);
               ed.onVisualAid.add(fixTableCaretPos);
               ed.onPreProcess.add(function(ed, o) {
                  var last = o.node.lastChild;
                  if (last && last.childNodes.length == 1 && last.firstChild.nodeName == 'BR')
                     ed.dom.remove(last);
               });
               fixTableCaretPos();
            }
            if (ed && ed.plugins.contextmenu) {
               ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
                  var sm, se = ed.selection, el = se.getNode() || ed.getBody();
                  if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th')) {
                     m.removeAll();
                     if (el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) {
                        m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
                        m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
                        m.addSeparator();
                     }
                     if (el.nodeName == 'IMG' && el.className.indexOf('mceItem') == -1) {
                        m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
                        m.addSeparator();
                     }
                     m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true, value : {action : 'insert'}});
                     m.add({title : 'table.props_desc', icon : 'table_props', cmd : 'mceInsertTable', ui : true});
                     m.add({title : 'table.del', icon : 'delete_table', cmd : 'mceTableDelete', ui : true});
                     m.addSeparator();
                     // Cell menu
                     sm = m.addMenu({title : 'table.cell'});
                     sm.add({title : 'table.cell_desc', icon : 'cell_props', cmd : 'mceTableCellProps', ui : true});
                     sm.add({title : 'table.split_cells_desc', icon : 'split_cells', cmd : 'mceTableSplitCells', ui : true});
                     sm.add({title : 'table.merge_cells_desc', icon : 'merge_cells', cmd : 'mceTableMergeCells', ui : true});
                     // Row menu
                     sm = m.addMenu({title : 'table.row'});
                     sm.add({title : 'table.row_desc', icon : 'row_props', cmd : 'mceTableRowProps', ui : true});
                     sm.add({title : 'table.row_before_desc', icon : 'row_before', cmd : 'mceTableInsertRowBefore'});
                     sm.add({title : 'table.row_after_desc', icon : 'row_after', cmd : 'mceTableInsertRowAfter'});
                     sm.add({title : 'table.delete_row_desc', icon : 'delete_row', cmd : 'mceTableDeleteRow'});
                     sm.addSeparator();
                     sm.add({title : 'table.cut_row_desc', icon : 'cut', cmd : 'mceTableCutRow'});
                     sm.add({title : 'table.copy_row_desc', icon : 'copy', cmd : 'mceTableCopyRow'});
                     sm.add({title : 'table.paste_row_before_desc', icon : 'paste', cmd : 'mceTablePasteRowBefore'});
                     sm.add({title : 'table.paste_row_after_desc', icon : 'paste', cmd : 'mceTablePasteRowAfter'});
                     // Column menu
                     sm = m.addMenu({title : 'table.col'});
                     sm.add({title : 'table.col_before_desc', icon : 'col_before', cmd : 'mceTableInsertColBefore'});
                     sm.add({title : 'table.col_after_desc', icon : 'col_after', cmd : 'mceTableInsertColAfter'});
                     sm.add({title : 'table.delete_col_desc', icon : 'delete_col', cmd : 'mceTableDeleteCol'});
                  } else
                     m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true});
               });
            }
         });
         // Add undo level when new rows are created using the tab key
         ed.onKeyDown.add(function(ed, e) {
            if (e.keyCode == 9 && ed.dom.getParent(ed.selection.getNode(), 'TABLE')) {
               if (!tinymce.isGecko && !tinymce.isOpera) {
                  tinyMCE.execInstanceCommand(ed.editorId, "mceTableMoveToNextRow", true);
                  return tinymce.dom.Event.cancel(e);
               }
               ed.undoManager.add();
            }
         });
         // Select whole table is a table border is clicked
         if (!tinymce.isIE) {
            if (ed.getParam('table_selection', true)) {
               ed.onClick.add(function(ed, e) {
                  e = e.target;
                  if (e.nodeName === 'TABLE')
                     ed.selection.select(e);
               });
            }
         }
         ed.onNodeChange.add(function(ed, cm, n) {
            var p = ed.dom.getParent(n, 'td,th,caption');
            cm.setActive('table', n.nodeName === 'TABLE' || !!p);
            if (p && p.nodeName === 'CAPTION')
               p = null;
            cm.setDisabled('delete_table', !p);
            cm.setDisabled('delete_col', !p);
            cm.setDisabled('delete_table', !p);
            cm.setDisabled('delete_row', !p);
            cm.setDisabled('col_after', !p);
            cm.setDisabled('col_before', !p);
            cm.setDisabled('row_after', !p);
            cm.setDisabled('row_before', !p);
            cm.setDisabled('row_props', !p);
            cm.setDisabled('cell_props', !p);
            cm.setDisabled('split_cells', !p || (parseInt(ed.dom.getAttrib(p, 'colspan', '1')) < 2 && parseInt(ed.dom.getAttrib(p, 'rowspan', '1')) < 2));
            cm.setDisabled('merge_cells', !p);
         });
         // Padd empty table cells
         if (!tinymce.isIE) {
            ed.onBeforeSetContent.add(function(ed, o) {
               if (o.initial)
                  o.content = o.content.replace(/<(td|th)([^>]+|)>\s*<\/(td|th)>/g, tinymce.isOpera ? '<$1$2>&nbsp;</$1>' : '<$1$2><br mce_bogus="1" /></$1>');
            });
         }
      },
      execCommand : function(cmd, ui, val) {
         var ed = this.editor, b;
         // Is table command
         switch (cmd) {
            case "mceTableMoveToNextRow":
            case "mceInsertTable":
            case "mceTableRowProps":
            case "mceTableCellProps":
            case "mceTableSplitCells":
            case "mceTableMergeCells":
            case "mceTableInsertRowBefore":
            case "mceTableInsertRowAfter":
            case "mceTableDeleteRow":
            case "mceTableInsertColBefore":
            case "mceTableInsertColAfter":
            case "mceTableDeleteCol":
            case "mceTableCutRow":
            case "mceTableCopyRow":
            case "mceTablePasteRowBefore":
            case "mceTablePasteRowAfter":
            case "mceTableDelete":
               ed.execCommand('mceBeginUndoLevel');
               this._doExecCommand(cmd, ui, val);
               ed.execCommand('mceEndUndoLevel');
               return true;
         }
         // Pass to next handler in chain
         return false;
      },
      getInfo : function() {
         return {
            longname : 'Tables',
            author : 'Moxiecode Systems AB',
            authorurl : 'http://tinymce.moxiecode.com',
            infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
            version : tinymce.majorVersion + "." + tinymce.minorVersion
         };
      },
      // Private plugin internal methods
      /**
       * Executes the table commands.
       */
      _doExecCommand : function(command, user_interface, value) {
         var inst = this.editor, ed = inst, url = this.url;
         var focusElm = inst.selection.getNode();
         var trElm = inst.dom.getParent(focusElm, "tr");
         var tdElm = inst.dom.getParent(focusElm, "td,th");
         var tableElm = inst.dom.getParent(focusElm, "table");
         var doc = inst.contentWindow.document;
         var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
         // Get first TD if no TD found
         if (trElm && tdElm == null)
            tdElm = trElm.cells[0];
         function inArray(ar, v) {
            for (var i=0; i<ar.length; i++) {
               // Is array
               if (ar[i].length > 0 && inArray(ar[i], v))
                  return true;
               // Found value
               if (ar[i] == v)
                  return true;
            }
            return false;
         }
         function select(dx, dy) {
            var td;
            grid = getTableGrid(tableElm);
            dx = dx || 0;
            dy = dy || 0;
            dx = Math.max(cpos.cellindex + dx, 0);
            dy = Math.max(cpos.rowindex + dy, 0);
            // Recalculate grid and select
            inst.execCommand('mceRepaint');
            td = getCell(grid, dy, dx);
            if (td) {
               inst.selection.select(td.firstChild || td);
               inst.selection.collapse(1);
            }
         };
         function makeTD() {
            var newTD = doc.createElement("td");
            if (!tinymce.isIE)
               newTD.innerHTML = '<br mce_bogus="1"/>';
         }
         function getColRowSpan(td) {
            var colspan = inst.dom.getAttrib(td, "colspan");
            var rowspan = inst.dom.getAttrib(td, "rowspan");
            colspan = colspan == "" ? 1 : parseInt(colspan);
            rowspan = rowspan == "" ? 1 : parseInt(rowspan);
            return {colspan : colspan, rowspan : rowspan};
         }
         function getCellPos(grid, td) {
            var x, y;
            for (y=0; y<grid.length; y++) {
               for (x=0; x<grid[y].length; x++) {
                  if (grid[y][x] == td)
                     return {cellindex : x, rowindex : y};
               }
            }
            return null;
         }
         function getCell(grid, row, col) {
            if (grid[row] && grid[row][col])
               return grid[row][col];
            return null;
         }
         function getNextCell(table, cell) {
            var cells = [], x = 0, i, j, cell, nextCell;
            for (i = 0; i < table.rows.length; i++)
               for (j = 0; j < table.rows[i].cells.length; j++, x++)
                  cells[x] = table.rows[i].cells[j];
            for (i = 0; i < cells.length; i++)
               if (cells[i] == cell)
                  if (nextCell = cells[i+1])
                     return nextCell;
         }
         function getTableGrid(table) {
            var grid = [], rows = table.rows, x, y, td, sd, xstart, x2, y2;
            for (y=0; y<rows.length; y++) {
               for (x=0; x<rows[y].cells.length; x++) {
                  td = rows[y].cells[x];
                  sd = getColRowSpan(td);
                  // All ready filled
                  for (xstart = x; grid[y] && grid[y][xstart]; xstart++) ;
                  // Fill box
                  for (y2=y; y2<y+sd['rowspan']; y2++) {
                     if (!grid[y2])
                        grid[y2] = [];
                     for (x2=xstart; x2<xstart+sd['colspan']; x2++)
                        grid[y2][x2] = td;
                  }
               }
            }
            return grid;
         }
         function trimRow(table, tr, td, new_tr) {
            var grid = getTableGrid(table), cpos = getCellPos(grid, td);
            var cells, lastElm;
            // Time to crop away some
            if (new_tr.cells.length != tr.childNodes.length) {
               cells = tr.childNodes;
               lastElm = null;
               for (var x=0; td = getCell(grid, cpos.rowindex, x); x++) {
                  var remove = true;
                  var sd = getColRowSpan(td);
                  // Remove due to rowspan
                  if (inArray(cells, td)) {
                     new_tr.childNodes[x]._delete = true;
                  } else if ((lastElm == null || td != lastElm) && sd.colspan > 1) { // Remove due to colspan
                     for (var i=x; i<x+td.colSpan; i++)
                        new_tr.childNodes[i]._delete = true;
                  }
                  if ((lastElm == null || td != lastElm) && sd.rowspan > 1)
                     td.rowSpan = sd.rowspan + 1;
                  lastElm = td;
               }
               deleteMarked(tableElm);
            }
         }
         function prevElm(node, name) {
            while ((node = node.previousSibling) != null) {
               if (node.nodeName == name)
                  return node;
            }
            return null;
         }
         function nextElm(node, names) {
            var namesAr = names.split(',');
            while ((node = node.nextSibling) != null) {
               for (var i=0; i<namesAr.length; i++) {
                  if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
                     return node;
               }
            }
            return null;
         }
         function deleteMarked(tbl) {
            if (tbl.rows == 0)
               return;
            var tr = tbl.rows[0];
            do {
               var next = nextElm(tr, "TR");
               // Delete row
               if (tr._delete) {
                  tr.parentNode.removeChild(tr);
                  continue;
               }
               // Delete cells
               var td = tr.cells[0];
               if (td.cells > 1) {
                  do {
                     var nexttd = nextElm(td, "TD,TH");
                     if (td._delete)
                        td.parentNode.removeChild(td);
                  } while ((td = nexttd) != null);
               }
            } while ((tr = next) != null);
         }
         function addRows(td_elm, tr_elm, rowspan) {
            // Add rows
            td_elm.rowSpan = 1;
            var trNext = nextElm(tr_elm, "TR");
            for (var i=1; i<rowspan && trNext; i++) {
               var newTD = doc.createElement("td");
               if (!tinymce.isIE)
                  newTD.innerHTML = '<br mce_bogus="1"/>';
               if (tinymce.isIE)
                  trNext.insertBefore(newTD, trNext.cells(td_elm.cellIndex));
               else
                  trNext.insertBefore(newTD, trNext.cells[td_elm.cellIndex]);
               trNext = nextElm(trNext, "TR");
            }
         }
         function copyRow(doc, table, tr) {
            var grid = getTableGrid(table);
            var newTR = tr.cloneNode(false);
            var cpos = getCellPos(grid, tr.cells[0]);
            var lastCell = null;
            var tableBorder = inst.dom.getAttrib(table, "border");
            var tdElm = null;
            for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
               var newTD = null;
               if (lastCell != tdElm) {
                  for (var i=0; i<tr.cells.length; i++) {
                     if (tdElm == tr.cells[i]) {
                        newTD = tdElm.cloneNode(true);
                        break;
                     }
                  }
               }
               if (newTD == null) {
                  newTD = doc.createElement("td");
                  if (!tinymce.isIE)
                     newTD.innerHTML = '<br mce_bogus="1"/>';
               }
               // Reset col/row span
               newTD.colSpan = 1;
               newTD.rowSpan = 1;
               newTR.appendChild(newTD);
               lastCell = tdElm;
            }
            return newTR;
         }
         // ---- Commands -----
         // Handle commands
         switch (command) {
            case "mceTableMoveToNextRow":
               var nextCell = getNextCell(tableElm, tdElm);
               if (!nextCell) {
                  inst.execCommand("mceTableInsertRowAfter", tdElm);
                  nextCell = getNextCell(tableElm, tdElm);
               }
               inst.selection.select(nextCell);
               inst.selection.collapse(true);
               return true;
            case "mceTableRowProps":
               if (trElm == null)
                  return true;
               if (user_interface) {
                  inst.windowManager.open({
                     url : url + '/row.htm',
                     width : 400 + parseInt(inst.getLang('table.rowprops_delta_width', 0)),
                     height : 295 + parseInt(inst.getLang('table.rowprops_delta_height', 0)),
                     inline : 1
                  }, {
                     plugin_url : url
                  });
               }
               return true;
            case "mceTableCellProps":
               if (tdElm == null)
                  return true;
               if (user_interface) {
                  inst.windowManager.open({
                     url : url + '/cell.htm',
                     width : 400 + parseInt(inst.getLang('table.cellprops_delta_width', 0)),
                     height : 295 + parseInt(inst.getLang('table.cellprops_delta_height', 0)),
                     inline : 1
                  }, {
                     plugin_url : url
                  });
               }
               return true;
            case "mceInsertTable":
               if (user_interface) {
                  inst.windowManager.open({
                     url : url + '/table.htm',
                     width : 400 + parseInt(inst.getLang('table.table_delta_width', 0)),
                     height : 320 + parseInt(inst.getLang('table.table_delta_height', 0)),
                     inline : 1
                  }, {
                     plugin_url : url,
                     action : value ? value.action : 0
                  });
               }
               return true;
            case "mceTableDelete":
               var table = inst.dom.getParent(inst.selection.getNode(), "table");
               if (table) {
                  table.parentNode.removeChild(table);
                  inst.execCommand('mceRepaint');
               }
               return true;
            case "mceTableSplitCells":
            case "mceTableMergeCells":
            case "mceTableInsertRowBefore":
            case "mceTableInsertRowAfter":
            case "mceTableDeleteRow":
            case "mceTableInsertColBefore":
            case "mceTableInsertColAfter":
            case "mceTableDeleteCol":
            case "mceTableCutRow":
            case "mceTableCopyRow":
            case "mceTablePasteRowBefore":
            case "mceTablePasteRowAfter":
               // No table just return (invalid command)
               if (!tableElm)
                  return true;
               // Table has a tbody use that reference
               // Changed logic by ApTest 2005.07.12 (www.aptest.com)
               // Now lookk at the focused element and take its parentNode.  That will be a tbody or a table.
               if (trElm && tableElm != trElm.parentNode)
                  tableElm = trElm.parentNode;
               if (tableElm && trElm) {
                  switch (command) {
                     case "mceTableCutRow":
                        if (!trElm || !tdElm)
                           return true;
                        inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
                        inst.execCommand("mceTableDeleteRow");
                        break;
                     case "mceTableCopyRow":
                        if (!trElm || !tdElm)
                           return true;
                        inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
                        break;
                     case "mceTablePasteRowBefore":
                        if (!trElm || !tdElm)
                           return true;
                        var newTR = inst.tableRowClipboard.cloneNode(true);
                        var prevTR = prevElm(trElm, "TR");
                        if (prevTR != null)
                           trimRow(tableElm, prevTR, prevTR.cells[0], newTR);
                        trElm.parentNode.insertBefore(newTR, trElm);
                        break;
                     case "mceTablePasteRowAfter":
                        if (!trElm || !tdElm)
                           return true;
                        var nextTR = nextElm(trElm, "TR");
                        var newTR = inst.tableRowClipboard.cloneNode(true);
                        trimRow(tableElm, trElm, tdElm, newTR);
                        if (nextTR == null)
                           trElm.parentNode.appendChild(newTR);
                        else
                           nextTR.parentNode.insertBefore(newTR, nextTR);
                        break;
                     case "mceTableInsertRowBefore":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(tableElm);
                        var cpos = getCellPos(grid, tdElm);
                        var newTR = doc.createElement("tr");
                        var lastTDElm = null;
                        cpos.rowindex--;
                        if (cpos.rowindex < 0)
                           cpos.rowindex = 0;
                        // Create cells
                        for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd['rowspan'] == 1) {
                                 var newTD = doc.createElement("td");
                                 if (!tinymce.isIE)
                                    newTD.innerHTML = '<br mce_bogus="1"/>';
                                 newTD.colSpan = tdElm.colSpan;
                                 newTR.appendChild(newTD);
                              } else
                                 tdElm.rowSpan = sd['rowspan'] + 1;
                              lastTDElm = tdElm;
                           }
                        }
                        trElm.parentNode.insertBefore(newTR, trElm);
                        select(0, 1);
                     break;
                     case "mceTableInsertRowAfter":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(tableElm);
                        var cpos = getCellPos(grid, tdElm);
                        var newTR = doc.createElement("tr");
                        var lastTDElm = null;
                        // Create cells
                        for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd['rowspan'] == 1) {
                                 var newTD = doc.createElement("td");
                                 if (!tinymce.isIE)
                                    newTD.innerHTML = '<br mce_bogus="1"/>';
                                 newTD.colSpan = tdElm.colSpan;
                                 newTR.appendChild(newTD);
                              } else
                                 tdElm.rowSpan = sd['rowspan'] + 1;
                              lastTDElm = tdElm;
                           }
                        }
                        if (newTR.hasChildNodes()) {
                           var nextTR = nextElm(trElm, "TR");
                           if (nextTR)
                              nextTR.parentNode.insertBefore(newTR, nextTR);
                           else
                              tableElm.appendChild(newTR);
                        }
                        select(0, 1);
                     break;
                     case "mceTableDeleteRow":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(tableElm);
                        var cpos = getCellPos(grid, tdElm);
                        // Only one row, remove whole table
                        if (grid.length == 1 && tableElm.nodeName == 'TBODY') {
                           inst.dom.remove(inst.dom.getParent(tableElm, "table"));
                           return true;
                        }
                        // Move down row spanned cells
                        var cells = trElm.cells;
                        var nextTR = nextElm(trElm, "TR");
                        for (var x=0; x<cells.length; x++) {
                           if (cells[x].rowSpan > 1) {
                              var newTD = cells[x].cloneNode(true);
                              var sd = getColRowSpan(cells[x]);
                              newTD.rowSpan = sd.rowspan - 1;
                              var nextTD = nextTR.cells[x];
                              if (nextTD == null)
                                 nextTR.appendChild(newTD);
                              else
                                 nextTR.insertBefore(newTD, nextTD);
                           }
                        }
                        // Delete cells
                        var lastTDElm = null;
                        for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd.rowspan > 1) {
                                 tdElm.rowSpan = sd.rowspan - 1;
                              } else {
                                 trElm = tdElm.parentNode;
                                 if (trElm.parentNode)
                                    trElm._delete = true;
                              }
                              lastTDElm = tdElm;
                           }
                        }
                        deleteMarked(tableElm);
                        select(0, -1);
                     break;
                     case "mceTableInsertColBefore":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(inst.dom.getParent(tableElm, "table"));
                        var cpos = getCellPos(grid, tdElm);
                        var lastTDElm = null;
                        for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd['colspan'] == 1) {
                                 var newTD = doc.createElement(tdElm.nodeName);
                                 if (!tinymce.isIE)
                                    newTD.innerHTML = '<br mce_bogus="1"/>';
                                 newTD.rowSpan = tdElm.rowSpan;
                                 tdElm.parentNode.insertBefore(newTD, tdElm);
                              } else
                                 tdElm.colSpan++;
                              lastTDElm = tdElm;
                           }
                        }
                        select();
                     break;
                     case "mceTableInsertColAfter":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(inst.dom.getParent(tableElm, "table"));
                        var cpos = getCellPos(grid, tdElm);
                        var lastTDElm = null;
                        for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd['colspan'] == 1) {
                                 var newTD = doc.createElement(tdElm.nodeName);
                                 if (!tinymce.isIE)
                                    newTD.innerHTML = '<br mce_bogus="1"/>';
                                 newTD.rowSpan = tdElm.rowSpan;
                                 var nextTD = nextElm(tdElm, "TD,TH");
                                 if (nextTD == null)
                                    tdElm.parentNode.appendChild(newTD);
                                 else
                                    nextTD.parentNode.insertBefore(newTD, nextTD);
                              } else
                                 tdElm.colSpan++;
                              lastTDElm = tdElm;
                           }
                        }
                        select(1);
                     break;
                     case "mceTableDeleteCol":
                        if (!trElm || !tdElm)
                           return true;
                        var grid = getTableGrid(tableElm);
                        var cpos = getCellPos(grid, tdElm);
                        var lastTDElm = null;
                        // Only one col, remove whole table
                        if ((grid.length > 1 && grid[0].length <= 1) && tableElm.nodeName == 'TBODY') {
                           inst.dom.remove(inst.dom.getParent(tableElm, "table"));
                           return true;
                        }
                        // Delete cells
                        for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
                           if (tdElm != lastTDElm) {
                              var sd = getColRowSpan(tdElm);
                              if (sd['colspan'] > 1)
                                 tdElm.colSpan = sd['colspan'] - 1;
                              else {
                                 if (tdElm.parentNode)
                                    tdElm.parentNode.removeChild(tdElm);
                              }
                              lastTDElm = tdElm;
                           }
                        }
                        select(-1);
                     break;
                  case "mceTableSplitCells":
                     if (!trElm || !tdElm)
                        return true;
                     var spandata = getColRowSpan(tdElm);
                     var colspan = spandata["colspan"];
                     var rowspan = spandata["rowspan"];
                     // Needs splitting
                     if (colspan > 1 || rowspan > 1) {
                        // Generate cols
                        tdElm.colSpan = 1;
                        for (var i=1; i<colspan; i++) {
                           var newTD = doc.createElement("td");
                           if (!tinymce.isIE)
                              newTD.innerHTML = '<br mce_bogus="1"/>';
                           trElm.insertBefore(newTD, nextElm(tdElm, "TD,TH"));
                           if (rowspan > 1)
                              addRows(newTD, trElm, rowspan);
                        }
                        addRows(tdElm, trElm, rowspan);
                     }
                     // Apply visual aids
                     tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
                     break;
                  case "mceTableMergeCells":
                     var rows = [];
                     var sel = inst.selection.getSel();
                     var grid = getTableGrid(tableElm);
                     if (tinymce.isIE || sel.rangeCount == 1) {
                        if (user_interface) {
                           // Setup template
                           var sp = getColRowSpan(tdElm);
                           inst.windowManager.open({
                              url : url + '/merge_cells.htm',
                              width : 240 + parseInt(inst.getLang('table.merge_cells_delta_width', 0)),
                              height : 110 + parseInt(inst.getLang('table.merge_cells_delta_height', 0)),
                              inline : 1
                           }, {
                              action : "update",
                              numcols : sp.colspan,
                              numrows : sp.rowspan,
                              plugin_url : url
                           });
                           return true;
                        } else {
                           var numRows = parseInt(value['numrows']);
                           var numCols = parseInt(value['numcols']);
                           var cpos = getCellPos(grid, tdElm);
                           if (("" + numRows) == "NaN")
                              numRows = 1;
                           if (("" + numCols) == "NaN")
                              numCols = 1;
                           // Get rows and cells
                           var tRows = tableElm.rows;
                           for (var y=cpos.rowindex; y<grid.length; y++) {
                              var rowCells = [];
                              for (var x=cpos.cellindex; x<grid[y].length; x++) {
                                 var td = getCell(grid, y, x);
                                 if (td && !inArray(rows, td) && !inArray(rowCells, td)) {
                                    var cp = getCellPos(grid, td);
                                    // Within range
                                    if (cp.cellindex < cpos.cellindex+numCols && cp.rowindex < cpos.rowindex+numRows)
                                       rowCells[rowCells.length] = td;
                                 }
                              }
                              if (rowCells.length > 0)
                                 rows[rows.length] = rowCells;
                              var td = getCell(grid, cpos.rowindex, cpos.cellindex);
                              each(ed.dom.select('br', td), function(e, i) {
                                 if (i > 0 && ed.dom.getAttrib('mce_bogus'))
                                    ed.dom.remove(e);
                              });
                           }
                           //return true;
                        }
                     } else {
                        var cells = [];
                        var sel = inst.selection.getSel();
                        var lastTR = null;
                        var curRow = null;
                        var x1 = -1, y1 = -1, x2, y2;
                        // Only one cell selected, whats the point?
                        if (sel.rangeCount < 2)
                           return true;
                        // Get all selected cells
                        for (var i=0; i<sel.rangeCount; i++) {
                           var rng = sel.getRangeAt(i);
                           var tdElm = rng.startContainer.childNodes[rng.startOffset];
                           if (!tdElm)
                              break;
                           if (tdElm.nodeName == "TD" || tdElm.nodeName == "TH")
                              cells[cells.length] = tdElm;
                        }
                        // Get rows and cells
                        var tRows = tableElm.rows;
                        for (var y=0; y<tRows.length; y++) {
                           var rowCells = [];
                           for (var x=0; x<tRows[y].cells.length; x++) {
                              var td = tRows[y].cells[x];
                              for (var i=0; i<cells.length; i++) {
                                 if (td == cells[i]) {
                                    rowCells[rowCells.length] = td;
                                 }
                              }
                           }
                           if (rowCells.length > 0)
                              rows[rows.length] = rowCells;
                        }
                        // Find selected cells in grid and box
                        var curRow = [];
                        var lastTR = null;
                        for (var y=0; y<grid.length; y++) {
                           for (var x=0; x<grid[y].length; x++) {
                              grid[y][x]._selected = false;
                              for (var i=0; i<cells.length; i++) {
                                 if (grid[y][x] == cells[i]) {
                                    // Get start pos
                                    if (x1 == -1) {
                                       x1 = x;
                                       y1 = y;
                                    }
                                    // Get end pos
                                    x2 = x;
                                    y2 = y;
                                    grid[y][x]._selected = true;
                                 }
                              }
                           }
                        }
                        // Is there gaps, if so deny
                        for (var y=y1; y<=y2; y++) {
                           for (var x=x1; x<=x2; x++) {
                              if (!grid[y][x]._selected) {
                                 alert("Invalid selection for merge.");
                                 return true;
                              }
                           }
                        }
                     }
                     // Validate selection and get total rowspan and colspan
                     var rowSpan = 1, colSpan = 1;
                     // Validate horizontal and get total colspan
                     var lastRowSpan = -1;
                     for (var y=0; y<rows.length; y++) {
                        var rowColSpan = 0;
                        for (var x=0; x<rows[y].length; x++) {
                           var sd = getColRowSpan(rows[y][x]);
                           rowColSpan += sd['colspan'];
                           if (lastRowSpan != -1 && sd['rowspan'] != lastRowSpan) {
                              alert("Invalid selection for merge.");
                              return true;
                           }
                           lastRowSpan = sd['rowspan'];
                        }
                        if (rowColSpan > colSpan)
                           colSpan = rowColSpan;
                        lastRowSpan = -1;
                     }
                     // Validate vertical and get total rowspan
                     var lastColSpan = -1;
                     for (var x=0; x<rows[0].length; x++) {
                        var colRowSpan = 0;
                        for (var y=0; y<rows.length; y++) {
                           var sd = getColRowSpan(rows[y][x]);
                           colRowSpan += sd['rowspan'];
                           if (lastColSpan != -1 && sd['colspan'] != lastColSpan) {
                              alert("Invalid selection for merge.");
                              return true;
                           }
                           lastColSpan = sd['colspan'];
                        }
                        if (colRowSpan > rowSpan)
                           rowSpan = colRowSpan;
                        lastColSpan = -1;
                     }
                     // Setup td
                     tdElm = rows[0][0];
                     tdElm.rowSpan = rowSpan;
                     tdElm.colSpan = colSpan;
                     // Merge cells
                     for (var y=0; y<rows.length; y++) {
                        for (var x=0; x<rows[y].length; x++) {
                           var html = rows[y][x].innerHTML;
                           var chk = html.replace(/[ \t\r\n]/g, "");
                           if (chk != "<br/>" && chk != "<br>" && chk != '<br mce_bogus="1"/>' && (x+y > 0))
                              tdElm.innerHTML += html;
                           // Not current cell
                           if (rows[y][x] != tdElm && !rows[y][x]._deleted) {
                              var cpos = getCellPos(grid, rows[y][x]);
                              var tr = rows[y][x].parentNode;
                              tr.removeChild(rows[y][x]);
                              rows[y][x]._deleted = true;
                              // Empty TR, remove it
                              if (!tr.hasChildNodes()) {
                                 tr.parentNode.removeChild(tr);
                                 var lastCell = null;
                                 for (var x=0; cellElm = getCell(grid, cpos.rowindex, x); x++) {
                                    if (cellElm != lastCell && cellElm.rowSpan > 1)
                                       cellElm.rowSpan--;
                                    lastCell = cellElm;
                                 }
                                 if (tdElm.rowSpan > 1)
                                    tdElm.rowSpan--;
                              }
                           }
                        }
                     }
                     // Remove all but one bogus br
                     each(ed.dom.select('br', tdElm), function(e, i) {
                        if (i > 0 && ed.dom.getAttrib(e, 'mce_bogus'))
                           ed.dom.remove(e);
                     });
                     break;
                  }
                  tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
                  inst.addVisual(tableElm);
                  inst.nodeChanged();
               }
            return true;
         }
         // Pass to next handler in chain
         return false;
      }
   });
   // Register plugin
   tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
})();
/**
 * editor_plugin_src.js
 *
 * Copyright 2009, Moxiecode Systems AB
 * Released under LGPL License.
 *
 * License: http://tinymce.moxiecode.com/license
 * Contributing: http://tinymce.moxiecode.com/contributing
 */
(function(tinymce) {
   var each = tinymce.each;
   // Checks if the selection/caret is at the start of the specified block element
   function isAtStart(rng, par) {
      var doc = par.ownerDocument, rng2 = doc.createRange(), elm;
      rng2.setStartBefore(par);
      rng2.setEnd(rng.endContainer, rng.endOffset);
      elm = doc.createElement('body');
      elm.appendChild(rng2.cloneContents());
      // Check for text characters of other elements that should be treated as content
      return elm.innerHTML.replace(/<(br|img|object|embed|input|textarea)[^>]*>/gi, '-').replace(/<[^>]+>/g, '').length == 0;
   };
   function getSpanVal(td, name) {
      return parseInt(td.getAttribute(name) || 1);
   }
   /**
    * Table Grid class.
    */
   function TableGrid(table, dom, selection) {
      var grid, startPos, endPos, selectedCell;
      buildGrid();
      selectedCell = dom.getParent(selection.getStart(), 'th,td');
      if (selectedCell) {
         startPos = getPos(selectedCell);
         endPos = findEndPos();
         selectedCell = getCell(startPos.x, startPos.y);
      }
      function cloneNode(node, children) {
         node = node.cloneNode(children);
         node.removeAttribute('id');
         return node;
      }
      function buildGrid() {
         var startY = 0;
         grid = [];
         each(['thead', 'tbody', 'tfoot'], function(part) {
            var rows = dom.select('> ' + part + ' tr', table);
            each(rows, function(tr, y) {
               y += startY;
               each(dom.select('> td, > th', tr), function(td, x) {
                  var x2, y2, rowspan, colspan;
                  // Skip over existing cells produced by rowspan
                  if (grid[y]) {
                     while (grid[y][x])
                        x++;
                  }
                  // Get col/rowspan from cell
                  rowspan = getSpanVal(td, 'rowspan');
                  colspan = getSpanVal(td, 'colspan');
                  // Fill out rowspan/colspan right and down
                  for (y2 = y; y2 < y + rowspan; y2++) {
                     if (!grid[y2])
                        grid[y2] = [];
                     for (x2 = x; x2 < x + colspan; x2++) {
                        grid[y2][x2] = {
                           part : part,
                           real : y2 == y && x2 == x,
                           elm : td,
                           rowspan : rowspan,
                           colspan : colspan
                        };
                     }
                  }
               });
            });
            startY += rows.length;
         });
      };
      function getCell(x, y) {
         var row;
         row = grid[y];
         if (row)
            return row[x];
      };
      function setSpanVal(td, name, val) {
         if (td) {
            val = parseInt(val);
            if (val === 1)
               td.removeAttribute(name, 1);
            else
               td.setAttribute(name, val, 1);
         }
      }
      function isCellSelected(cell) {
         return cell && (dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell);
      };
      function getSelectedRows() {
         var rows = [];
         each(table.rows, function(row) {
            each(row.cells, function(cell) {
               if (dom.hasClass(cell, 'mceSelected') || cell == selectedCell.elm) {
                  rows.push(row);
                  return false;
               }
            });
         });
         return rows;
      };
      function deleteTable() {
         var rng = dom.createRng();
         rng.setStartAfter(table);
         rng.setEndAfter(table);
         selection.setRng(rng);
         dom.remove(table);
      };
      function cloneCell(cell) {
         var formatNode;
         // Clone formats
         tinymce.walk(cell, function(node) {
            var curNode;
            if (node.nodeType == 3) {
               each(dom.getParents(node.parentNode, null, cell).reverse(), function(node) {
                  node = cloneNode(node, false);
                  if (!formatNode)
                     formatNode = curNode = node;
                  else if (curNode)
                     curNode.appendChild(node);
                  curNode = node;
               });
               // Add something to the inner node
               if (curNode)
                  curNode.innerHTML = tinymce.isIE ? '&nbsp;' : '<br data-mce-bogus="1" />';
               return false;
            }
         }, 'childNodes');
         cell = cloneNode(cell, false);
         setSpanVal(cell, 'rowSpan', 1);
         setSpanVal(cell, 'colSpan', 1);
         if (formatNode) {
            cell.appendChild(formatNode);
         } else {
            if (!tinymce.isIE)
               cell.innerHTML = '<br data-mce-bogus="1" />';
         }
         return cell;
      };
      function cleanup() {
         var rng = dom.createRng();
         // Empty rows
         each(dom.select('tr', table), function(tr) {
            if (tr.cells.length == 0)
               dom.remove(tr);
         });
         // Empty table
         if (dom.select('tr', table).length == 0) {
            rng.setStartAfter(table);
            rng.setEndAfter(table);
            selection.setRng(rng);
            dom.remove(table);
            return;
         }
         // Empty header/body/footer
         each(dom.select('thead,tbody,tfoot', table), function(part) {
            if (part.rows.length == 0)
               dom.remove(part);
         });
         // Restore selection to start position if it still exists
         buildGrid();
         // Restore the selection to the closest table position
         row = grid[Math.min(grid.length - 1, startPos.y)];
         if (row) {
            selection.select(row[Math.min(row.length - 1, startPos.x)].elm, true);
            selection.collapse(true);
         }
      };
      function fillLeftDown(x, y, rows, cols) {
         var tr, x2, r, c, cell;
         tr = grid[y][x].elm.parentNode;
         for (r = 1; r <= rows; r++) {
            tr = dom.getNext(tr, 'tr');
            if (tr) {
               // Loop left to find real cell
               for (x2 = x; x2 >= 0; x2--) {
                  cell = grid[y + r][x2].elm;
                  if (cell.parentNode == tr) {
                     // Append clones after
                     for (c = 1; c <= cols; c++)
                        dom.insertAfter(cloneCell(cell), cell);
                     break;
                  }
               }
               if (x2 == -1) {
                  // Insert nodes before first cell
                  for (c = 1; c <= cols; c++)
                     tr.insertBefore(cloneCell(tr.cells[0]), tr.cells[0]);
               }
            }
         }
      };
      function split() {
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               var colSpan, rowSpan, newCell, i;
               if (isCellSelected(cell)) {
                  cell = cell.elm;
                  colSpan = getSpanVal(cell, 'colspan');
                  rowSpan = getSpanVal(cell, 'rowspan');
                  if (colSpan > 1 || rowSpan > 1) {
                     setSpanVal(cell, 'rowSpan', 1);
                     setSpanVal(cell, 'colSpan', 1);
                     // Insert cells right
                     for (i = 0; i < colSpan - 1; i++)
                        dom.insertAfter(cloneCell(cell), cell);
                     fillLeftDown(x, y, rowSpan - 1, colSpan);
                  }
               }
            });
         });
      };
      function merge(cell, cols, rows) {
         var startX, startY, endX, endY, x, y, startCell, endCell, cell, children, count;
         // Use specified cell and cols/rows
         if (cell) {
            pos = getPos(cell);
            startX = pos.x;
            startY = pos.y;
            endX = startX + (cols - 1);
            endY = startY + (rows - 1);
         } else {
            startPos = endPos = null;
            // Calculate start/end pos by checking for selected cells in grid works better with context menu
            each(grid, function(row, y) {
               each(row, function(cell, x) {
                  if (isCellSelected(cell)) {
                     if (!startPos) {
                        startPos = {x: x, y: y};
                     }
                     endPos = {x: x, y: y};
                  }
               });
            });
            // Use selection
            startX = startPos.x;
            startY = startPos.y;
            endX = endPos.x;
            endY = endPos.y;
         }
         // Find start/end cells
         startCell = getCell(startX, startY);
         endCell = getCell(endX, endY);
         // Check if the cells exists and if they are of the same part for example tbody = tbody
         if (startCell && endCell && startCell.part == endCell.part) {
            // Split and rebuild grid
            split();
            buildGrid();
            // Set row/col span to start cell
            startCell = getCell(startX, startY).elm;
            setSpanVal(startCell, 'colSpan', (endX - startX) + 1);
            setSpanVal(startCell, 'rowSpan', (endY - startY) + 1);
            // Remove other cells and add it's contents to the start cell
            for (y = startY; y <= endY; y++) {
               for (x = startX; x <= endX; x++) {
                  if (!grid[y] || !grid[y][x])
                     continue;
                  cell = grid[y][x].elm;
                  if (cell != startCell) {
                     // Move children to startCell
                     children = tinymce.grep(cell.childNodes);
                     each(children, function(node) {
                        startCell.appendChild(node);
                     });
                     // Remove bogus nodes if there is children in the target cell
                     if (children.length) {
                        children = tinymce.grep(startCell.childNodes);
                        count = 0;
                        each(children, function(node) {
                           if (node.nodeName == 'BR' && dom.getAttrib(node, 'data-mce-bogus') && count++ < children.length - 1)
                              startCell.removeChild(node);
                        });
                     }
                     // Remove cell
                     dom.remove(cell);
                  }
               }
            }
            // Remove empty rows etc and restore caret location
            cleanup();
         }
      };
      function insertRow(before) {
         var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell, rowSpan;
         // Find first/last row
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               if (isCellSelected(cell)) {
                  cell = cell.elm;
                  rowElm = cell.parentNode;
                  newRow = cloneNode(rowElm, false);
                  posY = y;
                  if (before)
                     return false;
               }
            });
            if (before)
               return !posY;
         });
         for (x = 0; x < grid[0].length; x++) {
            // Cell not found could be because of an invalid table structure
            if (!grid[posY][x])
               continue;
            cell = grid[posY][x].elm;
            if (cell != lastCell) {
               if (!before) {
                  rowSpan = getSpanVal(cell, 'rowspan');
                  if (rowSpan > 1) {
                     setSpanVal(cell, 'rowSpan', rowSpan + 1);
                     continue;
                  }
               } else {
                  // Check if cell above can be expanded
                  if (posY > 0 && grid[posY - 1][x]) {
                     otherCell = grid[posY - 1][x].elm;
                     rowSpan = getSpanVal(otherCell, 'rowSpan');
                     if (rowSpan > 1) {
                        setSpanVal(otherCell, 'rowSpan', rowSpan + 1);
                        continue;
                     }
                  }
               }
               // Insert new cell into new row
               newCell = cloneCell(cell);
               setSpanVal(newCell, 'colSpan', cell.colSpan);
               newRow.appendChild(newCell);
               lastCell = cell;
            }
         }
         if (newRow.hasChildNodes()) {
            if (!before)
               dom.insertAfter(newRow, rowElm);
            else
               rowElm.parentNode.insertBefore(newRow, rowElm);
         }
      };
      function insertCol(before) {
         var posX, lastCell;
         // Find first/last column
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               if (isCellSelected(cell)) {
                  posX = x;
                  if (before)
                     return false;
               }
            });
            if (before)
               return !posX;
         });
         each(grid, function(row, y) {
            var cell, rowSpan, colSpan;
            if (!row[posX])
               return;
            cell = row[posX].elm;
            if (cell != lastCell) {
               colSpan = getSpanVal(cell, 'colspan');
               rowSpan = getSpanVal(cell, 'rowspan');
               if (colSpan == 1) {
                  if (!before) {
                     dom.insertAfter(cloneCell(cell), cell);
                     fillLeftDown(posX, y, rowSpan - 1, colSpan);
                  } else {
                     cell.parentNode.insertBefore(cloneCell(cell), cell);
                     fillLeftDown(posX, y, rowSpan - 1, colSpan);
                  }
               } else
                  setSpanVal(cell, 'colSpan', cell.colSpan + 1);
               lastCell = cell;
            }
         });
      };
      function deleteCols() {
         var cols = [];
         // Get selected column indexes
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               if (isCellSelected(cell) && tinymce.inArray(cols, x) === -1) {
                  each(grid, function(row) {
                     var cell = row[x].elm, colSpan;
                     colSpan = getSpanVal(cell, 'colSpan');
                     if (colSpan > 1)
                        setSpanVal(cell, 'colSpan', colSpan - 1);
                     else
                        dom.remove(cell);
                  });
                  cols.push(x);
               }
            });
         });
         cleanup();
      };
      function deleteRows() {
         var rows;
         function deleteRow(tr) {
            var nextTr, pos, lastCell;
            nextTr = dom.getNext(tr, 'tr');
            // Move down row spanned cells
            each(tr.cells, function(cell) {
               var rowSpan = getSpanVal(cell, 'rowSpan');
               if (rowSpan > 1) {
                  setSpanVal(cell, 'rowSpan', rowSpan - 1);
                  pos = getPos(cell);
                  fillLeftDown(pos.x, pos.y, 1, 1);
               }
            });
            // Delete cells
            pos = getPos(tr.cells[0]);
            each(grid[pos.y], function(cell) {
               var rowSpan;
               cell = cell.elm;
               if (cell != lastCell) {
                  rowSpan = getSpanVal(cell, 'rowSpan');
                  if (rowSpan <= 1)
                     dom.remove(cell);
                  else
                     setSpanVal(cell, 'rowSpan', rowSpan - 1);
                  lastCell = cell;
               }
            });
         };
         // Get selected rows and move selection out of scope
         rows = getSelectedRows();
         // Delete all selected rows
         each(rows.reverse(), function(tr) {
            deleteRow(tr);
         });
         cleanup();
      };
      function cutRows() {
         var rows = getSelectedRows();
         dom.remove(rows);
         cleanup();
         return rows;
      };
      function copyRows() {
         var rows = getSelectedRows();
         each(rows, function(row, i) {
            rows[i] = cloneNode(row, true);
         });
         return rows;
      };
      function pasteRows(rows, before) {
         var selectedRows = getSelectedRows(),
            targetRow = selectedRows[before ? 0 : selectedRows.length - 1],
            targetCellCount = targetRow.cells.length;
         // Calc target cell count
         each(grid, function(row) {
            var match;
            targetCellCount = 0;
            each(row, function(cell, x) {
               if (cell.real)
                  targetCellCount += cell.colspan;
               if (cell.elm.parentNode == targetRow)
                  match = 1;
            });
            if (match)
               return false;
         });
         if (!before)
            rows.reverse();
         each(rows, function(row) {
            var cellCount = row.cells.length, cell;
            // Remove col/rowspans
            for (i = 0; i < cellCount; i++) {
               cell = row.cells[i];
               setSpanVal(cell, 'colSpan', 1);
               setSpanVal(cell, 'rowSpan', 1);
            }
            // Needs more cells
            for (i = cellCount; i < targetCellCount; i++)
               row.appendChild(cloneCell(row.cells[cellCount - 1]));
            // Needs less cells
            for (i = targetCellCount; i < cellCount; i++)
               dom.remove(row.cells[i]);
            // Add before/after
            if (before)
               targetRow.parentNode.insertBefore(row, targetRow);
            else
               dom.insertAfter(row, targetRow);
         });
         // Remove current selection
         dom.removeClass(dom.select('td.mceSelected,th.mceSelected'), 'mceSelected');
      };
      function getPos(target) {
         var pos;
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               if (cell.elm == target) {
                  pos = {x : x, y : y};
                  return false;
               }
            });
            return !pos;
         });
         return pos;
      };
      function setStartCell(cell) {
         startPos = getPos(cell);
      };
      function findEndPos() {
         var pos, maxX, maxY;
         maxX = maxY = 0;
         each(grid, function(row, y) {
            each(row, function(cell, x) {
               var colSpan, rowSpan;
               if (isCellSelected(cell)) {
                  cell = grid[y][x];
                  if (x > maxX)
                     maxX = x;
                  if (y > maxY)
                     maxY = y;
                  if (cell.real) {
                     colSpan = cell.colspan - 1;
                     rowSpan = cell.rowspan - 1;
                     if (colSpan) {
                        if (x + colSpan > maxX)
                           maxX = x + colSpan;
                     }
                     if (rowSpan) {
                        if (y + rowSpan > maxY)
                           maxY = y + rowSpan;
                     }
                  }
               }
            });
         });
         return {x : maxX, y : maxY};
      };
      function setEndCell(cell) {
         var startX, startY, endX, endY, maxX, maxY, colSpan, rowSpan;
         endPos = getPos(cell);
         if (startPos && endPos) {
            // Get start/end positions
            startX = Math.min(startPos.x, endPos.x);
            startY = Math.min(startPos.y, endPos.y);
            endX = Math.max(startPos.x, endPos.x);
            endY = Math.max(startPos.y, endPos.y);
            // Expand end positon to include spans
            maxX = endX;
            maxY = endY;
            // Expand startX
            for (y = startY; y <= maxY; y++) {
               cell = grid[y][startX];
               if (!cell.real) {
                  if (startX - (cell.colspan - 1) < startX)
                     startX -= cell.colspan - 1;
               }
            }
            // Expand startY
            for (x = startX; x <= maxX; x++) {
               cell = grid[startY][x];
               if (!cell.real) {
                  if (startY - (cell.rowspan - 1) < startY)
                     startY -= cell.rowspan - 1;
               }
            }
            // Find max X, Y
            for (y = startY; y <= endY; y++) {
               for (x = startX; x <= endX; x++) {
                  cell = grid[y][x];
                  if (cell.real) {
                     colSpan = cell.colspan - 1;
                     rowSpan = cell.rowspan - 1;
                     if (colSpan) {
                        if (x + colSpan > maxX)
                           maxX = x + colSpan;
                     }
                     if (rowSpan) {
                        if (y + rowSpan > maxY)
                           maxY = y + rowSpan;
                     }
                  }
               }
            }
            // Remove current selection
            dom.removeClass(dom.select('td.mceSelected,th.mceSelected'), 'mceSelected');
            // Add new selection
            for (y = startY; y <= maxY; y++) {
               for (x = startX; x <= maxX; x++) {
                  if (grid[y][x])
                     dom.addClass(grid[y][x].elm, 'mceSelected');
               }
            }
         }
      };
      // Expose to public
      tinymce.extend(this, {
         deleteTable : deleteTable,
         split : split,
         merge : merge,
         insertRow : insertRow,
         insertCol : insertCol,
         deleteCols : deleteCols,
         deleteRows : deleteRows,
         cutRows : cutRows,
         copyRows : copyRows,
         pasteRows : pasteRows,
         getPos : getPos,
         setStartCell : setStartCell,
         setEndCell : setEndCell
      });
   };
   tinymce.create('tinymce.plugins.TablePlugin', {
      init : function(ed, url) {
         var winMan, clipboardRows, hasCellSelection = true; // Might be selected cells on reload
         function createTableGrid(node) {
            var selection = ed.selection, tblElm = ed.dom.getParent(node || selection.getNode(), 'table');
            if (tblElm)
               return new TableGrid(tblElm, ed.dom, selection);
         };
         function cleanup() {
            // Restore selection possibilities
            ed.getBody().style.webkitUserSelect = '';
            if (hasCellSelection) {
               ed.dom.removeClass(ed.dom.select('td.mceSelected,th.mceSelected'), 'mceSelected');
               hasCellSelection = false;
            }
         };
         // Register buttons
         each([
            ['table', 'table.desc', 'mceInsertTable', true],
            ['delete_table', 'table.del', 'mceTableDelete'],
            ['delete_col', 'table.delete_col_desc', 'mceTableDeleteCol'],
            ['delete_row', 'table.delete_row_desc', 'mceTableDeleteRow'],
            ['col_after', 'table.col_after_desc', 'mceTableInsertColAfter'],
            ['col_before', 'table.col_before_desc', 'mceTableInsertColBefore'],
            ['row_after', 'table.row_after_desc', 'mceTableInsertRowAfter'],
            ['row_before', 'table.row_before_desc', 'mceTableInsertRowBefore'],
            ['row_props', 'table.row_desc', 'mceTableRowProps', true],
            ['cell_props', 'table.cell_desc', 'mceTableCellProps', true],
            ['split_cells', 'table.split_cells_desc', 'mceTableSplitCells', true],
            ['merge_cells', 'table.merge_cells_desc', 'mceTableMergeCells', true]
         ], function(c) {
            ed.addButton(c[0], {title : c[1], cmd : c[2], ui : c[3]});
         });
         // Select whole table is a table border is clicked
         if (!tinymce.isIE) {
            ed.onClick.add(function(ed, e) {
               e = e.target;
               if (e.nodeName === 'TABLE') {
                  ed.selection.select(e);
                  ed.nodeChanged();
               }
            });
         }
         ed.onPreProcess.add(function(ed, args) {
            var nodes, i, node, dom = ed.dom, value;
            nodes = dom.select('table', args.node);
            i = nodes.length;
            while (i--) {
               node = nodes[i];
               dom.setAttrib(node, 'data-mce-style', '');
               if ((value = dom.getAttrib(node, 'width'))) {
                  dom.setStyle(node, 'width', value);
                  dom.setAttrib(node, 'width', '');
               }
               if ((value = dom.getAttrib(node, 'height'))) {
                  dom.setStyle(node, 'height', value);
                  dom.setAttrib(node, 'height', '');
               }
            }
         });
         // Handle node change updates
         ed.onNodeChange.add(function(ed, cm, n) {
            var p;
            n = ed.selection.getStart();
            p = ed.dom.getParent(n, 'td,th,caption');
            cm.setActive('table', n.nodeName === 'TABLE' || !!p);
            // Disable table tools if we are in caption
            if (p && p.nodeName === 'CAPTION')
               p = 0;
            cm.setDisabled('delete_table', !p);
            cm.setDisabled('delete_col', !p);
            cm.setDisabled('delete_table', !p);
            cm.setDisabled('delete_row', !p);
            cm.setDisabled('col_after', !p);
            cm.setDisabled('col_before', !p);
            cm.setDisabled('row_after', !p);
            cm.setDisabled('row_before', !p);
            cm.setDisabled('row_props', !p);
            cm.setDisabled('cell_props', !p);
            cm.setDisabled('split_cells', !p);
            cm.setDisabled('merge_cells', !p);
         });
         ed.onInit.add(function(ed) {
            var startTable, startCell, dom = ed.dom, tableGrid;
            winMan = ed.windowManager;
            // Add cell selection logic
            ed.onMouseDown.add(function(ed, e) {
               if (e.button != 2) {
                  cleanup();
                  startCell = dom.getParent(e.target, 'td,th');
                  startTable = dom.getParent(startCell, 'table');
               }
            });
            dom.bind(ed.getDoc(), 'mouseover', function(e) {
               var sel, table, target = e.target;
               if (startCell && (tableGrid || target != startCell) && (target.nodeName == 'TD' || target.nodeName == 'TH')) {
                  table = dom.getParent(target, 'table');
                  if (table == startTable) {
                     if (!tableGrid) {
                        tableGrid = createTableGrid(table);
                        tableGrid.setStartCell(startCell);
                        ed.getBody().style.webkitUserSelect = 'none';
                     }
                     tableGrid.setEndCell(target);
                     hasCellSelection = true;
                  }
                  // Remove current selection
                  sel = ed.selection.getSel();
                  try {
                     if (sel.removeAllRanges)
                        sel.removeAllRanges();
                     else
                        sel.empty();
                  } catch (ex) {
                     // IE9 might throw errors here
                  }
                  e.preventDefault();
               }
            });
            ed.onMouseUp.add(function(ed, e) {
               var rng, sel = ed.selection, selectedCells, nativeSel = sel.getSel(), walker, node, lastNode, endNode;
               // Move selection to startCell
               if (startCell) {
                  if (tableGrid)
                     ed.getBody().style.webkitUserSelect = '';
                  function setPoint(node, start) {
                     var walker = new tinymce.dom.TreeWalker(node, node);
                     do {
                        // Text node
                        if (node.nodeType == 3 && tinymce.trim(node.nodeValue).length != 0) {
                           if (start)
                              rng.setStart(node, 0);
                           else
                              rng.setEnd(node, node.nodeValue.length);
                           return;
                        }
                        // BR element
                        if (node.nodeName == 'BR') {
                           if (start)
                              rng.setStartBefore(node);
                           else
                              rng.setEndBefore(node);
                           return;
                        }
                     } while (node = (start ? walker.next() : walker.prev()));
                  }
                  // Try to expand text selection as much as we can only Gecko supports cell selection
                  selectedCells = dom.select('td.mceSelected,th.mceSelected');
                  if (selectedCells.length > 0) {
                     rng = dom.createRng();
                     node = selectedCells[0];
                     endNode = selectedCells[selectedCells.length - 1];
                     rng.setStartBefore(node);
                     rng.setEndAfter(node);
                     setPoint(node, 1);
                     walker = new tinymce.dom.TreeWalker(node, dom.getParent(selectedCells[0], 'table'));
                     do {
                        if (node.nodeName == 'TD' || node.nodeName == 'TH') {
                           if (!dom.hasClass(node, 'mceSelected'))
                              break;
                           lastNode = node;
                        }
                     } while (node = walker.next());
                     setPoint(lastNode);
                     sel.setRng(rng);
                  }
                  ed.nodeChanged();
                  startCell = tableGrid = startTable = null;
               }
            });
            ed.onKeyUp.add(function(ed, e) {
               cleanup();
            });
            ed.onKeyDown.add(function (ed, e) {
               fixTableCellSelection(ed);
            });
            ed.onMouseDown.add(function (ed, e) {
               if (e.button != 2) {
                  fixTableCellSelection(ed);
               }
            });
            function tableCellSelected(ed, rng, n, currentCell) {
               // The decision of when a table cell is selected is somewhat involved.  The fact that this code is
               // required is actually a pointer to the root cause of this bug. A cell is selected when the start
               // and end offsets are 0, the start container is a text, and the selection node is either a TR (most cases)
               // or the parent of the table (in the case of the selection containing the last cell of a table).
               var TEXT_NODE = 3, table = ed.dom.getParent(rng.startContainer, 'TABLE'),
               tableParent, allOfCellSelected, tableCellSelection;
               if (table)
               tableParent = table.parentNode;
               allOfCellSelected =rng.startContainer.nodeType == TEXT_NODE &&
                  rng.startOffset == 0 &&
                  rng.endOffset == 0 &&
                  currentCell &&
                  (n.nodeName=="TR" || n==tableParent);
               tableCellSelection = (n.nodeName=="TD"||n.nodeName=="TH")&& !currentCell;
               return  allOfCellSelected || tableCellSelection;
               // return false;
            }
            // this nasty hack is here to work around some WebKit selection bugs.
            function fixTableCellSelection(ed) {
               if (!tinymce.isWebKit)
                  return;
               var rng = ed.selection.getRng();
               var n = ed.selection.getNode();
               var currentCell = ed.dom.getParent(rng.startContainer, 'TD,TH');
               if (!tableCellSelected(ed, rng, n, currentCell))
                  return;
                  if (!currentCell) {
                     currentCell=n;
                  }
               // Get the very last node inside the table cell
               var end = currentCell.lastChild;
               while (end.lastChild)
                  end = end.lastChild;
               // Select the entire table cell. Nothing outside of the table cell should be selected.
               rng.setEnd(end, end.nodeValue.length);
               ed.selection.setRng(rng);
            }
            ed.plugins.table.fixTableCellSelection=fixTableCellSelection;
            // Add context menu
            if (ed && ed.plugins.contextmenu) {
               ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
                  var sm, se = ed.selection, el = se.getNode() || ed.getBody();
                  if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th') || ed.dom.select('td.mceSelected,th.mceSelected').length) {
                     m.removeAll();
                     if (el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) {
                        m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
                        m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
                        m.addSeparator();
                     }
                     if (el.nodeName == 'IMG' && el.className.indexOf('mceItem') == -1) {
                        m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
                        m.addSeparator();
                     }
                     m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', value : {action : 'insert'}});
                     m.add({title : 'table.props_desc', icon : 'table_props', cmd : 'mceInsertTable'});
                     m.add({title : 'table.del', icon : 'delete_table', cmd : 'mceTableDelete'});
                     m.addSeparator();
                     // Cell menu
                     sm = m.addMenu({title : 'table.cell'});
                     sm.add({title : 'table.cell_desc', icon : 'cell_props', cmd : 'mceTableCellProps'});
                     sm.add({title : 'table.split_cells_desc', icon : 'split_cells', cmd : 'mceTableSplitCells'});
                     sm.add({title : 'table.merge_cells_desc', icon : 'merge_cells', cmd : 'mceTableMergeCells'});
                     // Row menu
                     sm = m.addMenu({title : 'table.row'});
                     sm.add({title : 'table.row_desc', icon : 'row_props', cmd : 'mceTableRowProps'});
                     sm.add({title : 'table.row_before_desc', icon : 'row_before', cmd : 'mceTableInsertRowBefore'});
                     sm.add({title : 'table.row_after_desc', icon : 'row_after', cmd : 'mceTableInsertRowAfter'});
                     sm.add({title : 'table.delete_row_desc', icon : 'delete_row', cmd : 'mceTableDeleteRow'});
                     sm.addSeparator();
                     sm.add({title : 'table.cut_row_desc', icon : 'cut', cmd : 'mceTableCutRow'});
                     sm.add({title : 'table.copy_row_desc', icon : 'copy', cmd : 'mceTableCopyRow'});
                     sm.add({title : 'table.paste_row_before_desc', icon : 'paste', cmd : 'mceTablePasteRowBefore'}).setDisabled(!clipboardRows);
                     sm.add({title : 'table.paste_row_after_desc', icon : 'paste', cmd : 'mceTablePasteRowAfter'}).setDisabled(!clipboardRows);
                     // Column menu
                     sm = m.addMenu({title : 'table.col'});
                     sm.add({title : 'table.col_before_desc', icon : 'col_before', cmd : 'mceTableInsertColBefore'});
                     sm.add({title : 'table.col_after_desc', icon : 'col_after', cmd : 'mceTableInsertColAfter'});
                     sm.add({title : 'table.delete_col_desc', icon : 'delete_col', cmd : 'mceTableDeleteCol'});
                  } else
                     m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable'});
               });
            }
            // Fix to allow navigating up and down in a table in WebKit browsers.
            if (tinymce.isWebKit) {
               function moveSelection(ed, e) {
                  var VK = tinymce.VK;
                  var key = e.keyCode;
                  function handle(upBool, sourceNode, event) {
                     var siblingDirection = upBool ? 'previousSibling' : 'nextSibling';
                     var currentRow = ed.dom.getParent(sourceNode, 'tr');
                     var siblingRow = currentRow[siblingDirection];
                     if (siblingRow) {
                        moveCursorToRow(ed, sourceNode, siblingRow, upBool);
                        tinymce.dom.Event.cancel(event);
                        return true;
                     } else {
                        var tableNode = ed.dom.getParent(currentRow, 'table');
                        var middleNode = currentRow.parentNode;
                        var parentNodeName = middleNode.nodeName.toLowerCase();
                        if (parentNodeName === 'tbody' || parentNodeName === (upBool ? 'tfoot' : 'thead')) {
                           var targetParent = getTargetParent(upBool, tableNode, middleNode, 'tbody');
                           if (targetParent !== null) {
                              return moveToRowInTarget(upBool, targetParent, sourceNode, event);
                           }
                        }
                        return escapeTable(upBool, currentRow, siblingDirection, tableNode, event);
                     }
                  }
                  function getTargetParent(upBool, topNode, secondNode, nodeName) {
                     var tbodies = ed.dom.select('>' + nodeName, topNode);
                     var position = tbodies.indexOf(secondNode);
                     if (upBool && position === 0 || !upBool && position === tbodies.length - 1) {
                        return getFirstHeadOrFoot(upBool, topNode);
                     } else if (position === -1) {
                        var topOrBottom = secondNode.tagName.toLowerCase() === 'thead' ? 0 : tbodies.length - 1;
                        return tbodies[topOrBottom];
                     } else {
                        return tbodies[position + (upBool ? -1 : 1)];
                     }
                  }
                  function getFirstHeadOrFoot(upBool, parent) {
                     var tagName = upBool ? 'thead' : 'tfoot';
                     var headOrFoot = ed.dom.select('>' + tagName, parent);
                     return headOrFoot.length !== 0 ? headOrFoot[0] : null;
                  }
                  function moveToRowInTarget(upBool, targetParent, sourceNode, event) {
                     var targetRow = getChildForDirection(targetParent, upBool);
                     targetRow && moveCursorToRow(ed, sourceNode, targetRow, upBool);
                     tinymce.dom.Event.cancel(event);
                     return true;
                  }
                  function escapeTable(upBool, currentRow, siblingDirection, table, event) {
                     var tableSibling = table[siblingDirection];
                     if (tableSibling) {
                        moveCursorToStartOfElement(tableSibling);
                        return true;
                     } else {
                        var parentCell = ed.dom.getParent(table, 'td,th');
                        if (parentCell) {
                           return handle(upBool, parentCell, event);
                        } else {
                           var backUpSibling = getChildForDirection(currentRow, !upBool);
                           moveCursorToStartOfElement(backUpSibling);
                           return tinymce.dom.Event.cancel(event);
                        }
                     }
                  }
                  function getChildForDirection(parent, up) {
                     var child =  parent && parent[up ? 'lastChild' : 'firstChild'];
                     // BR is not a valid table child to return in this case we return the table cell
                     return child && child.nodeName === 'BR' ? ed.dom.getParent(child, 'td,th') : child;
                  }
                  function moveCursorToStartOfElement(n) {
                     ed.selection.setCursorLocation(n, 0);
                  }
                  function isVerticalMovement() {
                     return key == VK.UP || key == VK.DOWN;
                  }
                  function isInTable(ed) {
                     var node = ed.selection.getNode();
                     var currentRow = ed.dom.getParent(node, 'tr');
                     return currentRow !== null;
                  }
                  function columnIndex(column) {
                     var colIndex = 0;
                     var c = column;
                     while (c.previousSibling) {
                        c = c.previousSibling;
                        colIndex = colIndex + getSpanVal(c, "colspan");
                     }
                     return colIndex;
                  }
                  function findColumn(rowElement, columnIndex) {
                     var c = 0;
                     var r = 0;
                     each(rowElement.children, function(cell, i) {
                        c = c + getSpanVal(cell, "colspan");
                        r = i;
                        if (c > columnIndex)
                           return false;
                     });
                     return r;
                  }
                  function moveCursorToRow(ed, node, row, upBool) {
                     var srcColumnIndex = columnIndex(ed.dom.getParent(node, 'td,th'));
                     var tgtColumnIndex = findColumn(row, srcColumnIndex);
                     var tgtNode = row.childNodes[tgtColumnIndex];
                     var rowCellTarget = getChildForDirection(tgtNode, upBool);
                     moveCursorToStartOfElement(rowCellTarget || tgtNode);
                  }
                  function shouldFixCaret(preBrowserNode) {
                     var newNode = ed.selection.getNode();
                     var newParent = ed.dom.getParent(newNode, 'td,th');
                     var oldParent = ed.dom.getParent(preBrowserNode, 'td,th');
                     return newParent && newParent !== oldParent && checkSameParentTable(newParent, oldParent)
                  }
                  function checkSameParentTable(nodeOne, NodeTwo) {
                     return ed.dom.getParent(nodeOne, 'TABLE') === ed.dom.getParent(NodeTwo, 'TABLE');
                  }
                  if (isVerticalMovement() && isInTable(ed)) {
                     var preBrowserNode = ed.selection.getNode();
                     setTimeout(function() {
                        if (shouldFixCaret(preBrowserNode)) {
                           handle(!e.shiftKey && key === VK.UP, preBrowserNode, e);
                        }
                     }, 0);
                  }
               }
               ed.onKeyDown.add(moveSelection);
            }
            // Fixes an issue on Gecko where it's impossible to place the caret behind a table
            // This fix will force a paragraph element after the table but only when the forced_root_block setting is enabled
            function fixTableCaretPos() {
               var last;
               // Skip empty text nodes form the end
               for (last = ed.getBody().lastChild; last && last.nodeType == 3 && !last.nodeValue.length; last = last.previousSibling) ;
               if (last && last.nodeName == 'TABLE') {
                  if (ed.settings.forced_root_block)
                     ed.dom.add(ed.getBody(), ed.settings.forced_root_block, null, tinymce.isIE ? '&nbsp;' : '<br data-mce-bogus="1" />');
                  else
                     ed.dom.add(ed.getBody(), 'br', {'data-mce-bogus': '1'});
               }
            };
            // Fixes an bug where it's impossible to place the caret before a table in Gecko
            // this fix solves it by detecting when the caret is at the beginning of such a table
            // and then manually moves the caret infront of the table
            if (tinymce.isGecko) {
               ed.onKeyDown.add(function(ed, e) {
                  var rng, table, dom = ed.dom;
                  // On gecko it's not possible to place the caret before a table
                  if (e.keyCode == 37 || e.keyCode == 38) {
                     rng = ed.selection.getRng();
                     table = dom.getParent(rng.startContainer, 'table');
                     if (table && ed.getBody().firstChild == table) {
                        if (isAtStart(rng, table)) {
                           rng = dom.createRng();
                           rng.setStartBefore(table);
                           rng.setEndBefore(table);
                           ed.selection.setRng(rng);
                           e.preventDefault();
                        }
                     }
                  }
               });
            }
            ed.onKeyUp.add(fixTableCaretPos);
            ed.onSetContent.add(fixTableCaretPos);
            ed.onVisualAid.add(fixTableCaretPos);
            ed.onPreProcess.add(function(ed, o) {
               var last = o.node.lastChild;
               if (last && (last.nodeName == "BR" || (last.childNodes.length == 1 && (last.firstChild.nodeName == 'BR' || last.firstChild.nodeValue == '\u00a0'))) && last.previousSibling && last.previousSibling.nodeName == "TABLE") {
                  ed.dom.remove(last);
               }
            });
            /**
             * Fixes bug in Gecko where shift-enter in table cell does not place caret on new line
             */
            if (tinymce.isGecko) {
               ed.onKeyDown.add(function(ed, e) {
                  if (e.keyCode === tinymce.VK.ENTER && e.shiftKey) {
                     var node = ed.selection.getRng().startContainer;
                     var tableCell = dom.getParent(node, 'td,th');
                     if (tableCell) {
                        var zeroSizedNbsp = ed.getDoc().createTextNode("\uFEFF");
                        dom.insertAfter(zeroSizedNbsp, node);
                     }
                  }
               });
            }
            fixTableCaretPos();
            ed.startContent = ed.getContent({format : 'raw'});
         });
         // Register action commands
         each({
            mceTableSplitCells : function(grid) {
               grid.split();
            },
            mceTableMergeCells : function(grid) {
               var rowSpan, colSpan, cell;
               cell = ed.dom.getParent(ed.selection.getNode(), 'th,td');
               if (cell) {
                  rowSpan = cell.rowSpan;
                  colSpan = cell.colSpan;
               }
               if (!ed.dom.select('td.mceSelected,th.mceSelected').length) {
                  winMan.open({
                     url : url + '/merge_cells.htm',
                     width : 240 + parseInt(ed.getLang('table.merge_cells_delta_width', 0)),
                     height : 110 + parseInt(ed.getLang('table.merge_cells_delta_height', 0)),
                     inline : 1
                  }, {
                     rows : rowSpan,
                     cols : colSpan,
                     onaction : function(data) {
                        grid.merge(cell, data.cols, data.rows);
                     },
                     plugin_url : url
                  });
               } else
                  grid.merge();
            },
            mceTableInsertRowBefore : function(grid) {
               grid.insertRow(true);
            },
            mceTableInsertRowAfter : function(grid) {
               grid.insertRow();
            },
            mceTableInsertColBefore : function(grid) {
               grid.insertCol(true);
            },
            mceTableInsertColAfter : function(grid) {
               grid.insertCol();
            },
            mceTableDeleteCol : function(grid) {
               grid.deleteCols();
            },
            mceTableDeleteRow : function(grid) {
               grid.deleteRows();
            },
            mceTableCutRow : function(grid) {
               clipboardRows = grid.cutRows();
            },
            mceTableCopyRow : function(grid) {
               clipboardRows = grid.copyRows();
            },
            mceTablePasteRowBefore : function(grid) {
               grid.pasteRows(clipboardRows, true);
            },
            mceTablePasteRowAfter : function(grid) {
               grid.pasteRows(clipboardRows);
            },
            mceTableDelete : function(grid) {
               grid.deleteTable();
            }
         }, function(func, name) {
            ed.addCommand(name, function() {
               var grid = createTableGrid();
               if (grid) {
                  func(grid);
                  ed.execCommand('mceRepaint');
                  cleanup();
               }
            });
         });
         // Register dialog commands
         each({
            mceInsertTable : function(val) {
               winMan.open({
                  url : url + '/table.htm',
                  width : 400 + parseInt(ed.getLang('table.table_delta_width', 0)),
                  height : 320 + parseInt(ed.getLang('table.table_delta_height', 0)),
                  inline : 1
               }, {
                  plugin_url : url,
                  action : val ? val.action : 0
               });
            },
            mceTableRowProps : function() {
               winMan.open({
                  url : url + '/row.htm',
                  width : 400 + parseInt(ed.getLang('table.rowprops_delta_width', 0)),
                  height : 295 + parseInt(ed.getLang('table.rowprops_delta_height', 0)),
                  inline : 1
               }, {
                  plugin_url : url
               });
            },
            mceTableCellProps : function() {
               winMan.open({
                  url : url + '/cell.htm',
                  width : 400 + parseInt(ed.getLang('table.cellprops_delta_width', 0)),
                  height : 295 + parseInt(ed.getLang('table.cellprops_delta_height', 0)),
                  inline : 1
               }, {
                  plugin_url : url
               });
            }
         }, function(func, name) {
            ed.addCommand(name, function(ui, val) {
               func(val);
            });
         });
      }
   });
   // Register plugin
   tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
})(tinymce);