Merge pull request #14577 from Snuffleupagus/rm-isRef

Remove the `isRef` helper function
This commit is contained in:
Jonas Jenwald 2022-02-19 15:47:01 +01:00 committed by GitHub
commit fbed707592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 55 additions and 67 deletions

View File

@ -39,7 +39,7 @@ import {
createDefaultAppearance, createDefaultAppearance,
parseDefaultAppearance, parseDefaultAppearance,
} from "./default_appearance.js"; } from "./default_appearance.js";
import { Dict, isDict, isName, isRef, Name, RefSet } from "./primitives.js"; import { Dict, isDict, isName, Name, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
import { bidi } from "./bidi.js"; import { bidi } from "./bidi.js";
import { Catalog } from "./catalog.js"; import { Catalog } from "./catalog.js";
@ -99,7 +99,8 @@ class AnnotationFactory {
return undefined; return undefined;
} }
const id = isRef(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`; const id =
ref instanceof Ref ? ref.toString() : `annot_${idFactory.createObjId()}`;
// Determine the annotation's subtype. // Determine the annotation's subtype.
let subtype = dict.get("Subtype"); let subtype = dict.get("Subtype");
@ -212,7 +213,7 @@ class AnnotationFactory {
return -1; return -1;
} }
const pageRef = annotDict.getRaw("P"); const pageRef = annotDict.getRaw("P");
if (!isRef(pageRef)) { if (!(pageRef instanceof Ref)) {
return -1; return -1;
} }
const pageIndex = await pdfManager.ensureCatalog("getPageIndex", [ const pageIndex = await pdfManager.ensureCatalog("getPageIndex", [
@ -391,7 +392,7 @@ class Annotation {
if (Array.isArray(kids)) { if (Array.isArray(kids)) {
const kidIds = []; const kidIds = [];
for (const kid of kids) { for (const kid of kids) {
if (isRef(kid)) { if (kid instanceof Ref) {
kidIds.push(kid.toString()); kidIds.push(kid.toString());
} }
} }
@ -1051,7 +1052,7 @@ class MarkupAnnotation extends Annotation {
if (dict.has("IRT")) { if (dict.has("IRT")) {
const rawIRT = dict.getRaw("IRT"); const rawIRT = dict.getRaw("IRT");
this.data.inReplyTo = isRef(rawIRT) ? rawIRT.toString() : null; this.data.inReplyTo = rawIRT instanceof Ref ? rawIRT.toString() : null;
const rt = dict.get("RT"); const rt = dict.get("RT");
this.data.replyType = isName(rt) ? rt.name : AnnotationReplyType.REPLY; this.data.replyType = isName(rt) ? rt.name : AnnotationReplyType.REPLY;
@ -2144,7 +2145,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
const encrypt = evaluator.xref.encrypt; const encrypt = evaluator.xref.encrypt;
if (value) { if (value) {
if (isRef(this.parent)) { if (this.parent instanceof Ref) {
const parent = evaluator.xref.fetch(this.parent); const parent = evaluator.xref.fetch(this.parent);
let parentTransform = null; let parentTransform = null;
if (encrypt) { if (encrypt) {
@ -2567,7 +2568,7 @@ class PopupAnnotation extends Annotation {
const parentSubtype = parentItem.get("Subtype"); const parentSubtype = parentItem.get("Subtype");
this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null; this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null;
const rawParent = parameters.dict.getRaw("Parent"); const rawParent = parameters.dict.getRaw("Parent");
this.data.parentId = isRef(rawParent) ? rawParent.toString() : null; this.data.parentId = rawParent instanceof Ref ? rawParent.toString() : null;
const parentRect = parentItem.getArray("Rect"); const parentRect = parentItem.getArray("Rect");
if (Array.isArray(parentRect) && parentRect.length === 4) { if (Array.isArray(parentRect) && parentRect.length === 4) {

View File

@ -39,7 +39,6 @@ import {
Dict, Dict,
isDict, isDict,
isName, isName,
isRef,
isRefsEqual, isRefsEqual,
Name, Name,
Ref, Ref,
@ -151,7 +150,7 @@ class Catalog {
get acroFormRef() { get acroFormRef() {
const value = this._catDict.getRaw("AcroForm"); const value = this._catDict.getRaw("AcroForm");
return shadow(this, "acroFormRef", isRef(value) ? value : null); return shadow(this, "acroFormRef", value instanceof Ref ? value : null);
} }
get metadata() { get metadata() {
@ -288,7 +287,7 @@ class Catalog {
return null; return null;
} }
obj = obj.getRaw("First"); obj = obj.getRaw("First");
if (!isRef(obj)) { if (!(obj instanceof Ref)) {
return null; return null;
} }
@ -346,12 +345,12 @@ class Catalog {
i.parent.items.push(outlineItem); i.parent.items.push(outlineItem);
obj = outlineDict.getRaw("First"); obj = outlineDict.getRaw("First");
if (isRef(obj) && !processed.has(obj)) { if (obj instanceof Ref && !processed.has(obj)) {
queue.push({ obj, parent: outlineItem }); queue.push({ obj, parent: outlineItem });
processed.put(obj); processed.put(obj);
} }
obj = outlineDict.getRaw("Next"); obj = outlineDict.getRaw("Next");
if (isRef(obj) && !processed.has(obj)) { if (obj instanceof Ref && !processed.has(obj)) {
queue.push({ obj, parent: i.parent }); queue.push({ obj, parent: i.parent });
processed.put(obj); processed.put(obj);
} }
@ -420,7 +419,7 @@ class Catalog {
const groupRefs = []; const groupRefs = [];
// Ensure all the optional content groups are valid. // Ensure all the optional content groups are valid.
for (const groupRef of groupsData) { for (const groupRef of groupsData) {
if (!isRef(groupRef)) { if (!(groupRef instanceof Ref)) {
continue; continue;
} }
groupRefs.push(groupRef); groupRefs.push(groupRef);
@ -451,7 +450,7 @@ class Catalog {
const onParsed = []; const onParsed = [];
if (Array.isArray(refs)) { if (Array.isArray(refs)) {
for (const value of refs) { for (const value of refs) {
if (!isRef(value)) { if (!(value instanceof Ref)) {
continue; continue;
} }
if (contentGroupRefs.includes(value)) { if (contentGroupRefs.includes(value)) {
@ -469,7 +468,7 @@ class Catalog {
const order = []; const order = [];
for (const value of refs) { for (const value of refs) {
if (isRef(value) && contentGroupRefs.includes(value)) { if (value instanceof Ref && contentGroupRefs.includes(value)) {
parsedOrderRefs.put(value); // Handle "hidden" groups, see below. parsedOrderRefs.put(value); // Handle "hidden" groups, see below.
order.push(value.toString()); order.push(value.toString());
@ -1371,7 +1370,7 @@ class Catalog {
let found = false; let found = false;
for (let i = 0, ii = kids.length; i < ii; i++) { for (let i = 0, ii = kids.length; i < ii; i++) {
const kid = kids[i]; const kid = kids[i];
if (!isRef(kid)) { if (!(kid instanceof Ref)) {
throw new FormatError("Kid must be a reference."); throw new FormatError("Kid must be a reference.");
} }
if (isRefsEqual(kid, kidRef)) { if (isRefsEqual(kid, kidRef)) {
@ -1479,7 +1478,7 @@ class Catalog {
const fields = []; const fields = [];
const refs = []; const refs = [];
for (const obj of action.get("Fields") || []) { for (const obj of action.get("Fields") || []) {
if (isRef(obj)) { if (obj instanceof Ref) {
refs.push(obj.toString()); refs.push(obj.toString());
} else if (isString(obj)) { } else if (isString(obj)) {
fields.push(stringToPDFString(obj)); fields.push(stringToPDFString(obj));

View File

@ -22,7 +22,7 @@ import {
stringToPDFString, stringToPDFString,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
import { Dict, isName, isRef, RefSet } from "./primitives.js"; import { Dict, isName, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
function getLookupTableFactory(initializer) { function getLookupTableFactory(initializer) {
@ -316,7 +316,7 @@ function _collectJS(entry, xref, list, parents) {
} }
let parent = null; let parent = null;
if (isRef(entry)) { if (entry instanceof Ref) {
if (parents.has(entry)) { if (parents.has(entry)) {
// If we've already found entry then we've a cycle. // If we've already found entry then we've a cycle.
return; return;

View File

@ -44,7 +44,7 @@ import {
XRefEntryException, XRefEntryException,
XRefParseException, XRefParseException,
} from "./core_utils.js"; } from "./core_utils.js";
import { Dict, isDict, isName, isRef, Name, Ref } from "./primitives.js"; import { Dict, isDict, isName, Name, Ref } from "./primitives.js";
import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js"; import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js";
import { NullStream, Stream } from "./stream.js"; import { NullStream, Stream } from "./stream.js";
import { AnnotationFactory } from "./annotation.js"; import { AnnotationFactory } from "./annotation.js";
@ -1548,7 +1548,12 @@ class PDFDocument {
return shadow(this, "calculationOrderIds", null); return shadow(this, "calculationOrderIds", null);
} }
const ids = calculationOrder.filter(isRef).map(ref => ref.toString()); const ids = [];
for (const id of calculationOrder) {
if (id instanceof Ref) {
ids.push(id.toString());
}
}
if (ids.length === 0) { if (ids.length === 0) {
return shadow(this, "calculationOrderIds", null); return shadow(this, "calculationOrderIds", null);
} }

View File

@ -41,7 +41,6 @@ import {
EOF, EOF,
isDict, isDict,
isName, isName,
isRef,
Name, Name,
Ref, Ref,
RefSet, RefSet,
@ -1119,7 +1118,7 @@ class PartialEvaluator {
let fontRef; let fontRef;
if (font) { if (font) {
// Loading by ref. // Loading by ref.
if (!isRef(font)) { if (!(font instanceof Ref)) {
throw new FormatError('The "font" object should be a reference.'); throw new FormatError('The "font" object should be a reference.');
} }
fontRef = font; fontRef = font;
@ -1185,7 +1184,7 @@ class PartialEvaluator {
} }
const { descriptor, hash } = preEvaluatedFont; const { descriptor, hash } = preEvaluatedFont;
const fontRefIsRef = isRef(fontRef); const fontRefIsRef = fontRef instanceof Ref;
let fontID; let fontID;
if (fontRefIsRef) { if (fontRefIsRef) {
fontID = `f${fontRef.toString()}`; fontID = `f${fontRef.toString()}`;
@ -1482,7 +1481,7 @@ class PartialEvaluator {
currentResult.push(nestedResult); currentResult.push(nestedResult);
// Recursively parse a subarray. // Recursively parse a subarray.
this._parseVisibilityExpression(object, nestingCounter, nestedResult); this._parseVisibilityExpression(object, nestingCounter, nestedResult);
} else if (isRef(raw)) { } else if (raw instanceof Ref) {
// Reference to an OCG dictionary. // Reference to an OCG dictionary.
currentResult.push(raw.toString()); currentResult.push(raw.toString());
} }
@ -1542,7 +1541,7 @@ class PartialEvaluator {
: null, : null,
expression: null, expression: null,
}; };
} else if (isRef(optionalContentGroups)) { } else if (optionalContentGroups instanceof Ref) {
return { return {
type: optionalContentType, type: optionalContentType,
id: optionalContentGroups.toString(), id: optionalContentGroups.toString(),
@ -3783,13 +3782,13 @@ class PartialEvaluator {
const encoding = baseDict.getRaw("Encoding"); const encoding = baseDict.getRaw("Encoding");
if (isName(encoding)) { if (isName(encoding)) {
hash.update(encoding.name); hash.update(encoding.name);
} else if (isRef(encoding)) { } else if (encoding instanceof Ref) {
hash.update(encoding.toString()); hash.update(encoding.toString());
} else if (isDict(encoding)) { } else if (isDict(encoding)) {
for (const entry of encoding.getRawValues()) { for (const entry of encoding.getRawValues()) {
if (isName(entry)) { if (isName(entry)) {
hash.update(entry.name); hash.update(entry.name);
} else if (isRef(entry)) { } else if (entry instanceof Ref) {
hash.update(entry.toString()); hash.update(entry.toString());
} else if (Array.isArray(entry)) { } else if (Array.isArray(entry)) {
// 'Differences' array (fixes bug1157493.pdf). // 'Differences' array (fixes bug1157493.pdf).
@ -3800,7 +3799,7 @@ class PartialEvaluator {
const diffEntry = entry[j]; const diffEntry = entry[j];
if (isName(diffEntry)) { if (isName(diffEntry)) {
diffBuf[j] = diffEntry.name; diffBuf[j] = diffEntry.name;
} else if (isNum(diffEntry) || isRef(diffEntry)) { } else if (isNum(diffEntry) || diffEntry instanceof Ref) {
diffBuf[j] = diffEntry.toString(); diffBuf[j] = diffEntry.toString();
} }
} }
@ -3830,7 +3829,7 @@ class PartialEvaluator {
if (Array.isArray(widths)) { if (Array.isArray(widths)) {
const widthsBuf = []; const widthsBuf = [];
for (const entry of widths) { for (const entry of widths) {
if (isNum(entry) || isRef(entry)) { if (isNum(entry) || entry instanceof Ref) {
widthsBuf.push(entry.toString()); widthsBuf.push(entry.toString());
} }
} }
@ -3844,12 +3843,12 @@ class PartialEvaluator {
if (Array.isArray(compositeWidths)) { if (Array.isArray(compositeWidths)) {
const widthsBuf = []; const widthsBuf = [];
for (const entry of compositeWidths) { for (const entry of compositeWidths) {
if (isNum(entry) || isRef(entry)) { if (isNum(entry) || entry instanceof Ref) {
widthsBuf.push(entry.toString()); widthsBuf.push(entry.toString());
} else if (Array.isArray(entry)) { } else if (Array.isArray(entry)) {
const subWidthsBuf = []; const subWidthsBuf = [];
for (const element of entry) { for (const element of entry) {
if (isNum(element) || isRef(element)) { if (isNum(element) || element instanceof Ref) {
subWidthsBuf.push(element.toString()); subWidthsBuf.push(element.toString());
} }
} }

View File

@ -394,10 +394,6 @@ function isDict(v, type) {
); );
} }
function isRef(v) {
return v instanceof Ref;
}
function isRefsEqual(v1, v2) { function isRefsEqual(v1, v2) {
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
@ -426,7 +422,6 @@ export {
isCmd, isCmd,
isDict, isDict,
isName, isName,
isRef,
isRefsEqual, isRefsEqual,
Name, Name,
Ref, Ref,

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { isDict, isName, isRef } from "./primitives.js"; import { isDict, isName, Ref } from "./primitives.js";
import { isString, stringToPDFString, warn } from "../shared/util.js"; import { isString, stringToPDFString, warn } from "../shared/util.js";
import { NumberTree } from "./name_number_tree.js"; import { NumberTree } from "./name_number_tree.js";
@ -75,7 +75,7 @@ class StructElementNode {
parseKids() { parseKids() {
let pageObjId = null; let pageObjId = null;
const objRef = this.dict.getRaw("Pg"); const objRef = this.dict.getRaw("Pg");
if (isRef(objRef)) { if (objRef instanceof Ref) {
pageObjId = objRef.toString(); pageObjId = objRef.toString();
} }
const kids = this.dict.get("K"); const kids = this.dict.get("K");
@ -110,7 +110,7 @@ class StructElementNode {
// Find the dictionary for the kid. // Find the dictionary for the kid.
let kidDict = null; let kidDict = null;
if (isRef(kid)) { if (kid instanceof Ref) {
kidDict = this.dict.xref.fetch(kid); kidDict = this.dict.xref.fetch(kid);
} else if (isDict(kid)) { } else if (isDict(kid)) {
kidDict = kid; kidDict = kid;
@ -119,7 +119,7 @@ class StructElementNode {
return null; return null;
} }
const pageRef = kidDict.getRaw("Pg"); const pageRef = kidDict.getRaw("Pg");
if (isRef(pageRef)) { if (pageRef instanceof Ref) {
pageObjId = pageRef.toString(); pageObjId = pageRef.toString();
} }
@ -130,9 +130,10 @@ class StructElementNode {
} }
return new StructElement({ return new StructElement({
type: StructElementType.STREAM_CONTENT, type: StructElementType.STREAM_CONTENT,
refObjId: isRef(kidDict.getRaw("Stm")) refObjId:
? kidDict.getRaw("Stm").toString() kidDict.getRaw("Stm") instanceof Ref
: null, ? kidDict.getRaw("Stm").toString()
: null,
pageObjId, pageObjId,
mcid: kidDict.get("MCID"), mcid: kidDict.get("MCID"),
}); });
@ -144,9 +145,10 @@ class StructElementNode {
} }
return new StructElement({ return new StructElement({
type: StructElementType.OBJECT, type: StructElementType.OBJECT,
refObjId: isRef(kidDict.getRaw("Obj")) refObjId:
? kidDict.getRaw("Obj").toString() kidDict.getRaw("Obj") instanceof Ref
: null, ? kidDict.getRaw("Obj").toString()
: null,
pageObjId, pageObjId,
}); });
} }
@ -203,7 +205,7 @@ class StructTreePage {
} }
const map = new Map(); const map = new Map();
for (const ref of parentArray) { for (const ref of parentArray) {
if (isRef(ref)) { if (ref instanceof Ref) {
this.addNode(this.rootDict.xref.fetch(ref), map); this.addNode(this.rootDict.xref.fetch(ref), map);
} }
} }

View File

@ -14,7 +14,7 @@
*/ */
import { bytesToString, escapeString, warn } from "../shared/util.js"; import { bytesToString, escapeString, warn } from "../shared/util.js";
import { Dict, isDict, isName, isRef, Name } from "./primitives.js"; import { Dict, isDict, isName, Name, Ref } from "./primitives.js";
import { escapePDFName, parseXFAPath } from "./core_utils.js"; import { escapePDFName, parseXFAPath } from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js"; import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
@ -73,7 +73,7 @@ function numberToString(value) {
function writeValue(value, buffer, transform) { function writeValue(value, buffer, transform) {
if (isName(value)) { if (isName(value)) {
buffer.push(`/${escapePDFName(value.name)}`); buffer.push(`/${escapePDFName(value.name)}`);
} else if (isRef(value)) { } else if (value instanceof Ref) {
buffer.push(`${value.num} ${value.gen} R`); buffer.push(`${value.num} ${value.gen} R`);
} else if (Array.isArray(value)) { } else if (Array.isArray(value)) {
writeArray(value, buffer, transform); writeArray(value, buffer, transform);

View File

@ -19,7 +19,6 @@ import {
isCmd, isCmd,
isDict, isDict,
isName, isName,
isRef,
isRefsEqual, isRefsEqual,
Name, Name,
Ref, Ref,
@ -534,18 +533,6 @@ describe("primitives", function () {
}); });
}); });
describe("isRef", function () {
it("handles non-refs", function () {
const nonRef = {};
expect(isRef(nonRef)).toEqual(false);
});
it("handles refs", function () {
const ref = Ref.get(1, 0);
expect(isRef(ref)).toEqual(true);
});
});
describe("isRefsEqual", function () { describe("isRefsEqual", function () {
it("should handle Refs pointing to the same object", function () { it("should handle Refs pointing to the same object", function () {
const ref1 = Ref.get(1, 0); const ref1 = Ref.get(1, 0);

View File

@ -13,11 +13,11 @@
* limitations under the License. * limitations under the License.
*/ */
import { isRef, Ref } from "../../src/core/primitives.js";
import { Page, PDFDocument } from "../../src/core/document.js"; import { Page, PDFDocument } from "../../src/core/document.js";
import { assert } from "../../src/shared/util.js"; import { assert } from "../../src/shared/util.js";
import { DocStats } from "../../src/core/core_utils.js"; import { DocStats } from "../../src/core/core_utils.js";
import { isNodeJS } from "../../src/shared/is_node.js"; import { isNodeJS } from "../../src/shared/is_node.js";
import { Ref } from "../../src/core/primitives.js";
import { StringStream } from "../../src/core/stream.js"; import { StringStream } from "../../src/core/stream.js";
const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/"; const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
@ -106,10 +106,10 @@ class XRefMock {
} }
fetchIfRef(obj) { fetchIfRef(obj) {
if (!isRef(obj)) { if (obj instanceof Ref) {
return obj; return this.fetch(obj);
} }
return this.fetch(obj); return obj;
} }
async fetchIfRefAsync(obj) { async fetchIfRefAsync(obj) {