display_utils_spec.js 16 KB

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