Skip to content

Commit f9d5632

Browse files
Merge pull request #12349 from calixteman/followup_12344
Follow-up of pr #12344
2 parents eea97ea + 64a6efd commit f9d5632

File tree

9 files changed

+69
-56
lines changed

9 files changed

+69
-56
lines changed

src/core/core_utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ function isWhiteSpace(ch) {
165165
return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
166166
}
167167

168+
/**
169+
* AcroForm field names use an array like notation to refer to
170+
* repeated XFA elements e.g. foo.bar[nnn].
171+
* see: XFA Spec Chapter 3 - Repeated Elements
172+
*
173+
* @param {string} path - XFA path name.
174+
* @returns {Array} - Array of Objects with the name and pos of
175+
* each part of the path.
176+
*/
177+
function parseXFAPath(path) {
178+
const positionPattern = /(.+)\[([0-9]+)\]$/;
179+
return path.split(".").map(component => {
180+
const m = component.match(positionPattern);
181+
if (m) {
182+
return { name: m[1], pos: parseInt(m[2], 10) };
183+
}
184+
return { name: component, pos: 0 };
185+
});
186+
}
187+
168188
export {
169189
getLookupTableFactory,
170190
MissingDataException,
@@ -173,6 +193,7 @@ export {
173193
getInheritableProperty,
174194
toRomanNumerals,
175195
log2,
196+
parseXFAPath,
176197
readInt8,
177198
readUint16,
178199
readUint32,

src/core/worker.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,13 @@ class WorkerMessageHandler {
589589
}
590590
xref.resetNewRef();
591591

592-
return incrementalUpdate(
593-
stream.bytes,
594-
newXrefInfo,
592+
return incrementalUpdate({
593+
originalData: stream.bytes,
594+
xrefInfo: newXrefInfo,
595595
newRefs,
596596
xref,
597-
xfaDatasets
598-
);
597+
datasetsRef: xfaDatasets,
598+
});
599599
});
600600
});
601601

src/core/writer.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
*/
1515
/* eslint no-var: error */
1616

17-
import {
18-
bytesToString,
19-
escapeString,
20-
parseXFAPath,
21-
warn,
22-
} from "../shared/util.js";
17+
import { bytesToString, escapeString, warn } from "../shared/util.js";
2318
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
2419
import { SimpleDOMNode, SimpleXMLParser } from "../shared/xml_parser.js";
2520
import { calculateMD5 } from "./crypto.js";
21+
import { parseXFAPath } from "./core_utils.js";
2622

2723
function writeDict(dict, buffer, transform) {
2824
buffer.push("<<");
@@ -175,7 +171,13 @@ function updateXFA(datasetsRef, newRefs, xref) {
175171
newRefs.push({ ref: datasetsRef, data });
176172
}
177173

178-
function incrementalUpdate(originalData, xrefInfo, newRefs, xref, datasetsRef) {
174+
function incrementalUpdate({
175+
originalData,
176+
xrefInfo,
177+
newRefs,
178+
xref = null,
179+
datasetsRef = null,
180+
}) {
179181
updateXFA(datasetsRef, newRefs, xref);
180182

181183
const newXref = new Dict(null);

src/shared/util.js

-21
Original file line numberDiff line numberDiff line change
@@ -910,26 +910,6 @@ const createObjectURL = (function createObjectURLClosure() {
910910
};
911911
})();
912912

913-
/**
914-
* AcroForm field names use an array like notation to refer to
915-
* repeated XFA elements e.g. foo.bar[nnn].
916-
* see: XFA Spec Chapter 3 - Repeated Elements
917-
*
918-
* @param {string} path - XFA path name.
919-
* @returns {Array} - Array of Objects with the name and pos of
920-
* each part of the path.
921-
*/
922-
function parseXFAPath(path) {
923-
const positionPattern = /(.+)\[([0-9]+)\]$/;
924-
return path.split(".").map(component => {
925-
const m = component.match(positionPattern);
926-
if (m) {
927-
return { name: m[1], pos: parseInt(m[2], 10) };
928-
}
929-
return { name: component, pos: 0 };
930-
});
931-
}
932-
933913
const XMLEntities = {
934914
/* < */ 0x3c: "&lt;",
935915
/* > */ 0x3e: "&gt;",
@@ -1027,7 +1007,6 @@ export {
10271007
createValidAbsoluteUrl,
10281008
IsLittleEndianCached,
10291009
IsEvalSupportedCached,
1030-
parseXFAPath,
10311010
removeNullCharacters,
10321011
setVerbosityLevel,
10331012
shadow,

src/shared/xml_parser.js

+12
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ class SimpleDOMNode {
329329
return this.childNodes && this.childNodes.length > 0;
330330
}
331331

332+
/**
333+
* Search a node in the tree with the given path
334+
* foo.bar[nnn], i.e. find the nnn-th node named
335+
* bar under a node named foo.
336+
*
337+
* @param {Array} paths - an array of objects as
338+
* returned by {parseXFAPath}.
339+
* @param {number} pos - the current position in
340+
* the paths array.
341+
* @returns {SimpleDOMNode} The node corresponding
342+
* to the path or null if not found.
343+
*/
332344
searchNode(paths, pos) {
333345
if (pos >= paths.length) {
334346
return this;

test/unit/core_utils_spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getInheritableProperty,
1919
isWhiteSpace,
2020
log2,
21+
parseXFAPath,
2122
toRomanNumerals,
2223
} from "../../src/core/core_utils.js";
2324
import { XRefMock } from "./test_utils.js";
@@ -211,4 +212,18 @@ describe("core_utils", function () {
211212
expect(isWhiteSpace(undefined)).toEqual(false);
212213
});
213214
});
215+
216+
describe("parseXFAPath", function () {
217+
it("should get a correctly parsed path", function () {
218+
const path = "foo.bar[12].oof[3].rab.FOO[123].BAR[456]";
219+
expect(parseXFAPath(path)).toEqual([
220+
{ name: "foo", pos: 0 },
221+
{ name: "bar", pos: 12 },
222+
{ name: "oof", pos: 3 },
223+
{ name: "rab", pos: 0 },
224+
{ name: "FOO", pos: 123 },
225+
{ name: "BAR", pos: 456 },
226+
]);
227+
});
228+
});
214229
});

test/unit/util_spec.js

-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
isNum,
2626
isSameOrigin,
2727
isString,
28-
parseXFAPath,
2928
removeNullCharacters,
3029
string32,
3130
stringToBytes,
@@ -334,20 +333,6 @@ describe("util", function () {
334333
});
335334
});
336335

