2
0

parser_spec.js 7.5 KB

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