Skip to content

Commit aff4a14

Browse files
authored
Fixes issue #1042 (#2778)
* Fixes issue #1042 * Fixes unit tests that broke after last commit --------- Co-authored-by: fe-cj <[email protected]>
1 parent de526ac commit aff4a14

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/DocumentContext.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class DocumentContext extends EventEmitter {
6565
}
6666
}
6767

68-
markEnding(endingCell, originalXOffset) {
68+
markEnding(endingCell, originalXOffset, discountY) {
6969
this.page = endingCell._columnEndingContext.page;
7070
this.x = endingCell._columnEndingContext.x + originalXOffset;
71-
this.y = endingCell._columnEndingContext.y;
71+
this.y = endingCell._columnEndingContext.y - discountY;
7272
this.availableWidth = endingCell._columnEndingContext.availableWidth;
7373
this.availableHeight = endingCell._columnEndingContext.availableHeight;
7474
this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;

src/LayoutBuilder.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class LayoutBuilder {
476476
}
477477

478478
ColumnCalculator.buildColumnWidths(columns, availableWidth);
479-
let result = this.processRow(columns, columns, gaps);
479+
let result = this.processRow(false, columns, columns, gaps);
480480
addAll(columnNode.positions, result.positions);
481481

482482
function gapArray(gap) {
@@ -510,7 +510,7 @@ class LayoutBuilder {
510510
return null;
511511
}
512512

513-
processRow(columns, widths, gaps, tableBody, tableRow, height) {
513+
processRow(dontBreakRows, columns, widths, gaps, tableBody, tableRow, height) {
514514
const updatePageBreakData = (page, prevY) => {
515515
let pageDesc;
516516
// Find page break data for this row and page
@@ -571,6 +571,7 @@ class LayoutBuilder {
571571
if (endingCell) {
572572
// We store a reference of the ending cell in the first cell of the rowspan
573573
column._endingCell = endingCell;
574+
column._endingCell._startingRowSpanY = column._startingRowSpanY;
574575
}
575576

576577
// Check if exists and retrieve the cell that started the rowspan in case we are in the cell just after
@@ -588,10 +589,17 @@ class LayoutBuilder {
588589

589590
// We pass the endingSpanCell reference to store the context just after processing rowspan cell
590591
this.writer.context().beginColumn(width, leftOffset, endingSpanCell);
592+
591593
if (!column._span) {
592594
this.processNode(column);
593595
addAll(positions, column.positions);
594596
} else if (column._columnEndingContext) {
597+
let discountY = 0;
598+
if (dontBreakRows) {
599+
// Calculate how many points we have to discount to Y when dontBreakRows and rowSpan are combined
600+
const ctxBeforeRowSpanLastRow = this.writer.contextStack[this.writer.contextStack.length - 1];
601+
discountY = ctxBeforeRowSpanLastRow.y - column._startingRowSpanY;
602+
}
595603
let originalXOffset = 0;
596604
// If context was saved from an unbreakable block and we are not in an unbreakable block anymore
597605
// We have to sum the originalX (X before starting unbreakable block) to X
@@ -600,7 +608,7 @@ class LayoutBuilder {
600608
}
601609
// row-span ending
602610
// Recover the context after processing the rowspanned cell
603-
this.writer.context().markEnding(column, originalXOffset);
611+
this.writer.context().markEnding(column, originalXOffset, discountY);
604612
}
605613
}
606614

@@ -708,6 +716,16 @@ class LayoutBuilder {
708716

709717
let rowHeights = tableNode.table.heights;
710718
for (let i = 0, l = tableNode.table.body.length; i < l; i++) {
719+
// if dontBreakRows and row starts a rowspan
720+
// we store the 'y' of the beginning of each rowSpan
721+
if (processor.dontBreakRows) {
722+
tableNode.table.body[i].forEach(cell => {
723+
if (cell.rowSpan && cell.rowSpan > 1) {
724+
cell._startingRowSpanY = this.writer.context().y;
725+
}
726+
});
727+
}
728+
711729
processor.beginRow(i, this.writer);
712730

713731
let height;
@@ -723,7 +741,7 @@ class LayoutBuilder {
723741
height = undefined;
724742
}
725743

726-
let result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, height);
744+
let result = this.processRow(processor.dontBreakRows, tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, height);
727745
addAll(tableNode.positions, result.positions);
728746

729747
processor.endRow(i, this.writer, result.pageBreaks);

tests/unit/LayoutBuilder.spec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1679,15 +1679,15 @@ describe('LayoutBuilder', function () {
16791679
it('should return an empty array if no page breaks occur', function () {
16801680
var doc = createTable(1, 0);
16811681

1682-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1682+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
16831683

16841684
assert(result.pageBreaks instanceof Array);
16851685
assert.equal(result.pageBreaks.length, 0);
16861686
});
16871687

16881688
it('on page break should return an entry with ending/starting positions', function () {
16891689
var doc = createTable(0, 1, 10, 5, 5);
1690-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1690+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
16911691

16921692
assert(result.pageBreaks instanceof Array);
16931693
assert.equal(result.pageBreaks.length, 1);
@@ -1697,7 +1697,7 @@ describe('LayoutBuilder', function () {
16971697

16981698
it('on page break should return an entry with ending/starting positions 2', function () {
16991699
var doc = createTable(0, 1, 10, 5);
1700-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1700+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
17011701

17021702
assert(result.pageBreaks instanceof Array);
17031703
assert.equal(result.pageBreaks.length, 1);
@@ -1708,14 +1708,14 @@ describe('LayoutBuilder', function () {
17081708

17091709
it('on multi-pass page break (columns or table columns) should treat bottom-most page-break as the ending position ', function () {
17101710
var doc = createTable(0, 1, 10, 5, 7);
1711-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1711+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
17121712

17131713
assert.equal(result.pageBreaks[0].prevY, 40 + 12 * 7);
17141714
});
17151715

17161716
it('on multiple page breaks (more than 2 pages), should return all entries with ending/starting positions', function () {
17171717
var doc = createTable(0, 1, 100, 90, 90);
1718-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1718+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
17191719

17201720
assert(result.pageBreaks instanceof Array);
17211721
assert.equal(result.pageBreaks.length, 2);
@@ -1727,7 +1727,7 @@ describe('LayoutBuilder', function () {
17271727

17281728
it('on multiple page breaks (more than 2 pages), should return all entries with ending/starting positions 2', function () {
17291729
var doc = createTable(0, 1, 100, 90);
1730-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1730+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
17311731

17321732
assert(result.pageBreaks instanceof Array);
17331733
assert.equal(result.pageBreaks.length, 2);
@@ -1739,7 +1739,7 @@ describe('LayoutBuilder', function () {
17391739

17401740
it('on multiple and multi-pass page breaks should calculate bottom-most endings for every page', function () {
17411741
var doc = createTable(0, 1, 100, 90, 92);
1742-
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
1742+
var result = builder2.processRow(false, doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
17431743

17441744
assert(result.pageBreaks instanceof Array);
17451745
assert.equal(result.pageBreaks.length, 2);

0 commit comments

Comments
 (0)