2
0

core_utils_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 _primitives = require("../../core/primitives.js");
  24. var _core_utils = require("../../core/core_utils.js");
  25. var _test_utils = require("./test_utils.js");
  26. describe("core_utils", function () {
  27. describe("getInheritableProperty", function () {
  28. it("handles non-dictionary arguments", function () {
  29. expect((0, _core_utils.getInheritableProperty)({
  30. dict: null,
  31. key: "foo"
  32. })).toEqual(undefined);
  33. expect((0, _core_utils.getInheritableProperty)({
  34. dict: undefined,
  35. key: "foo"
  36. })).toEqual(undefined);
  37. });
  38. it("handles dictionaries that do not contain the property", function () {
  39. const emptyDict = new _primitives.Dict();
  40. expect((0, _core_utils.getInheritableProperty)({
  41. dict: emptyDict,
  42. key: "foo"
  43. })).toEqual(undefined);
  44. const filledDict = new _primitives.Dict();
  45. filledDict.set("bar", "baz");
  46. expect((0, _core_utils.getInheritableProperty)({
  47. dict: filledDict,
  48. key: "foo"
  49. })).toEqual(undefined);
  50. });
  51. it("fetches the property if it is not inherited", function () {
  52. const ref = _primitives.Ref.get(10, 0);
  53. const xref = new _test_utils.XRefMock([{
  54. ref,
  55. data: "quux"
  56. }]);
  57. const dict = new _primitives.Dict(xref);
  58. dict.set("foo", "bar");
  59. expect((0, _core_utils.getInheritableProperty)({
  60. dict,
  61. key: "foo"
  62. })).toEqual("bar");
  63. dict.set("baz", ["qux", ref]);
  64. expect((0, _core_utils.getInheritableProperty)({
  65. dict,
  66. key: "baz",
  67. getArray: true
  68. })).toEqual(["qux", "quux"]);
  69. });
  70. it("fetches the property if it is inherited and present on one level", function () {
  71. const ref = _primitives.Ref.get(10, 0);
  72. const xref = new _test_utils.XRefMock([{
  73. ref,
  74. data: "quux"
  75. }]);
  76. const firstDict = new _primitives.Dict(xref);
  77. const secondDict = new _primitives.Dict(xref);
  78. firstDict.set("Parent", secondDict);
  79. secondDict.set("foo", "bar");
  80. expect((0, _core_utils.getInheritableProperty)({
  81. dict: firstDict,
  82. key: "foo"
  83. })).toEqual("bar");
  84. secondDict.set("baz", ["qux", ref]);
  85. expect((0, _core_utils.getInheritableProperty)({
  86. dict: firstDict,
  87. key: "baz",
  88. getArray: true
  89. })).toEqual(["qux", "quux"]);
  90. });
  91. it("fetches the property if it is inherited and present on multiple levels", function () {
  92. const ref = _primitives.Ref.get(10, 0);
  93. const xref = new _test_utils.XRefMock([{
  94. ref,
  95. data: "quux"
  96. }]);
  97. const firstDict = new _primitives.Dict(xref);
  98. const secondDict = new _primitives.Dict(xref);
  99. firstDict.set("Parent", secondDict);
  100. firstDict.set("foo", "bar1");
  101. secondDict.set("foo", "bar2");
  102. expect((0, _core_utils.getInheritableProperty)({
  103. dict: firstDict,
  104. key: "foo"
  105. })).toEqual("bar1");
  106. expect((0, _core_utils.getInheritableProperty)({
  107. dict: firstDict,
  108. key: "foo",
  109. getArray: false,
  110. stopWhenFound: false
  111. })).toEqual(["bar1", "bar2"]);
  112. firstDict.set("baz", ["qux1", ref]);
  113. secondDict.set("baz", ["qux2", ref]);
  114. expect((0, _core_utils.getInheritableProperty)({
  115. dict: firstDict,
  116. key: "baz",
  117. getArray: true,
  118. stopWhenFound: false
  119. })).toEqual([["qux1", "quux"], ["qux2", "quux"]]);
  120. });
  121. });
  122. describe("toRomanNumerals", function () {
  123. it("handles invalid arguments", function () {
  124. for (const input of ["foo", -1, 0]) {
  125. expect(function () {
  126. (0, _core_utils.toRomanNumerals)(input);
  127. }).toThrow(new Error("The number should be a positive integer."));
  128. }
  129. });
  130. it("converts numbers to uppercase Roman numerals", function () {
  131. expect((0, _core_utils.toRomanNumerals)(1)).toEqual("I");
  132. expect((0, _core_utils.toRomanNumerals)(6)).toEqual("VI");
  133. expect((0, _core_utils.toRomanNumerals)(7)).toEqual("VII");
  134. expect((0, _core_utils.toRomanNumerals)(8)).toEqual("VIII");
  135. expect((0, _core_utils.toRomanNumerals)(10)).toEqual("X");
  136. expect((0, _core_utils.toRomanNumerals)(40)).toEqual("XL");
  137. expect((0, _core_utils.toRomanNumerals)(100)).toEqual("C");
  138. expect((0, _core_utils.toRomanNumerals)(500)).toEqual("D");
  139. expect((0, _core_utils.toRomanNumerals)(1000)).toEqual("M");
  140. expect((0, _core_utils.toRomanNumerals)(2019)).toEqual("MMXIX");
  141. });
  142. it("converts numbers to lowercase Roman numerals", function () {
  143. expect((0, _core_utils.toRomanNumerals)(1, true)).toEqual("i");
  144. expect((0, _core_utils.toRomanNumerals)(6, true)).toEqual("vi");
  145. expect((0, _core_utils.toRomanNumerals)(7, true)).toEqual("vii");
  146. expect((0, _core_utils.toRomanNumerals)(8, true)).toEqual("viii");
  147. expect((0, _core_utils.toRomanNumerals)(10, true)).toEqual("x");
  148. expect((0, _core_utils.toRomanNumerals)(40, true)).toEqual("xl");
  149. expect((0, _core_utils.toRomanNumerals)(100, true)).toEqual("c");
  150. expect((0, _core_utils.toRomanNumerals)(500, true)).toEqual("d");
  151. expect((0, _core_utils.toRomanNumerals)(1000, true)).toEqual("m");
  152. expect((0, _core_utils.toRomanNumerals)(2019, true)).toEqual("mmxix");
  153. });
  154. });
  155. describe("log2", function () {
  156. it("handles values smaller than/equal to zero", function () {
  157. expect((0, _core_utils.log2)(0)).toEqual(0);
  158. expect((0, _core_utils.log2)(-1)).toEqual(0);
  159. });
  160. it("handles values larger than zero", function () {
  161. expect((0, _core_utils.log2)(1)).toEqual(0);
  162. expect((0, _core_utils.log2)(2)).toEqual(1);
  163. expect((0, _core_utils.log2)(3)).toEqual(2);
  164. expect((0, _core_utils.log2)(3.14)).toEqual(2);
  165. });
  166. });
  167. describe("isWhiteSpace", function () {
  168. it("handles space characters", function () {
  169. expect((0, _core_utils.isWhiteSpace)(0x20)).toEqual(true);
  170. expect((0, _core_utils.isWhiteSpace)(0x09)).toEqual(true);
  171. expect((0, _core_utils.isWhiteSpace)(0x0d)).toEqual(true);
  172. expect((0, _core_utils.isWhiteSpace)(0x0a)).toEqual(true);
  173. });
  174. it("handles non-space characters", function () {
  175. expect((0, _core_utils.isWhiteSpace)(0x0b)).toEqual(false);
  176. expect((0, _core_utils.isWhiteSpace)(null)).toEqual(false);
  177. expect((0, _core_utils.isWhiteSpace)(undefined)).toEqual(false);
  178. });
  179. });
  180. describe("parseXFAPath", function () {
  181. it("should get a correctly parsed path", function () {
  182. const path = "foo.bar[12].oof[3].rab.FOO[123].BAR[456]";
  183. expect((0, _core_utils.parseXFAPath)(path)).toEqual([{
  184. name: "foo",
  185. pos: 0
  186. }, {
  187. name: "bar",
  188. pos: 12
  189. }, {
  190. name: "oof",
  191. pos: 3
  192. }, {
  193. name: "rab",
  194. pos: 0
  195. }, {
  196. name: "FOO",
  197. pos: 123
  198. }, {
  199. name: "BAR",
  200. pos: 456
  201. }]);
  202. });
  203. });
  204. describe("escapePDFName", function () {
  205. it("should escape PDF name", function () {
  206. expect((0, _core_utils.escapePDFName)("hello")).toEqual("hello");
  207. expect((0, _core_utils.escapePDFName)("\xfehello")).toEqual("#fehello");
  208. expect((0, _core_utils.escapePDFName)("he\xfell\xffo")).toEqual("he#fell#ffo");
  209. expect((0, _core_utils.escapePDFName)("\xfehe\xfell\xffo\xff")).toEqual("#fehe#fell#ffo#ff");
  210. expect((0, _core_utils.escapePDFName)("#h#e#l#l#o")).toEqual("#23h#23e#23l#23l#23o");
  211. expect((0, _core_utils.escapePDFName)("#()<>[]{}/%")).toEqual("#23#28#29#3c#3e#5b#5d#7b#7d#2f#25");
  212. });
  213. });
  214. describe("encodeToXmlString", function () {
  215. it("should get a correctly encoded string with some entities", function () {
  216. const str = "\"\u0397ell😂' & <W😂rld>";
  217. expect((0, _core_utils.encodeToXmlString)(str)).toEqual("&quot;&#x397;ell&#x1F602;&apos; &amp; &lt;W&#x1F602;rld&gt;");
  218. });
  219. it("should get a correctly encoded basic ascii string", function () {
  220. const str = "hello world";
  221. expect((0, _core_utils.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((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  232. cssFontInfo.fontFamily = `"blah blah \\" blah blah"`;
  233. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(true);
  234. cssFontInfo.fontFamily = `'blah blah ' blah blah'`;
  235. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  236. cssFontInfo.fontFamily = `'blah blah \\' blah blah'`;
  237. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(true);
  238. cssFontInfo.fontFamily = `"blah blah `;
  239. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  240. cssFontInfo.fontFamily = `blah blah"`;
  241. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  242. cssFontInfo.fontFamily = `'blah blah `;
  243. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  244. cssFontInfo.fontFamily = `blah blah'`;
  245. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  246. cssFontInfo.fontFamily = "blah blah blah";
  247. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(true);
  248. cssFontInfo.fontFamily = "blah 0blah blah";
  249. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  250. cssFontInfo.fontFamily = "blah blah -0blah";
  251. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  252. cssFontInfo.fontFamily = "blah blah --blah";
  253. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(false);
  254. cssFontInfo.fontFamily = "blah blah -blah";
  255. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(true);
  256. cssFontInfo.fontFamily = "blah fdqAJqjHJK23kl23__--Kj blah";
  257. expect((0, _core_utils.validateCSSFont)(cssFontInfo)).toEqual(true);
  258. cssFontInfo.fontFamily = "blah fdqAJqjH$JK23kl23__--Kj blah";
  259. expect((0, _core_utils.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. (0, _core_utils.validateCSSFont)(cssFontInfo);
  268. expect(cssFontInfo.fontWeight).toEqual("100");
  269. cssFontInfo.fontWeight = "700";
  270. (0, _core_utils.validateCSSFont)(cssFontInfo);
  271. expect(cssFontInfo.fontWeight).toEqual("700");
  272. cssFontInfo.fontWeight = "normal";
  273. (0, _core_utils.validateCSSFont)(cssFontInfo);
  274. expect(cssFontInfo.fontWeight).toEqual("normal");
  275. cssFontInfo.fontWeight = 314;
  276. (0, _core_utils.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. (0, _core_utils.validateCSSFont)(cssFontInfo);
  286. expect(cssFontInfo.italicAngle).toEqual("10");
  287. cssFontInfo.italicAngle = -123;
  288. (0, _core_utils.validateCSSFont)(cssFontInfo);
  289. expect(cssFontInfo.italicAngle).toEqual("14");
  290. cssFontInfo.italicAngle = "91";
  291. (0, _core_utils.validateCSSFont)(cssFontInfo);
  292. expect(cssFontInfo.italicAngle).toEqual("14");
  293. cssFontInfo.italicAngle = 2.718;
  294. (0, _core_utils.validateCSSFont)(cssFontInfo);
  295. expect(cssFontInfo.italicAngle).toEqual("2.718");
  296. });
  297. });
  298. });