2
0

display_utils_spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 _display_utils = require("../../display/display_utils.js");
  24. var _util = require("../../shared/util.js");
  25. var _is_node = require("../../shared/is_node.js");
  26. describe("display_utils", function () {
  27. describe("binary search", function () {
  28. function isTrue(boolean) {
  29. return boolean;
  30. }
  31. function isGreater3(number) {
  32. return number > 3;
  33. }
  34. it("empty array", function () {
  35. expect((0, _display_utils.binarySearchFirstItem)([], isTrue)).toEqual(0);
  36. });
  37. it("single boolean entry", function () {
  38. expect((0, _display_utils.binarySearchFirstItem)([false], isTrue)).toEqual(1);
  39. expect((0, _display_utils.binarySearchFirstItem)([true], isTrue)).toEqual(0);
  40. });
  41. it("three boolean entries", function () {
  42. expect((0, _display_utils.binarySearchFirstItem)([true, true, true], isTrue)).toEqual(0);
  43. expect((0, _display_utils.binarySearchFirstItem)([false, true, true], isTrue)).toEqual(1);
  44. expect((0, _display_utils.binarySearchFirstItem)([false, false, true], isTrue)).toEqual(2);
  45. expect((0, _display_utils.binarySearchFirstItem)([false, false, false], isTrue)).toEqual(3);
  46. });
  47. it("three numeric entries", function () {
  48. expect((0, _display_utils.binarySearchFirstItem)([0, 1, 2], isGreater3)).toEqual(3);
  49. expect((0, _display_utils.binarySearchFirstItem)([2, 3, 4], isGreater3)).toEqual(2);
  50. expect((0, _display_utils.binarySearchFirstItem)([4, 5, 6], isGreater3)).toEqual(0);
  51. });
  52. it("three numeric entries and a start index", function () {
  53. expect((0, _display_utils.binarySearchFirstItem)([0, 1, 2, 3, 4], isGreater3, 2)).toEqual(4);
  54. expect((0, _display_utils.binarySearchFirstItem)([2, 3, 4], isGreater3, 2)).toEqual(2);
  55. expect((0, _display_utils.binarySearchFirstItem)([4, 5, 6], isGreater3, 1)).toEqual(1);
  56. });
  57. });
  58. describe("DOMCanvasFactory", function () {
  59. let canvasFactory;
  60. beforeAll(function () {
  61. canvasFactory = new _display_utils.DOMCanvasFactory();
  62. });
  63. afterAll(function () {
  64. canvasFactory = null;
  65. });
  66. it("`create` should throw an error if the dimensions are invalid", function () {
  67. expect(function () {
  68. return canvasFactory.create(-1, 1);
  69. }).toThrow(new Error("Invalid canvas size"));
  70. expect(function () {
  71. return canvasFactory.create(1, -1);
  72. }).toThrow(new Error("Invalid canvas size"));
  73. });
  74. it("`create` should return a canvas if the dimensions are valid", function () {
  75. if (_is_node.isNodeJS) {
  76. pending("Document is not supported in Node.js.");
  77. }
  78. const {
  79. canvas,
  80. context
  81. } = canvasFactory.create(20, 40);
  82. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  83. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  84. expect(canvas.width).toBe(20);
  85. expect(canvas.height).toBe(40);
  86. });
  87. it("`reset` should throw an error if no canvas is provided", function () {
  88. const canvasAndContext = {
  89. canvas: null,
  90. context: null
  91. };
  92. expect(function () {
  93. return canvasFactory.reset(canvasAndContext, 20, 40);
  94. }).toThrow(new Error("Canvas is not specified"));
  95. });
  96. it("`reset` should throw an error if the dimensions are invalid", function () {
  97. const canvasAndContext = {
  98. canvas: "foo",
  99. context: "bar"
  100. };
  101. expect(function () {
  102. return canvasFactory.reset(canvasAndContext, -1, 1);
  103. }).toThrow(new Error("Invalid canvas size"));
  104. expect(function () {
  105. return canvasFactory.reset(canvasAndContext, 1, -1);
  106. }).toThrow(new Error("Invalid canvas size"));
  107. });
  108. it("`reset` should alter the canvas/context if the dimensions are valid", function () {
  109. if (_is_node.isNodeJS) {
  110. pending("Document is not supported in Node.js.");
  111. }
  112. const canvasAndContext = canvasFactory.create(20, 40);
  113. canvasFactory.reset(canvasAndContext, 60, 80);
  114. const {
  115. canvas,
  116. context
  117. } = canvasAndContext;
  118. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  119. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  120. expect(canvas.width).toBe(60);
  121. expect(canvas.height).toBe(80);
  122. });
  123. it("`destroy` should throw an error if no canvas is provided", function () {
  124. expect(function () {
  125. return canvasFactory.destroy({});
  126. }).toThrow(new Error("Canvas is not specified"));
  127. });
  128. it("`destroy` should clear the canvas/context", function () {
  129. if (_is_node.isNodeJS) {
  130. pending("Document is not supported in Node.js.");
  131. }
  132. const canvasAndContext = canvasFactory.create(20, 40);
  133. canvasFactory.destroy(canvasAndContext);
  134. const {
  135. canvas,
  136. context
  137. } = canvasAndContext;
  138. expect(canvas).toBe(null);
  139. expect(context).toBe(null);
  140. });
  141. });
  142. describe("DOMSVGFactory", function () {
  143. let svgFactory;
  144. beforeAll(function () {
  145. svgFactory = new _display_utils.DOMSVGFactory();
  146. });
  147. afterAll(function () {
  148. svgFactory = null;
  149. });
  150. it("`create` should throw an error if the dimensions are invalid", function () {
  151. expect(function () {
  152. return svgFactory.create(-1, 0);
  153. }).toThrow(new Error("Invalid SVG dimensions"));
  154. expect(function () {
  155. return svgFactory.create(0, -1);
  156. }).toThrow(new Error("Invalid SVG dimensions"));
  157. });
  158. it("`create` should return an SVG element if the dimensions are valid", function () {
  159. if (_is_node.isNodeJS) {
  160. pending("Document is not supported in Node.js.");
  161. }
  162. const svg = svgFactory.create(20, 40);
  163. expect(svg instanceof SVGSVGElement).toBe(true);
  164. expect(svg.getAttribute("version")).toBe("1.1");
  165. expect(svg.getAttribute("width")).toBe("20px");
  166. expect(svg.getAttribute("height")).toBe("40px");
  167. expect(svg.getAttribute("preserveAspectRatio")).toBe("none");
  168. expect(svg.getAttribute("viewBox")).toBe("0 0 20 40");
  169. });
  170. it("`createElement` should throw an error if the type is not a string", function () {
  171. expect(function () {
  172. return svgFactory.createElement(true);
  173. }).toThrow(new Error("Invalid SVG element type"));
  174. });
  175. it("`createElement` should return an SVG element if the type is valid", function () {
  176. if (_is_node.isNodeJS) {
  177. pending("Document is not supported in Node.js.");
  178. }
  179. const svg = svgFactory.createElement("svg:rect");
  180. expect(svg instanceof SVGRectElement).toBe(true);
  181. });
  182. });
  183. describe("getFilenameFromUrl", function () {
  184. it("should get the filename from an absolute URL", function () {
  185. const url = "https://server.org/filename.pdf";
  186. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  187. });
  188. it("should get the filename from a relative URL", function () {
  189. const url = "../../filename.pdf";
  190. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  191. });
  192. it("should get the filename from a URL with an anchor", function () {
  193. const url = "https://server.org/filename.pdf#foo";
  194. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  195. });
  196. it("should get the filename from a URL with query parameters", function () {
  197. const url = "https://server.org/filename.pdf?foo=bar";
  198. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  199. });
  200. });
  201. describe("getPdfFilenameFromUrl", function () {
  202. it("gets PDF filename", function () {
  203. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/file1.pdf")).toEqual("file1.pdf");
  204. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/file2.pdf")).toEqual("file2.pdf");
  205. });
  206. it("gets fallback filename", function () {
  207. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/file1.txt")).toEqual("document.pdf");
  208. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/file2.txt")).toEqual("document.pdf");
  209. });
  210. it("gets custom fallback filename", function () {
  211. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/file1.txt", "qwerty1.pdf")).toEqual("qwerty1.pdf");
  212. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/file2.txt", "qwerty2.pdf")).toEqual("qwerty2.pdf");
  213. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/file3.txt", "")).toEqual("");
  214. });
  215. it("gets fallback filename when url is not a string", function () {
  216. expect((0, _display_utils.getPdfFilenameFromUrl)(null)).toEqual("document.pdf");
  217. expect((0, _display_utils.getPdfFilenameFromUrl)(null, "file.pdf")).toEqual("file.pdf");
  218. });
  219. it("gets PDF filename from URL containing leading/trailing whitespace", function () {
  220. expect((0, _display_utils.getPdfFilenameFromUrl)(" /pdfs/file1.pdf ")).toEqual("file1.pdf");
  221. expect((0, _display_utils.getPdfFilenameFromUrl)(" http://www.example.com/pdfs/file2.pdf ")).toEqual("file2.pdf");
  222. });
  223. it("gets PDF filename from query string", function () {
  224. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/pdfs.html?name=file1.pdf")).toEqual("file1.pdf");
  225. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/pdf.html?file2.pdf")).toEqual("file2.pdf");
  226. });
  227. it("gets PDF filename from hash string", function () {
  228. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/pdfs.html#name=file1.pdf")).toEqual("file1.pdf");
  229. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/pdf.html#file2.pdf")).toEqual("file2.pdf");
  230. });
  231. it("gets correct PDF filename when multiple ones are present", function () {
  232. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/file1.pdf?name=file.pdf")).toEqual("file1.pdf");
  233. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/pdfs/file2.pdf#file.pdf")).toEqual("file2.pdf");
  234. });
  235. it("gets PDF filename from URI-encoded data", function () {
  236. const encodedUrl = encodeURIComponent("http://www.example.com/pdfs/file1.pdf");
  237. expect((0, _display_utils.getPdfFilenameFromUrl)(encodedUrl)).toEqual("file1.pdf");
  238. const encodedUrlWithQuery = encodeURIComponent("http://www.example.com/pdfs/file.txt?file2.pdf");
  239. expect((0, _display_utils.getPdfFilenameFromUrl)(encodedUrlWithQuery)).toEqual("file2.pdf");
  240. });
  241. it("gets PDF filename from data mistaken for URI-encoded", function () {
  242. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/%AA.pdf")).toEqual("%AA.pdf");
  243. expect((0, _display_utils.getPdfFilenameFromUrl)("/pdfs/%2F.pdf")).toEqual("%2F.pdf");
  244. });
  245. it("gets PDF filename from (some) standard protocols", function () {
  246. expect((0, _display_utils.getPdfFilenameFromUrl)("http://www.example.com/file1.pdf")).toEqual("file1.pdf");
  247. expect((0, _display_utils.getPdfFilenameFromUrl)("https://www.example.com/file2.pdf")).toEqual("file2.pdf");
  248. expect((0, _display_utils.getPdfFilenameFromUrl)("file:///path/to/files/file3.pdf")).toEqual("file3.pdf");
  249. expect((0, _display_utils.getPdfFilenameFromUrl)("ftp://www.example.com/file4.pdf")).toEqual("file4.pdf");
  250. });
  251. it('gets PDF filename from query string appended to "blob:" URL', function () {
  252. if (_is_node.isNodeJS) {
  253. pending("Blob in not supported in Node.js.");
  254. }
  255. const typedArray = new Uint8Array([1, 2, 3, 4, 5]);
  256. const blobUrl = URL.createObjectURL(new Blob([typedArray], {
  257. type: "application/pdf"
  258. }));
  259. expect(blobUrl.startsWith("blob:")).toEqual(true);
  260. expect((0, _display_utils.getPdfFilenameFromUrl)(blobUrl + "?file.pdf")).toEqual("file.pdf");
  261. });
  262. it('gets fallback filename from query string appended to "data:" URL', function () {
  263. const typedArray = new Uint8Array([1, 2, 3, 4, 5]),
  264. str = (0, _util.bytesToString)(typedArray);
  265. const dataUrl = `data:application/pdf;base64,${btoa(str)}`;
  266. expect(dataUrl.startsWith("data:")).toEqual(true);
  267. expect((0, _display_utils.getPdfFilenameFromUrl)(dataUrl + "?file1.pdf")).toEqual("document.pdf");
  268. expect((0, _display_utils.getPdfFilenameFromUrl)(" " + dataUrl + "?file2.pdf")).toEqual("document.pdf");
  269. });
  270. });
  271. describe("isValidFetchUrl", function () {
  272. it("handles invalid Fetch URLs", function () {
  273. expect((0, _display_utils.isValidFetchUrl)(null)).toEqual(false);
  274. expect((0, _display_utils.isValidFetchUrl)(100)).toEqual(false);
  275. expect((0, _display_utils.isValidFetchUrl)("foo")).toEqual(false);
  276. expect((0, _display_utils.isValidFetchUrl)("/foo", 100)).toEqual(false);
  277. });
  278. it("handles relative Fetch URLs", function () {
  279. expect((0, _display_utils.isValidFetchUrl)("/foo", "file://www.example.com")).toEqual(false);
  280. expect((0, _display_utils.isValidFetchUrl)("/foo", "http://www.example.com")).toEqual(true);
  281. });
  282. it("handles unsupported Fetch protocols", function () {
  283. expect((0, _display_utils.isValidFetchUrl)("file://www.example.com")).toEqual(false);
  284. expect((0, _display_utils.isValidFetchUrl)("ftp://www.example.com")).toEqual(false);
  285. });
  286. it("handles supported Fetch protocols", function () {
  287. expect((0, _display_utils.isValidFetchUrl)("http://www.example.com")).toEqual(true);
  288. expect((0, _display_utils.isValidFetchUrl)("https://www.example.com")).toEqual(true);
  289. });
  290. });
  291. describe("PDFDateString", function () {
  292. describe("toDateObject", function () {
  293. it("converts PDF date strings to JavaScript `Date` objects", function () {
  294. const expectations = {
  295. undefined: null,
  296. null: null,
  297. 42: null,
  298. 2019: null,
  299. D2019: null,
  300. "D:": null,
  301. "D:201": null,
  302. "D:2019": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  303. "D:20190": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  304. "D:201900": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  305. "D:201913": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  306. "D:201902": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  307. "D:2019020": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  308. "D:20190200": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  309. "D:20190232": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  310. "D:20190203": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  311. "D:20190431": new Date(Date.UTC(2019, 4, 1, 0, 0, 0)),
  312. "D:201902030": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  313. "D:2019020300": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  314. "D:2019020324": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  315. "D:2019020304": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  316. "D:20190203040": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  317. "D:201902030400": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  318. "D:201902030460": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  319. "D:201902030405": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  320. "D:2019020304050": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  321. "D:20190203040500": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  322. "D:20190203040560": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  323. "D:20190203040506": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  324. "D:20190203040506F": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  325. "D:20190203040506Z": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  326. "D:20190203040506-": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  327. "D:20190203040506+": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  328. "D:20190203040506+'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  329. "D:20190203040506+0": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  330. "D:20190203040506+01": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  331. "D:20190203040506+00'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  332. "D:20190203040506+24'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  333. "D:20190203040506+01'": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  334. "D:20190203040506+01'0": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  335. "D:20190203040506+01'00": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  336. "D:20190203040506+01'60": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  337. "D:20190203040506+0102": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  338. "D:20190203040506+01'02": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  339. "D:20190203040506+01'02'": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  340. "D:20190203040506+05'07": new Date(Date.UTC(2019, 1, 2, 22, 58, 6))
  341. };
  342. for (const [input, expectation] of Object.entries(expectations)) {
  343. const result = _display_utils.PDFDateString.toDateObject(input);
  344. if (result) {
  345. expect(result.getTime()).toEqual(expectation.getTime());
  346. } else {
  347. expect(result).toEqual(expectation);
  348. }
  349. }
  350. });
  351. });
  352. });
  353. });