alecpl
2009-12-15 58fb6502e3550e59afa8799d36dfce61a18f8b1b
program/js/tiny_mce/plugins/table/editor_plugin_src.js
@@ -1,879 +1,1045 @@
/**
 * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $
 * $Id: editor_plugin_src.js 1209 2009-08-20 12:35:10Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
 */
/* Import plugin specific language pack */
tinyMCE.importPluginLanguagePack('table');
(function() {
   var each = tinymce.each;
var TinyMCE_TablePlugin = {
   getInfo : function() {
      return {
         longname : 'Tables',
         author : 'Moxiecode Systems AB',
         authorurl : 'http://tinymce.moxiecode.com',
         infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_table.html',
         version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
      };
   },
   // 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;
   initInstance : function(inst) {
      if (tinyMCE.isGecko) {
         var doc = inst.getDoc();
         tinyMCE.addEvent(doc, "mouseup", TinyMCE_TablePlugin._mouseDownHandler);
      }
      rng2.setStartBefore(par);
      rng2.setEnd(rng.endContainer, rng.endOffset);
      inst.tableRowClipboard = null;
   },
      elm = doc.createElement('body');
      elm.appendChild(rng2.cloneContents());
   /**
    * Returns the HTML contents of the table control.
    */
   getControlHTML : function(control_name) {
      var controls = new Array(
         ['table', 'table.gif', 'lang_table_desc', 'mceInsertTable', true],
         ['delete_table', 'table_delete.gif', 'lang_table_del', 'mceTableDelete'],
         ['delete_col', 'table_delete_col.gif', 'lang_table_delete_col_desc', 'mceTableDeleteCol'],
         ['delete_row', 'table_delete_row.gif', 'lang_table_delete_row_desc', 'mceTableDeleteRow'],
         ['col_after', 'table_insert_col_after.gif', 'lang_table_col_after_desc', 'mceTableInsertColAfter'],
         ['col_before', 'table_insert_col_before.gif', 'lang_table_col_before_desc', 'mceTableInsertColBefore'],
         ['row_after', 'table_insert_row_after.gif', 'lang_table_row_after_desc', 'mceTableInsertRowAfter'],
         ['row_before', 'table_insert_row_before.gif', 'lang_table_row_before_desc', 'mceTableInsertRowBefore'],
         ['row_props', 'table_row_props.gif', 'lang_table_row_desc', 'mceTableRowProps', true],
         ['cell_props', 'table_cell_props.gif', 'lang_table_cell_desc', 'mceTableCellProps', true],
         ['split_cells', 'table_split_cells.gif', 'lang_table_split_cells_desc', 'mceTableSplitCells', true],
         ['merge_cells', 'table_merge_cells.gif', 'lang_table_merge_cells_desc', 'mceTableMergeCells', true]);
      // 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;
   };
      // Render table control
      for (var i=0; i<controls.length; i++) {
         var but = controls[i];
         var cmd = 'tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + but[3] + '\', ' + (but.length > 4 ? but[4] : false) + (but.length > 5 ? ', \'' + but[5] + '\'' : '') + ');return false;';
   tinymce.create('tinymce.plugins.TablePlugin', {
      init : function(ed, url) {
         var t = this;
         if (but[0] == control_name)
            return tinyMCE.getButtonHTML(control_name, but[2], '{$pluginurl}/images/'+ but[1], but[3], (but.length > 4 ? but[4] : false));
      }
         t.editor = ed;
         t.url = url;
      // Special tablecontrols
      if (control_name == "tablecontrols") {
         var html = "";
         // 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]});
         });
         html += tinyMCE.getControlHTML("table");
         html += tinyMCE.getControlHTML("separator");
         html += tinyMCE.getControlHTML("row_props");
         html += tinyMCE.getControlHTML("cell_props");
         html += tinyMCE.getControlHTML("separator");
         html += tinyMCE.getControlHTML("row_before");
         html += tinyMCE.getControlHTML("row_after");
         html += tinyMCE.getControlHTML("delete_row");
         html += tinyMCE.getControlHTML("separator");
         html += tinyMCE.getControlHTML("col_before");
         html += tinyMCE.getControlHTML("col_after");
         html += tinyMCE.getControlHTML("delete_col");
         html += tinyMCE.getControlHTML("separator");
         html += tinyMCE.getControlHTML("split_cells");
         html += tinyMCE.getControlHTML("merge_cells");
         if (ed.getParam('inline_styles')) {
            // Force move of attribs to styles in strict mode
            ed.onPreProcess.add(function(ed, o) {
               var dom = ed.dom;
         return html;
      }
               each(dom.select('table', o.node), function(n) {
                  var v;
      return "";
   },
                  if (v = dom.getAttrib(n, 'width')) {
                     dom.setStyle(n, 'width', v);
                     dom.setAttrib(n, 'width');
                  }
   /**
    * Executes the table commands.
    */
   execCommand : function(editor_id, element, command, user_interface, value) {
      // Is table command
      switch (command) {
         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":
            var inst = tinyMCE.getInstanceById(editor_id);
                  if (v = dom.getAttrib(n, 'height')) {
                     dom.setStyle(n, 'height', v);
                     dom.setAttrib(n, 'height');
                  }
               });
            });
         }
            inst.execCommand('mceBeginUndoLevel');
            TinyMCE_TablePlugin._doExecCommand(editor_id, element, command, user_interface, value);
            inst.execCommand('mceEndUndoLevel');
         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;
            return true;
      }
                  if (last && last.nodeName == 'TABLE')
                     ed.dom.add(ed.getBody(), 'p', null, '<br mce_bogus="1" />');
               };
      // Pass to next handler in chain
      return false;
   },
               // 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;
   handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
      var colspan = "1", rowspan = "1", tdElm;
                     // 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');
      var inst = tinyMCE.getInstanceById(editor_id);
                        if (table && ed.getBody().firstChild == table) {
                           if (isAtStart(rng, table)) {
                              rng = dom.createRng();
      // Reset table controls
      tinyMCE.switchClass(editor_id + '_table', 'mceButtonNormal');
      tinyMCE.switchClass(editor_id + '_delete_table', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_row_props', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_cell_props', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_row_before', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_row_after', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_delete_row', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_col_before', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_col_after', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_delete_col', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_split_cells', 'mceButtonDisabled');
      tinyMCE.switchClass(editor_id + '_merge_cells', 'mceButtonDisabled');
                              rng.setStartBefore(table);
                              rng.setEndBefore(table);
      // Within a td element
      if (tdElm = tinyMCE.getParentElement(node, "td,th")) {
         tinyMCE.switchClass(editor_id + '_cell_props', 'mceButtonSelected');
         tinyMCE.switchClass(editor_id + '_delete_table', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_row_before', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_row_after', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_delete_row', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_col_before', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_col_after', 'mceButtonNormal');
         tinyMCE.switchClass(editor_id + '_delete_col', 'mceButtonNormal');
                              ed.selection.setRng(rng);
         colspan = tinyMCE.getAttrib(tdElm, "colspan");
         rowspan = tinyMCE.getAttrib(tdElm, "rowspan");
                              e.preventDefault();
                           }
                        }
                     }
                  });
               }
         colspan = colspan == "" ? "1" : colspan;
         rowspan = rowspan == "" ? "1" : rowspan;
               ed.onKeyUp.add(fixTableCaretPos);
               ed.onSetContent.add(fixTableCaretPos);
               ed.onVisualAid.add(fixTableCaretPos);
         if (colspan != "1" || rowspan != "1")
            tinyMCE.switchClass(editor_id + '_split_cells', 'mceButtonNormal');
      }
               ed.onPreProcess.add(function(ed, o) {
                  var last = o.node.lastChild;
      // Within a tr element
      if (tinyMCE.getParentElement(node, "tr"))
         tinyMCE.switchClass(editor_id + '_row_props', 'mceButtonSelected');
                  if (last && last.childNodes.length == 1 && last.firstChild.nodeName == 'BR')
                     ed.dom.remove(last);
               });
      // Within table
      if (tinyMCE.getParentElement(node, "table")) {
         tinyMCE.switchClass(editor_id + '_table', 'mceButtonSelected');
         tinyMCE.switchClass(editor_id + '_merge_cells', 'mceButtonNormal');
      }
   },
               fixTableCaretPos();
            }
   // Private plugin internal methods
            if (ed && ed.plugins.contextmenu) {
               ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
                  var sm, se = ed.selection, el = se.getNode() || ed.getBody();
   _mouseDownHandler : function(e) {
      var elm = tinyMCE.isMSIE ? event.srcElement : e.target;
      var focusElm = tinyMCE.selectedInstance.getFocusElement();
                  if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th')) {
                     m.removeAll();
      // If press on special Mozilla create TD/TR thingie
      if (elm.nodeName == "BODY" && (focusElm.nodeName == "TD" || focusElm.nodeName == "TH" || (focusElm.parentNode && focusElm.parentNode.nodeName == "TD") ||(focusElm.parentNode && focusElm.parentNode.nodeName == "TH") )) {
         window.setTimeout(function() {
            var tableElm = tinyMCE.getParentElement(focusElm, "table");
            tinyMCE.handleVisualAid(tableElm, true, tinyMCE.settings['visual'], tinyMCE.selectedInstance);
         }, 10);
      }
   },
                     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();
                     }
   /**
    * Executes the table commands.
    */
   _doExecCommand : function(editor_id, element, command, user_interface, value) {
      var inst = tinyMCE.getInstanceById(editor_id);
      var focusElm = inst.getFocusElement();
      var trElm = tinyMCE.getParentElement(focusElm, "tr");
      var tdElm = tinyMCE.getParentElement(focusElm, "td,th");
      var tableElm = tinyMCE.getParentElement(focusElm, "table");
      var doc = inst.contentWindow.document;
      var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
                     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();
                     }
      // Get first TD if no TD found
      if (trElm && tdElm == null)
         tdElm = trElm.cells[0];
                     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();
      // ------- Inner functions ---------
      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;
                     // 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});
            // Found value
            if (ar[i] == v)
                     // 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;
      }
      },
      function makeTD() {
         var newTD = doc.createElement("td");
         newTD.innerHTML = "&nbsp;";
      }
      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
         };
      },
      function getColRowSpan(td) {
         var colspan = tinyMCE.getAttrib(td, "colspan");
         var rowspan = tinyMCE.getAttrib(td, "rowspan");
      // Private plugin internal methods
         colspan = colspan == "" ? 1 : parseInt(colspan);
         rowspan = rowspan == "" ? 1 : parseInt(rowspan);
      /**
       * 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") : "";
         return {colspan : colspan, rowspan : rowspan};
      }
         // Get first TD if no TD found
         if (trElm && tdElm == null)
            tdElm = trElm.cells[0];
      function getCellPos(grid, td) {
         var x, y;
         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;
         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};
               // Found value
               if (ar[i] == v)
                  return true;
            }
            return false;
         }
         return null;
      }
         function select(dx, dy) {
            var td;
      function getCell(grid, row, col) {
         if (grid[row] && grid[row][col])
            return grid[row][col];
            grid = getTableGrid(tableElm);
            dx = dx || 0;
            dy = dy || 0;
            dx = Math.max(cpos.cellindex + dx, 0);
            dy = Math.max(cpos.rowindex + dy, 0);
         return null;
      }
            // Recalculate grid and select
            inst.execCommand('mceRepaint');
            td = getCell(grid, dy, dx);
      function getTableGrid(table) {
         var grid = new Array(), rows = table.rows, x, y, td, sd, xstart, x2, y2;
            if (td) {
               inst.selection.select(td.firstChild || td);
               inst.selection.collapse(1);
            }
         };
         for (y=0; y<rows.length; y++) {
            for (x=0; x<rows[y].cells.length; x++) {
               td = rows[y].cells[x];
               sd = getColRowSpan(td);
         function makeTD() {
            var newTD = doc.createElement("td");
               // All ready filled
               for (xstart = x; grid[y] && grid[y][xstart]; xstart++) ;
            if (!tinymce.isIE)
               newTD.innerHTML = '<br mce_bogus="1"/>';
         }
               // Fill box
               for (y2=y; y2<y+sd['rowspan']; y2++) {
                  if (!grid[y2])
                     grid[y2] = new Array();
         function getColRowSpan(td) {
            var colspan = inst.dom.getAttrib(td, "colspan");
            var rowspan = inst.dom.getAttrib(td, "rowspan");
                  for (x2=xstart; x2<xstart+sd['colspan']; x2++)
                     grid[y2][x2] = td;
            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;
         }
         return grid;
      }
         function getCell(grid, row, col) {
            if (grid[row] && grid[row][col])
               return grid[row][col];
      function trimRow(table, tr, td, new_tr) {
         var grid = getTableGrid(table), cpos = getCellPos(grid, td);
         var cells, lastElm;
            return null;
         }
         // Time to crop away some
         if (new_tr.cells.length != tr.childNodes.length) {
            cells = tr.childNodes;
            lastElm = null;
         function getNextCell(table, cell) {
            var cells = [], x = 0, i, j, cell, nextCell;
            for (var x=0; td = getCell(grid, cpos.rowindex, x); x++) {
               var remove = true;
               var sd = getColRowSpan(td);
            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];
               // 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;
            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;
                  }
               }
               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 grid;
         }
         return null;
      }
         function trimRow(table, tr, td, new_tr) {
            var grid = getTableGrid(table), cpos = getCellPos(grid, td);
            var cells, lastElm;
      function nextElm(node, names) {
         var namesAr = names.split(',');
            // Time to crop away some
            if (new_tr.cells.length != tr.childNodes.length) {
               cells = tr.childNodes;
               lastElm = null;
         while ((node = node.nextSibling) != null) {
            for (var i=0; i<namesAr.length; i++) {
               if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
               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;
         }
         return null;
      }
         function nextElm(node, names) {
            var namesAr = names.split(',');
      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");
            newTD.innerHTML = "&nbsp;";
            if (tinyMCE.isMSIE)
               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 = tinyMCE.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;
                  }
            while ((node = node.nextSibling) != null) {
               for (var i=0; i<namesAr.length; i++) {
                  if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
                     return node;
               }
            }
            if (newTD == null) {
               newTD = doc.createElement("td");
               newTD.innerHTML = "&nbsp;";
            }
            // Reset col/row span
            newTD.colSpan = 1;
            newTD.rowSpan = 1;
            newTR.appendChild(newTD);
            lastCell = tdElm;
            return null;
         }
         return newTR;
      }
         function deleteMarked(tbl) {
            if (tbl.rows == 0)
               return;
      // ---- Commands -----
            var tr = tbl.rows[0];
            do {
               var next = nextElm(tr, "TR");
      // Handle commands
      switch (command) {
         case "mceTableRowProps":
            if (trElm == null)
               return true;
               // Delete row
               if (tr._delete) {
                  tr.parentNode.removeChild(tr);
                  continue;
               }
            if (user_interface) {
               // Setup template
               var template = new Array();
               // Delete cells
               var td = tr.cells[0];
               if (td.cells > 1) {
                  do {
                     var nexttd = nextElm(td, "TD,TH");
               template['file'] = '../../plugins/table/row.htm';
               template['width'] = 380;
               template['height'] = 295;
                     if (td._delete)
                        td.parentNode.removeChild(td);
                  } while ((td = nexttd) != null);
               }
            } while ((tr = next) != null);
         }
               // Language specific width and height addons
               template['width'] += tinyMCE.getLang('lang_table_rowprops_delta_width', 0);
               template['height'] += tinyMCE.getLang('lang_table_rowprops_delta_height', 0);
         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");
               // Open window
               tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
               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");
            }
         }
            return true;
         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;
         case "mceTableCellProps":
            if (tdElm == null)
               return true;
            for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
               var newTD = null;
            if (user_interface) {
               // Setup template
               var template = new Array();
               template['file'] = '../../plugins/table/cell.htm';
               template['width'] = 380;
               template['height'] = 295;
               // Language specific width and height addons
               template['width'] += tinyMCE.getLang('lang_table_cellprops_delta_width', 0);
               template['height'] += tinyMCE.getLang('lang_table_cellprops_delta_height', 0);
               // Open window
               tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
            }
            return true;
         case "mceInsertTable":
            if (user_interface) {
               // Setup template
               var template = new Array();
               template['file'] = '../../plugins/table/table.htm';
               template['width'] = 380;
               template['height'] = 295;
               // Language specific width and height addons
               template['width'] += tinyMCE.getLang('lang_table_table_delta_width', 0);
               template['height'] += tinyMCE.getLang('lang_table_table_delta_height', 0);
               // Open window
               tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes", action : value});
            }
            return true;
         case "mceTableDelete":
            var table = tinyMCE.getParentElement(inst.getFocusElement(), "table");
            if (table) {
               table.parentNode.removeChild(table);
               inst.repaint();
            }
            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");
                              newTD.innerHTML = "&nbsp;";
                              newTD.colSpan = tdElm.colSpan;
                              newTR.appendChild(newTD);
                           } else
                              tdElm.rowSpan = sd['rowspan'] + 1;
                           lastTDElm = tdElm;
                        }
               if (lastCell != tdElm) {
                  for (var i=0; i<tr.cells.length; i++) {
                     if (tdElm == tr.cells[i]) {
                        newTD = tdElm.cloneNode(true);
                        break;
                     }
                     trElm.parentNode.insertBefore(newTR, trElm);
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex + 1, cpos.cellindex), tinyMCE.isGecko, true); // Only collape on gecko
                  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");
                              newTD.innerHTML = "&nbsp;";
                              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);
                     }
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex, cpos.cellindex), tinyMCE.isGecko, true); // Only collape on gecko
                  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 = tinyMCE.getParentElement(tableElm, "table"); // Look for table instead of tbody
                        tableElm.parentNode.removeChild(tableElm);
                        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);
                     cpos.rowindex--;
                     if (cpos.rowindex < 0)
                        cpos.rowindex = 0;
                     // Recalculate grid and select
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex, 0), tinyMCE.isGecko, true); // Only collape on gecko
                  break;
                  case "mceTableInsertColBefore":
                     if (!trElm || !tdElm)
                        return true;
                     var grid = getTableGrid(tableElm);
                     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);
                              newTD.innerHTML = "&nbsp;";
                              newTD.rowSpan = tdElm.rowSpan;
                              tdElm.parentNode.insertBefore(newTD, tdElm);
                           } else
                              tdElm.colSpan++;
                           lastTDElm = tdElm;
                        }
                     }
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex, cpos.cellindex + 1), tinyMCE.isGecko, true); // Only collape on gecko
                  break;
                  case "mceTableInsertColAfter":
                     if (!trElm || !tdElm)
                        return true;
                     var grid = getTableGrid(tableElm);
                     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);
                              newTD.innerHTML = "&nbsp;";
                              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;
                        }
                     }
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex, cpos.cellindex), tinyMCE.isGecko, true); // Only collape on gecko
                  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 = tinyMCE.getParentElement(tableElm, "table"); // Look for table instead of tbody
                        tableElm.parentNode.removeChild(tableElm);
                        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;
                        }
                     }
                     cpos.cellindex--;
                     if (cpos.cellindex < 0)
                        cpos.cellindex = 0;
                     // Recalculate grid and select
                     grid = getTableGrid(tableElm);
                     inst.selection.selectNode(getCell(grid, cpos.rowindex, 0), tinyMCE.isGecko, true); // Only collape on gecko
                  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");
                        newTD.innerHTML = "&nbsp;";
                        trElm.insertBefore(newTD, nextElm(tdElm, "TD,TH"));
                        if (rowspan > 1)
                           addRows(newTD, trElm, rowspan);
                     }
                     addRows(tdElm, trElm, rowspan);
                  }
               }
                  // Apply visual aids
                  tableElm = tinyMCE.getParentElement(inst.getFocusElement(), "table");
                  break;
               if (newTD == null) {
                  newTD = doc.createElement("td");
               case "mceTableMergeCells":
                  var rows = new Array();
                  var sel = inst.getSel();
                  var grid = getTableGrid(tableElm);
                  if (!tinymce.isIE)
                     newTD.innerHTML = '<br mce_bogus="1"/>';
               }
                  if (tinyMCE.isMSIE || sel.rangeCount == 1) {
                     if (user_interface) {
                        // Setup template
                        var template = new Array();
                        var sp = getColRowSpan(tdElm);
               // Reset col/row span
               newTD.colSpan = 1;
               newTD.rowSpan = 1;
                        template['file'] = '../../plugins/table/merge_cells.htm';
                        template['width'] = 250;
                        template['height'] = 105 + (tinyMCE.isNS7 ? 25 : 0);
               newTR.appendChild(newTD);
                        // Language specific width and height addons
                        template['width'] += tinyMCE.getLang('lang_table_merge_cells_delta_width', 0);
                        template['height'] += tinyMCE.getLang('lang_table_merge_cells_delta_height', 0);
               lastCell = tdElm;
            }
                        // Open window
                        tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes", action : "update", numcols : sp.colspan, numrows : sp.rowspan});
            return newTR;
         }
                        return true;
                     } else {
                        var numRows = parseInt(value['numrows']);
                        var numCols = parseInt(value['numcols']);
         // ---- 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);
                        if (("" + numRows) == "NaN")
                           numRows = 1;
                        // Only one row, remove whole table
                        if (grid.length == 1 && tableElm.nodeName == 'TBODY') {
                           inst.dom.remove(inst.dom.getParent(tableElm, "table"));
                           return true;
                        }
                        if (("" + numCols) == "NaN")
                           numCols = 1;
                        // 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=cpos.rowindex; y<grid.length; y++) {
                           var rowCells = new Array();
                        for (var y=0; y<tRows.length; y++) {
                           var rowCells = [];
                           for (var x=cpos.cellindex; x<grid[y].length; x++) {
                              var td = getCell(grid, y, x);
                           for (var x=0; x<tRows[y].cells.length; x++) {
                              var td = tRows[y].cells[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)
                              for (var i=0; i<cells.length; i++) {
                                 if (td == cells[i]) {
                                    rowCells[rowCells.length] = td;
                                 }
                              }
                           }
@@ -881,193 +1047,156 @@
                              rows[rows.length] = rowCells;
                        }
                        //return true;
                     }
                  } else {
                     var cells = new Array();
                     var sel = inst.getSel();
                     var lastTR = null;
                     var curRow = null;
                     var x1 = -1, y1 = -1, x2, y2;
                        // 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;
                     // Only one cell selected, whats the point?
                     if (sel.rangeCount < 2)
                        return true;
                              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 all selected cells
                     for (var i=0; i<sel.rangeCount; i++) {
                        var rng = sel.getRangeAt(i);
                        var tdElm = rng.startContainer.childNodes[rng.startOffset];
                                    // Get end pos
                                    x2 = x;
                                    y2 = y;
                        if (!tdElm)
                           break;
                        if (tdElm.nodeName == "TD")
                           cells[cells.length] = tdElm;
                     }
                     // Get rows and cells
                     var tRows = tableElm.rows;
                     for (var y=0; y<tRows.length; y++) {
                        var rowCells = new Array();
                        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 = new Array();
                     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;
                                    grid[y][x]._selected = true;
                                 }
                              }
                           }
                        }
                                 // 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;
                              }
                           }
                        }
                     }
                     // 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) {
                     // 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;
                           }
                        }
                     }
                  }
                  // 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'];
                        }
                        lastRowSpan = sd['rowspan'];
                        if (rowColSpan > colSpan)
                           colSpan = rowColSpan;
                        lastRowSpan = -1;
                     }
                     if (rowColSpan > colSpan)
                        colSpan = rowColSpan;
                     // Validate vertical and get total rowspan
                     var lastColSpan = -1;
                     for (var x=0; x<rows[0].length; x++) {
                        var colRowSpan = 0;
                     lastRowSpan = -1;
                  }
                        for (var y=0; y<rows.length; y++) {
                           var sd = getColRowSpan(rows[y][x]);
                  // Validate vertical and get total rowspan
                  var lastColSpan = -1;
                  for (var x=0; x<rows[0].length; x++) {
                     var colRowSpan = 0;
                           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++) {
                        var sd = getColRowSpan(rows[y][x]);
                        for (var x=0; x<rows[y].length; x++) {
                           var html = rows[y][x].innerHTML;
                           var chk = html.replace(/[ \t\r\n]/g, "");
                        colRowSpan += sd['rowspan'];
                           if (chk != "<br/>" && chk != "<br>" && chk != '<br mce_bogus="1"/>' && (x+y > 0))
                              tdElm.innerHTML += html;
                        if (lastColSpan != -1 && sd['colspan'] != lastColSpan) {
                           alert("Invalid selection for merge.");
                           return true;
                        }
                           // 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;
                        lastColSpan = sd['colspan'];
                     }
                              tr.removeChild(rows[y][x]);
                              rows[y][x]._deleted = true;
                     if (colRowSpan > rowSpan)
                        rowSpan = colRowSpan;
                              // Empty TR, remove it
                              if (!tr.hasChildNodes()) {
                                 tr.parentNode.removeChild(tr);
                     lastColSpan = -1;
                  }
                                 var lastCell = null;
                                 for (var x=0; cellElm = getCell(grid, cpos.rowindex, x); x++) {
                                    if (cellElm != lastCell && cellElm.rowSpan > 1)
                                       cellElm.rowSpan--;
                  // Setup td
                  tdElm = rows[0][0];
                  tdElm.rowSpan = rowSpan;
                  tdElm.colSpan = colSpan;
                                    lastCell = cellElm;
                                 }
                  // 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 = tinyMCE.regexpReplace(html, "[ \t\r\n]", "");
                        if (chk != "<br/>" && chk != "<br>" && chk != "&nbsp;" && (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--;
                              }
                              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;
                  }
                  break;
                  tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
                  inst.addVisual(tableElm);
                  inst.nodeChanged();
               }
               tableElm = tinyMCE.getParentElement(inst.getFocusElement(), "table");
               tinyMCE.handleVisualAid(tableElm, true, tinyMCE.settings['visual'], tinyMCE.selectedInstance);
               tinyMCE.triggerNodeChange();
               inst.repaint();
            }
            return true;
         }
         return true;
         // Pass to next handler in chain
         return false;
      }
   });
      // Pass to next handler in chain
      return false;
   }
};
tinyMCE.addPlugin("table", TinyMCE_TablePlugin);
   // Register plugin
   tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
})();