parser_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 _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 as zero", function () {
  125. const input = new _stream.StringStream(".");
  126. const lexer = new _parser.Lexer(input);
  127. expect(lexer.getNumber()).toEqual(0);
  128. const numbers = ["..", "-.", "+.", "-\r\n.", "+\r\n."];
  129. for (const number of numbers) {
  130. const invalidInput = new _stream.StringStream(number);
  131. const invalidLexer = new _parser.Lexer(invalidInput);
  132. expect(function () {
  133. return invalidLexer.getNumber();
  134. }).toThrowError(_util.FormatError, /^Invalid number:\s/);
  135. }
  136. });
  137. it("should handle glued numbers and operators", function () {
  138. const input = new _stream.StringStream("123ET");
  139. const lexer = new _parser.Lexer(input);
  140. expect(lexer.getNumber()).toEqual(123);
  141. expect(lexer.currentChar).toEqual(0x45);
  142. });
  143. });
  144. describe("getString", function () {
  145. it("should stop parsing strings at the end of stream", function () {
  146. const input = new _stream.StringStream("(1$4)");
  147. input.getByte = function (super_getByte) {
  148. const ch = super_getByte.call(input);
  149. return ch === 0x24 ? -1 : ch;
  150. }.bind(input, input.getByte);
  151. const lexer = new _parser.Lexer(input);
  152. expect(lexer.getString()).toEqual("1");
  153. });
  154. it("should ignore escaped CR and LF", function () {
  155. const input = new _stream.StringStream("(\\101\\\r\n\\102\\\r\\103\\\n\\104)");
  156. const lexer = new _parser.Lexer(input);
  157. expect(lexer.getString()).toEqual("ABCD");
  158. });
  159. });
  160. describe("getHexString", function () {
  161. it("should not throw exception on bad input", function () {
  162. const input = new _stream.StringStream("<7 0 2 15 5 2 2 2 4 3 2 4>");
  163. const lexer = new _parser.Lexer(input);
  164. expect(lexer.getHexString()).toEqual('p!U"$2');
  165. });
  166. });
  167. describe("getName", function () {
  168. it("should handle Names with invalid usage of NUMBER SIGN (#)", function () {
  169. const inputNames = ["/# 680 0 R", "/#AQwerty", "/#A<</B"];
  170. const expectedNames = ["#", "#AQwerty", "#A"];
  171. for (let i = 0, ii = inputNames.length; i < ii; i++) {
  172. const input = new _stream.StringStream(inputNames[i]);
  173. const lexer = new _parser.Lexer(input);
  174. expect(lexer.getName()).toEqual(_primitives.Name.get(expectedNames[i]));
  175. }
  176. });
  177. });
  178. describe("getObj", function () {
  179. it("should stop immediately when the start of a command is " + "a non-visible ASCII character (issue 13999)", function () {
  180. const input = new _stream.StringStream("\x14q\nQ");
  181. const lexer = new _parser.Lexer(input);
  182. let obj = lexer.getObj();
  183. expect(obj instanceof _primitives.Cmd).toEqual(true);
  184. expect(obj.cmd).toEqual("\x14");
  185. obj = lexer.getObj();
  186. expect(obj instanceof _primitives.Cmd).toEqual(true);
  187. expect(obj.cmd).toEqual("q");
  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).toEqual(_primitives.EOF);
  193. });
  194. });
  195. });
  196. describe("Linearization", function () {
  197. it("should not find a linearization dictionary", function () {
  198. const stream1 = new _stream.StringStream("3 0 obj\n" + "<<\n" + "/Length 4622\n" + "/Filter /FlateDecode\n" + ">>\n" + "endobj");
  199. expect(_parser.Linearization.create(stream1)).toEqual(null);
  200. const stream2 = new _stream.StringStream("1 0 obj\n" + "<<\n" + "/Linearized 0\n" + ">>\n" + "endobj");
  201. expect(_parser.Linearization.create(stream2)).toEqual(null);
  202. });
  203. it("should accept a valid linearization dictionary", function () {
  204. 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");
  205. const expectedLinearizationDict = {
  206. length: 90,
  207. hints: [1388, 863],
  208. objectNumberFirst: 133,
  209. endFirst: 43573,
  210. numPages: 18,
  211. mainXRefEntriesOffset: 193883,
  212. pageFirst: 0
  213. };
  214. expect(_parser.Linearization.create(stream)).toEqual(expectedLinearizationDict);
  215. });
  216. it("should reject a linearization dictionary with invalid " + "integer parameters", function () {
  217. 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");
  218. expect(function () {
  219. return _parser.Linearization.create(stream1);
  220. }).toThrow(new Error('The "L" parameter in the linearization ' + "dictionary does not equal the stream length."));
  221. 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");
  222. expect(function () {
  223. return _parser.Linearization.create(stream2);
  224. }).toThrow(new Error('The "E" parameter in the linearization dictionary is invalid.'));
  225. 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");
  226. expect(function () {
  227. return _parser.Linearization.create(stream3);
  228. }).toThrow(new Error('The "O" parameter in the linearization dictionary is invalid.'));
  229. });
  230. it("should reject a linearization dictionary with invalid hint parameters", function () {
  231. 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");
  232. expect(function () {
  233. return _parser.Linearization.create(stream1);
  234. }).toThrow(new Error("Hint array in the linearization dictionary is invalid."));
  235. 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");
  236. expect(function () {
  237. return _parser.Linearization.create(stream2);
  238. }).toThrow(new Error("Hint array in the linearization dictionary is invalid."));
  239. 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");
  240. expect(function () {
  241. return _parser.Linearization.create(stream3);
  242. }).toThrow(new Error("Hint (2) in the linearization dictionary is invalid."));
  243. });
  244. });
  245. });