2
0

parser_spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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('Lexer', function () {
  29. it('should stop parsing numbers at the end of stream', function () {
  30. var input = new _stream.StringStream('11.234');
  31. var lexer = new _parser.Lexer(input);
  32. var result = lexer.getNumber();
  33. expect(result).toEqual(11.234);
  34. });
  35. it('should parse PostScript numbers', function () {
  36. var numbers = ['-.002', '34.5', '-3.62', '123.6e10', '1E-5', '-1.', '0.0', '123', '-98', '43445', '0', '+17'];
  37. for (var i = 0, ii = numbers.length; i < ii; i++) {
  38. var num = numbers[i];
  39. var input = new _stream.StringStream(num);
  40. var lexer = new _parser.Lexer(input);
  41. var result = lexer.getNumber();
  42. expect(result).toEqual(parseFloat(num));
  43. }
  44. });
  45. it('should ignore double negative before number', function () {
  46. var input = new _stream.StringStream('--205.88');
  47. var lexer = new _parser.Lexer(input);
  48. var result = lexer.getNumber();
  49. expect(result).toEqual(-205.88);
  50. });
  51. it('should ignore minus signs in the middle of number', function () {
  52. var input = new _stream.StringStream('205--.88');
  53. var lexer = new _parser.Lexer(input);
  54. var result = lexer.getNumber();
  55. expect(result).toEqual(205.88);
  56. });
  57. it('should ignore line-breaks between operator and digit in number', function () {
  58. var minusInput = new _stream.StringStream('-\r\n205.88');
  59. var minusLexer = new _parser.Lexer(minusInput);
  60. expect(minusLexer.getNumber()).toEqual(-205.88);
  61. var plusInput = new _stream.StringStream('+\r\n205.88');
  62. var plusLexer = new _parser.Lexer(plusInput);
  63. expect(plusLexer.getNumber()).toEqual(205.88);
  64. });
  65. it('should treat a single decimal point as zero', function () {
  66. var input = new _stream.StringStream('.');
  67. var lexer = new _parser.Lexer(input);
  68. expect(lexer.getNumber()).toEqual(0);
  69. var numbers = ['..', '-.', '+.', '-\r\n.', '+\r\n.'];
  70. var _loop = function _loop() {
  71. var number = numbers[_i];
  72. var input = new _stream.StringStream(number);
  73. var lexer = new _parser.Lexer(input);
  74. expect(function () {
  75. return lexer.getNumber();
  76. }).toThrowError(_util.FormatError, /^Invalid number:\s/);
  77. };
  78. for (var _i = 0; _i < numbers.length; _i++) {
  79. _loop();
  80. }
  81. });
  82. it('should handle glued numbers and operators', function () {
  83. var input = new _stream.StringStream('123ET');
  84. var lexer = new _parser.Lexer(input);
  85. var value = lexer.getNumber();
  86. expect(value).toEqual(123);
  87. expect(lexer.currentChar).toEqual(0x45);
  88. });
  89. it('should stop parsing strings at the end of stream', function () {
  90. var input = new _stream.StringStream('(1$4)');
  91. input.getByte = function (super_getByte) {
  92. var ch = super_getByte.call(input);
  93. return ch === 0x24 ? -1 : ch;
  94. }.bind(input, input.getByte);
  95. var lexer = new _parser.Lexer(input);
  96. var result = lexer.getString();
  97. expect(result).toEqual('1');
  98. });
  99. it('should not throw exception on bad input', function () {
  100. var input = new _stream.StringStream('<7 0 2 15 5 2 2 2 4 3 2 4>');
  101. var lexer = new _parser.Lexer(input);
  102. var result = lexer.getHexString();
  103. expect(result).toEqual('p!U"$2');
  104. });
  105. it('should ignore escaped CR and LF', function () {
  106. var input = new _stream.StringStream('(\\101\\\r\n\\102\\\r\\103\\\n\\104)');
  107. var lexer = new _parser.Lexer(input);
  108. var result = lexer.getString();
  109. expect(result).toEqual('ABCD');
  110. });
  111. it('should handle Names with invalid usage of NUMBER SIGN (#)', function () {
  112. var inputNames = ['/# 680 0 R', '/#AQwerty', '/#A<</B'];
  113. var expectedNames = ['#', '#AQwerty', '#A'];
  114. for (var i = 0, ii = inputNames.length; i < ii; i++) {
  115. var input = new _stream.StringStream(inputNames[i]);
  116. var lexer = new _parser.Lexer(input);
  117. var result = lexer.getName();
  118. expect(result).toEqual(_primitives.Name.get(expectedNames[i]));
  119. }
  120. });
  121. });
  122. describe('Linearization', function () {
  123. it('should not find a linearization dictionary', function () {
  124. var stream1 = new _stream.StringStream('3 0 obj\n' + '<<\n' + '/Length 4622\n' + '/Filter /FlateDecode\n' + '>>\n' + 'endobj');
  125. expect(_parser.Linearization.create(stream1)).toEqual(null);
  126. var stream2 = new _stream.StringStream('1 0 obj\n' + '<<\n' + '/Linearized 0\n' + '>>\n' + 'endobj');
  127. expect(_parser.Linearization.create(stream2)).toEqual(null);
  128. });
  129. it('should accept a valid linearization dictionary', function () {
  130. 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');
  131. var expectedLinearizationDict = {
  132. length: 90,
  133. hints: [1388, 863],
  134. objectNumberFirst: 133,
  135. endFirst: 43573,
  136. numPages: 18,
  137. mainXRefEntriesOffset: 193883,
  138. pageFirst: 0
  139. };
  140. expect(_parser.Linearization.create(stream)).toEqual(expectedLinearizationDict);
  141. });
  142. it('should reject a linearization dictionary with invalid ' + 'integer parameters', function () {
  143. 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');
  144. expect(function () {
  145. return _parser.Linearization.create(stream1);
  146. }).toThrow(new Error('The "L" parameter in the linearization ' + 'dictionary does not equal the stream length.'));
  147. 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');
  148. expect(function () {
  149. return _parser.Linearization.create(stream2);
  150. }).toThrow(new Error('The "E" parameter in the linearization ' + 'dictionary is invalid.'));
  151. 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');
  152. expect(function () {
  153. return _parser.Linearization.create(stream3);
  154. }).toThrow(new Error('The "O" parameter in the linearization ' + 'dictionary is invalid.'));
  155. });
  156. it('should reject a linearization dictionary with invalid hint parameters', function () {
  157. 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');
  158. expect(function () {
  159. return _parser.Linearization.create(stream1);
  160. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  161. 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');
  162. expect(function () {
  163. return _parser.Linearization.create(stream2);
  164. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  165. 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');
  166. expect(function () {
  167. return _parser.Linearization.create(stream3);
  168. }).toThrow(new Error('Hint (2) in the linearization dictionary ' + 'is invalid.'));
  169. });
  170. });
  171. });