1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Add a getDocId method to the idFactory, in Page instances, to avoid passing around PDFManager instances unnecessarily (PR 7941 follow-up)

This way we can avoid manually building a "document id" in multiple places in `evaluator.js`, and it also let's us avoid passing in an otherwise unnecessary `PDFManager` instance when creating a `PartialEvaluator`.
This commit is contained in:
Jonas Jenwald 2019-04-20 12:36:49 +02:00
parent 55d9b35d37
commit 34952b732e
7 changed files with 36 additions and 46 deletions

View file

@ -20,10 +20,10 @@ import {
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
AnnotationType, stringToBytes, stringToUTF8String
} from '../../src/shared/util';
import { createIdFactory, XRefMock } from './test_utils';
import { Dict, Name, Ref } from '../../src/core/primitives';
import { Lexer, Parser } from '../../src/core/parser';
import { StringStream } from '../../src/core/stream';
import { XRefMock } from './test_utils';
describe('annotation', function() {
class PDFManagerMock {
@ -43,26 +43,13 @@ describe('annotation', function() {
}
}
class IdFactoryMock {
constructor(params) {
this.uniquePrefix = params.prefix || 'p0_';
this.idCounters = {
obj: params.startObjId || 0,
};
}
createObjId() {
return this.uniquePrefix + (++this.idCounters.obj);
}
}
let pdfManagerMock, idFactoryMock;
beforeAll(function(done) {
pdfManagerMock = new PDFManagerMock({
docBaseUrl: null,
});
idFactoryMock = new IdFactoryMock({ });
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
done();
});
@ -97,10 +84,7 @@ describe('annotation', function() {
annotationDict.set('Subtype', Name.get('Link'));
const xref = new XRefMock();
const idFactory = new IdFactoryMock({
prefix: 'p0_',
startObjId: 0,
});
const idFactory = createIdFactory(/* pageIndex = */ 0);
const annotation1 = AnnotationFactory.create(xref, annotationDict,
pdfManagerMock, idFactory).then(({ data, }) => {

View file

@ -13,30 +13,25 @@
* limitations under the License.
*/
import { Page } from '../../src/core/document';
import { createIdFactory } from './test_utils';
describe('document', function () {
describe('Page', function () {
it('should create correct objId using the idFactory', function () {
var page1 = new Page({
pdfManager: { },
pageIndex: 0,
});
var page2 = new Page({
pdfManager: { },
pageIndex: 1,
});
var idFactory1 = page1.idFactory, idFactory2 = page2.idFactory;
const idFactory1 = createIdFactory(/* pageIndex = */ 0);
const idFactory2 = createIdFactory(/* pageIndex = */ 1);
expect(idFactory1.createObjId()).toEqual('p0_1');
expect(idFactory1.createObjId()).toEqual('p0_2');
expect(idFactory1.getDocId()).toEqual('g_d0');
expect(idFactory2.createObjId()).toEqual('p1_1');
expect(idFactory2.createObjId()).toEqual('p1_2');
expect(idFactory2.getDocId()).toEqual('g_d0');
expect(idFactory1.createObjId()).toEqual('p0_3');
expect(idFactory1.createObjId()).toEqual('p0_4');
expect(idFactory1.getDocId()).toEqual('g_d0');
});
});
});

View file

@ -13,13 +13,13 @@
* limitations under the License.
*/
import { createIdFactory, XRefMock } from './test_utils';
import { Dict, Name } from '../../src/core/primitives';
import { FormatError, OPS } from '../../src/shared/util';
import { Stream, StringStream } from '../../src/core/stream';
import { OperatorList } from '../../src/core/operator_list';
import { PartialEvaluator } from '../../src/core/evaluator';
import { WorkerTask } from '../../src/core/worker';
import { XRefMock } from './test_utils';
describe('evaluator', function() {
function HandlerMock() {
@ -37,8 +37,6 @@ describe('evaluator', function() {
},
};
function PdfManagerMock() { }
function runOperatorListCheck(evaluator, stream, resources, callback) {
var result = new OperatorList();
var task = new WorkerTask('OperatorListCheck');
@ -58,10 +56,10 @@ describe('evaluator', function() {
beforeAll(function(done) {
partialEvaluator = new PartialEvaluator({
pdfManager: new PdfManagerMock(),
xref: new XRefMock(),
handler: new HandlerMock(),
pageIndex: 0,
idFactory: createIdFactory(/* pageIndex = */ 0),
});
done();
});

View file

@ -16,6 +16,7 @@
import { assert, CMapCompressionType } from '../../src/shared/util';
import isNodeJS from '../../src/shared/is_node';
import { isRef } from '../../src/core/primitives';
import { Page } from '../../src/core/document';
class DOMFileReaderFactory {
static async fetch(params) {
@ -158,6 +159,18 @@ class XRefMock {
}
}
function createIdFactory(pageIndex) {
const page = new Page({
pdfManager: {
get docId() {
return 'd0';
},
},
pageIndex,
});
return page.idFactory;
}
export {
DOMFileReaderFactory,
NodeFileReaderFactory,
@ -166,4 +179,5 @@ export {
XRefMock,
buildGetDocumentParams,
TEST_PDFS_PATH,
createIdFactory,
};