display_utils_spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. DOMCanvasFactory,
  17. DOMSVGFactory,
  18. getFilenameFromUrl,
  19. getPdfFilenameFromUrl,
  20. isValidFetchUrl,
  21. PDFDateString,
  22. } from "../../src/display/display_utils.js";
  23. import { bytesToString } from "../../src/shared/util.js";
  24. import { isNodeJS } from "../../src/shared/is_node.js";
  25. describe("display_utils", function () {
  26. describe("DOMCanvasFactory", function () {
  27. let canvasFactory;
  28. beforeAll(function () {
  29. canvasFactory = new DOMCanvasFactory();
  30. });
  31. afterAll(function () {
  32. canvasFactory = null;
  33. });
  34. it("`create` should throw an error if the dimensions are invalid", function () {
  35. // Invalid width.
  36. expect(function () {
  37. return canvasFactory.create(-1, 1);
  38. }).toThrow(new Error("Invalid canvas size"));
  39. // Invalid height.
  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 (isNodeJS) {
  46. pending("Document is not supported in Node.js.");
  47. }
  48. const { canvas, context } = canvasFactory.create(20, 40);
  49. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  50. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  51. expect(canvas.width).toBe(20);
  52. expect(canvas.height).toBe(40);
  53. });
  54. it("`reset` should throw an error if no canvas is provided", function () {
  55. const canvasAndContext = { canvas: null, context: null };
  56. expect(function () {
  57. return canvasFactory.reset(canvasAndContext, 20, 40);
  58. }).toThrow(new Error("Canvas is not specified"));
  59. });
  60. it("`reset` should throw an error if the dimensions are invalid", function () {
  61. const canvasAndContext = { canvas: "foo", context: "bar" };
  62. // Invalid width.
  63. expect(function () {
  64. return canvasFactory.reset(canvasAndContext, -1, 1);
  65. }).toThrow(new Error("Invalid canvas size"));
  66. // Invalid height.
  67. expect(function () {
  68. return canvasFactory.reset(canvasAndContext, 1, -1);
  69. }).toThrow(new Error("Invalid canvas size"));
  70. });
  71. it("`reset` should alter the canvas/context if the dimensions are valid", function () {
  72. if (isNodeJS) {
  73. pending("Document is not supported in Node.js.");
  74. }
  75. const canvasAndContext = canvasFactory.create(20, 40);
  76. canvasFactory.reset(canvasAndContext, 60, 80);
  77. const { canvas, context } = canvasAndContext;
  78. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  79. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  80. expect(canvas.width).toBe(60);
  81. expect(canvas.height).toBe(80);
  82. });
  83. it("`destroy` should throw an error if no canvas is provided", function () {
  84. expect(function () {
  85. return canvasFactory.destroy({});
  86. }).toThrow(new Error("Canvas is not specified"));
  87. });
  88. it("`destroy` should clear the canvas/context", function () {
  89. if (isNodeJS) {
  90. pending("Document is not supported in Node.js.");
  91. }
  92. const canvasAndContext = canvasFactory.create(20, 40);
  93. canvasFactory.destroy(canvasAndContext);
  94. const { canvas, context } = canvasAndContext;
  95. expect(canvas).toBe(null);
  96. expect(context).toBe(null);
  97. });
  98. });
  99. describe("DOMSVGFactory", function () {
  100. let svgFactory;
  101. beforeAll(function () {
  102. svgFactory = new DOMSVGFactory();
  103. });
  104. afterAll(function () {
  105. svgFactory = null;
  106. });
  107. it("`create` should throw an error if the dimensions are invalid", function () {
  108. // Invalid width.
  109. expect(function () {
  110. return svgFactory.create(-1, 0);
  111. }).toThrow(new Error("Invalid SVG dimensions"));
  112. // Invalid height.
  113. expect(function () {
  114. return svgFactory.create(0, -1);
  115. }).toThrow(new Error("Invalid SVG dimensions"));
  116. });
  117. it("`create` should return an SVG element if the dimensions are valid", function () {
  118. if (isNodeJS) {
  119. pending("Document is not supported in Node.js.");
  120. }
  121. const svg = svgFactory.create(20, 40);
  122. expect(svg instanceof SVGSVGElement).toBe(true);
  123. expect(svg.getAttribute("version")).toBe("1.1");
  124. expect(svg.getAttribute("width")).toBe("20px");
  125. expect(svg.getAttribute("height")).toBe("40px");
  126. expect(svg.getAttribute("preserveAspectRatio")).toBe("none");
  127. expect(svg.getAttribute("viewBox")).toBe("0 0 20 40");
  128. });
  129. it("`createElement` should throw an error if the type is not a string", function () {
  130. expect(function () {
  131. return svgFactory.createElement(true);
  132. }).toThrow(new Error("Invalid SVG element type"));
  133. });
  134. it("`createElement` should return an SVG element if the type is valid", function () {
  135. if (isNodeJS) {
  136. pending("Document is not supported in Node.js.");
  137. }
  138. const svg = svgFactory.createElement("svg:rect");
  139. expect(svg instanceof SVGRectElement).toBe(true);
  140. });
  141. });
  142. describe("getFilenameFromUrl", function () {
  143. it("should get the filename from an absolute URL", function () {
  144. const url = "https://server.org/filename.pdf";
  145. expect(getFilenameFromUrl(url)).toEqual("filename.pdf");
  146. });
  147. it("should get the filename from a relative URL", function () {
  148. const url = "../../filename.pdf";
  149. expect(getFilenameFromUrl(url)).toEqual("filename.pdf");
  150. });
  151. it("should get the filename from a URL with an anchor", function () {
  152. const url = "https://server.org/filename.pdf#foo";
  153. expect(getFilenameFromUrl(url)).toEqual("filename.pdf");
  154. });
  155. it("should get the filename from a URL with query parameters", function () {
  156. const url = "https://server.org/filename.pdf?foo=bar";
  157. expect(getFilenameFromUrl(url)).toEqual("filename.pdf");
  158. });
  159. it("should get the filename from a relative URL, keeping the anchor", function () {
  160. const url = "../../part1#part2.pdf";
  161. expect(getFilenameFromUrl(url, /* onlyStripPath = */ true)).toEqual(
  162. "part1#part2.pdf"
  163. );
  164. });
  165. });
  166. describe("getPdfFilenameFromUrl", function () {
  167. it("gets PDF filename", function () {
  168. // Relative URL
  169. expect(getPdfFilenameFromUrl("/pdfs/file1.pdf")).toEqual("file1.pdf");
  170. // Absolute URL
  171. expect(
  172. getPdfFilenameFromUrl("http://www.example.com/pdfs/file2.pdf")
  173. ).toEqual("file2.pdf");
  174. });
  175. it("gets fallback filename", function () {
  176. // Relative URL
  177. expect(getPdfFilenameFromUrl("/pdfs/file1.txt")).toEqual("document.pdf");
  178. // Absolute URL
  179. expect(
  180. getPdfFilenameFromUrl("http://www.example.com/pdfs/file2.txt")
  181. ).toEqual("document.pdf");
  182. });
  183. it("gets custom fallback filename", function () {
  184. // Relative URL
  185. expect(getPdfFilenameFromUrl("/pdfs/file1.txt", "qwerty1.pdf")).toEqual(
  186. "qwerty1.pdf"
  187. );
  188. // Absolute URL
  189. expect(
  190. getPdfFilenameFromUrl(
  191. "http://www.example.com/pdfs/file2.txt",
  192. "qwerty2.pdf"
  193. )
  194. ).toEqual("qwerty2.pdf");
  195. // An empty string should be a valid custom fallback filename.
  196. expect(getPdfFilenameFromUrl("/pdfs/file3.txt", "")).toEqual("");
  197. });
  198. it("gets fallback filename when url is not a string", function () {
  199. expect(getPdfFilenameFromUrl(null)).toEqual("document.pdf");
  200. expect(getPdfFilenameFromUrl(null, "file.pdf")).toEqual("file.pdf");
  201. });
  202. it("gets PDF filename from URL containing leading/trailing whitespace", function () {
  203. // Relative URL
  204. expect(getPdfFilenameFromUrl(" /pdfs/file1.pdf ")).toEqual(
  205. "file1.pdf"
  206. );
  207. // Absolute URL
  208. expect(
  209. getPdfFilenameFromUrl(" http://www.example.com/pdfs/file2.pdf ")
  210. ).toEqual("file2.pdf");
  211. });
  212. it("gets PDF filename from query string", function () {
  213. // Relative URL
  214. expect(getPdfFilenameFromUrl("/pdfs/pdfs.html?name=file1.pdf")).toEqual(
  215. "file1.pdf"
  216. );
  217. // Absolute URL
  218. expect(
  219. getPdfFilenameFromUrl("http://www.example.com/pdfs/pdf.html?file2.pdf")
  220. ).toEqual("file2.pdf");
  221. });
  222. it("gets PDF filename from hash string", function () {
  223. // Relative URL
  224. expect(getPdfFilenameFromUrl("/pdfs/pdfs.html#name=file1.pdf")).toEqual(
  225. "file1.pdf"
  226. );
  227. // Absolute URL
  228. expect(
  229. getPdfFilenameFromUrl("http://www.example.com/pdfs/pdf.html#file2.pdf")
  230. ).toEqual("file2.pdf");
  231. });
  232. it("gets correct PDF filename when multiple ones are present", function () {
  233. // Relative URL
  234. expect(getPdfFilenameFromUrl("/pdfs/file1.pdf?name=file.pdf")).toEqual(
  235. "file1.pdf"
  236. );
  237. // Absolute URL
  238. expect(
  239. getPdfFilenameFromUrl("http://www.example.com/pdfs/file2.pdf#file.pdf")
  240. ).toEqual("file2.pdf");
  241. });
  242. it("gets PDF filename from URI-encoded data", function () {
  243. const encodedUrl = encodeURIComponent(
  244. "http://www.example.com/pdfs/file1.pdf"
  245. );
  246. expect(getPdfFilenameFromUrl(encodedUrl)).toEqual("file1.pdf");
  247. const encodedUrlWithQuery = encodeURIComponent(
  248. "http://www.example.com/pdfs/file.txt?file2.pdf"
  249. );
  250. expect(getPdfFilenameFromUrl(encodedUrlWithQuery)).toEqual("file2.pdf");
  251. });
  252. it("gets PDF filename from data mistaken for URI-encoded", function () {
  253. expect(getPdfFilenameFromUrl("/pdfs/%AA.pdf")).toEqual("%AA.pdf");
  254. expect(getPdfFilenameFromUrl("/pdfs/%2F.pdf")).toEqual("%2F.pdf");
  255. });
  256. it("gets PDF filename from (some) standard protocols", function () {
  257. // HTTP
  258. expect(getPdfFilenameFromUrl("http://www.example.com/file1.pdf")).toEqual(
  259. "file1.pdf"
  260. );
  261. // HTTPS
  262. expect(
  263. getPdfFilenameFromUrl("https://www.example.com/file2.pdf")
  264. ).toEqual("file2.pdf");
  265. // File
  266. expect(getPdfFilenameFromUrl("file:///path/to/files/file3.pdf")).toEqual(
  267. "file3.pdf"
  268. );
  269. // FTP
  270. expect(getPdfFilenameFromUrl("ftp://www.example.com/file4.pdf")).toEqual(
  271. "file4.pdf"
  272. );
  273. });
  274. it('gets PDF filename from query string appended to "blob:" URL', function () {
  275. if (isNodeJS) {
  276. pending("Blob in not supported in Node.js.");
  277. }
  278. const typedArray = new Uint8Array([1, 2, 3, 4, 5]);
  279. const blobUrl = URL.createObjectURL(
  280. new Blob([typedArray], { type: "application/pdf" })
  281. );
  282. // Sanity check to ensure that a "blob:" URL was returned.
  283. expect(blobUrl.startsWith("blob:")).toEqual(true);
  284. expect(getPdfFilenameFromUrl(blobUrl + "?file.pdf")).toEqual("file.pdf");
  285. });
  286. it('gets fallback filename from query string appended to "data:" URL', function () {
  287. const typedArray = new Uint8Array([1, 2, 3, 4, 5]),
  288. str = bytesToString(typedArray);
  289. const dataUrl = `data:application/pdf;base64,${btoa(str)}`;
  290. // Sanity check to ensure that a "data:" URL was returned.
  291. expect(dataUrl.startsWith("data:")).toEqual(true);
  292. expect(getPdfFilenameFromUrl(dataUrl + "?file1.pdf")).toEqual(
  293. "document.pdf"
  294. );
  295. // Should correctly detect a "data:" URL with leading whitespace.
  296. expect(getPdfFilenameFromUrl(" " + dataUrl + "?file2.pdf")).toEqual(
  297. "document.pdf"
  298. );
  299. });
  300. });
  301. describe("isValidFetchUrl", function () {
  302. it("handles invalid Fetch URLs", function () {
  303. expect(isValidFetchUrl(null)).toEqual(false);
  304. expect(isValidFetchUrl(100)).toEqual(false);
  305. expect(isValidFetchUrl("foo")).toEqual(false);
  306. expect(isValidFetchUrl("/foo", 100)).toEqual(false);
  307. });
  308. it("handles relative Fetch URLs", function () {
  309. expect(isValidFetchUrl("/foo", "file://www.example.com")).toEqual(false);
  310. expect(isValidFetchUrl("/foo", "http://www.example.com")).toEqual(true);
  311. });
  312. it("handles unsupported Fetch protocols", function () {
  313. expect(isValidFetchUrl("file://www.example.com")).toEqual(false);
  314. expect(isValidFetchUrl("ftp://www.example.com")).toEqual(false);
  315. });
  316. it("handles supported Fetch protocols", function () {
  317. expect(isValidFetchUrl("http://www.example.com")).toEqual(true);
  318. expect(isValidFetchUrl("https://www.example.com")).toEqual(true);
  319. });
  320. });
  321. describe("PDFDateString", function () {
  322. describe("toDateObject", function () {
  323. it("converts PDF date strings to JavaScript `Date` objects", function () {
  324. const expectations = {
  325. undefined: null,
  326. null: null,
  327. 42: null,
  328. 2019: null,
  329. D2019: null,
  330. "D:": null,
  331. "D:201": null,
  332. "D:2019": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  333. "D:20190": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  334. "D:201900": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  335. "D:201913": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  336. "D:201902": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  337. "D:2019020": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  338. "D:20190200": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  339. "D:20190232": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  340. "D:20190203": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  341. // Invalid dates like the 31th of April are handled by JavaScript:
  342. "D:20190431": new Date(Date.UTC(2019, 4, 1, 0, 0, 0)),
  343. "D:201902030": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  344. "D:2019020300": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  345. "D:2019020324": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  346. "D:2019020304": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  347. "D:20190203040": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  348. "D:201902030400": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  349. "D:201902030460": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  350. "D:201902030405": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  351. "D:2019020304050": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  352. "D:20190203040500": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  353. "D:20190203040560": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  354. "D:20190203040506": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  355. "D:20190203040506F": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  356. "D:20190203040506Z": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  357. "D:20190203040506-": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  358. "D:20190203040506+": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  359. "D:20190203040506+'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  360. "D:20190203040506+0": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  361. "D:20190203040506+01": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  362. "D:20190203040506+00'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  363. "D:20190203040506+24'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  364. "D:20190203040506+01'": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  365. "D:20190203040506+01'0": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  366. "D:20190203040506+01'00": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  367. "D:20190203040506+01'60": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  368. "D:20190203040506+0102": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  369. "D:20190203040506+01'02": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  370. "D:20190203040506+01'02'": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  371. // Offset hour and minute that result in a day change:
  372. "D:20190203040506+05'07": new Date(Date.UTC(2019, 1, 2, 22, 58, 6)),
  373. };
  374. for (const [input, expectation] of Object.entries(expectations)) {
  375. const result = PDFDateString.toDateObject(input);
  376. if (result) {
  377. expect(result.getTime()).toEqual(expectation.getTime());
  378. } else {
  379. expect(result).toEqual(expectation);
  380. }
  381. }
  382. });
  383. });
  384. });
  385. });