core_utils_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Copyright 2019 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 { Dict, Ref } from "../../src/core/primitives.js";
  16. import {
  17. encodeToXmlString,
  18. escapePDFName,
  19. escapeString,
  20. getInheritableProperty,
  21. isAscii,
  22. isWhiteSpace,
  23. log2,
  24. parseXFAPath,
  25. stringToUTF16HexString,
  26. stringToUTF16String,
  27. toRomanNumerals,
  28. validateCSSFont,
  29. } from "../../src/core/core_utils.js";
  30. import { XRefMock } from "./test_utils.js";
  31. describe("core_utils", function () {
  32. describe("getInheritableProperty", function () {
  33. it("handles non-dictionary arguments", function () {
  34. expect(getInheritableProperty({ dict: null, key: "foo" })).toEqual(
  35. undefined
  36. );
  37. expect(getInheritableProperty({ dict: undefined, key: "foo" })).toEqual(
  38. undefined
  39. );
  40. });
  41. it("handles dictionaries that do not contain the property", function () {
  42. // Empty dictionary.
  43. const emptyDict = new Dict();
  44. expect(getInheritableProperty({ dict: emptyDict, key: "foo" })).toEqual(
  45. undefined
  46. );
  47. // Filled dictionary with a different property.
  48. const filledDict = new Dict();
  49. filledDict.set("bar", "baz");
  50. expect(getInheritableProperty({ dict: filledDict, key: "foo" })).toEqual(
  51. undefined
  52. );
  53. });
  54. it("fetches the property if it is not inherited", function () {
  55. const ref = Ref.get(10, 0);
  56. const xref = new XRefMock([{ ref, data: "quux" }]);
  57. const dict = new Dict(xref);
  58. // Regular values should be fetched.
  59. dict.set("foo", "bar");
  60. expect(getInheritableProperty({ dict, key: "foo" })).toEqual("bar");
  61. // Array value should be fetched (with references resolved).
  62. dict.set("baz", ["qux", ref]);
  63. expect(
  64. getInheritableProperty({ dict, key: "baz", getArray: true })
  65. ).toEqual(["qux", "quux"]);
  66. });
  67. it("fetches the property if it is inherited and present on one level", function () {
  68. const ref = Ref.get(10, 0);
  69. const xref = new XRefMock([{ ref, data: "quux" }]);
  70. const firstDict = new Dict(xref);
  71. const secondDict = new Dict(xref);
  72. firstDict.set("Parent", secondDict);
  73. // Regular values should be fetched.
  74. secondDict.set("foo", "bar");
  75. expect(getInheritableProperty({ dict: firstDict, key: "foo" })).toEqual(
  76. "bar"
  77. );
  78. // Array value should be fetched (with references resolved).
  79. secondDict.set("baz", ["qux", ref]);
  80. expect(
  81. getInheritableProperty({ dict: firstDict, key: "baz", getArray: true })
  82. ).toEqual(["qux", "quux"]);
  83. });
  84. it("fetches the property if it is inherited and present on multiple levels", function () {
  85. const ref = Ref.get(10, 0);
  86. const xref = new XRefMock([{ ref, data: "quux" }]);
  87. const firstDict = new Dict(xref);
  88. const secondDict = new Dict(xref);
  89. firstDict.set("Parent", secondDict);
  90. // Regular values should be fetched.
  91. firstDict.set("foo", "bar1");
  92. secondDict.set("foo", "bar2");
  93. expect(getInheritableProperty({ dict: firstDict, key: "foo" })).toEqual(
  94. "bar1"
  95. );
  96. expect(
  97. getInheritableProperty({
  98. dict: firstDict,
  99. key: "foo",
  100. getArray: false,
  101. stopWhenFound: false,
  102. })
  103. ).toEqual(["bar1", "bar2"]);
  104. // Array value should be fetched (with references resolved).
  105. firstDict.set("baz", ["qux1", ref]);
  106. secondDict.set("baz", ["qux2", ref]);
  107. expect(
  108. getInheritableProperty({
  109. dict: firstDict,
  110. key: "baz",
  111. getArray: true,
  112. stopWhenFound: false,
  113. })
  114. ).toEqual([
  115. ["qux1", "quux"],
  116. ["qux2", "quux"],
  117. ]);
  118. });
  119. });
  120. describe("toRomanNumerals", function () {
  121. it("handles invalid arguments", function () {
  122. for (const input of ["foo", -1, 0]) {
  123. expect(function () {
  124. toRomanNumerals(input);
  125. }).toThrow(new Error("The number should be a positive integer."));
  126. }
  127. });
  128. it("converts numbers to uppercase Roman numerals", function () {
  129. expect(toRomanNumerals(1)).toEqual("I");
  130. expect(toRomanNumerals(6)).toEqual("VI");
  131. expect(toRomanNumerals(7)).toEqual("VII");
  132. expect(toRomanNumerals(8)).toEqual("VIII");
  133. expect(toRomanNumerals(10)).toEqual("X");
  134. expect(toRomanNumerals(40)).toEqual("XL");
  135. expect(toRomanNumerals(100)).toEqual("C");
  136. expect(toRomanNumerals(500)).toEqual("D");
  137. expect(toRomanNumerals(1000)).toEqual("M");
  138. expect(toRomanNumerals(2019)).toEqual("MMXIX");
  139. });
  140. it("converts numbers to lowercase Roman numerals", function () {
  141. expect(toRomanNumerals(1, /* lowercase = */ true)).toEqual("i");
  142. expect(toRomanNumerals(6, /* lowercase = */ true)).toEqual("vi");
  143. expect(toRomanNumerals(7, /* lowercase = */ true)).toEqual("vii");
  144. expect(toRomanNumerals(8, /* lowercase = */ true)).toEqual("viii");
  145. expect(toRomanNumerals(10, /* lowercase = */ true)).toEqual("x");
  146. expect(toRomanNumerals(40, /* lowercase = */ true)).toEqual("xl");
  147. expect(toRomanNumerals(100, /* lowercase = */ true)).toEqual("c");
  148. expect(toRomanNumerals(500, /* lowercase = */ true)).toEqual("d");
  149. expect(toRomanNumerals(1000, /* lowercase = */ true)).toEqual("m");
  150. expect(toRomanNumerals(2019, /* lowercase = */ true)).toEqual("mmxix");
  151. });
  152. });
  153. describe("log2", function () {
  154. it("handles values smaller than/equal to zero", function () {
  155. expect(log2(0)).toEqual(0);
  156. expect(log2(-1)).toEqual(0);
  157. });
  158. it("handles values larger than zero", function () {
  159. expect(log2(1)).toEqual(0);
  160. expect(log2(2)).toEqual(1);
  161. expect(log2(3)).toEqual(2);
  162. expect(log2(3.14)).toEqual(2);
  163. });
  164. });
  165. describe("isWhiteSpace", function () {
  166. it("handles space characters", function () {
  167. expect(isWhiteSpace(0x20)).toEqual(true);
  168. expect(isWhiteSpace(0x09)).toEqual(true);
  169. expect(isWhiteSpace(0x0d)).toEqual(true);
  170. expect(isWhiteSpace(0x0a)).toEqual(true);
  171. });
  172. it("handles non-space characters", function () {
  173. expect(isWhiteSpace(0x0b)).toEqual(false);
  174. expect(isWhiteSpace(null)).toEqual(false);
  175. expect(isWhiteSpace(undefined)).toEqual(false);
  176. });
  177. });
  178. describe("parseXFAPath", function () {
  179. it("should get a correctly parsed path", function () {
  180. const path = "foo.bar[12].oof[3].rab.FOO[123].BAR[456]";
  181. expect(parseXFAPath(path)).toEqual([
  182. { name: "foo", pos: 0 },
  183. { name: "bar", pos: 12 },
  184. { name: "oof", pos: 3 },
  185. { name: "rab", pos: 0 },
  186. { name: "FOO", pos: 123 },
  187. { name: "BAR", pos: 456 },
  188. ]);
  189. });
  190. });
  191. describe("escapePDFName", function () {
  192. it("should escape PDF name", function () {
  193. expect(escapePDFName("hello")).toEqual("hello");
  194. expect(escapePDFName("\xfehello")).toEqual("#fehello");
  195. expect(escapePDFName("he\xfell\xffo")).toEqual("he#fell#ffo");
  196. expect(escapePDFName("\xfehe\xfell\xffo\xff")).toEqual(
  197. "#fehe#fell#ffo#ff"
  198. );
  199. expect(escapePDFName("#h#e#l#l#o")).toEqual("#23h#23e#23l#23l#23o");
  200. expect(escapePDFName("#()<>[]{}/%")).toEqual(
  201. "#23#28#29#3c#3e#5b#5d#7b#7d#2f#25"
  202. );
  203. });
  204. });
  205. describe("escapeString", function () {
  206. it("should escape (, ), \\n, \\r, and \\", function () {
  207. expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual(
  208. "\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)"
  209. );
  210. });
  211. });
  212. describe("encodeToXmlString", function () {
  213. it("should get a correctly encoded string with some entities", function () {
  214. const str = "\"\u0397ell😂' & <W😂rld>";
  215. expect(encodeToXmlString(str)).toEqual(
  216. "&quot;&#x397;ell&#x1F602;&apos; &amp; &lt;W&#x1F602;rld&gt;"
  217. );
  218. });
  219. it("should get a correctly encoded basic ascii string", function () {
  220. const str = "hello world";
  221. expect(encodeToXmlString(str)).toEqual(str);
  222. });
  223. });
  224. describe("validateCSSFont", function () {
  225. it("Check font family", function () {
  226. const cssFontInfo = {
  227. fontFamily: `"blah blah " blah blah"`,
  228. fontWeight: 0,
  229. italicAngle: 0,
  230. };
  231. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  232. cssFontInfo.fontFamily = `"blah blah \\" blah blah"`;
  233. expect(validateCSSFont(cssFontInfo)).toEqual(true);
  234. cssFontInfo.fontFamily = `'blah blah ' blah blah'`;
  235. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  236. cssFontInfo.fontFamily = `'blah blah \\' blah blah'`;
  237. expect(validateCSSFont(cssFontInfo)).toEqual(true);
  238. cssFontInfo.fontFamily = `"blah blah `;
  239. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  240. cssFontInfo.fontFamily = `blah blah"`;
  241. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  242. cssFontInfo.fontFamily = `'blah blah `;
  243. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  244. cssFontInfo.fontFamily = `blah blah'`;
  245. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  246. cssFontInfo.fontFamily = "blah blah blah";
  247. expect(validateCSSFont(cssFontInfo)).toEqual(true);
  248. cssFontInfo.fontFamily = "blah 0blah blah";
  249. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  250. cssFontInfo.fontFamily = "blah blah -0blah";
  251. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  252. cssFontInfo.fontFamily = "blah blah --blah";
  253. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  254. cssFontInfo.fontFamily = "blah blah -blah";
  255. expect(validateCSSFont(cssFontInfo)).toEqual(true);
  256. cssFontInfo.fontFamily = "blah fdqAJqjHJK23kl23__--Kj blah";
  257. expect(validateCSSFont(cssFontInfo)).toEqual(true);
  258. cssFontInfo.fontFamily = "blah fdqAJqjH$JK23kl23__--Kj blah";
  259. expect(validateCSSFont(cssFontInfo)).toEqual(false);
  260. });
  261. it("Check font weight", function () {
  262. const cssFontInfo = {
  263. fontFamily: "blah",
  264. fontWeight: 100,
  265. italicAngle: 0,
  266. };
  267. validateCSSFont(cssFontInfo);
  268. expect(cssFontInfo.fontWeight).toEqual("100");
  269. cssFontInfo.fontWeight = "700";
  270. validateCSSFont(cssFontInfo);
  271. expect(cssFontInfo.fontWeight).toEqual("700");
  272. cssFontInfo.fontWeight = "normal";
  273. validateCSSFont(cssFontInfo);
  274. expect(cssFontInfo.fontWeight).toEqual("normal");
  275. cssFontInfo.fontWeight = 314;
  276. validateCSSFont(cssFontInfo);
  277. expect(cssFontInfo.fontWeight).toEqual("400");
  278. });
  279. it("Check italic angle", function () {
  280. const cssFontInfo = {
  281. fontFamily: "blah",
  282. fontWeight: 100,
  283. italicAngle: 10,
  284. };
  285. validateCSSFont(cssFontInfo);
  286. expect(cssFontInfo.italicAngle).toEqual("10");
  287. cssFontInfo.italicAngle = -123;
  288. validateCSSFont(cssFontInfo);
  289. expect(cssFontInfo.italicAngle).toEqual("14");
  290. cssFontInfo.italicAngle = "91";
  291. validateCSSFont(cssFontInfo);
  292. expect(cssFontInfo.italicAngle).toEqual("14");
  293. cssFontInfo.italicAngle = 2.718;
  294. validateCSSFont(cssFontInfo);
  295. expect(cssFontInfo.italicAngle).toEqual("2.718");
  296. });
  297. });
  298. describe("isAscii", function () {
  299. it("handles ascii/non-ascii strings", function () {
  300. expect(isAscii("hello world")).toEqual(true);
  301. expect(isAscii("こんにちは世界の")).toEqual(false);
  302. expect(isAscii("hello world in Japanese is こんにちは世界の")).toEqual(
  303. false
  304. );
  305. });
  306. });
  307. describe("stringToUTF16HexString", function () {
  308. it("should encode a string in UTF16 hexadecimal format", function () {
  309. expect(stringToUTF16HexString("hello world")).toEqual(
  310. "00680065006c006c006f00200077006f0072006c0064"
  311. );
  312. expect(stringToUTF16HexString("こんにちは世界の")).toEqual(
  313. "30533093306b3061306f4e16754c306e"
  314. );
  315. });
  316. });
  317. describe("stringToUTF16String", function () {
  318. it("should encode a string in UTF16", function () {
  319. expect(stringToUTF16String("hello world")).toEqual(
  320. "\0h\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d"
  321. );
  322. expect(stringToUTF16String("こんにちは世界の")).toEqual(
  323. "\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f\x4e\x16\x75\x4c\x30\x6e"
  324. );
  325. });
  326. it("should encode a string in UTF16BE with a BOM", function () {
  327. expect(
  328. stringToUTF16String("hello world", /* bigEndian = */ true)
  329. ).toEqual("\xfe\xff\0h\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d");
  330. expect(
  331. stringToUTF16String("こんにちは世界の", /* bigEndian = */ true)
  332. ).toEqual(
  333. "\xfe\xff\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f\x4e\x16\x75\x4c\x30\x6e"
  334. );
  335. });
  336. });
  337. });