parser_spec.js 6.6 KB

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