diff --git a/src/layout-manager.js b/src/layout-manager.js index 098df3a..e02973d 100644 --- a/src/layout-manager.js +++ b/src/layout-manager.js @@ -5,21 +5,28 @@ var RowSpanCell = Cell.RowSpanCell; var ColSpanCell = Cell.ColSpanCell; (function(){ + function scanForConflict(table, rowIndex, columnIndex, cell) { + for(var y = rowIndex; y >= 0; y--){ + var row2 = table[y]; + var xMax = (y === rowIndex) ? columnIndex : row2.length; + for(var x = 0; x < xMax; x++){ + var cell2 = row2[x]; + while(cellsConflict(cell,cell2)){ + cell.x++; + } + } + } + } + function layoutTable(table){ table.forEach(function(row,rowIndex){ row.forEach(function(cell,columnIndex){ cell.y = rowIndex; cell.x = columnIndex; - for(var y = rowIndex; y >= 0; y--){ - var row2 = table[y]; - var xMax = (y === rowIndex) ? columnIndex : row2.length; - for(var x = 0; x < xMax; x++){ - var cell2 = row2[x]; - while(cellsConflict(cell,cell2)){ - cell.x++; - } - } - } + + // Because the initial scan may leave some cells with the same coordinates we scan twice + scanForConflict(table, rowIndex, columnIndex, cell); + scanForConflict(table, rowIndex, columnIndex, cell); }); }); }