2
0

parser_spec.js 6.8 KB

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