diff --git a/src/js/table.js b/src/js/table.js index 4441028..15f27fe 100644 --- a/src/js/table.js +++ b/src/js/table.js @@ -12,11 +12,14 @@ Table.prototype = { }, insert: function (rows, cols) { - var html = this._html(rows, cols); + var htmlHeader, html; + htmlHeader = this._html(0, cols, "header"); + html = this._html(rows, cols); this._editor.pasteHTML( '' + + '' + htmlHeader + '' + '' + html + '' + @@ -39,10 +42,10 @@ Table.prototype = { this._editor.checkSelection(); }, - _html: function (rows, cols) { + _html: function (rows, cols, defaultText) { var html = '', x, y, - text = getSelectionText(this._doc); + text = (defaultText)? defaultText: getSelectionText(this._doc); for (x = 0; x <= rows; x++) { html += ''; @@ -71,17 +74,22 @@ Table.prototype = { e.preventDefault(); e.stopPropagation(); table = this._getTableElements(el); + var tbody = this._getTBody(getParentOf(el, 'table')); if (e.shiftKey) { this._tabBackwards(el.previousSibling, table.row); } else { if (this._isLastCell(el, table.row, table.root)) { - this._insertRow(getParentOf(el, 'tbody'), table.row.cells.length); + this._insertRow(tbody, table.row.cells.length); } placeCaretAtNode(this._doc, el); } } }, + _getTBody: function (table) { + return table.querySelector('tbody'); + }, + _getTableElements: function (el) { return { cell: getParentOf(el, 'td'), @@ -109,8 +117,8 @@ Table.prototype = { _isLastCell: function (el, row, table) { return ( - (row.cells.length - 1) === el.cellIndex && - (table.rows.length - 1) === row.rowIndex + (row.cells.length - 1) === el.cellIndex && + (table.rows.length - 1) === row.rowIndex ); },