Skip to content

Commit 26200ce

Browse files
committed
Refactor some xfa*** getters in document.js
- it's a follow-up of PR #14735.
1 parent 38e9a46 commit 26200ce

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

src/core/dataset_reader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DatasetReader {
5252
} else {
5353
const parser = new DatasetXMLParser({ hasAttributes: true });
5454
try {
55-
parser.parseFromString(data.xdp);
55+
parser.parseFromString(data["xdp:xdp"]);
5656
} catch (_) {}
5757
this.node = parser.node;
5858
}

src/core/document.js

+54-62
Original file line numberDiff line numberDiff line change
@@ -821,76 +821,30 @@ class PDFDocument {
821821
});
822822
}
823823

824-
get xfaDatasets() {
824+
get xfaStreams() {
825825
const acroForm = this.catalog.acroForm;
826826
if (!acroForm) {
827-
return shadow(this, "xfaDatasets", null);
828-
}
829-
830-
const xfa = acroForm.get("XFA");
831-
if (xfa instanceof BaseStream && !xfa.isEmpty) {
832-
try {
833-
const xdp = stringToUTF8String(xfa.getString());
834-
return shadow(this, "xfaDatasets", new DatasetReader({ xdp }));
835-
} catch (_) {
836-
warn("XFA - Invalid utf-8 string.");
837-
return shadow(this, "xfaDatasets", null);
838-
}
839-
}
840-
841-
if (!Array.isArray(xfa) || xfa.length === 0) {
842-
return null;
843-
}
844-
845-
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
846-
if (xfa[i] !== "datasets") {
847-
continue;
848-
}
849-
const data = this.xref.fetchIfRef(xfa[i + 1]);
850-
if (!(data instanceof BaseStream) || data.isEmpty) {
851-
continue;
852-
}
853-
try {
854-
const datasets = stringToUTF8String(data.getString());
855-
return shadow(this, "xfaDatasets", new DatasetReader({ datasets }));
856-
} catch (_) {
857-
warn("XFA - Invalid utf-8 string.");
858-
return shadow(this, "xfaDatasets", null);
859-
}
860-
}
861-
862-
return shadow(this, "xfaDatasets", null);
863-
}
864-
865-
get xfaData() {
866-
const acroForm = this.catalog.acroForm;
867-
if (!acroForm) {
868-
return null;
827+
return shadow(this, "xfaStreams", null);
869828
}
870829

871830
const xfa = acroForm.get("XFA");
872831
const entries = {
873-
"xdp:xdp": "",
874-
template: "",
875-
datasets: "",
876-
config: "",
877-
connectionSet: "",
878-
localeSet: "",
879-
stylesheet: "",
880-
"/xdp:xdp": "",
832+
"xdp:xdp": null,
833+
template: null,
834+
datasets: null,
835+
config: null,
836+
connectionSet: null,
837+
localeSet: null,
838+
stylesheet: null,
839+
"/xdp:xdp": null,
881840
};
882841
if (xfa instanceof BaseStream && !xfa.isEmpty) {
883-
try {
884-
entries["xdp:xdp"] = stringToUTF8String(xfa.getString());
885-
return entries;
886-
} catch (_) {
887-
warn("XFA - Invalid utf-8 string.");
888-
return null;
889-
}
842+
entries["xdp:xdp"] = xfa;
843+
return shadow(this, "xfaStreams", entries);
890844
}
891845

892846
if (!Array.isArray(xfa) || xfa.length === 0) {
893-
return null;
847+
return shadow(this, "xfaStreams", null);
894848
}
895849

896850
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
@@ -910,14 +864,52 @@ class PDFDocument {
910864
if (!(data instanceof BaseStream) || data.isEmpty) {
911865
continue;
912866
}
867+
entries[name] = data;
868+
}
869+
return shadow(this, "xfaStreams", entries);
870+
}
871+
872+
get xfaDatasets() {
873+
const streams = this.xfaStreams;
874+
if (!streams) {
875+
return shadow(this, "xfaDatasets", null);
876+
}
877+
for (const key of ["datasets", "xdp:xdp"]) {
878+
const stream = streams[key];
879+
if (!stream) {
880+
continue;
881+
}
882+
try {
883+
const str = stringToUTF8String(stream.getString());
884+
const data = Object.fromEntries([[key, str]]);
885+
return shadow(this, "xfaDatasets", new DatasetReader(data));
886+
} catch (_) {
887+
warn("XFA - Invalid utf-8 string.");
888+
break;
889+
}
890+
}
891+
return shadow(this, "xfaDatasets", null);
892+
}
893+
894+
get xfaData() {
895+
const streams = this.xfaStreams;
896+
if (!streams) {
897+
return shadow(this, "xfaData", null);
898+
}
899+
const data = Object.create(null);
900+
for (const [key, stream] of Object.entries(streams)) {
901+
if (!stream) {
902+
data[key] = "";
903+
continue;
904+
}
913905
try {
914-
entries[name] = stringToUTF8String(data.getString());
906+
data[key] = stringToUTF8String(stream.getString());
915907
} catch (_) {
916908
warn("XFA - Invalid utf-8 string.");
917-
return null;
909+
return shadow(this, "xfaData", null);
918910
}
919911
}
920-
return entries;
912+
return shadow(this, "xfaData", data);
921913
}
922914

923915
get xfaFactory() {

0 commit comments

Comments
 (0)