document_spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 pdfManager = {
  51. get docId() {
  52. return "d0";
  53. }
  54. };
  55. const stream = new _stream.StringStream("Dummy_PDF_data");
  56. function getDocument(acroForm) {
  57. const pdfDocument = new _document.PDFDocument(pdfManager, stream);
  58. pdfDocument.catalog = {
  59. acroForm
  60. };
  61. return pdfDocument;
  62. }
  63. it("should get form info when no form data is present", function () {
  64. const pdfDocument = getDocument(null);
  65. expect(pdfDocument.formInfo).toEqual({
  66. hasAcroForm: false,
  67. hasXfa: false
  68. });
  69. });
  70. it("should get form info when XFA is present", function () {
  71. const acroForm = new _primitives.Dict();
  72. acroForm.set("XFA", []);
  73. let pdfDocument = getDocument(acroForm);
  74. expect(pdfDocument.formInfo).toEqual({
  75. hasAcroForm: false,
  76. hasXfa: false
  77. });
  78. acroForm.set("XFA", ["foo", "bar"]);
  79. pdfDocument = getDocument(acroForm);
  80. expect(pdfDocument.formInfo).toEqual({
  81. hasAcroForm: false,
  82. hasXfa: true
  83. });
  84. acroForm.set("XFA", new _stream.StringStream(""));
  85. pdfDocument = getDocument(acroForm);
  86. expect(pdfDocument.formInfo).toEqual({
  87. hasAcroForm: false,
  88. hasXfa: false
  89. });
  90. acroForm.set("XFA", new _stream.StringStream("non-empty"));
  91. pdfDocument = getDocument(acroForm);
  92. expect(pdfDocument.formInfo).toEqual({
  93. hasAcroForm: false,
  94. hasXfa: true
  95. });
  96. });
  97. it("should get form info when AcroForm is present", function () {
  98. const acroForm = new _primitives.Dict();
  99. acroForm.set("Fields", []);
  100. let pdfDocument = getDocument(acroForm);
  101. expect(pdfDocument.formInfo).toEqual({
  102. hasAcroForm: false,
  103. hasXfa: false
  104. });
  105. acroForm.set("Fields", ["foo", "bar"]);
  106. pdfDocument = getDocument(acroForm);
  107. expect(pdfDocument.formInfo).toEqual({
  108. hasAcroForm: true,
  109. hasXfa: false
  110. });
  111. acroForm.set("Fields", ["foo", "bar"]);
  112. acroForm.set("SigFlags", 2);
  113. pdfDocument = getDocument(acroForm);
  114. expect(pdfDocument.formInfo).toEqual({
  115. hasAcroForm: true,
  116. hasXfa: false
  117. });
  118. const annotationDict = new _primitives.Dict();
  119. annotationDict.set("FT", _primitives.Name.get("Sig"));
  120. annotationDict.set("Rect", [0, 0, 0, 0]);
  121. const annotationRef = _primitives.Ref.get(11, 0);
  122. const kidsDict = new _primitives.Dict();
  123. kidsDict.set("Kids", [annotationRef]);
  124. const kidsRef = _primitives.Ref.get(10, 0);
  125. pdfDocument.xref = new _test_utils.XRefMock([{
  126. ref: annotationRef,
  127. data: annotationDict
  128. }, {
  129. ref: kidsRef,
  130. data: kidsDict
  131. }]);
  132. acroForm.set("Fields", [kidsRef]);
  133. acroForm.set("SigFlags", 3);
  134. pdfDocument = getDocument(acroForm);
  135. expect(pdfDocument.formInfo).toEqual({
  136. hasAcroForm: false,
  137. hasXfa: false
  138. });
  139. });
  140. });
  141. });