@@ -51,14 +51,17 @@ export function downcastInsertTable( options = {} ) {
5151 headingColumns : table . getAttribute ( 'headingColumns' ) || 0
5252 } ;
5353
54+ // Cache for created table rows.
55+ const viewRows = new Map ( ) ;
56+
5457 for ( const tableWalkerValue of tableWalker ) {
5558 const { row, cell } = tableWalkerValue ;
5659
5760 const tableSection = getOrCreateTableSection ( getSectionName ( row , tableAttributes ) , tableElement , conversionApi ) ;
5861 const tableRow = table . getChild ( row ) ;
5962
60- // Check if row was converted
61- const trElement = getOrCreateTr ( tableRow , row , tableSection , conversionApi ) ;
63+ const trElement = viewRows . get ( row ) || createTr ( tableRow , row , tableSection , conversionApi ) ;
64+ viewRows . set ( row , trElement ) ;
6265
6366 // Consume table cell - it will be always consumed as we convert whole table at once.
6467 conversionApi . consumable . consume ( cell , 'insert' ) ;
@@ -104,9 +107,14 @@ export function downcastInsertRow( options = {} ) {
104107 headingColumns : table . getAttribute ( 'headingColumns' ) || 0
105108 } ;
106109
110+ // Cache for created table rows.
111+ const viewRows = new Map ( ) ;
112+
107113 for ( const tableWalkerValue of tableWalker ) {
108114 const tableSection = getOrCreateTableSection ( getSectionName ( row , tableAttributes ) , tableElement , conversionApi ) ;
109- const trElement = getOrCreateTr ( tableRow , row , tableSection , conversionApi ) ;
115+
116+ const trElement = viewRows . get ( row ) || createTr ( tableRow , row , tableSection , conversionApi ) ;
117+ viewRows . set ( row , trElement ) ;
110118
111119 // Consume table cell - it will be always consumed as we convert whole row at once.
112120 conversionApi . consumable . consume ( tableWalkerValue . cell , 'insert' ) ;
@@ -404,29 +412,25 @@ function createViewTableCellElement( tableWalkerValue, tableAttributes, insertPo
404412 }
405413}
406414
407- // Creates or returns an existing `<tr>` element from the view.
415+ // Creates `<tr>` view element .
408416//
409417// @param {module:engine/view/element~Element } tableRow
410418// @param {Number } rowIndex
411419// @param {module:engine/view/element~Element } tableSection
412420// @param {Object } conversionApi
413421// @returns {module:engine/view/element~Element }
414- function getOrCreateTr ( tableRow , rowIndex , tableSection , conversionApi ) {
415- let trElement = conversionApi . mapper . toViewElement ( tableRow ) ;
422+ function createTr ( tableRow , rowIndex , tableSection , conversionApi ) {
423+ // Will always consume since we're converting <tableRow> element from a parent <table>.
424+ conversionApi . consumable . consume ( tableRow , 'insert' ) ;
416425
417- if ( ! trElement ) {
418- // Will always consume since we're converting <tableRow> element from a parent <table>.
419- conversionApi . consumable . consume ( tableRow , 'insert' ) ;
426+ const trElement = conversionApi . writer . createContainerElement ( 'tr' ) ;
427+ conversionApi . mapper . bindElements ( tableRow , trElement ) ;
420428
421- trElement = conversionApi . writer . createContainerElement ( 'tr ' ) ;
422- conversionApi . mapper . bindElements ( tableRow , trElement ) ;
429+ const headingRows = tableRow . parent . getAttribute ( 'headingRows ' ) || 0 ;
430+ const offset = headingRows > 0 && rowIndex >= headingRows ? rowIndex - headingRows : rowIndex ;
423431
424- const headingRows = tableRow . parent . getAttribute ( 'headingRows' ) || 0 ;
425- const offset = headingRows > 0 && rowIndex >= headingRows ? rowIndex - headingRows : rowIndex ;
426-
427- const position = conversionApi . writer . createPositionAt ( tableSection , offset ) ;
428- conversionApi . writer . insert ( position , trElement ) ;
429- }
432+ const position = conversionApi . writer . createPositionAt ( tableSection , offset ) ;
433+ conversionApi . writer . insert ( position , trElement ) ;
430434
431435 return trElement ;
432436}
0 commit comments