diff --git a/src/modules/table/table.test.js b/src/modules/table/table.test.js index 3e9731bba..f286c1223 100644 --- a/src/modules/table/table.test.js +++ b/src/modules/table/table.test.js @@ -4,7 +4,7 @@ * Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net */ -describe('Tables Jodit Editor Tests', () => { +describe.only('Tables Jodit Editor Tests', () => { describe('Methods', () => { it('After init container must have one element .jodit-table-resizer', () => { const editor = getJodit(); @@ -238,10 +238,14 @@ describe('Tables Jodit Editor Tests', () => { }); }); - describe.only('appendColumn', () => { + describe('appendColumn', () => { describe('Simple append column', () => { - it('should append column in the end', () => { - const editor = getJodit(); + it('should append column in the end', async () => { + const editor = getJodit({ + cleanHTML: { + fillEmptyParagraph: true + } + }); editor.value = '' + @@ -251,47 +255,169 @@ describe('Tables Jodit Editor Tests', () => { editor .getInstance(Jodit.modules.Table) - .appendColumn(editor.editor.firstChild, -1, true); + .appendColumn(editor.editor.firstChild); + + await waitingForEvent(editor, 'finishedCleanHTMLWorker'); expect(editor.value.toLowerCase()).equals( '
' + '' + - '' + - '' + + '' + + '' + '' + '
12
3
12
3
' ); }); }); - describe('With colspan', () => { - it('should append correct', () => { - const editor = getJodit(); - - editor.value = - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '






'; - - editor - .getInstance(Jodit.modules.Table) - .appendColumn(editor.editor.firstChild, 0, true); - - expect(editor.value.toLowerCase()).equals( - '' + + describe('with arguments', () => { + [ + [ + // 0 + '
' + + '' + + '
12
', + '' + + '' + + '' + + '' + + '
1
2
', + 0, + true + ], + [ + // 1 + '' + + '' + + '
12
', + '' + + '' + + '' + + '' + + '
1
2
', + 0, + true + ], + [ + // 2 + '' + + '' + + '' + + '
12
34
', + '' + + '' + + '' + + '' + + '' + + '
1
2
3
4
', + 0, + true + ], + [ + // 3 + '' + + '' + + '
12
', + '' + + '' + + '' + + '' + + '
1
2
', + 0, + true + ], + [ + // 4 + '' + + '' + + '' + + '
12
34
', + '' + + '' + + '' + + '' + + '' + + '
12
34
', + 1, + true + ], + [ + // 5 + '' + + '' + + '
12
', + '' + + '' + + '' + + '' + + '

12
', + 0, + false + ], + [ + // 6 + '' + + '' + + '' + + '
12
34
', + '' + + '' + + '' + + '' + + '' + + '

12

34
', + 0, + false + ], + [ + // 7 + '' + + '' + + '' + + '
12
3
', + '' + + '' + + '' + + '' + + '' + + '
1
2
3
', + 0, + true + ], + [ + // 8 + '' + + '' + + '' + + '
123
45
', + '' + + '' + + '' + + '' + + '' + + '
1
23
45
', + 0, + true + ], + [ + // 9 + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '






', + '' + '' + '' + '' + @@ -308,57 +434,137 @@ describe('Tables Jodit Editor Tests', () => { '' + '' + '' + - '


' - ); - }); - }); - - describe('with second argument ', () => { - it('should append column after that column', () => { - const editor = getJodit(); - - editor.value = + '', + 0, + true + ], + [ + // 10 + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '






', + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '








', + 0, + false + ], + [ + // 11 '' + - '' + - '' + - '
12
3
'; - - editor - .getInstance(Jodit.modules.Table) - .appendColumn(editor.editor.firstChild, 0, true); - - expect(editor.value.toLowerCase()).equals( + '' + + '' + + '
' + + '
' + + '
' + + '' + + '' + + '
' + + '
' + + '' + + '' + + '', '' + '' + - '' + - '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + '' + - '
12
3






' - ); - }); - - describe('and third = false ', () => { - it('should append column before that column', () => { - const editor = getJodit(); - - editor.value = - '' + - '' + - '' + - '
12
3
'; - - editor - .getInstance(Jodit.modules.Table) - .appendColumn(editor.editor.firstChild, 1, false); + '', + 1, + true + ], + [ + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '



', + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '




', + 2, + false + ] + ].forEach(([value, result, column, after], index) => { + describe(`Case #${index} Value is ${value} and column is ${column} and after is ${after}`, () => { + it('should return result ' + result, async () => { + const editor = getJodit({ + cleanHTML: { + fillEmptyParagraph: true + } + }); + + editor.value = value; + + editor + .getInstance(Jodit.modules.Table) + .appendColumn( + editor.editor.firstChild, + editor.editor.querySelectorAll('td')[ + column + ], + after + ); + await waitingForEvent( + editor, + 'finishedCleanHTMLWorker' + ); - expect(editor.value.toLowerCase()).equals( - '' + - '' + - '' + - '' + - '' + - '
12
3
' - ); + // if () {} + strCompare(sortAttributes(editor.value), result); + expect(sortAttributes(editor.value)).equals(result); + }); }); }); }); diff --git a/src/modules/table/table.ts b/src/modules/table/table.ts index a3edfd107..504b14cd6 100644 --- a/src/modules/table/table.ts +++ b/src/modules/table/table.ts @@ -18,6 +18,7 @@ import { Dom } from 'jodit/core/dom'; import { getContainer } from 'jodit/core/global'; import { $$, + // $$, attr, cssPath, isNumber, @@ -399,65 +400,47 @@ export class Table extends ViewComponent { return Table.__removeRow(table, rowIndex); } - private static __appendColumn( + /** + * Insert column before / after all the columns containing the selected cells + */ + appendColumn( table: HTMLTableElement, - j: number, - after: boolean, - create: ICreate + selectedCell: HTMLTableCellElement, + insertAfter: boolean ): void { const box = Table.__formalMatrix(table); - let i: number; + const columnIndex = insertAfter + ? selectedCell.cellIndex + ((selectedCell.colSpan || 1) - 1) + : selectedCell.cellIndex; - if (j === undefined || j < 0) { - j = Table.__getColumnsCount(table) - 1; - } + const newColumnIndex = insertAfter ? columnIndex + 1 : columnIndex; - for (i = 0; i < box.length; i += 1) { - const cell = create.element('td'); - const td = box[i][j]; + for (let i = 0; i < box.length; ) { + const cells = box[i]; - let added: boolean = false; + if ( + cells[columnIndex] !== cells[newColumnIndex] || + columnIndex === newColumnIndex + ) { + const cell = this.j.createInside.element('td'); + if (insertAfter) { + Dom.after(cells[columnIndex], cell); + } else { + Dom.before(cells[columnIndex], cell); + } - if (after) { - if ( - (box[i] && td && j + 1 >= box[i].length) || - td !== box[i][j + 1] - ) { - if (td.nextSibling) { - Dom.before(td.nextSibling, cell); - } else { - td.parentNode && td.parentNode.appendChild(cell); - } - added = true; + if (cells[columnIndex].rowSpan > 1) { + cell.rowSpan = cells[columnIndex].rowSpan; } } else { - if ( - j - 1 < 0 || - (box[i][j] !== box[i][j - 1] && box[i][j].parentNode) - ) { - Dom.before(box[i][j], cell); - added = true; - } + cells[columnIndex].colSpan += 1; } - if (!added) { - attr( - box[i][j], - 'colspan', - parseInt(attr(box[i][j], 'colspan') || '1', 10) + 1 - ); - } + i += cells[columnIndex].rowSpan || 1; } } - /** - * Insert column before / after all the columns containing the selected cells - */ - appendColumn(table: HTMLTableElement, j: number, after: boolean): void { - return Table.__appendColumn(table, j, after, this.j.createInside); - } - private static __removeColumn(table: HTMLTableElement, j: number): void { const box = Table.__formalMatrix(table); diff --git a/src/plugins/select-cells/select-cells.ts b/src/plugins/select-cells/select-cells.ts index de51a02d2..a0bdf016d 100644 --- a/src/plugins/select-cells/select-cells.ts +++ b/src/plugins/select-cells/select-cells.ts @@ -411,7 +411,7 @@ export class selectCells extends Plugin { case 'addcolumnbefore': this.__tableModule.appendColumn( table, - cell.cellIndex, + cell, command === 'addcolumnafter' ); break; diff --git a/test/bootstrap.js b/test/bootstrap.js index e3f1cf610..a4f4d9943 100644 --- a/test/bootstrap.js +++ b/test/bootstrap.js @@ -1310,17 +1310,21 @@ Object.defineProperty(navigator, 'userAgent', { /** * - * @param {string} result - * @param {string} real + * @param {string} textA + * @param {string} textB * @return {boolean} */ -function strCompare(result, real, len = 30) { - for (let i = 0; i < Math.max(result.length, real.length); i += 1) { - if (result[i] !== real[i]) { +function strCompare(textA, textB, len = 30) { + for (let i = 0; i < Math.max(textA.length, textB.length); i += 1) { + if (textA[i] !== textB[i]) { // eslint-disable-next-line no-console - console.log('result', result.substring(i - len, i + len)); + console.log( + `textA: ${textA.substring(i - len, i)}|${textA.substring(i, i + len)}` + ); // eslint-disable-next-line no-console - console.log('real', real.substring(i - len, i + len)); + console.log( + `textB: ${textB.substring(i - len, i)}|${textB.substring(i, i + len)}` + ); return false; }