parser_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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 _parser = require("../../core/parser");
  24. var _util = require("../../shared/util");
  25. var _primitives = require("../../core/primitives");
  26. var _stream = require("../../core/stream");
  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. var string = 'q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 ' + '/F /A85 ID abc123~> EI Q';
  32. var input = new _stream.StringStream(string);
  33. var 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. var string = 'q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 ' + '/F /A85 ID abc123~> Q';
  44. var input = new _stream.StringStream(string);
  45. var 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. var input = new _stream.StringStream('');
  60. var 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. var input = new _stream.StringStream('123');
  66. var 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. var input = new _stream.StringStream('');
  74. var 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. var input = new _stream.StringStream('123');
  80. var 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. var input = new _stream.StringStream('11.234');
  88. var lexer = new _parser.Lexer(input);
  89. expect(lexer.getNumber()).toEqual(11.234);
  90. });
  91. it('should parse PostScript numbers', function () {
  92. var numbers = ['-.002', '34.5', '-3.62', '123.6e10', '1E-5', '-1.', '0.0', '123', '-98', '43445', '0', '+17'];
  93. for (var _i = 0, _numbers = numbers; _i < _numbers.length; _i++) {
  94. var number = _numbers[_i];
  95. var input = new _stream.StringStream(number);
  96. var lexer = new _parser.Lexer(input);
  97. var result = lexer.getNumber(),
  98. expected = parseFloat(number);
  99. if (result !== expected && Math.abs(result - expected) < 1e-15) {
  100. console.error("Fuzzy matching \"".concat(result, "\" with \"").concat(expected, "\" to ") + 'work-around rounding bugs in Chromium browsers.');
  101. expect(true).toEqual(true);
  102. continue;
  103. }
  104. expect(result).toEqual(expected);
  105. }
  106. });
  107. it('should ignore double negative before number', function () {
  108. var input = new _stream.StringStream('--205.88');
  109. var lexer = new _parser.Lexer(input);
  110. expect(lexer.getNumber()).toEqual(-205.88);
  111. });
  112. it('should ignore minus signs in the middle of number', function () {
  113. var input = new _stream.StringStream('205--.88');
  114. var lexer = new _parser.Lexer(input);
  115. expect(lexer.getNumber()).toEqual(205.88);
  116. });
  117. it('should ignore line-breaks between operator and digit in number', function () {
  118. var minusInput = new _stream.StringStream('-\r\n205.88');
  119. var minusLexer = new _parser.Lexer(minusInput);
  120. expect(minusLexer.getNumber()).toEqual(-205.88);
  121. var plusInput = new _stream.StringStream('+\r\n205.88');
  122. var plusLexer = new _parser.Lexer(plusInput);
  123. expect(plusLexer.getNumber()).toEqual(205.88);
  124. });
  125. it('should treat a single decimal point as zero', function () {
  126. var input = new _stream.StringStream('.');
  127. var lexer = new _parser.Lexer(input);
  128. expect(lexer.getNumber()).toEqual(0);
  129. var numbers = ['..', '-.', '+.', '-\r\n.', '+\r\n.'];
  130. var _loop = function _loop() {
  131. var number = _numbers2[_i2];
  132. var input = new _stream.StringStream(number);
  133. var lexer = new _parser.Lexer(input);
  134. expect(function () {
  135. return lexer.getNumber();
  136. }).toThrowError(_util.FormatError, /^Invalid number:\s/);
  137. };
  138. for (var _i2 = 0, _numbers2 = numbers; _i2 < _numbers2.length; _i2++) {
  139. _loop();
  140. }
  141. });
  142. it('should handle glued numbers and operators', function () {
  143. var input = new _stream.StringStream('123ET');
  144. var lexer = new _parser.Lexer(input);
  145. expect(lexer.getNumber()).toEqual(123);
  146. expect(lexer.currentChar).toEqual(0x45);
  147. });
  148. });
  149. describe('getString', function () {
  150. it('should stop parsing strings at the end of stream', function () {
  151. var input = new _stream.StringStream('(1$4)');
  152. input.getByte = function (super_getByte) {
  153. var ch = super_getByte.call(input);
  154. return ch === 0x24 ? -1 : ch;
  155. }.bind(input, input.getByte);
  156. var lexer = new _parser.Lexer(input);
  157. expect(lexer.getString()).toEqual('1');
  158. });
  159. it('should ignore escaped CR and LF', function () {
  160. var input = new _stream.StringStream('(\\101\\\r\n\\102\\\r\\103\\\n\\104)');
  161. var lexer = new _parser.Lexer(input);
  162. expect(lexer.getString()).toEqual('ABCD');
  163. });
  164. });
  165. describe('getHexString', function () {
  166. it('should not throw exception on bad input', function () {
  167. var input = new _stream.StringStream('<7 0 2 15 5 2 2 2 4 3 2 4>');
  168. var lexer = new _parser.Lexer(input);
  169. expect(lexer.getHexString()).toEqual('p!U"$2');
  170. });
  171. });
  172. describe('getName', function () {
  173. it('should handle Names with invalid usage of NUMBER SIGN (#)', function () {
  174. var inputNames = ['/# 680 0 R', '/#AQwerty', '/#A<</B'];
  175. var expectedNames = ['#', '#AQwerty', '#A'];
  176. for (var i = 0, ii = inputNames.length; i < ii; i++) {
  177. var input = new _stream.StringStream(inputNames[i]);
  178. var lexer = new _parser.Lexer(input);
  179. expect(lexer.getName()).toEqual(_primitives.Name.get(expectedNames[i]));
  180. }
  181. });
  182. });
  183. });
  184. describe('Linearization', function () {
  185. it('should not find a linearization dictionary', function () {
  186. var stream1 = new _stream.StringStream('3 0 obj\n' + '<<\n' + '/Length 4622\n' + '/Filter /FlateDecode\n' + '>>\n' + 'endobj');
  187. expect(_parser.Linearization.create(stream1)).toEqual(null);
  188. var stream2 = new _stream.StringStream('1 0 obj\n' + '<<\n' + '/Linearized 0\n' + '>>\n' + 'endobj');
  189. expect(_parser.Linearization.create(stream2)).toEqual(null);
  190. });
  191. it('should accept a valid linearization dictionary', function () {
  192. var 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');
  193. var expectedLinearizationDict = {
  194. length: 90,
  195. hints: [1388, 863],
  196. objectNumberFirst: 133,
  197. endFirst: 43573,
  198. numPages: 18,
  199. mainXRefEntriesOffset: 193883,
  200. pageFirst: 0
  201. };
  202. expect(_parser.Linearization.create(stream)).toEqual(expectedLinearizationDict);
  203. });
  204. it('should reject a linearization dictionary with invalid ' + 'integer parameters', function () {
  205. var 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');
  206. expect(function () {
  207. return _parser.Linearization.create(stream1);
  208. }).toThrow(new Error('The "L" parameter in the linearization ' + 'dictionary does not equal the stream length.'));
  209. var 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');
  210. expect(function () {
  211. return _parser.Linearization.create(stream2);
  212. }).toThrow(new Error('The "E" parameter in the linearization ' + 'dictionary is invalid.'));
  213. var 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');
  214. expect(function () {
  215. return _parser.Linearization.create(stream3);
  216. }).toThrow(new Error('The "O" parameter in the linearization ' + 'dictionary is invalid.'));
  217. });
  218. it('should reject a linearization dictionary with invalid hint parameters', function () {
  219. var 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');
  220. expect(function () {
  221. return _parser.Linearization.create(stream1);
  222. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  223. var 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');
  224. expect(function () {
  225. return _parser.Linearization.create(stream2);
  226. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  227. var 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');
  228. expect(function () {
  229. return _parser.Linearization.create(stream3);
  230. }).toThrow(new Error('Hint (2) in the linearization dictionary ' + 'is invalid.'));
  231. });
  232. });
  233. });