Skip to content

Commit

Permalink
section is allowed only in document root
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Dec 1, 2024
1 parent ece4927 commit 3ed1342
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ var docDefinition = {
pageOrientation: 'inherit',
pageMargins: 'inherit',
section: [
'SECTION 7',
'SECTION 6',
'Text in section with page definition as previous page. Page size, orientation and margins are inherited.'
]
},
{
header: function (currentPage, pageCount) { return 'header in section 8: ' + currentPage.toString() + ' of ' + pageCount; },
footer: function (currentPage, pageCount) { return 'footer in section 8: ' + currentPage.toString() + ' of ' + pageCount; },
section: [
'SECTION 8',
'SECTION 7',
'Text in section with page definition as defined in document.'
]
}
Expand Down
4 changes: 4 additions & 0 deletions src/DocMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class DocMeasure {
return this.measureNode(docStructure);
}

measureBlock(node) {
return this.measureNode(node);
}

measureNode(node) {
return this.styleStack.auto(node, () => {
// TODO: refactor + rethink whether this is the proper way to handle margins
Expand Down
22 changes: 16 additions & 6 deletions src/DocPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ class DocPreprocessor {
this.parentNode = null;
this.tocs = [];
this.nodeReferences = [];
return this.preprocessNode(docStructure);
return this.preprocessNode(docStructure, true);
}

preprocessNode(node) {
preprocessBlock(node) {
this.parentNode = null;
this.tocs = [];
this.nodeReferences = [];
return this.preprocessNode(node);
}

preprocessNode(node, isSectionAllowed = false) {
// expand shortcuts and casting values
if (Array.isArray(node)) {
node = { stack: node };
Expand All @@ -34,12 +41,15 @@ class DocPreprocessor {
}

if (node.section) {
// TODO: add a check that section node is in root
if (!isSectionAllowed) {
throw new Error(`Incorrect document structure, section node is only allowed at the root level of document structure: ${stringifyNode(node)}`);
}

return this.preprocessSection(node);
} else if (node.columns) {
return this.preprocessColumns(node);
} else if (node.stack) {
return this.preprocessVerticalContainer(node);
return this.preprocessVerticalContainer(node, isSectionAllowed);
} else if (node.ul) {
return this.preprocessList(node);
} else if (node.ol) {
Expand Down Expand Up @@ -83,11 +93,11 @@ class DocPreprocessor {
return node;
}

preprocessVerticalContainer(node) {
preprocessVerticalContainer(node, isSectionAllowed) {
let items = node.stack;

for (let i = 0, l = items.length; i < l; i++) {
items[i] = this.preprocessNode(items[i]);
items[i] = this.preprocessNode(items[i], isSectionAllowed);
}

return node;
Expand Down
8 changes: 4 additions & 4 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ class LayoutBuilder {

if (pageBackground) {
this.writer.beginUnbreakableBlock(pageSize.width, pageSize.height);
pageBackground = this.docPreprocessor.preprocessDocument(pageBackground);
this.processNode(this.docMeasure.measureDocument(pageBackground));
pageBackground = this.docPreprocessor.preprocessBlock(pageBackground);
this.processNode(this.docMeasure.measureBlock(pageBackground));
this.writer.commitUnbreakableBlock(0, 0);
context.backgroundLength[context.page] += pageBackground.positions.length;
}
Expand Down Expand Up @@ -247,8 +247,8 @@ class LayoutBuilder {
if (node) {
let sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.writer.context().getCurrentPage().pageMargins);
this.writer.beginUnbreakableBlock(sizes.width, sizes.height);
node = this.docPreprocessor.preprocessDocument(node);
this.processNode(this.docMeasure.measureDocument(node));
node = this.docPreprocessor.preprocessBlock(node);
this.processNode(this.docMeasure.measureBlock(node));
this.writer.commitUnbreakableBlock(sizes.x, sizes.y);
}
}
Expand Down
107 changes: 77 additions & 30 deletions tests/unit/DocPreprocessor.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const assert = require('assert');

const DocPreprocessor = require('../../js/DocPreprocessor').default;
Expand Down Expand Up @@ -79,28 +78,28 @@ describe('DocPreprocessor', function () {

it('should normalize text', function () {
var ddContent = [
{ text: 'Abc123' },
{ text: '12' },
{ text: '12.34' },
{ text: '0' },
{ text: '' },
{ text: new String('Abcdef') },
{ text: 56 },
{ text: 56.78 },
{ text: 0 },
{ text: true },
{ text: false },
{ text: 'true' },
{ text: 'false' },
{ text: [] }, // is text with nested texts
{ text: '[]' },
{ text: [1, 2, 3] }, // is text with nested texts
{ text: {} },
{ text: '{}' },
{ text: null },
{ text: 'null' },
{ text: undefined },
{ text: 'undefined' },
{text: 'Abc123'},
{text: '12'},
{text: '12.34'},
{text: '0'},
{text: ''},
{text: new String('Abcdef')},
{text: 56},
{text: 56.78},
{text: 0},
{text: true},
{text: false},
{text: 'true'},
{text: 'false'},
{text: []}, // is text with nested texts
{text: '[]'},
{text: [1, 2, 3]}, // is text with nested texts
{text: {}},
{text: '{}'},
{text: null},
{text: 'null'},
{text: undefined},
{text: 'undefined'},
];
var result = docPreprocessor.preprocessNode(ddContent);

Expand Down Expand Up @@ -138,13 +137,13 @@ describe('DocPreprocessor', function () {
it('should replace tab as 4 spaces', function () {
var ddContent = [
'a\tb',
{ text: 'a\tb' },
{text: 'a\tb'},
'a\tb\tc',
{ text: 'a\tb\tc' },
{text: 'a\tb\tc'},
{
text: [
'A\tB',
{ text: 'A\tB' },
{text: 'A\tB'},
]
}
];
Expand Down Expand Up @@ -188,8 +187,7 @@ describe('DocPreprocessor', function () {
it('should support simple toc on begin of document', function () {
var ddContent = [
{
toc: {
}
toc: {}
},
{
text: 'Header 1',
Expand Down Expand Up @@ -222,8 +220,7 @@ describe('DocPreprocessor', function () {
tocItem: true
},
{
toc: {
}
toc: {}
},
];
var result = docPreprocessor.preprocessDocument(ddContent);
Expand All @@ -239,4 +236,54 @@ describe('DocPreprocessor', function () {

});

describe('section', function () {

it('should support section', function () {
var ddContent = [
{
section: [],
},
{
section: [],
},
];
assert.doesNotThrow(function () {
docPreprocessor.preprocessDocument(ddContent);
});
});

it('should support section in stack', function () {
var ddContent = [
{
stack: [
{
section: [],
},
{
section: [],
},
]
}
];
assert.doesNotThrow(function () {
docPreprocessor.preprocessDocument(ddContent);
});
});

it('should support section only in root', function () {
var ddContent = [
{
table: {
body: [
[{section: []}],
]
}
},
];

assert.throws(() => docPreprocessor.preprocessDocument(ddContent), /Incorrect document structure, section node is only allowed at the root level of document structure/);
});

});

});

0 comments on commit 3ed1342

Please sign in to comment.