document_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. var _test_utils = require("./test_utils.js");
  24. var _primitives = require("../../core/primitives.js");
  25. var _document = require("../../core/document.js");
  26. var _stream = require("../../core/stream.js");
  27. describe("document", function () {
  28. describe("Page", function () {
  29. it("should create correct objId/fontId using the idFactory", function () {
  30. const idFactory1 = (0, _test_utils.createIdFactory)(0);
  31. const idFactory2 = (0, _test_utils.createIdFactory)(1);
  32. expect(idFactory1.createObjId()).toEqual("p0_1");
  33. expect(idFactory1.createObjId()).toEqual("p0_2");
  34. expect(idFactory1.createFontId()).toEqual("f1");
  35. expect(idFactory1.createFontId()).toEqual("f2");
  36. expect(idFactory1.getDocId()).toEqual("g_d0");
  37. expect(idFactory2.createObjId()).toEqual("p1_1");
  38. expect(idFactory2.createObjId()).toEqual("p1_2");
  39. expect(idFactory2.createFontId()).toEqual("f1");
  40. expect(idFactory2.createFontId()).toEqual("f2");
  41. expect(idFactory2.getDocId()).toEqual("g_d0");
  42. expect(idFactory1.createObjId()).toEqual("p0_3");
  43. expect(idFactory1.createObjId()).toEqual("p0_4");
  44. expect(idFactory1.createFontId()).toEqual("f3");
  45. expect(idFactory1.createFontId()).toEqual("f4");
  46. expect(idFactory1.getDocId()).toEqual("g_d0");
  47. });
  48. });
  49. describe("PDFDocument", function () {
  50. const stream = new _stream.StringStream("Dummy_PDF_data");
  51. function getDocument(acroForm, xref = new _test_utils.XRefMock()) {
  52. const catalog = {
  53. acroForm
  54. };
  55. const pdfManager = {
  56. get docId() {
  57. return "d0";
  58. },
  59. ensureDoc(prop, args) {
  60. return pdfManager.ensure(pdfDocument, prop, args);
  61. },
  62. ensureCatalog(prop, args) {
  63. return pdfManager.ensure(catalog, prop, args);
  64. },
  65. async ensure(obj, prop, args) {
  66. const value = obj[prop];
  67. if (typeof value === "function") {
  68. return value.apply(obj, args);
  69. }
  70. return value;
  71. }
  72. };
  73. const pdfDocument = new _document.PDFDocument(pdfManager, stream);
  74. pdfDocument.xref = xref;
  75. pdfDocument.catalog = catalog;
  76. return pdfDocument;
  77. }
  78. it("should get form info when no form data is present", function () {
  79. const pdfDocument = getDocument(null);
  80. expect(pdfDocument.formInfo).toEqual({
  81. hasAcroForm: false,
  82. hasSignatures: false,
  83. hasXfa: false,
  84. hasFields: false
  85. });
  86. });
  87. it("should get form info when XFA is present", function () {
  88. const acroForm = new _primitives.Dict();
  89. acroForm.set("XFA", []);
  90. let pdfDocument = getDocument(acroForm);
  91. expect(pdfDocument.formInfo).toEqual({
  92. hasAcroForm: false,
  93. hasSignatures: false,
  94. hasXfa: false,
  95. hasFields: false
  96. });
  97. acroForm.set("XFA", ["foo", "bar"]);
  98. pdfDocument = getDocument(acroForm);
  99. expect(pdfDocument.formInfo).toEqual({
  100. hasAcroForm: false,
  101. hasSignatures: false,
  102. hasXfa: true,
  103. hasFields: false
  104. });
  105. acroForm.set("XFA", new _stream.StringStream(""));
  106. pdfDocument = getDocument(acroForm);
  107. expect(pdfDocument.formInfo).toEqual({
  108. hasAcroForm: false,
  109. hasSignatures: false,
  110. hasXfa: false,
  111. hasFields: false
  112. });
  113. acroForm.set("XFA", new _stream.StringStream("non-empty"));
  114. pdfDocument = getDocument(acroForm);
  115. expect(pdfDocument.formInfo).toEqual({
  116. hasAcroForm: false,
  117. hasSignatures: false,
  118. hasXfa: true,
  119. hasFields: false
  120. });
  121. });
  122. it("should get form info when AcroForm is present", function () {
  123. const acroForm = new _primitives.Dict();
  124. acroForm.set("Fields", []);
  125. let pdfDocument = getDocument(acroForm);
  126. expect(pdfDocument.formInfo).toEqual({
  127. hasAcroForm: false,
  128. hasSignatures: false,
  129. hasXfa: false,
  130. hasFields: false
  131. });
  132. acroForm.set("Fields", ["foo", "bar"]);
  133. pdfDocument = getDocument(acroForm);
  134. expect(pdfDocument.formInfo).toEqual({
  135. hasAcroForm: true,
  136. hasSignatures: false,
  137. hasXfa: false,
  138. hasFields: true
  139. });
  140. acroForm.set("Fields", ["foo", "bar"]);
  141. acroForm.set("SigFlags", 2);
  142. pdfDocument = getDocument(acroForm);
  143. expect(pdfDocument.formInfo).toEqual({
  144. hasAcroForm: true,
  145. hasSignatures: false,
  146. hasXfa: false,
  147. hasFields: true
  148. });
  149. const annotationDict = new _primitives.Dict();
  150. annotationDict.set("FT", _primitives.Name.get("Sig"));
  151. annotationDict.set("Rect", [0, 0, 0, 0]);
  152. const annotationRef = _primitives.Ref.get(11, 0);
  153. const kidsDict = new _primitives.Dict();
  154. kidsDict.set("Kids", [annotationRef]);
  155. const kidsRef = _primitives.Ref.get(10, 0);
  156. const xref = new _test_utils.XRefMock([{
  157. ref: annotationRef,
  158. data: annotationDict
  159. }, {
  160. ref: kidsRef,
  161. data: kidsDict
  162. }]);
  163. acroForm.set("Fields", [kidsRef]);
  164. acroForm.set("SigFlags", 3);
  165. pdfDocument = getDocument(acroForm, xref);
  166. expect(pdfDocument.formInfo).toEqual({
  167. hasAcroForm: false,
  168. hasSignatures: true,
  169. hasXfa: false,
  170. hasFields: true
  171. });
  172. });
  173. it("should get calculation order array or null", function () {
  174. const acroForm = new _primitives.Dict();
  175. let pdfDocument = getDocument(acroForm);
  176. expect(pdfDocument.calculationOrderIds).toEqual(null);
  177. acroForm.set("CO", [_primitives.Ref.get(1, 0), _primitives.Ref.get(2, 0), _primitives.Ref.get(3, 0)]);
  178. pdfDocument = getDocument(acroForm);
  179. expect(pdfDocument.calculationOrderIds).toEqual(["1R", "2R", "3R"]);
  180. acroForm.set("CO", []);
  181. pdfDocument = getDocument(acroForm);
  182. expect(pdfDocument.calculationOrderIds).toEqual(null);
  183. acroForm.set("CO", ["1", "2"]);
  184. pdfDocument = getDocument(acroForm);
  185. expect(pdfDocument.calculationOrderIds).toEqual(null);
  186. acroForm.set("CO", ["1", _primitives.Ref.get(1, 0), "2"]);
  187. pdfDocument = getDocument(acroForm);
  188. expect(pdfDocument.calculationOrderIds).toEqual(["1R"]);
  189. });
  190. it("should get field objects array or null", async function () {
  191. const acroForm = new _primitives.Dict();
  192. let pdfDocument = getDocument(acroForm);
  193. let fields = await pdfDocument.fieldObjects;
  194. expect(fields).toEqual(null);
  195. acroForm.set("Fields", []);
  196. pdfDocument = getDocument(acroForm);
  197. fields = await pdfDocument.fieldObjects;
  198. expect(fields).toEqual(null);
  199. const kid1Ref = _primitives.Ref.get(314, 0);
  200. const kid11Ref = _primitives.Ref.get(159, 0);
  201. const kid2Ref = _primitives.Ref.get(265, 0);
  202. const kid2BisRef = _primitives.Ref.get(266, 0);
  203. const parentRef = _primitives.Ref.get(358, 0);
  204. const allFields = Object.create(null);
  205. for (const name of ["parent", "kid1", "kid2", "kid11"]) {
  206. const buttonWidgetDict = new _primitives.Dict();
  207. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  208. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  209. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  210. buttonWidgetDict.set("T", name);
  211. allFields[name] = buttonWidgetDict;
  212. }
  213. allFields.kid1.set("Kids", [kid11Ref]);
  214. allFields.parent.set("Kids", [kid1Ref, kid2Ref, kid2BisRef]);
  215. const xref = new _test_utils.XRefMock([{
  216. ref: parentRef,
  217. data: allFields.parent
  218. }, {
  219. ref: kid1Ref,
  220. data: allFields.kid1
  221. }, {
  222. ref: kid11Ref,
  223. data: allFields.kid11
  224. }, {
  225. ref: kid2Ref,
  226. data: allFields.kid2
  227. }, {
  228. ref: kid2BisRef,
  229. data: allFields.kid2
  230. }]);
  231. acroForm.set("Fields", [parentRef]);
  232. pdfDocument = getDocument(acroForm, xref);
  233. fields = await pdfDocument.fieldObjects;
  234. for (const [name, objs] of Object.entries(fields)) {
  235. fields[name] = objs.map(obj => obj.id);
  236. }
  237. expect(fields["parent.kid1"]).toEqual(["314R"]);
  238. expect(fields["parent.kid1.kid11"]).toEqual(["159R"]);
  239. expect(fields["parent.kid2"]).toEqual(["265R", "266R"]);
  240. expect(fields.parent).toEqual(["358R"]);
  241. });
  242. it("should check if fields have any actions", async function () {
  243. const acroForm = new _primitives.Dict();
  244. let pdfDocument = getDocument(acroForm);
  245. let hasJSActions = await pdfDocument.hasJSActions;
  246. expect(hasJSActions).toEqual(false);
  247. acroForm.set("Fields", []);
  248. pdfDocument = getDocument(acroForm);
  249. hasJSActions = await pdfDocument.hasJSActions;
  250. expect(hasJSActions).toEqual(false);
  251. const kid1Ref = _primitives.Ref.get(314, 0);
  252. const kid11Ref = _primitives.Ref.get(159, 0);
  253. const kid2Ref = _primitives.Ref.get(265, 0);
  254. const parentRef = _primitives.Ref.get(358, 0);
  255. const allFields = Object.create(null);
  256. for (const name of ["parent", "kid1", "kid2", "kid11"]) {
  257. const buttonWidgetDict = new _primitives.Dict();
  258. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  259. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  260. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  261. buttonWidgetDict.set("T", name);
  262. allFields[name] = buttonWidgetDict;
  263. }
  264. allFields.kid1.set("Kids", [kid11Ref]);
  265. allFields.parent.set("Kids", [kid1Ref, kid2Ref]);
  266. const xref = new _test_utils.XRefMock([{
  267. ref: parentRef,
  268. data: allFields.parent
  269. }, {
  270. ref: kid1Ref,
  271. data: allFields.kid1
  272. }, {
  273. ref: kid11Ref,
  274. data: allFields.kid11
  275. }, {
  276. ref: kid2Ref,
  277. data: allFields.kid2
  278. }]);
  279. acroForm.set("Fields", [parentRef]);
  280. pdfDocument = getDocument(acroForm, xref);
  281. hasJSActions = await pdfDocument.hasJSActions;
  282. expect(hasJSActions).toEqual(false);
  283. const JS = _primitives.Name.get("JavaScript");
  284. const additionalActionsDict = new _primitives.Dict();
  285. const eDict = new _primitives.Dict();
  286. eDict.set("JS", "hello()");
  287. eDict.set("S", JS);
  288. additionalActionsDict.set("E", eDict);
  289. allFields.kid2.set("AA", additionalActionsDict);
  290. pdfDocument = getDocument(acroForm, xref);
  291. hasJSActions = await pdfDocument.hasJSActions;
  292. expect(hasJSActions).toEqual(true);
  293. });
  294. });
  295. });