UNPKG

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