Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up StyleContextStack.autopush() for large tables #2733

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading