parser_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 _parser = require("../../core/parser.js");
  25. var _util = require("../../shared/util.js");
  26. var _stream = require("../../core/stream.js");
  27. describe("parser", function () {
  28. describe("Parser", function () {
  29. describe("inlineStreamSkipEI", function () {
  30. it("should skip over the EI marker if it is found", function () {
  31. const string = "q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 " + "/F /A85 ID abc123~> EI Q";
  32. const input = new _stream.StringStream(string);
  33. const parser = new _parser.Parser({
  34. lexer: new _parser.Lexer(input),
  35. xref: null,
  36. allowStreams: true
  37. });
  38. parser.inlineStreamSkipEI(input);
  39. expect(input.pos).toEqual(string.indexOf("Q"));
  40. expect(input.peekByte()).toEqual(0x51);
  41. });
  42. it("should skip to the end of stream if the EI marker is not found", function () {
  43. const string = "q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 /F /A85 ID abc123~> Q";
  44. const input = new _stream.StringStream(string);
  45. const parser = new _parser.Parser({
  46. lexer: new _parser.Lexer(input),
  47. xref: null,
  48. allowStreams: true
  49. });
  50. parser.inlineStreamSkipEI(input);
  51. expect(input.pos).toEqual(string.length);
  52. expect(input.peekByte()).toEqual(-1);
  53. });
  54. });
  55. });
  56. describe("Lexer", function () {
  57. describe("nextChar", function () {
  58. it("should return and set -1 when the end of the stream is reached", function () {
  59. const input = new _stream.StringStream("");
  60. const lexer = new _parser.Lexer(input);
  61. expect(lexer.nextChar()).toEqual(-1);
  62. expect(lexer.currentChar).toEqual(-1);
  63. });
  64. it("should return and set the character after the current position", function () {
  65. const input = new _stream.StringStream("123");
  66. const lexer = new _parser.Lexer(input);
  67. expect(lexer.nextChar()).toEqual(0x32);
  68. expect(lexer.currentChar).toEqual(0x32);
  69. });
  70. });
  71. describe("peekChar", function () {
  72. it("should only return -1 when the end of the stream is reached", function () {
  73. const input = new _stream.StringStream("");
  74. const lexer = new _parser.Lexer(input);
  75. expect(lexer.peekChar()).toEqual(-1);
  76. expect(lexer.currentChar).toEqual(-1);
  77. });
  78. it("should only return the character after the current position", function () {
  79. const input = new _stream.StringStream("123");
  80. const lexer = new _parser.Lexer(input);
  81. expect(lexer.peekChar()).toEqual(0x32);
  82. expect(lexer.currentChar).toEqual(0x31);
  83. });
  84. });
  85. describe("getNumber", function () {
  86. it("should stop parsing numbers at the end of stream", function () {
  87. const input = new _stream.StringStream("11.234");
  88. const lexer = new _parser.Lexer(input);
  89. expect(lexer.getNumber()).toEqual(11.234);
  90. });
  91. it("should parse PostScript numbers", function () {
  92. const numbers = ["-.002", "34.5", "-3.62", "123.6e10", "1E-5", "-1.", "0.0", "123", "-98", "43445", "0", "+17"];
  93. for (const number of numbers) {
  94. const input = new _stream.StringStream(number);
  95. const lexer = new _parser.Lexer(input);
  96. const result = lexer.getNumber(),
  97. expected = parseFloat(number);
  98. if (result !== expected && Math.abs(result - expected) < 1e-15) {
  99. console.error(`Fuzzy matching "${result}" with "${expected}" to ` + "work-around rounding bugs in Chromium browsers.");
  100. expect(true).toEqual(true);
  101. continue;
  102. }
  103. expect(result).toEqual(expected);
  104. }
  105. });
  106. it("should ignore double negative before number", function () {
  107. const input = new _stream.StringStream("--205.88");
  108. const lexer = new _parser.Lexer(input);
  109. expect(lexer.getNumber()).toEqual(-205.88);
  110. });
  111. it("should ignore minus signs in the middle of number", function () {
  112. const input = new _stream.StringStream("205--.88");
  113. const lexer = new _parser.Lexer(input);
  114. expect(lexer.getNumber()).toEqual(205.88);
  115. });
  116. it("should ignore line-breaks between operator and digit in number", function () {
  117. const minusInput = new _stream.StringStream("-\r\n205.88");
  118. const minusLexer = new _parser.Lexer(minusInput);
  119. expect(minusLexer.getNumber()).toEqual(-205.88);
  120. const plusInput = new _stream.StringStream("+\r\n205.88");
  121. const plusLexer = new _parser.Lexer(plusInput);
  122. expect(plusLexer.getNumber()).toEqual(205.88);
  123. });
  124. it("should treat a single decimal point, or minus/plus sign, as zero", function () {
  125. const validNums = [".", "-", "+", "-.", "+.", "-\r\n.", "+\r\n."];
  126. for (const number of validNums) {
  127. const validInput = new _stream.StringStream(number);
  128. const validLexer = new _parser.Lexer(validInput);
  129. expect(validLexer.getNumber()).toEqual(0);
  130. }
  131. const invalidNums = ["..", ".-", ".+"];
  132. for (const number of invalidNums) {
  133. const invalidInput = new _stream.StringStream(number);
  134. const invalidLexer = new _parser.Lexer(invalidInput);
  135. expect(function () {
  136. return invalidLexer.getNumber();
  137. }).toThrowError(_util.FormatError, /^Invalid number:\s/);
  138. }
  139. });
  140. it("should handle glued numbers and operators", function () {
  141. const input = new _stream.StringStream("123ET");
  142. const lexer = new _parser.Lexer(input);
  143. expect(lexer.getNumber()).toEqual(123);
  144. expect(lexer.currentChar).toEqual(0x45);
  145. });
  146. });
  147. describe("getString", function () {
  148. it("should stop parsing strings at the end of stream", function () {
  149. const input = new _stream.StringStream("(1$4)");
  150. input.getByte = function (super_getByte) {
  151. const ch = super_getByte.call(input);
  152. return ch === 0x24 ? -1 : ch;
  153. }.bind(input, input.getByte);
  154. const lexer = new _parser.Lexer(input);
  155. expect(lexer.getString()).toEqual("1");
  156. });
  157. it("should ignore escaped CR and LF", function () {
  158. const input = new _stream.StringStream("(\\101\\\r\n\\102\\\r\\103\\\n\\104)");
  159. const lexer = new _parser.Lexer(input);
  160. expect(lexer.getString()).toEqual("ABCD");
  161. });
  162. });
  163. describe("getHexString", function () {
  164. it("should not throw exception on bad input", function () {
  165. const input = new _stream.StringStream("<7 0 2 15 5 2 2 2 4 3 2 4>");
  166. const lexer = new _parser.Lexer(input);
  167. expect(lexer.getHexString()).toEqual('p!U"$2');
  168. });
  169. });
  170. describe("getName", function () {
  171. it("should handle Names with invalid usage of NUMBER SIGN (#)", function () {
  172. const inputNames = ["/# 680 0 R", "/#AQwerty", "/#A<</B"];
  173. const expectedNames = ["#", "#AQwerty", "#A"];
  174. for (let i = 0, ii = inputNames.length; i < ii; i++) {
  175. const input = new _stream.StringStream(inputNames[i]);
  176. const lexer = new _parser.Lexer(input);
  177. expect(lexer.getName()).toEqual(_primitives.Name.get(expectedNames[i]));
  178. }
  179. });
  180. });
  181. describe("getObj", function () {
  182. it("should stop immediately when the start of a command is " + "a non-visible ASCII character (issue 13999)", function () {
  183. const input = new _stream.StringStream("\x14q\nQ");
  184. const lexer = new _parser.Lexer(input);
  185. let obj = lexer.getObj();
  186. expect(obj instanceof _primitives.Cmd).toEqual(true);
  187. expect(obj.cmd).toEqual("\x14");
  188. obj = lexer.getObj();
  189. expect(obj instanceof _primitives.Cmd).toEqual(true);
  190. expect(obj.cmd).toEqual("q");
  191. obj = lexer.getObj();
  192. expect(obj instanceof _primitives.Cmd).toEqual(true);
  193. expect(obj.cmd).toEqual("Q");
  194. obj = lexer.getObj();
  195. expect(obj).toEqual(_primitives.EOF);
  196. });
  197. });
  198. });
  199. describe("Linearization", function () {
  200. it("should not find a linearization dictionary", function () {
  201. const stream1 = new _stream.StringStream("3 0 obj\n" + "<<\n" + "/Length 4622\n" + "/Filter /FlateDecode\n" + ">>\n" + "endobj");
  202. expect(_parser.Linearization.create(stream1)).toEqual(null);
  203. const stream2 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 0\n" + ">>\n" + "endobj");
  204. expect(_parser.Linearization.create(stream2)).toEqual(null);
  205. });
  206. it("should accept a valid linearization dictionary", function () {
  207. const stream = new _stream.StringStream("131 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H [ 1388 863 ]\n" + "/L 90\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  208. const expectedLinearizationDict = {
  209. length: 90,
  210. hints: [1388, 863],
  211. objectNumberFirst: 133,
  212. endFirst: 43573,
  213. numPages: 18,
  214. mainXRefEntriesOffset: 193883,
  215. pageFirst: 0
  216. };
  217. expect(_parser.Linearization.create(stream)).toEqual(expectedLinearizationDict);
  218. });
  219. it("should reject a linearization dictionary with invalid " + "integer parameters", function () {
  220. const stream1 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H [ 1388 863 ]\n" + "/L 196622\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  221. expect(function () {
  222. return _parser.Linearization.create(stream1);
  223. }).toThrow(new Error('The "L" parameter in the linearization ' + "dictionary does not equal the stream length."));
  224. const stream2 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H [ 1388 863 ]\n" + "/L 84\n" + "/E 0\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  225. expect(function () {
  226. return _parser.Linearization.create(stream2);
  227. }).toThrow(new Error('The "E" parameter in the linearization dictionary is invalid.'));
  228. const stream3 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O /abc\n" + "/H [ 1388 863 ]\n" + "/L 89\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  229. expect(function () {
  230. return _parser.Linearization.create(stream3);
  231. }).toThrow(new Error('The "O" parameter in the linearization dictionary is invalid.'));
  232. });
  233. it("should reject a linearization dictionary with invalid hint parameters", function () {
  234. const stream1 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H 1388\n" + "/L 80\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  235. expect(function () {
  236. return _parser.Linearization.create(stream1);
  237. }).toThrow(new Error("Hint array in the linearization dictionary is invalid."));
  238. const stream2 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H [ 1388 ]\n" + "/L 84\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  239. expect(function () {
  240. return _parser.Linearization.create(stream2);
  241. }).toThrow(new Error("Hint array in the linearization dictionary is invalid."));
  242. const stream3 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 1\n" + "/O 133\n" + "/H [ 1388 863 0 234]\n" + "/L 93\n" + "/E 43573\n" + "/N 18\n" + "/T 193883\n" + ">>\n" + "endobj");
  243. expect(function () {
  244. return _parser.Linearization.create(stream3);
  245. }).toThrow(new Error("Hint (2) in the linearization dictionary is invalid."));
  246. });
  247. });
  248. });