parser_spec.js 7.7 KB

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