parser_spec.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var _parser = require('../../core/parser');
  17. var _primitives = require('../../core/primitives');
  18. var _stream = require('../../core/stream');
  19. describe('parser', function () {
  20. describe('Lexer', function () {
  21. it('should stop parsing numbers at the end of stream', function () {
  22. var input = new _stream.StringStream('11.234');
  23. var lexer = new _parser.Lexer(input);
  24. var result = lexer.getNumber();
  25. expect(result).toEqual(11.234);
  26. });
  27. it('should parse PostScript numbers', function () {
  28. var numbers = ['-.002', '34.5', '-3.62', '123.6e10', '1E-5', '-1.', '0.0', '123', '-98', '43445', '0', '+17'];
  29. for (var i = 0, ii = numbers.length; i < ii; i++) {
  30. var num = numbers[i];
  31. var input = new _stream.StringStream(num);
  32. var lexer = new _parser.Lexer(input);
  33. var result = lexer.getNumber();
  34. expect(result).toEqual(parseFloat(num));
  35. }
  36. });
  37. it('should ignore double negative before number', function () {
  38. var input = new _stream.StringStream('--205.88');
  39. var lexer = new _parser.Lexer(input);
  40. var result = lexer.getNumber();
  41. expect(result).toEqual(-205.88);
  42. });
  43. it('should handle glued numbers and operators', function () {
  44. var input = new _stream.StringStream('123ET');
  45. var lexer = new _parser.Lexer(input);
  46. var value = lexer.getNumber();
  47. expect(value).toEqual(123);
  48. expect(lexer.currentChar).toEqual(0x45);
  49. });
  50. it('should stop parsing strings at the end of stream', function () {
  51. var input = new _stream.StringStream('(1$4)');
  52. input.getByte = function (super_getByte) {
  53. var ch = super_getByte.call(input);
  54. return ch === 0x24 ? -1 : ch;
  55. }.bind(input, input.getByte);
  56. var lexer = new _parser.Lexer(input);
  57. var result = lexer.getString();
  58. expect(result).toEqual('1');
  59. });
  60. it('should not throw exception on bad input', function () {
  61. var input = new _stream.StringStream('<7 0 2 15 5 2 2 2 4 3 2 4>');
  62. var lexer = new _parser.Lexer(input);
  63. var result = lexer.getHexString();
  64. expect(result).toEqual('p!U"$2');
  65. });
  66. it('should ignore escaped CR and LF', function () {
  67. var input = new _stream.StringStream('(\\101\\\r\n\\102\\\r\\103\\\n\\104)');
  68. var lexer = new _parser.Lexer(input);
  69. var result = lexer.getString();
  70. expect(result).toEqual('ABCD');
  71. });
  72. it('should handle Names with invalid usage of NUMBER SIGN (#)', function () {
  73. var inputNames = ['/# 680 0 R', '/#AQwerty', '/#A<</B'];
  74. var expectedNames = ['#', '#AQwerty', '#A'];
  75. for (var i = 0, ii = inputNames.length; i < ii; i++) {
  76. var input = new _stream.StringStream(inputNames[i]);
  77. var lexer = new _parser.Lexer(input);
  78. var result = lexer.getName();
  79. expect(result).toEqual(_primitives.Name.get(expectedNames[i]));
  80. }
  81. });
  82. });
  83. describe('Linearization', function () {
  84. it('should not find a linearization dictionary', function () {
  85. var stream1 = new _stream.StringStream('3 0 obj\n' + '<<\n' + '/Length 4622\n' + '/Filter /FlateDecode\n' + '>>\n' + 'endobj');
  86. expect(_parser.Linearization.create(stream1)).toEqual(null);
  87. var stream2 = new _stream.StringStream('1 0 obj\n' + '<<\n' + '/Linearized 0\n' + '>>\n' + 'endobj');
  88. expect(_parser.Linearization.create(stream2)).toEqual(null);
  89. });
  90. it('should accept a valid linearization dictionary', function () {
  91. 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');
  92. var expectedLinearizationDict = {
  93. length: 90,
  94. hints: [1388, 863],
  95. objectNumberFirst: 133,
  96. endFirst: 43573,
  97. numPages: 18,
  98. mainXRefEntriesOffset: 193883,
  99. pageFirst: 0
  100. };
  101. expect(_parser.Linearization.create(stream)).toEqual(expectedLinearizationDict);
  102. });
  103. it('should reject a linearization dictionary with invalid ' + 'integer parameters', function () {
  104. 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');
  105. expect(function () {
  106. return _parser.Linearization.create(stream1);
  107. }).toThrow(new Error('The "L" parameter in the linearization ' + 'dictionary does not equal the stream length.'));
  108. 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');
  109. expect(function () {
  110. return _parser.Linearization.create(stream2);
  111. }).toThrow(new Error('The "E" parameter in the linearization ' + 'dictionary is invalid.'));
  112. 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');
  113. expect(function () {
  114. return _parser.Linearization.create(stream3);
  115. }).toThrow(new Error('The "O" parameter in the linearization ' + 'dictionary is invalid.'));
  116. });
  117. it('should reject a linearization dictionary with invalid hint parameters', function () {
  118. 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');
  119. expect(function () {
  120. return _parser.Linearization.create(stream1);
  121. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  122. 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');
  123. expect(function () {
  124. return _parser.Linearization.create(stream2);
  125. }).toThrow(new Error('Hint array in the linearization dictionary ' + 'is invalid.'));
  126. 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');
  127. expect(function () {
  128. return _parser.Linearization.create(stream3);
  129. }).toThrow(new Error('Hint (2) in the linearization dictionary ' + 'is invalid.'));
  130. });
  131. });
  132. });