Skip to content

Commit

Permalink
backport PR #2759
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Jul 30, 2024
1 parent c89ec07 commit 852ef22
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fixes and validates input values headerRows and keepWithHeaderRows

## 0.2.10 - 2024-03-07

- Removed unused brfs dependency
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ function isUndefined(variable) {
return variable === undefined;
}

/**
* @param {any} variable
* @returns {boolean}
*/
function isPositiveInteger(variable) {
if (!isNumber(variable) || !Number.isInteger(variable) || variable <= 0) {
return false;
}
return true;
}

function pack() {
var result = {};

Expand Down Expand Up @@ -117,6 +128,7 @@ module.exports = {
isObject: isObject,
isNull: isNull,
isUndefined: isUndefined,
isPositiveInteger: isPositiveInteger,
pack: pack,
fontStringify: fontStringify,
offsetVector: offsetVector,
Expand Down
25 changes: 21 additions & 4 deletions src/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var ColumnCalculator = require('./columnCalculator');
var isFunction = require('./helpers').isFunction;
var isNumber = require('./helpers').isNumber;
var isPositiveInteger = require('./helpers').isPositiveInteger;

function TableProcessor(tableNode) {
this.tableNode = tableNode;
Expand All @@ -24,12 +25,28 @@ TableProcessor.prototype.beginTable = function (writer) {
this.rowSpanData = prepareRowSpanData();
this.cleanUpRepeatables = false;

this.headerRows = tableNode.table.headerRows || 0;
if (this.headerRows > tableNode.table.body.length) {
throw new Error(`Too few rows in the table. Property headerRows requires at least ${this.headerRows}, contains only ${tableNode.table.body.length}`);
// headersRows and rowsWithoutPageBreak (headerRows + keepWithHeaderRows)
this.headerRows = 0;
this.rowsWithoutPageBreak = 0;

var headerRows = tableNode.table.headerRows;

if (isPositiveInteger(headerRows)) {
this.headerRows = headerRows;

if (this.headerRows > tableNode.table.body.length) {
throw new Error(`Too few rows in the table. Property headerRows requires at least ${this.headerRows}, contains only ${tableNode.table.body.length}`);
}

this.rowsWithoutPageBreak = this.headerRows;

const keepWithHeaderRows = tableNode.table.keepWithHeaderRows;

if (isPositiveInteger(keepWithHeaderRows)) {
this.rowsWithoutPageBreak += keepWithHeaderRows;
}
}

this.rowsWithoutPageBreak = this.headerRows + (tableNode.table.keepWithHeaderRows || 0);
this.dontBreakRows = tableNode.table.dontBreakRows || false;

if (this.rowsWithoutPageBreak) {
Expand Down
130 changes: 130 additions & 0 deletions tests/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,134 @@ describe('TableProcessor', function () {
assert.equal(fakeWriter.popFromRepeatables.callCount, 1);
});
});

describe('headerRows and keepWithHeaderRows (issue #2754)', function () {
var fakeWriter = {
context: function () {
return {
availableWidth: 56473,
};
},
beginUnbreakableBlock: function () { },
};

const inputTable = {
table: {
widths: [{ width: 20 }, { width: 20 }],
body: [['a', 'b'], ['c', 'd'], ['e', 'f']]
},
_offsets: {
total: 9
},
_layout: {
paddingLeft: function () { },
paddingRight: function () { },
paddingBottom: function () { },
paddingTop: function () { },
vLineWidth: function () { },
hLineWidth: function () { },
fillColor: function () { },
fillOpacity: function () { }
}
};

it('should ignore wrong values for headerRows - 1', function () {
inputTable.table.headerRows = '2';
inputTable.table.keepWithHeaderRows = 2;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 0);
assert.equal(tableProcessor.rowsWithoutPageBreak, 0);
});

it('should ignore wrong values for headerRows - 2', function () {
inputTable.table.headerRows = -5;
inputTable.table.keepWithHeaderRows = 2;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 0);
assert.equal(tableProcessor.rowsWithoutPageBreak, 0);
});


it('should ignore wrong values for headerRows - 3', function () {
inputTable.table.headerRows = 2.5;
inputTable.table.keepWithHeaderRows = 2;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 0);
assert.equal(tableProcessor.rowsWithoutPageBreak, 0);
});

it('should ignore keepWithHeaderRows if headerRows is not bigger than 0', function () {
inputTable.table.headerRows = 0;
inputTable.table.keepWithHeaderRows = 2;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 0);
assert.equal(tableProcessor.rowsWithoutPageBreak, 0);
});

it('should ignore wrong values for keepWithHeaderRows - 1', function () {
inputTable.table.headerRows = 1;
inputTable.table.keepWithHeaderRows = 1.5;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 1);
assert.equal(tableProcessor.rowsWithoutPageBreak, 1);
});

it('should ignore wrong values for keepWithHeaderRows - 2', function () {
inputTable.table.headerRows = 2;
inputTable.table.keepWithHeaderRows = '1.5';

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.headerRows, 2);
assert.equal(tableProcessor.rowsWithoutPageBreak, 2);
});

it('should sum up headerRows and keepWithHeaderRows - 1', function () {
inputTable.table.headerRows = 1;
inputTable.table.keepWithHeaderRows = 2;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.rowsWithoutPageBreak, 3);
assert.equal(tableProcessor.headerRows, 1);
});

it('should sum up headerRows and keepWithHeaderRows - 2', function () {
inputTable.table.headerRows = 1;
inputTable.table.keepWithHeaderRows = 0;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
tableProcessor.beginTable(fakeWriter);
assert.equal(tableProcessor.rowsWithoutPageBreak, 1);
assert.equal(tableProcessor.headerRows, 1);
});

it('should throw exception when headerRows > table rows', function () {
inputTable.table.headerRows = 5;
inputTable.table.keepWithHeaderRows = 0;

var tableProcessor = new TableProcessor(inputTable);
tableProcessor.drawHorizontalLine = function() { };
assert.throws(() => tableProcessor.beginTable(fakeWriter), /Too few rows in the table/);
});
});

});

0 comments on commit 852ef22

Please sign in to comment.