Refactor some xfa*** getters in document.js

- it's a follow-up of PR #14735.
This commit is contained in:
Calixte Denizet 2022-04-01 12:55:28 +02:00
parent 38e9a46a85
commit f4fcb59a5e
2 changed files with 43 additions and 52 deletions

View File

@ -52,7 +52,7 @@ class DatasetReader {
} else { } else {
const parser = new DatasetXMLParser({ hasAttributes: true }); const parser = new DatasetXMLParser({ hasAttributes: true });
try { try {
parser.parseFromString(data.xdp); parser.parseFromString(data["xdp:xdp"]);
} catch (_) {} } catch (_) {}
this.node = parser.node; this.node = parser.node;
} }

View File

@ -821,48 +821,7 @@ class PDFDocument {
}); });
} }
get xfaDatasets() { get _xfaStreams() {
const acroForm = this.catalog.acroForm;
if (!acroForm) {
return shadow(this, "xfaDatasets", null);
}
const xfa = acroForm.get("XFA");
if (xfa instanceof BaseStream && !xfa.isEmpty) {
try {
const xdp = stringToUTF8String(xfa.getString());
return shadow(this, "xfaDatasets", new DatasetReader({ xdp }));
} catch (_) {
warn("XFA - Invalid utf-8 string.");
return shadow(this, "xfaDatasets", null);
}
}
if (!Array.isArray(xfa) || xfa.length === 0) {
return null;
}
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
if (xfa[i] !== "datasets") {
continue;
}
const data = this.xref.fetchIfRef(xfa[i + 1]);
if (!(data instanceof BaseStream) || data.isEmpty) {
continue;
}
try {
const datasets = stringToUTF8String(data.getString());
return shadow(this, "xfaDatasets", new DatasetReader({ datasets }));
} catch (_) {
warn("XFA - Invalid utf-8 string.");
return shadow(this, "xfaDatasets", null);
}
}
return shadow(this, "xfaDatasets", null);
}
get xfaData() {
const acroForm = this.catalog.acroForm; const acroForm = this.catalog.acroForm;
if (!acroForm) { if (!acroForm) {
return null; return null;
@ -880,13 +839,8 @@ class PDFDocument {
"/xdp:xdp": "", "/xdp:xdp": "",
}; };
if (xfa instanceof BaseStream && !xfa.isEmpty) { if (xfa instanceof BaseStream && !xfa.isEmpty) {
try { entries["xdp:xdp"] = xfa;
entries["xdp:xdp"] = stringToUTF8String(xfa.getString());
return entries; return entries;
} catch (_) {
warn("XFA - Invalid utf-8 string.");
return null;
}
} }
if (!Array.isArray(xfa) || xfa.length === 0) { if (!Array.isArray(xfa) || xfa.length === 0) {
@ -910,14 +864,51 @@ class PDFDocument {
if (!(data instanceof BaseStream) || data.isEmpty) { if (!(data instanceof BaseStream) || data.isEmpty) {
continue; continue;
} }
entries[name] = data;
}
return entries;
}
get xfaDatasets() {
const streams = this._xfaStreams;
if (!streams) {
return shadow(this, "xfaDatasets", null);
}
for (const key of ["datasets", "xdp:xdp"]) {
const stream = streams[key];
if (!stream) {
continue;
}
try { try {
entries[name] = stringToUTF8String(data.getString()); const str = stringToUTF8String(stream.getString());
const data = { [key]: str };
return shadow(this, "xfaDatasets", new DatasetReader(data));
} catch (_) {
warn("XFA - Invalid utf-8 string.");
break;
}
}
return shadow(this, "xfaDatasets", null);
}
get xfaData() {
const streams = this._xfaStreams;
if (!streams) {
return null;
}
const data = Object.create(null);
for (const [key, stream] of Object.entries(streams)) {
if (!stream) {
continue;
}
try {
data[key] = stringToUTF8String(stream.getString());
} catch (_) { } catch (_) {
warn("XFA - Invalid utf-8 string."); warn("XFA - Invalid utf-8 string.");
return null; return null;
} }
} }
return entries; return data;
} }
get xfaFactory() { get xfaFactory() {