337-
describe("parseXFAPath", function () {
338-
it("should get a correctly parsed path", function () {
339-
const path = "foo.bar[12].oof[3].rab.FOO[123].BAR[456]";
340-
expect(parseXFAPath(path)).toEqual([
341-
{ name: "foo", pos: 0 },
342-
{ name: "bar", pos: 12 },
343-
{ name: "oof", pos: 3 },
344-
{ name: "rab", pos: 0 },
345-
{ name: "FOO", pos: 123 },
346-
{ name: "BAR", pos: 456 },
347-
]);
348-
});
349-
});
350-
351336
describe("encodeToXmlString", function () {
352337
it("should get a correctly encoded string with some entities", function () {
353338
const str = "\"\u0397ell😂' & <W😂rld>";

test/unit/writer_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("Writer", function () {
3737
info: {},
3838
};
3939

40-
let data = incrementalUpdate(originalData, xrefInfo, newRefs, null, null);
40+
let data = incrementalUpdate({ originalData, xrefInfo, newRefs });
4141
data = bytesToString(data);
4242

4343
const expected =

test/unit/xml_spec.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { parseXFAPath } from "../../src/shared/util.js";
16+
import { parseXFAPath } from "../../src/core/core_utils.js";
1717
import { SimpleXMLParser } from "../../src/shared/xml_parser.js";
1818

1919
describe("XML", function () {
@@ -69,7 +69,7 @@ describe("XML", function () {
6969
});
7070

7171
it("should dump a xml tree", function () {
72-
let xml = `
72+
const xml = `
7373
<a>
7474
<b>
7575
<c a="123"/>
@@ -87,9 +87,7 @@ describe("XML", function () {
8787
<h>
8888
<i/>
8989
<j/>
90-
<k>
91-
W&#x1F602;rld
92-
<g a="654"/>
90+
<k>&#xA;W&#x1F602;rld&#xA;<g a="654"/>
9391
</k>
9492
</h>
9593
<b>
@@ -98,13 +96,14 @@ describe("XML", function () {
9896
<g a="121110"/>
9997
</b>
10098
</a>`;
101-
xml = xml.replace(/\s+/g, "");
10299
const root = new SimpleXMLParser(true).parseFromString(xml)
103100
.documentElement;
104101
const buffer = [];
105102
root.dump(buffer);
106103

107-
expect(buffer.join("").replace(/\s+/g, "")).toEqual(xml);
104+
expect(buffer.join("").replace(/\s+/g, "")).toEqual(
105+
xml.replace(/\s+/g, "")
106+
);
108107
});
109108
});
110109
});

0 commit comments

Comments
 (0)