mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-21 15:48:06 +02:00
Implement a single getInheritableProperty
utility function
This function combines the logic of two separate methods into one. The loop limit is also a good thing to have for the calls in `src/core/annotation.js`. Moreover, since this is important functionality, a set of unit tests and documentation is added.
This commit is contained in:
parent
4e5eb59a33
commit
f308d73d40
4 changed files with 189 additions and 57 deletions
|
@ -14,9 +14,12 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
bytesToString, isArrayBuffer, isBool, isEmptyObj, isNum, isSpace, isString,
|
||||
log2, ReadableStream, removeNullCharacters, stringToBytes, stringToPDFString
|
||||
bytesToString, getInheritableProperty, isArrayBuffer, isBool, isEmptyObj,
|
||||
isNum, isSpace, isString, log2, ReadableStream, removeNullCharacters,
|
||||
stringToBytes, stringToPDFString
|
||||
} from '../../src/shared/util';
|
||||
import { Dict, Ref } from '../../src/core/primitives';
|
||||
import { XRefMock } from './test_utils';
|
||||
|
||||
describe('util', function() {
|
||||
describe('bytesToString', function() {
|
||||
|
@ -50,6 +53,106 @@ 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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue