diff static/js/tiny_mce/plugins/table/editor_plugin_src.js @ 442:6c182ceb7147

Fixing #217; upgrade TinyMCE to 3.4.2 and enable the paste plugin.
author Brian Neal <bgneal@gmail.com>
date Thu, 26 May 2011 00:43:49 +0000
parents 88b2b9cb8c1f
children
line wrap: on
line diff
--- a/static/js/tiny_mce/plugins/table/editor_plugin_src.js	Wed May 25 02:39:08 2011 +0000
+++ b/static/js/tiny_mce/plugins/table/editor_plugin_src.js	Thu May 26 00:43:49 2011 +0000
@@ -104,8 +104,19 @@
 			return parseInt(td.getAttribute(name) || 1);
 		};
 
+		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 dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell;
+			return cell && (dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell);
 		};
 
 		function getSelectedRows() {
@@ -155,20 +166,21 @@
 
 					// Add something to the inner node
 					if (curNode)
-						curNode.innerHTML = tinymce.isIE ? '&nbsp;' : '<br _mce_bogus="1" />';
+						curNode.innerHTML = tinymce.isIE ? '&nbsp;' : '<br data-mce-bogus="1" />';
 
 					return false;
 				}
 			}, 'childNodes');
 
 			cell = cloneNode(cell, false);
-			cell.rowSpan = cell.colSpan = 1;
+			setSpanVal(cell, 'rowSpan', 1);
+			setSpanVal(cell, 'colSpan', 1);
 
 			if (formatNode) {
 				cell.appendChild(formatNode);
 			} else {
 				if (!tinymce.isIE)
-					cell.innerHTML = '<br _mce_bogus="1" />';
+					cell.innerHTML = '<br data-mce-bogus="1" />';
 			}
 
 			return cell;
@@ -250,7 +262,8 @@
 						rowSpan = getSpanVal(cell, 'rowspan');
 
 						if (colSpan > 1 || rowSpan > 1) {
-							cell.colSpan = cell.rowSpan = 1;
+							setSpanVal(cell, 'rowSpan', 1);
+							setSpanVal(cell, 'colSpan', 1);
 
 							// Insert cells right
 							for (i = 0; i < colSpan - 1; i++)
@@ -264,7 +277,7 @@
 		};
 
 		function merge(cell, cols, rows) {
-			var startX, startY, endX, endY, x, y, startCell, endCell, cell, children;
+			var startX, startY, endX, endY, x, y, startCell, endCell, cell, children, count;
 
 			// Use specified cell and cols/rows
 			if (cell) {
@@ -293,23 +306,34 @@
 
 				// Set row/col span to start cell
 				startCell = getCell(startX, startY).elm;
-				startCell.colSpan = (endX - startX) + 1;
-				startCell.rowSpan = (endY - startY) + 1;
+				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, i) {
-								// Jump over last BR element
-								if (node.nodeName != 'BR' || i != children.length - 1)
-									startCell.appendChild(node);
+							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);
 						}
@@ -322,7 +346,7 @@
 		};
 
 		function insertRow(before) {
-			var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell;
+			var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell, rowSpan;
 
 			// Find first/last row
 			each(grid, function(row, y) {
@@ -343,30 +367,35 @@
 			});
 
 			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) {
-							cell.rowSpan = 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');
+							rowSpan = getSpanVal(otherCell, 'rowSpan');
 							if (rowSpan > 1) {
-								otherCell.rowSpan = rowSpan + 1;
+								setSpanVal(otherCell, 'rowSpan', rowSpan + 1);
 								continue;
 							}
 						}
 					}
 
 					// Insert new cell into new row
-					newCell = cloneCell(cell)
-					newCell.colSpan = cell.colSpan;
+					newCell = cloneCell(cell);
+					setSpanVal(newCell, 'colSpan', cell.colSpan);
+
 					newRow.appendChild(newCell);
 
 					lastCell = cell;
@@ -400,8 +429,12 @@
 			});
 
 			each(grid, function(row, y) {
-				var cell = row[posX].elm, rowSpan, colSpan;
+				var cell, rowSpan, colSpan;
 
+				if (!row[posX])
+					return;
+
+				cell = row[posX].elm;
 				if (cell != lastCell) {
 					colSpan = getSpanVal(cell, 'colspan');
 					rowSpan = getSpanVal(cell, 'rowspan');
@@ -415,7 +448,7 @@
 							fillLeftDown(posX, y, rowSpan - 1, colSpan);
 						}
 					} else
-						cell.colSpan++;
+						setSpanVal(cell, 'colSpan', cell.colSpan + 1);
 
 					lastCell = cell;
 				}
@@ -432,10 +465,10 @@
 						each(grid, function(row) {
 							var cell = row[x].elm, colSpan;
 
-							colSpan = getSpanVal(cell, 'colspan');
+							colSpan = getSpanVal(cell, 'colSpan');
 
 							if (colSpan > 1)
-								cell.colSpan = colSpan - 1;
+								setSpanVal(cell, 'colSpan', colSpan - 1);
 							else
 								dom.remove(cell);
 						});
@@ -458,10 +491,10 @@
 
 				// Move down row spanned cells
 				each(tr.cells, function(cell) {
-					var rowSpan = getSpanVal(cell, 'rowspan');
+					var rowSpan = getSpanVal(cell, 'rowSpan');
 
 					if (rowSpan > 1) {
-						cell.rowSpan = rowSpan - 1;
+						setSpanVal(cell, 'rowSpan', rowSpan - 1);
 						pos = getPos(cell);
 						fillLeftDown(pos.x, pos.y, 1, 1);
 					}
@@ -475,12 +508,12 @@
 					cell = cell.elm;
 
 					if (cell != lastCell) {
-						rowSpan = getSpanVal(cell, 'rowspan');
+						rowSpan = getSpanVal(cell, 'rowSpan');
 
 						if (rowSpan <= 1)
 							dom.remove(cell);
 						else
-							cell.rowSpan = rowSpan - 1;
+							setSpanVal(cell, 'rowSpan', rowSpan - 1);
 
 						lastCell = cell;
 					}
@@ -548,7 +581,8 @@
 				// Remove col/rowspans
 				for (i = 0; i < cellCount; i++) {
 					cell = row.cells[i];
-					cell.colSpan = cell.rowSpan = 1;
+					setSpanVal(cell, 'colSpan', 1);
+					setSpanVal(cell, 'rowSpan', 1);
 				}
 
 				// Needs more cells
@@ -690,8 +724,10 @@
 
 				// Add new selection
 				for (y = startY; y <= maxY; y++) {
-					for (x = startX; x <= maxX; x++)
-						dom.addClass(grid[y][x].elm, 'mceSelected');
+					for (x = startX; x <= maxX; x++) {
+						if (grid[y][x])
+							dom.addClass(grid[y][x].elm, 'mceSelected');
+					}
 				}
 			}
 		};
@@ -754,11 +790,34 @@
 				ed.onClick.add(function(ed, e) {
 					e = e.target;
 
-					if (e.nodeName === 'TABLE')
+					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;
@@ -819,10 +878,14 @@
 						// Remove current selection
 						sel = ed.selection.getSel();
 
-						if (sel.removeAllRanges)
-							sel.removeAllRanges();
-						else
-							sel.empty();
+						try {
+							if (sel.removeAllRanges)
+								sel.removeAllRanges();
+							else
+								sel.empty();
+						} catch (ex) {
+							// IE9 might throw errors here
+						}
 
 						e.preventDefault();
 					}