document_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. ensureCatalog(prop, args) {
  60. return pdfManager.ensure(catalog, prop, args);
  61. },
  62. ensure(obj, prop, args) {
  63. return new Promise(function (resolve) {
  64. const value = obj[prop];
  65. if (typeof value === "function") {
  66. resolve(value.apply(obj, args));
  67. } else {
  68. resolve(value);
  69. }
  70. });
  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. hasXfa: false,
  83. hasFields: false
  84. });
  85. });
  86. it("should get form info when XFA is present", function () {
  87. const acroForm = new _primitives.Dict();
  88. acroForm.set("XFA", []);
  89. let pdfDocument = getDocument(acroForm);
  90. expect(pdfDocument.formInfo).toEqual({
  91. hasAcroForm: false,
  92. hasXfa: false,
  93. hasFields: false
  94. });
  95. acroForm.set("XFA", ["foo", "bar"]);
  96. pdfDocument = getDocument(acroForm);
  97. expect(pdfDocument.formInfo).toEqual({
  98. hasAcroForm: false,
  99. hasXfa: true,
  100. hasFields: false
  101. });
  102. acroForm.set("XFA", new _stream.StringStream(""));
  103. pdfDocument = getDocument(acroForm);
  104. expect(pdfDocument.formInfo).toEqual({
  105. hasAcroForm: false,
  106. hasXfa: false,
  107. hasFields: false
  108. });
  109. acroForm.set("XFA", new _stream.StringStream("non-empty"));
  110. pdfDocument = getDocument(acroForm);
  111. expect(pdfDocument.formInfo).toEqual({
  112. hasAcroForm: false,
  113. hasXfa: true,
  114. hasFields: false
  115. });
  116. });
  117. it("should get form info when AcroForm is present", function () {
  118. const acroForm = new _primitives.Dict();
  119. acroForm.set("Fields", []);
  120. let pdfDocument = getDocument(acroForm);
  121. expect(pdfDocument.formInfo).toEqual({
  122. hasAcroForm: false,
  123. hasXfa: false,
  124. hasFields: false
  125. });
  126. acroForm.set("Fields", ["foo", "bar"]);
  127. pdfDocument = getDocument(acroForm);
  128. expect(pdfDocument.formInfo).toEqual({
  129. hasAcroForm: true,
  130. hasXfa: false,
  131. hasFields: true
  132. });
  133. acroForm.set("Fields", ["foo", "bar"]);
  134. acroForm.set("SigFlags", 2);
  135. pdfDocument = getDocument(acroForm);
  136. expect(pdfDocument.formInfo).toEqual({
  137. hasAcroForm: true,
  138. hasXfa: false,
  139. hasFields: true
  140. });
  141. const annotationDict = new _primitives.Dict();
  142. annotationDict.set("FT", _primitives.Name.get("Sig"));
  143. annotationDict.set("Rect", [0, 0, 0, 0]);
  144. const annotationRef = _primitives.Ref.get(11, 0);
  145. const kidsDict = new _primitives.Dict();
  146. kidsDict.set("Kids", [annotationRef]);
  147. const kidsRef = _primitives.Ref.get(10, 0);
  148. const xref = new _test_utils.XRefMock([{
  149. ref: annotationRef,
  150. data: annotationDict
  151. }, {
  152. ref: kidsRef,
  153. data: kidsDict
  154. }]);
  155. acroForm.set("Fields", [kidsRef]);
  156. acroForm.set("SigFlags", 3);
  157. pdfDocument = getDocument(acroForm, xref);
  158. expect(pdfDocument.formInfo).toEqual({
  159. hasAcroForm: false,
  160. hasXfa: false,
  161. hasFields: true
  162. });
  163. });
  164. it("should get calculation order array or null", function () {
  165. const acroForm = new _primitives.Dict();
  166. let pdfDocument = getDocument(acroForm);
  167. expect(pdfDocument.calculationOrderIds).toEqual(null);
  168. acroForm.set("CO", [_primitives.Ref.get(1, 0), _primitives.Ref.get(2, 0), _primitives.Ref.get(3, 0)]);
  169. pdfDocument = getDocument(acroForm);
  170. expect(pdfDocument.calculationOrderIds).toEqual(["1R", "2R", "3R"]);
  171. acroForm.set("CO", []);
  172. pdfDocument = getDocument(acroForm);
  173. expect(pdfDocument.calculationOrderIds).toEqual(null);
  174. acroForm.set("CO", ["1", "2"]);
  175. pdfDocument = getDocument(acroForm);
  176. expect(pdfDocument.calculationOrderIds).toEqual(null);
  177. acroForm.set("CO", ["1", _primitives.Ref.get(1, 0), "2"]);
  178. pdfDocument = getDocument(acroForm);
  179. expect(pdfDocument.calculationOrderIds).toEqual(["1R"]);
  180. });
  181. it("should get field objects array or null", async function () {
  182. const acroForm = new _primitives.Dict();
  183. let pdfDocument = getDocument(acroForm);
  184. let fields = await pdfDocument.fieldObjects;
  185. expect(fields).toEqual(null);
  186. acroForm.set("Fields", []);
  187. pdfDocument = getDocument(acroForm);
  188. fields = await pdfDocument.fieldObjects;
  189. expect(fields).toEqual(null);
  190. const kid1Ref = _primitives.Ref.get(314, 0);
  191. const kid11Ref = _primitives.Ref.get(159, 0);
  192. const kid2Ref = _primitives.Ref.get(265, 0);
  193. const kid2BisRef = _primitives.Ref.get(266, 0);
  194. const parentRef = _primitives.Ref.get(358, 0);
  195. const allFields = Object.create(null);
  196. for (const name of ["parent", "kid1", "kid2", "kid11"]) {
  197. const buttonWidgetDict = new _primitives.Dict();
  198. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  199. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  200. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  201. buttonWidgetDict.set("T", name);
  202. allFields[name] = buttonWidgetDict;
  203. }
  204. allFields.kid1.set("Kids", [kid11Ref]);
  205. allFields.parent.set("Kids", [kid1Ref, kid2Ref, kid2BisRef]);
  206. const xref = new _test_utils.XRefMock([{
  207. ref: parentRef,
  208. data: allFields.parent
  209. }, {
  210. ref: kid1Ref,
  211. data: allFields.kid1
  212. }, {
  213. ref: kid11Ref,
  214. data: allFields.kid11
  215. }, {
  216. ref: kid2Ref,
  217. data: allFields.kid2
  218. }, {
  219. ref: kid2BisRef,
  220. data: allFields.kid2
  221. }]);
  222. acroForm.set("Fields", [parentRef]);
  223. pdfDocument = getDocument(acroForm, xref);
  224. fields = await pdfDocument.fieldObjects;
  225. for (const [name, objs] of Object.entries(fields)) {
  226. fields[name] = objs.map(obj => obj.id);
  227. }
  228. expect(fields["parent.kid1"]).toEqual(["314R"]);
  229. expect(fields["parent.kid1.kid11"]).toEqual(["159R"]);
  230. expect(fields["parent.kid2"]).toEqual(["265R", "266R"]);
  231. expect(fields.parent).toEqual(["358R"]);
  232. });
  233. it("should check if fields have any actions", async function () {
  234. const acroForm = new _primitives.Dict();
  235. let pdfDocument = getDocument(acroForm);
  236. let hasJSActions = await pdfDocument.hasJSActions;
  237. expect(hasJSActions).toEqual(false);
  238. acroForm.set("Fields", []);
  239. pdfDocument = getDocument(acroForm);
  240. hasJSActions = await pdfDocument.hasJSActions;
  241. expect(hasJSActions).toEqual(false);
  242. const kid1Ref = _primitives.Ref.get(314, 0);
  243. const kid11Ref = _primitives.Ref.get(159, 0);
  244. const kid2Ref = _primitives.Ref.get(265, 0);
  245. const parentRef = _primitives.Ref.get(358, 0);
  246. const allFields = Object.create(null);
  247. for (const name of ["parent", "kid1", "kid2", "kid11"]) {
  248. const buttonWidgetDict = new _primitives.Dict();
  249. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  250. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  251. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  252. buttonWidgetDict.set("T", name);
  253. allFields[name] = buttonWidgetDict;
  254. }
  255. allFields.kid1.set("Kids", [kid11Ref]);
  256. allFields.parent.set("Kids", [kid1Ref, kid2Ref]);
  257. const xref = new _test_utils.XRefMock([{
  258. ref: parentRef,
  259. data: allFields.parent
  260. }, {
  261. ref: kid1Ref,
  262. data: allFields.kid1
  263. }, {
  264. ref: kid11Ref,
  265. data: allFields.kid11
  266. }, {
  267. ref: kid2Ref,
  268. data: allFields.kid2
  269. }]);
  270. acroForm.set("Fields", [parentRef]);
  271. pdfDocument = getDocument(acroForm, xref);
  272. hasJSActions = await pdfDocument.hasJSActions;
  273. expect(hasJSActions).toEqual(false);
  274. const JS = _primitives.Name.get("JavaScript");
  275. const additionalActionsDict = new _primitives.Dict();
  276. const eDict = new _primitives.Dict();
  277. eDict.set("JS", "hello()");
  278. eDict.set("S", JS);
  279. additionalActionsDict.set("E", eDict);
  280. allFields.kid2.set("AA", additionalActionsDict);
  281. pdfDocument = getDocument(acroForm, xref);
  282. hasJSActions = await pdfDocument.hasJSActions;
  283. expect(hasJSActions).toEqual(true);
  284. });
  285. });
  286. });