Skip to content

Commit 5bc5791

Browse files
Merge pull request #21257 from Snuffleupagus/deepCompare-Refs
Update the `deepCompare` helper function to handle `Ref`s and `Name`s correctly
2 parents 6bbcb46 + 326df1f commit 5bc5791

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/core/core_utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
Util,
2424
warn,
2525
} from "../shared/util.js";
26-
import { Dict, isName, Ref, RefSet } from "./primitives.js";
26+
import { Dict, isName, isRefsEqual, Name, Ref, RefSet } from "./primitives.js";
2727
import { BaseStream } from "./base_stream.js";
2828

2929
const PDF_VERSION_REGEXP = /^[1-9]\.\d$/;
@@ -210,6 +210,13 @@ function deepCompare(a, b) {
210210
if (a === b) {
211211
return true;
212212
}
213+
if (a instanceof Ref && b instanceof Ref) {
214+
return isRefsEqual(a, b);
215+
}
216+
if (a instanceof Name && b instanceof Name) {
217+
return a.name === b.name;
218+
}
219+
213220
if (a instanceof Dict && b instanceof Dict) {
214221
if (a.size !== b.size) {
215222
return false;

test/unit/core_utils_spec.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import {
3232
toRomanNumerals,
3333
validateCSSFont,
3434
} from "../../src/core/core_utils.js";
35-
import { Dict, Ref } from "../../src/core/primitives.js";
35+
import {
36+
clearPrimitiveCaches,
37+
Dict,
38+
Name,
39+
Ref,
40+
} from "../../src/core/primitives.js";
3641
import { XRefMock } from "./test_utils.js";
3742

3843
describe("core_utils", function () {
@@ -499,6 +504,20 @@ describe("core_utils", function () {
499504
expect(deepCompare(a, b)).toBeTrue();
500505
});
501506

507+
it("should return true for Dicts with same Ref values, after clearing cached Refs", function () {
508+
const refA = Ref.get(10, 0);
509+
clearPrimitiveCaches();
510+
const refB = Ref.get(10, 0);
511+
// Ensure that Ref-objects are not identical, after clearing the cache.
512+
expect(refA).not.toBe(refB);
513+
514+
const a = new Dict();
515+
a.set("Foo", refA);
516+
const b = new Dict();
517+
b.set("Foo", refB);
518+
expect(deepCompare(a, b)).toBeTrue();
519+
});
520+
502521
it("should return false for Dicts with different Ref values", function () {
503522
const a = new Dict();
504523
a.set("Foo", Ref.get(10, 0));
@@ -556,6 +575,30 @@ describe("core_utils", function () {
556575
it("should return false for arrays with different values", function () {
557576
expect(deepCompare([Ref.get(1, 0)], [Ref.get(2, 0)])).toBeFalse();
558577
});
578+
579+
it("should return true for equal Names", function () {
580+
const name1 = Name.get("name"),
581+
name2 = Name.get("name");
582+
expect(name1).toBe(name2); // Names are cached.
583+
584+
expect(deepCompare(name1, name2)).toBeTrue();
585+
});
586+
587+
it("should return false for different Names", function () {
588+
const name1 = Name.get("name"),
589+
name2 = Name.get("otherName");
590+
expect(deepCompare(name1, name2)).toBeFalse();
591+
});
592+
593+
it("should return true for equal Names, after clearing cached Names", function () {
594+
const name1 = Name.get("name");
595+
clearPrimitiveCaches();
596+
const name2 = Name.get("name");
597+
// Ensure that Name-objects are not identical, after clearing the cache.
598+
expect(name1).not.toBe(name2);
599+
600+
expect(deepCompare(name1, name2)).toBeTrue();
601+
});
559602
});
560603

561604
describe("getModificationDate", function () {

0 commit comments

Comments
 (0)