From f4fcb59a5e66e9d4276864cbe06067c2d8cdcdde Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 1 Apr 2022 12:55:28 +0200 Subject: [PATCH] Refactor some xfa*** getters in document.js - it's a follow-up of PR #14735. --- src/core/dataset_reader.js | 2 +- src/core/document.js | 93 +++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/src/core/dataset_reader.js b/src/core/dataset_reader.js index 71a9b7561..34dce7587 100644 --- a/src/core/dataset_reader.js +++ b/src/core/dataset_reader.js @@ -52,7 +52,7 @@ class DatasetReader { } else { const parser = new DatasetXMLParser({ hasAttributes: true }); try { - parser.parseFromString(data.xdp); + parser.parseFromString(data["xdp:xdp"]); } catch (_) {} this.node = parser.node; } diff --git a/src/core/document.js b/src/core/document.js index 06e7aa509..f3baa93e9 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -821,48 +821,7 @@ class PDFDocument { }); } - get xfaDatasets() { - 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() { + get _xfaStreams() { const acroForm = this.catalog.acroForm; if (!acroForm) { return null; @@ -880,13 +839,8 @@ class PDFDocument { "/xdp:xdp": "", }; if (xfa instanceof BaseStream && !xfa.isEmpty) { - try { - entries["xdp:xdp"] = stringToUTF8String(xfa.getString()); - return entries; - } catch (_) { - warn("XFA - Invalid utf-8 string."); - return null; - } + entries["xdp:xdp"] = xfa; + return entries; } if (!Array.isArray(xfa) || xfa.length === 0) { @@ -910,14 +864,51 @@ class PDFDocument { if (!(data instanceof BaseStream) || data.isEmpty) { 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 { - 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 (_) { warn("XFA - Invalid utf-8 string."); return null; } } - return entries; + return data; } get xfaFactory() {