Skip to content

Commit

Permalink
Merge pull request #2733 from estanglerbm/stylecontext_autopush_faste…
Browse files Browse the repository at this point in the history
…r_override

Speed up StyleContextStack.autopush() for large tables
  • Loading branch information
liborm85 authored Jul 30, 2024
2 parents 225f6d8 + d0f6560 commit c7caabb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
47 changes: 3 additions & 44 deletions src/styleContextStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,9 @@ StyleContextStack.prototype.autopush = function (item) {
this.push(styleNames[i]);
}

var styleProperties = [
'font',
'fontSize',
'fontFeatures',
'bold',
'italics',
'alignment',
'color',
'columnGap',
'fillColor',
'fillOpacity',
'decoration',
'decorationStyle',
'decorationColor',
'background',
'lineHeight',
'characterSpacing',
'noWrap',
'markerColor',
'leadingIndent',
'sup',
'sub'
//'tableCellPadding'
// 'cellBorder',
// 'headerCellBorder',
// 'oddRowCellBorder',
// 'evenRowCellBorder',
// 'tableBorder'
];
var styleOverrideObject = {};
var pushStyleOverrideObject = false;

styleProperties.forEach(function (key) {
if (!isUndefined(item[key]) && !isNull(item[key])) {
styleOverrideObject[key] = item[key];
pushStyleOverrideObject = true;
}
});

if (pushStyleOverrideObject) {
this.push(styleOverrideObject);
}

return styleNames.length + (pushStyleOverrideObject ? 1 : 0);
// rather than spend significant time making a styleOverrideObject, just add item
this.push(item);
return styleNames.length + 1;
};

/**
Expand Down
46 changes: 40 additions & 6 deletions tests/styleContextStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ describe('StyleContextStack', function () {

var defaultStyle = { fontSize: 12, bold: false, font: 'Helvetica' };

var styleProperties = [
'font',
'fontSize',
'fontFeatures',
'bold',
'italics',
'alignment',
'color',
'columnGap',
'fillColor',
'fillOpacity',
'decoration',
'decorationStyle',
'decorationColor',
'background',
'lineHeight',
'characterSpacing',
'noWrap',
'markerColor',
'leadingIndent',
'sup',
'sub'
//'tableCellPadding'
// 'cellBorder',
// 'headerCellBorder',
// 'oddRowCellBorder',
// 'evenRowCellBorder',
// 'tableBorder'
];

var stackWithDefaultStyle;
var fullStack;

Expand Down Expand Up @@ -113,21 +143,25 @@ describe('StyleContextStack', function () {
});

describe('autopush', function () {
it('should not push anything if no style nor style-property is defined', function () {
assert.equal(fullStack.autopush({ anotherProperty: 'test' }), 0);
it('should not push any style if no style nor style-property is defined', function () {
assert.equal(fullStack.autopush({ anotherProperty: 'test' }), 1);
assert.equal(fullStack.styleOverrides.length, 1);
styleProperties.forEach(function(key) {
assert.equal(fullStack.styleOverrides[0][key], undefined);
});
});

it('should push style name if object specifies it in the style property', function () {
assert.equal(fullStack.styleOverrides.length, 0);
assert.equal(fullStack.autopush({ anotherProperty: 'test', style: 'header' }), 1);
assert.equal(fullStack.styleOverrides.length, 1);
assert.equal(fullStack.autopush({ anotherProperty: 'test', style: 'header' }), 2);
assert.equal(fullStack.styleOverrides.length, 2);
assert.equal(fullStack.styleOverrides[0], 'header');
});

it('should push all style names if object specifies them as an array in the style property', function () {
assert.equal(fullStack.styleOverrides.length, 0);
assert.equal(fullStack.autopush({ anotherProperty: 'test', style: ['header', 'small'] }), 2);
assert.equal(fullStack.styleOverrides.length, 2);
assert.equal(fullStack.autopush({ anotherProperty: 'test', style: ['header', 'small'] }), 3);
assert.equal(fullStack.styleOverrides.length, 3);
assert.equal(fullStack.styleOverrides[0], 'header');
assert.equal(fullStack.styleOverrides[1], 'small');
});
Expand Down

0 comments on commit c7caabb

Please sign in to comment.