mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-21 15:48:06 +02:00
Move worker-thread only functions from src/shared/util.js
and into a new src/core/core_utils.js
file
The `src/shared/util.js` file is being bundled into both the `pdf.js` and `pdf.worker.js` files, meaning that its code is by definition duplicated. Some main-thread only utility functions have already been moved to a separate `src/display/display_utils.js` file, and this patch simply extends that concept to utility functions which are used *only* on the worker-thread. Note in particular the `getInheritableProperty` function, which expects a `Dict` as input and thus *cannot* possibly ever be used on the main-thread.
This commit is contained in:
parent
a1f7517996
commit
db5dc14158
21 changed files with 359 additions and 308 deletions
|
@ -12,6 +12,7 @@
|
|||
"cff_parser_spec.js",
|
||||
"cmap_spec.js",
|
||||
"colorspace_spec.js",
|
||||
"core_utils_spec.js",
|
||||
"crypto_spec.js",
|
||||
"display_svg_spec.js",
|
||||
"display_utils_spec.js",
|
||||
|
|
158
test/unit/core_utils_spec.js
Normal file
158
test/unit/core_utils_spec.js
Normal file
|
@ -0,0 +1,158 @@
|
|||
/* Copyright 2019 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Dict, Ref } from '../../src/core/primitives';
|
||||
import {
|
||||
getInheritableProperty, toRomanNumerals
|
||||
} from '../../src/core/core_utils';
|
||||
import { XRefMock } from './test_utils';
|
||||
|
||||
describe('core_utils', function() {
|
||||
describe('getInheritableProperty', function() {
|
||||
it('handles non-dictionary arguments', function() {
|
||||
expect(getInheritableProperty({ dict: null, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
expect(getInheritableProperty({ dict: undefined, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
});
|
||||
|
||||
it('handles dictionaries that do not contain the property', function() {
|
||||
// Empty dictionary.
|
||||
const emptyDict = new Dict();
|
||||
expect(getInheritableProperty({ dict: emptyDict, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
|
||||
// Filled dictionary with a different property.
|
||||
const filledDict = new Dict();
|
||||
filledDict.set('bar', 'baz');
|
||||
expect(getInheritableProperty({ dict: filledDict, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
});
|
||||
|
||||
it('fetches the property if it is not inherited', function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const dict = new Dict(xref);
|
||||
|
||||
// Regular values should be fetched.
|
||||
dict.set('foo', 'bar');
|
||||
expect(getInheritableProperty({ dict, key: 'foo', })).toEqual('bar');
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
dict.set('baz', ['qux', ref]);
|
||||
expect(getInheritableProperty({ dict, key: 'baz', getArray: true, }))
|
||||
.toEqual(['qux', 'quux']);
|
||||
});
|
||||
|
||||
it('fetches the property if it is inherited and present on one level',
|
||||
function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const firstDict = new Dict(xref);
|
||||
const secondDict = new Dict(xref);
|
||||
firstDict.set('Parent', secondDict);
|
||||
|
||||
// Regular values should be fetched.
|
||||
secondDict.set('foo', 'bar');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo', }))
|
||||
.toEqual('bar');
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
secondDict.set('baz', ['qux', ref]);
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'baz',
|
||||
getArray: true, }))
|
||||
.toEqual(['qux', 'quux']);
|
||||
});
|
||||
|
||||
it('fetches the property if it is inherited and present on multiple levels',
|
||||
function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const firstDict = new Dict(xref);
|
||||
const secondDict = new Dict(xref);
|
||||
firstDict.set('Parent', secondDict);
|
||||
|
||||
// Regular values should be fetched.
|
||||
firstDict.set('foo', 'bar1');
|
||||
secondDict.set('foo', 'bar2');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo', }))
|
||||
.toEqual('bar1');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo',
|
||||
getArray: false, stopWhenFound: false, }))
|
||||
.toEqual(['bar1', 'bar2']);
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
firstDict.set('baz', ['qux1', ref]);
|
||||
secondDict.set('baz', ['qux2', ref]);
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'baz',
|
||||
getArray: true, stopWhenFound: false, }))
|
||||
.toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
|
||||
});
|
||||
|
||||
it('stops searching when the loop limit is reached', function() {
|
||||
const dict = new Dict();
|
||||
let currentDict = dict;
|
||||
let parentDict = null;
|
||||
for (let i = 0; i < 150; i++) { // Exceeds the loop limit of 100.
|
||||
parentDict = new Dict();
|
||||
currentDict.set('Parent', parentDict);
|
||||
currentDict = parentDict;
|
||||
}
|
||||
parentDict.set('foo', 'bar'); // Never found because of loop limit.
|
||||
expect(getInheritableProperty({ dict, key: 'foo', })).toEqual(undefined);
|
||||
|
||||
dict.set('foo', 'baz');
|
||||
expect(getInheritableProperty({ dict, key: 'foo', getArray: false,
|
||||
stopWhenFound: false, }))
|
||||
.toEqual(['baz']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toRomanNumerals', function() {
|
||||
it('handles invalid arguments', function() {
|
||||
for (const input of ['foo', -1, 0]) {
|
||||
expect(function() {
|
||||
toRomanNumerals(input);
|
||||
}).toThrow(new Error('The number should be a positive integer.'));
|
||||
}
|
||||
});
|
||||
|
||||
it('converts numbers to uppercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1)).toEqual('I');
|
||||
expect(toRomanNumerals(6)).toEqual('VI');
|
||||
expect(toRomanNumerals(7)).toEqual('VII');
|
||||
expect(toRomanNumerals(8)).toEqual('VIII');
|
||||
expect(toRomanNumerals(10)).toEqual('X');
|
||||
expect(toRomanNumerals(40)).toEqual('XL');
|
||||
expect(toRomanNumerals(100)).toEqual('C');
|
||||
expect(toRomanNumerals(500)).toEqual('D');
|
||||
expect(toRomanNumerals(1000)).toEqual('M');
|
||||
expect(toRomanNumerals(2019)).toEqual('MMXIX');
|
||||
});
|
||||
|
||||
it('converts numbers to lowercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1, /* lowercase = */ true)).toEqual('i');
|
||||
expect(toRomanNumerals(6, /* lowercase = */ true)).toEqual('vi');
|
||||
expect(toRomanNumerals(7, /* lowercase = */ true)).toEqual('vii');
|
||||
expect(toRomanNumerals(8, /* lowercase = */ true)).toEqual('viii');
|
||||
expect(toRomanNumerals(10, /* lowercase = */ true)).toEqual('x');
|
||||
expect(toRomanNumerals(40, /* lowercase = */ true)).toEqual('xl');
|
||||
expect(toRomanNumerals(100, /* lowercase = */ true)).toEqual('c');
|
||||
expect(toRomanNumerals(500, /* lowercase = */ true)).toEqual('d');
|
||||
expect(toRomanNumerals(1000, /* lowercase = */ true)).toEqual('m');
|
||||
expect(toRomanNumerals(2019, /* lowercase = */ true)).toEqual('mmxix');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -53,6 +53,7 @@ function initializePDFJS(callback) {
|
|||
'pdfjs-test/unit/cff_parser_spec',
|
||||
'pdfjs-test/unit/cmap_spec',
|
||||
'pdfjs-test/unit/colorspace_spec',
|
||||
'pdfjs-test/unit/core_utils_spec',
|
||||
'pdfjs-test/unit/crypto_spec',
|
||||
'pdfjs-test/unit/custom_spec',
|
||||
'pdfjs-test/unit/display_svg_spec',
|
||||
|
|
|
@ -14,13 +14,11 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
bytesToString, createPromiseCapability, createValidAbsoluteUrl,
|
||||
getInheritableProperty, isArrayBuffer, isBool, isEmptyObj, isNum,
|
||||
isSameOrigin, isSpace, isString, log2, ReadableStream, removeNullCharacters,
|
||||
string32, stringToBytes, stringToPDFString, toRomanNumerals, URL
|
||||
bytesToString, createPromiseCapability, createValidAbsoluteUrl, isArrayBuffer,
|
||||
isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2,
|
||||
ReadableStream, removeNullCharacters, string32, stringToBytes,
|
||||
stringToPDFString, URL
|
||||
} from '../../src/shared/util';
|
||||
import { Dict, Ref } from '../../src/core/primitives';
|
||||
import { XRefMock } from './test_utils';
|
||||
|
||||
describe('util', function() {
|
||||
describe('bytesToString', function() {
|
||||
|
@ -54,106 +52,6 @@ describe('util', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getInheritableProperty', function() {
|
||||
it('handles non-dictionary arguments', function() {
|
||||
expect(getInheritableProperty({ dict: null, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
expect(getInheritableProperty({ dict: undefined, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
});
|
||||
|
||||
it('handles dictionaries that do not contain the property', function() {
|
||||
// Empty dictionary.
|
||||
const emptyDict = new Dict();
|
||||
expect(getInheritableProperty({ dict: emptyDict, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
|
||||
// Filled dictionary with a different property.
|
||||
const filledDict = new Dict();
|
||||
filledDict.set('bar', 'baz');
|
||||
expect(getInheritableProperty({ dict: filledDict, key: 'foo', }))
|
||||
.toEqual(undefined);
|
||||
});
|
||||
|
||||
it('fetches the property if it is not inherited', function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const dict = new Dict(xref);
|
||||
|
||||
// Regular values should be fetched.
|
||||
dict.set('foo', 'bar');
|
||||
expect(getInheritableProperty({ dict, key: 'foo', })).toEqual('bar');
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
dict.set('baz', ['qux', ref]);
|
||||
expect(getInheritableProperty({ dict, key: 'baz', getArray: true, }))
|
||||
.toEqual(['qux', 'quux']);
|
||||
});
|
||||
|
||||
it('fetches the property if it is inherited and present on one level',
|
||||
function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const firstDict = new Dict(xref);
|
||||
const secondDict = new Dict(xref);
|
||||
firstDict.set('Parent', secondDict);
|
||||
|
||||
// Regular values should be fetched.
|
||||
secondDict.set('foo', 'bar');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo', }))
|
||||
.toEqual('bar');
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
secondDict.set('baz', ['qux', ref]);
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'baz',
|
||||
getArray: true, }))
|
||||
.toEqual(['qux', 'quux']);
|
||||
});
|
||||
|
||||
it('fetches the property if it is inherited and present on multiple levels',
|
||||
function() {
|
||||
const ref = new Ref(10, 0);
|
||||
const xref = new XRefMock([{ ref, data: 'quux', }]);
|
||||
const firstDict = new Dict(xref);
|
||||
const secondDict = new Dict(xref);
|
||||
firstDict.set('Parent', secondDict);
|
||||
|
||||
// Regular values should be fetched.
|
||||
firstDict.set('foo', 'bar1');
|
||||
secondDict.set('foo', 'bar2');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo', }))
|
||||
.toEqual('bar1');
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'foo',
|
||||
getArray: false, stopWhenFound: false, }))
|
||||
.toEqual(['bar1', 'bar2']);
|
||||
|
||||
// Array value should be fetched (with references resolved).
|
||||
firstDict.set('baz', ['qux1', ref]);
|
||||
secondDict.set('baz', ['qux2', ref]);
|
||||
expect(getInheritableProperty({ dict: firstDict, key: 'baz',
|
||||
getArray: true, stopWhenFound: false, }))
|
||||
.toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
|
||||
});
|
||||
|
||||
it('stops searching when the loop limit is reached', function() {
|
||||
const dict = new Dict();
|
||||
let currentDict = dict;
|
||||
let parentDict = null;
|
||||
for (let i = 0; i < 150; i++) { // Exceeds the loop limit of 100.
|
||||
parentDict = new Dict();
|
||||
currentDict.set('Parent', parentDict);
|
||||
currentDict = parentDict;
|
||||
}
|
||||
parentDict.set('foo', 'bar'); // Never found because of loop limit.
|
||||
expect(getInheritableProperty({ dict, key: 'foo', })).toEqual(undefined);
|
||||
|
||||
dict.set('foo', 'baz');
|
||||
expect(getInheritableProperty({ dict, key: 'foo', getArray: false,
|
||||
stopWhenFound: false, }))
|
||||
.toEqual(['baz']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isArrayBuffer', function() {
|
||||
it('handles array buffer values', function() {
|
||||
expect(isArrayBuffer(new ArrayBuffer(0))).toEqual(true);
|
||||
|
@ -321,42 +219,6 @@ describe('util', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toRomanNumerals', function() {
|
||||
it('handles invalid arguments', function() {
|
||||
for (const input of ['foo', -1, 0]) {
|
||||
expect(function() {
|
||||
toRomanNumerals(input);
|
||||
}).toThrow(new Error('The number should be a positive integer.'));
|
||||
}
|
||||
});
|
||||
|
||||
it('converts numbers to uppercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1)).toEqual('I');
|
||||
expect(toRomanNumerals(6)).toEqual('VI');
|
||||
expect(toRomanNumerals(7)).toEqual('VII');
|
||||
expect(toRomanNumerals(8)).toEqual('VIII');
|
||||
expect(toRomanNumerals(10)).toEqual('X');
|
||||
expect(toRomanNumerals(40)).toEqual('XL');
|
||||
expect(toRomanNumerals(100)).toEqual('C');
|
||||
expect(toRomanNumerals(500)).toEqual('D');
|
||||
expect(toRomanNumerals(1000)).toEqual('M');
|
||||
expect(toRomanNumerals(2019)).toEqual('MMXIX');
|
||||
});
|
||||
|
||||
it('converts numbers to lowercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1, /* lowercase = */ true)).toEqual('i');
|
||||
expect(toRomanNumerals(6, /* lowercase = */ true)).toEqual('vi');
|
||||
expect(toRomanNumerals(7, /* lowercase = */ true)).toEqual('vii');
|
||||
expect(toRomanNumerals(8, /* lowercase = */ true)).toEqual('viii');
|
||||
expect(toRomanNumerals(10, /* lowercase = */ true)).toEqual('x');
|
||||
expect(toRomanNumerals(40, /* lowercase = */ true)).toEqual('xl');
|
||||
expect(toRomanNumerals(100, /* lowercase = */ true)).toEqual('c');
|
||||
expect(toRomanNumerals(500, /* lowercase = */ true)).toEqual('d');
|
||||
expect(toRomanNumerals(1000, /* lowercase = */ true)).toEqual('m');
|
||||
expect(toRomanNumerals(2019, /* lowercase = */ true)).toEqual('mmxix');
|
||||
});
|
||||
});
|
||||
|
||||
describe('URL', function() {
|
||||
it('should return an Object', function() {
|
||||
const url = new URL('https://example.com');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue