display_utils_spec.js 16 KB

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