Guard against adding children to ended structure elements.

This commit is contained in:
Ben Schmidt 2020-11-04 01:07:52 +11:00 committed by Luiz Américo
parent 9c0aaec172
commit 057215fc8f
2 changed files with 18 additions and 0 deletions

View File

@ -48,6 +48,10 @@ class PDFStructureElement {
}
add(child) {
if (this._ended) {
throw new Error(`Cannot add child to already-ended structure element`);
}
if (!this.dictionary.data.K) {
this.dictionary.data.K = [];
}

View File

@ -412,6 +412,20 @@ EMC
`endobj`
]);
});
test('validation', () => {
let struct;
struct = document.struct('Foo').end();
expect(() => {
struct.add(document.struct('Bar'));
}).toThrow();
struct = document.struct('Foo', []);
expect(() => {
struct.add(document.struct('Bar'));
}).toThrow();
});
});
describe('accessible document', () => {