2
0

annotation_spec.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. var _annotation = require('../../core/annotation');
  24. var _util = require('../../shared/util');
  25. var _primitives = require('../../core/primitives');
  26. var _parser = require('../../core/parser');
  27. var _stream = require('../../core/stream');
  28. var _test_utils = require('./test_utils');
  29. describe('annotation', function () {
  30. function PDFManagerMock(params) {
  31. this.docBaseUrl = params.docBaseUrl || null;
  32. }
  33. PDFManagerMock.prototype = {};
  34. function IdFactoryMock(params) {
  35. var uniquePrefix = params.prefix || 'p0_';
  36. var idCounters = { obj: params.startObjId || 0 };
  37. return {
  38. createObjId: function createObjId() {
  39. return uniquePrefix + ++idCounters.obj;
  40. }
  41. };
  42. }
  43. IdFactoryMock.prototype = {};
  44. var pdfManagerMock, idFactoryMock;
  45. beforeAll(function (done) {
  46. pdfManagerMock = new PDFManagerMock({ docBaseUrl: null });
  47. idFactoryMock = new IdFactoryMock({});
  48. done();
  49. });
  50. afterAll(function () {
  51. pdfManagerMock = null;
  52. idFactoryMock = null;
  53. });
  54. describe('AnnotationFactory', function () {
  55. it('should get id for annotation', function () {
  56. var annotationDict = new _primitives.Dict();
  57. annotationDict.set('Type', _primitives.Name.get('Annot'));
  58. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  59. var annotationRef = new _primitives.Ref(10, 0);
  60. var xref = new _test_utils.XRefMock([{
  61. ref: annotationRef,
  62. data: annotationDict
  63. }]);
  64. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  65. var data = annotation.data;
  66. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  67. expect(data.id).toEqual('10R');
  68. });
  69. it('should handle, and get fallback id\'s for, annotations that are not ' + 'indirect objects (issue 7569)', function () {
  70. var annotationDict = new _primitives.Dict();
  71. annotationDict.set('Type', _primitives.Name.get('Annot'));
  72. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  73. var xref = new _test_utils.XRefMock();
  74. var idFactory = new IdFactoryMock({
  75. prefix: 'p0_',
  76. startObjId: 0
  77. });
  78. var annotation1 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory);
  79. var annotation2 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory);
  80. var data1 = annotation1.data,
  81. data2 = annotation2.data;
  82. expect(data1.annotationType).toEqual(_util.AnnotationType.LINK);
  83. expect(data2.annotationType).toEqual(_util.AnnotationType.LINK);
  84. expect(data1.id).toEqual('annot_p0_1');
  85. expect(data2.id).toEqual('annot_p0_2');
  86. });
  87. it('should handle missing /Subtype', function () {
  88. var annotationDict = new _primitives.Dict();
  89. annotationDict.set('Type', _primitives.Name.get('Annot'));
  90. var annotationRef = new _primitives.Ref(1, 0);
  91. var xref = new _test_utils.XRefMock([{
  92. ref: annotationRef,
  93. data: annotationDict
  94. }]);
  95. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  96. var data = annotation.data;
  97. expect(data.annotationType).toBeUndefined();
  98. });
  99. });
  100. describe('Annotation', function () {
  101. var dict, ref;
  102. beforeAll(function (done) {
  103. dict = new _primitives.Dict();
  104. ref = new _primitives.Ref(1, 0);
  105. done();
  106. });
  107. afterAll(function () {
  108. dict = ref = null;
  109. });
  110. it('should set and get flags', function () {
  111. var annotation = new _annotation.Annotation({
  112. dict: dict,
  113. ref: ref
  114. });
  115. annotation.setFlags(13);
  116. expect(annotation.hasFlag(_util.AnnotationFlag.INVISIBLE)).toEqual(true);
  117. expect(annotation.hasFlag(_util.AnnotationFlag.NOZOOM)).toEqual(true);
  118. expect(annotation.hasFlag(_util.AnnotationFlag.PRINT)).toEqual(true);
  119. expect(annotation.hasFlag(_util.AnnotationFlag.READONLY)).toEqual(false);
  120. });
  121. it('should be viewable and not printable by default', function () {
  122. var annotation = new _annotation.Annotation({
  123. dict: dict,
  124. ref: ref
  125. });
  126. expect(annotation.viewable).toEqual(true);
  127. expect(annotation.printable).toEqual(false);
  128. });
  129. it('should set and get a valid rectangle', function () {
  130. var annotation = new _annotation.Annotation({
  131. dict: dict,
  132. ref: ref
  133. });
  134. annotation.setRectangle([117, 694, 164.298, 720]);
  135. expect(annotation.rectangle).toEqual([117, 694, 164.298, 720]);
  136. });
  137. it('should not set and get an invalid rectangle', function () {
  138. var annotation = new _annotation.Annotation({
  139. dict: dict,
  140. ref: ref
  141. });
  142. annotation.setRectangle([117, 694, 164.298]);
  143. expect(annotation.rectangle).toEqual([0, 0, 0, 0]);
  144. });
  145. it('should reject a color if it is not an array', function () {
  146. var annotation = new _annotation.Annotation({
  147. dict: dict,
  148. ref: ref
  149. });
  150. annotation.setColor('red');
  151. expect(annotation.color).toEqual(new Uint8Array([0, 0, 0]));
  152. });
  153. it('should set and get a transparent color', function () {
  154. var annotation = new _annotation.Annotation({
  155. dict: dict,
  156. ref: ref
  157. });
  158. annotation.setColor([]);
  159. expect(annotation.color).toEqual(null);
  160. });
  161. it('should set and get a grayscale color', function () {
  162. var annotation = new _annotation.Annotation({
  163. dict: dict,
  164. ref: ref
  165. });
  166. annotation.setColor([0.4]);
  167. expect(annotation.color).toEqual(new Uint8Array([102, 102, 102]));
  168. });
  169. it('should set and get an RGB color', function () {
  170. var annotation = new _annotation.Annotation({
  171. dict: dict,
  172. ref: ref
  173. });
  174. annotation.setColor([0, 0, 1]);
  175. expect(annotation.color).toEqual(new Uint8Array([0, 0, 255]));
  176. });
  177. it('should set and get a CMYK color', function () {
  178. var annotation = new _annotation.Annotation({
  179. dict: dict,
  180. ref: ref
  181. });
  182. annotation.setColor([0.1, 0.92, 0.84, 0.02]);
  183. expect(annotation.color).toEqual(new Uint8Array([233, 59, 47]));
  184. });
  185. it('should not set and get an invalid color', function () {
  186. var annotation = new _annotation.Annotation({
  187. dict: dict,
  188. ref: ref
  189. });
  190. annotation.setColor([0.4, 0.6]);
  191. expect(annotation.color).toEqual(new Uint8Array([0, 0, 0]));
  192. });
  193. });
  194. describe('AnnotationBorderStyle', function () {
  195. it('should set and get a valid width', function () {
  196. var borderStyle = new _annotation.AnnotationBorderStyle();
  197. borderStyle.setWidth(3);
  198. expect(borderStyle.width).toEqual(3);
  199. });
  200. it('should not set and get an invalid width', function () {
  201. var borderStyle = new _annotation.AnnotationBorderStyle();
  202. borderStyle.setWidth('three');
  203. expect(borderStyle.width).toEqual(1);
  204. });
  205. it('should set and get a valid style', function () {
  206. var borderStyle = new _annotation.AnnotationBorderStyle();
  207. borderStyle.setStyle(_primitives.Name.get('D'));
  208. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.DASHED);
  209. });
  210. it('should not set and get an invalid style', function () {
  211. var borderStyle = new _annotation.AnnotationBorderStyle();
  212. borderStyle.setStyle('Dashed');
  213. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.SOLID);
  214. });
  215. it('should set and get a valid dash array', function () {
  216. var borderStyle = new _annotation.AnnotationBorderStyle();
  217. borderStyle.setDashArray([1, 2, 3]);
  218. expect(borderStyle.dashArray).toEqual([1, 2, 3]);
  219. });
  220. it('should not set and get an invalid dash array', function () {
  221. var borderStyle = new _annotation.AnnotationBorderStyle();
  222. borderStyle.setDashArray([0, 0]);
  223. expect(borderStyle.dashArray).toEqual([3]);
  224. });
  225. it('should set and get a valid horizontal corner radius', function () {
  226. var borderStyle = new _annotation.AnnotationBorderStyle();
  227. borderStyle.setHorizontalCornerRadius(3);
  228. expect(borderStyle.horizontalCornerRadius).toEqual(3);
  229. });
  230. it('should not set and get an invalid horizontal corner radius', function () {
  231. var borderStyle = new _annotation.AnnotationBorderStyle();
  232. borderStyle.setHorizontalCornerRadius('three');
  233. expect(borderStyle.horizontalCornerRadius).toEqual(0);
  234. });
  235. it('should set and get a valid vertical corner radius', function () {
  236. var borderStyle = new _annotation.AnnotationBorderStyle();
  237. borderStyle.setVerticalCornerRadius(3);
  238. expect(borderStyle.verticalCornerRadius).toEqual(3);
  239. });
  240. it('should not set and get an invalid horizontal corner radius', function () {
  241. var borderStyle = new _annotation.AnnotationBorderStyle();
  242. borderStyle.setVerticalCornerRadius('three');
  243. expect(borderStyle.verticalCornerRadius).toEqual(0);
  244. });
  245. });
  246. describe('LinkAnnotation', function () {
  247. it('should correctly parse a URI action', function () {
  248. var actionDict = new _primitives.Dict();
  249. actionDict.set('Type', _primitives.Name.get('Action'));
  250. actionDict.set('S', _primitives.Name.get('URI'));
  251. actionDict.set('URI', 'http://www.ctan.org/tex-archive/info/lshort');
  252. var annotationDict = new _primitives.Dict();
  253. annotationDict.set('Type', _primitives.Name.get('Annot'));
  254. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  255. annotationDict.set('A', actionDict);
  256. var annotationRef = new _primitives.Ref(820, 0);
  257. var xref = new _test_utils.XRefMock([{
  258. ref: annotationRef,
  259. data: annotationDict
  260. }]);
  261. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  262. var data = annotation.data;
  263. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  264. expect(data.url).toEqual('http://www.ctan.org/tex-archive/info/lshort');
  265. expect(data.unsafeUrl).toEqual('http://www.ctan.org/tex-archive/info/lshort');
  266. expect(data.dest).toBeUndefined();
  267. });
  268. it('should correctly parse a URI action, where the URI entry ' + 'is missing a protocol', function () {
  269. var actionDict = new _primitives.Dict();
  270. actionDict.set('Type', _primitives.Name.get('Action'));
  271. actionDict.set('S', _primitives.Name.get('URI'));
  272. actionDict.set('URI', 'www.hmrc.gov.uk');
  273. var annotationDict = new _primitives.Dict();
  274. annotationDict.set('Type', _primitives.Name.get('Annot'));
  275. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  276. annotationDict.set('A', actionDict);
  277. var annotationRef = new _primitives.Ref(353, 0);
  278. var xref = new _test_utils.XRefMock([{
  279. ref: annotationRef,
  280. data: annotationDict
  281. }]);
  282. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  283. var data = annotation.data;
  284. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  285. expect(data.url).toEqual('http://www.hmrc.gov.uk/');
  286. expect(data.unsafeUrl).toEqual('http://www.hmrc.gov.uk');
  287. expect(data.dest).toBeUndefined();
  288. });
  289. it('should correctly parse a URI action, where the URI entry ' + 'has an incorrect encoding (bug 1122280)', function () {
  290. var actionStream = new _stream.StringStream('<<\n' + '/Type /Action\n' + '/S /URI\n' + '/URI (http://www.example.com/\\303\\274\\303\\266\\303\\244)\n' + '>>\n');
  291. var lexer = new _parser.Lexer(actionStream);
  292. var parser = new _parser.Parser(lexer);
  293. var actionDict = parser.getObj();
  294. var annotationDict = new _primitives.Dict();
  295. annotationDict.set('Type', _primitives.Name.get('Annot'));
  296. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  297. annotationDict.set('A', actionDict);
  298. var annotationRef = new _primitives.Ref(8, 0);
  299. var xref = new _test_utils.XRefMock([{
  300. ref: annotationRef,
  301. data: annotationDict
  302. }]);
  303. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  304. var data = annotation.data;
  305. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  306. expect(data.url).toEqual(new URL((0, _util.stringToUTF8String)('http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4')).href);
  307. expect(data.unsafeUrl).toEqual((0, _util.stringToUTF8String)('http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4'));
  308. expect(data.dest).toBeUndefined();
  309. });
  310. it('should correctly parse a GoTo action', function () {
  311. var actionDict = new _primitives.Dict();
  312. actionDict.set('Type', _primitives.Name.get('Action'));
  313. actionDict.set('S', _primitives.Name.get('GoTo'));
  314. actionDict.set('D', 'page.157');
  315. var annotationDict = new _primitives.Dict();
  316. annotationDict.set('Type', _primitives.Name.get('Annot'));
  317. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  318. annotationDict.set('A', actionDict);
  319. var annotationRef = new _primitives.Ref(798, 0);
  320. var xref = new _test_utils.XRefMock([{
  321. ref: annotationRef,
  322. data: annotationDict
  323. }]);
  324. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  325. var data = annotation.data;
  326. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  327. expect(data.url).toBeUndefined();
  328. expect(data.unsafeUrl).toBeUndefined();
  329. expect(data.dest).toEqual('page.157');
  330. });
  331. it('should correctly parse a GoToR action, where the FileSpec entry ' + 'is a string containing a relative URL', function () {
  332. var actionDict = new _primitives.Dict();
  333. actionDict.set('Type', _primitives.Name.get('Action'));
  334. actionDict.set('S', _primitives.Name.get('GoToR'));
  335. actionDict.set('F', '../../0013/001346/134685E.pdf');
  336. actionDict.set('D', '4.3');
  337. actionDict.set('NewWindow', true);
  338. var annotationDict = new _primitives.Dict();
  339. annotationDict.set('Type', _primitives.Name.get('Annot'));
  340. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  341. annotationDict.set('A', actionDict);
  342. var annotationRef = new _primitives.Ref(489, 0);
  343. var xref = new _test_utils.XRefMock([{
  344. ref: annotationRef,
  345. data: annotationDict
  346. }]);
  347. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  348. var data = annotation.data;
  349. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  350. expect(data.url).toBeUndefined();
  351. expect(data.unsafeUrl).toEqual('../../0013/001346/134685E.pdf#4.3');
  352. expect(data.dest).toBeUndefined();
  353. expect(data.newWindow).toEqual(true);
  354. });
  355. it('should correctly parse a GoToR action, containing a relative URL, ' + 'with the "docBaseUrl" parameter specified', function () {
  356. var actionDict = new _primitives.Dict();
  357. actionDict.set('Type', _primitives.Name.get('Action'));
  358. actionDict.set('S', _primitives.Name.get('GoToR'));
  359. actionDict.set('F', '../../0013/001346/134685E.pdf');
  360. actionDict.set('D', '4.3');
  361. var annotationDict = new _primitives.Dict();
  362. annotationDict.set('Type', _primitives.Name.get('Annot'));
  363. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  364. annotationDict.set('A', actionDict);
  365. var annotationRef = new _primitives.Ref(489, 0);
  366. var xref = new _test_utils.XRefMock([{
  367. ref: annotationRef,
  368. data: annotationDict
  369. }]);
  370. var pdfManager = new PDFManagerMock({ docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf' });
  371. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock);
  372. var data = annotation.data;
  373. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  374. expect(data.url).toEqual('http://www.example.com/0013/001346/134685E.pdf#4.3');
  375. expect(data.unsafeUrl).toEqual('../../0013/001346/134685E.pdf#4.3');
  376. expect(data.dest).toBeUndefined();
  377. });
  378. it('should correctly parse a GoToR action, with named destination', function () {
  379. var actionDict = new _primitives.Dict();
  380. actionDict.set('Type', _primitives.Name.get('Action'));
  381. actionDict.set('S', _primitives.Name.get('GoToR'));
  382. actionDict.set('F', 'http://www.example.com/test.pdf');
  383. actionDict.set('D', '15');
  384. var annotationDict = new _primitives.Dict();
  385. annotationDict.set('Type', _primitives.Name.get('Annot'));
  386. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  387. annotationDict.set('A', actionDict);
  388. var annotationRef = new _primitives.Ref(495, 0);
  389. var xref = new _test_utils.XRefMock([{
  390. ref: annotationRef,
  391. data: annotationDict
  392. }]);
  393. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  394. var data = annotation.data;
  395. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  396. expect(data.url).toEqual('http://www.example.com/test.pdf#15');
  397. expect(data.unsafeUrl).toEqual('http://www.example.com/test.pdf#15');
  398. expect(data.dest).toBeUndefined();
  399. expect(data.newWindow).toBeFalsy();
  400. });
  401. it('should correctly parse a GoToR action, with explicit destination array', function () {
  402. var actionDict = new _primitives.Dict();
  403. actionDict.set('Type', _primitives.Name.get('Action'));
  404. actionDict.set('S', _primitives.Name.get('GoToR'));
  405. actionDict.set('F', 'http://www.example.com/test.pdf');
  406. actionDict.set('D', [14, _primitives.Name.get('XYZ'), null, 298.043, null]);
  407. var annotationDict = new _primitives.Dict();
  408. annotationDict.set('Type', _primitives.Name.get('Annot'));
  409. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  410. annotationDict.set('A', actionDict);
  411. var annotationRef = new _primitives.Ref(489, 0);
  412. var xref = new _test_utils.XRefMock([{
  413. ref: annotationRef,
  414. data: annotationDict
  415. }]);
  416. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  417. var data = annotation.data;
  418. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  419. expect(data.url).toEqual(new URL('http://www.example.com/test.pdf#' + '[14,{"name":"XYZ"},null,298.043,null]').href);
  420. expect(data.unsafeUrl).toEqual('http://www.example.com/test.pdf#' + '[14,{"name":"XYZ"},null,298.043,null]');
  421. expect(data.dest).toBeUndefined();
  422. expect(data.newWindow).toBeFalsy();
  423. });
  424. it('should correctly parse a Launch action, where the FileSpec dict ' + 'contains a relative URL, with the "docBaseUrl" parameter specified', function () {
  425. var fileSpecDict = new _primitives.Dict();
  426. fileSpecDict.set('Type', _primitives.Name.get('FileSpec'));
  427. fileSpecDict.set('F', 'Part II/Part II.pdf');
  428. fileSpecDict.set('UF', 'Part II/Part II.pdf');
  429. var actionDict = new _primitives.Dict();
  430. actionDict.set('Type', _primitives.Name.get('Action'));
  431. actionDict.set('S', _primitives.Name.get('Launch'));
  432. actionDict.set('F', fileSpecDict);
  433. actionDict.set('NewWindow', true);
  434. var annotationDict = new _primitives.Dict();
  435. annotationDict.set('Type', _primitives.Name.get('Annot'));
  436. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  437. annotationDict.set('A', actionDict);
  438. var annotationRef = new _primitives.Ref(88, 0);
  439. var xref = new _test_utils.XRefMock([{
  440. ref: annotationRef,
  441. data: annotationDict
  442. }]);
  443. var pdfManager = new PDFManagerMock({ docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf' });
  444. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock);
  445. var data = annotation.data;
  446. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  447. expect(data.url).toEqual(new URL('http://www.example.com/test/pdfs/Part II/Part II.pdf').href);
  448. expect(data.unsafeUrl).toEqual('Part II/Part II.pdf');
  449. expect(data.dest).toBeUndefined();
  450. expect(data.newWindow).toEqual(true);
  451. });
  452. it('should recover valid URLs from JavaScript actions having certain ' + 'white-listed formats', function () {
  453. function checkJsAction(params) {
  454. var jsEntry = params.jsEntry;
  455. var expectedUrl = params.expectedUrl;
  456. var expectedUnsafeUrl = params.expectedUnsafeUrl;
  457. var expectedNewWindow = params.expectedNewWindow;
  458. var actionDict = new _primitives.Dict();
  459. actionDict.set('Type', _primitives.Name.get('Action'));
  460. actionDict.set('S', _primitives.Name.get('JavaScript'));
  461. actionDict.set('JS', jsEntry);
  462. var annotationDict = new _primitives.Dict();
  463. annotationDict.set('Type', _primitives.Name.get('Annot'));
  464. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  465. annotationDict.set('A', actionDict);
  466. var annotationRef = new _primitives.Ref(46, 0);
  467. var xref = new _test_utils.XRefMock([{
  468. ref: annotationRef,
  469. data: annotationDict
  470. }]);
  471. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  472. var data = annotation.data;
  473. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  474. expect(data.url).toEqual(expectedUrl);
  475. expect(data.unsafeUrl).toEqual(expectedUnsafeUrl);
  476. expect(data.dest).toBeUndefined();
  477. expect(data.newWindow).toEqual(expectedNewWindow);
  478. }
  479. checkJsAction({
  480. jsEntry: 'function someFun() { return "qwerty"; } someFun();',
  481. expectedUrl: undefined,
  482. expectedUnsafeUrl: undefined,
  483. expectedNewWindow: undefined
  484. });
  485. checkJsAction({
  486. jsEntry: 'window.open(\'http://www.example.com/test.pdf\')',
  487. expectedUrl: new URL('http://www.example.com/test.pdf').href,
  488. expectedUnsafeUrl: 'http://www.example.com/test.pdf',
  489. expectedNewWindow: undefined
  490. });
  491. checkJsAction({
  492. jsEntry: new _stream.StringStream('app.launchURL("http://www.example.com/test.pdf", true)'),
  493. expectedUrl: new URL('http://www.example.com/test.pdf').href,
  494. expectedUnsafeUrl: 'http://www.example.com/test.pdf',
  495. expectedNewWindow: true
  496. });
  497. });
  498. it('should correctly parse a Named action', function () {
  499. var actionDict = new _primitives.Dict();
  500. actionDict.set('Type', _primitives.Name.get('Action'));
  501. actionDict.set('S', _primitives.Name.get('Named'));
  502. actionDict.set('N', _primitives.Name.get('GoToPage'));
  503. var annotationDict = new _primitives.Dict();
  504. annotationDict.set('Type', _primitives.Name.get('Annot'));
  505. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  506. annotationDict.set('A', actionDict);
  507. var annotationRef = new _primitives.Ref(12, 0);
  508. var xref = new _test_utils.XRefMock([{
  509. ref: annotationRef,
  510. data: annotationDict
  511. }]);
  512. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  513. var data = annotation.data;
  514. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  515. expect(data.url).toBeUndefined();
  516. expect(data.unsafeUrl).toBeUndefined();
  517. expect(data.action).toEqual('GoToPage');
  518. });
  519. it('should correctly parse a simple Dest', function () {
  520. var annotationDict = new _primitives.Dict();
  521. annotationDict.set('Type', _primitives.Name.get('Annot'));
  522. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  523. annotationDict.set('Dest', _primitives.Name.get('LI0'));
  524. var annotationRef = new _primitives.Ref(583, 0);
  525. var xref = new _test_utils.XRefMock([{
  526. ref: annotationRef,
  527. data: annotationDict
  528. }]);
  529. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  530. var data = annotation.data;
  531. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  532. expect(data.url).toBeUndefined();
  533. expect(data.unsafeUrl).toBeUndefined();
  534. expect(data.dest).toEqual('LI0');
  535. });
  536. it('should correctly parse a simple Dest, with explicit destination array', function () {
  537. var annotationDict = new _primitives.Dict();
  538. annotationDict.set('Type', _primitives.Name.get('Annot'));
  539. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  540. annotationDict.set('Dest', [new _primitives.Ref(17, 0), _primitives.Name.get('XYZ'), 0, 841.89, null]);
  541. var annotationRef = new _primitives.Ref(10, 0);
  542. var xref = new _test_utils.XRefMock([{
  543. ref: annotationRef,
  544. data: annotationDict
  545. }]);
  546. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  547. var data = annotation.data;
  548. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  549. expect(data.url).toBeUndefined();
  550. expect(data.unsafeUrl).toBeUndefined();
  551. expect(data.dest).toEqual([{
  552. num: 17,
  553. gen: 0
  554. }, { name: 'XYZ' }, 0, 841.89, null]);
  555. });
  556. it('should correctly parse a Dest, which violates the specification ' + 'by containing a dictionary', function () {
  557. var destDict = new _primitives.Dict();
  558. destDict.set('Type', _primitives.Name.get('Action'));
  559. destDict.set('S', _primitives.Name.get('GoTo'));
  560. destDict.set('D', 'page.157');
  561. var annotationDict = new _primitives.Dict();
  562. annotationDict.set('Type', _primitives.Name.get('Annot'));
  563. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  564. annotationDict.set('Dest', destDict);
  565. var annotationRef = new _primitives.Ref(798, 0);
  566. var xref = new _test_utils.XRefMock([{
  567. ref: annotationRef,
  568. data: annotationDict
  569. }]);
  570. var annotation = _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock);
  571. var data = annotation.data;
  572. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  573. expect(data.url).toBeUndefined();
  574. expect(data.unsafeUrl).toBeUndefined();
  575. expect(data.dest).toEqual('page.157');
  576. });
  577. });
  578. describe('WidgetAnnotation', function () {
  579. var widgetDict;
  580. beforeEach(function (done) {
  581. widgetDict = new _primitives.Dict();
  582. widgetDict.set('Type', _primitives.Name.get('Annot'));
  583. widgetDict.set('Subtype', _primitives.Name.get('Widget'));
  584. done();
  585. });
  586. afterEach(function () {
  587. widgetDict = null;
  588. });
  589. it('should handle unknown field names', function () {
  590. var widgetRef = new _primitives.Ref(20, 0);
  591. var xref = new _test_utils.XRefMock([{
  592. ref: widgetRef,
  593. data: widgetDict
  594. }]);
  595. var annotation = _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  596. var data = annotation.data;
  597. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  598. expect(data.fieldName).toEqual('');
  599. });
  600. it('should construct the field name when there are no ancestors', function () {
  601. widgetDict.set('T', 'foo');
  602. var widgetRef = new _primitives.Ref(21, 0);
  603. var xref = new _test_utils.XRefMock([{
  604. ref: widgetRef,
  605. data: widgetDict
  606. }]);
  607. var annotation = _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  608. var data = annotation.data;
  609. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  610. expect(data.fieldName).toEqual('foo');
  611. });
  612. it('should construct the field name when there are ancestors', function () {
  613. var firstParent = new _primitives.Dict();
  614. firstParent.set('T', 'foo');
  615. var secondParent = new _primitives.Dict();
  616. secondParent.set('Parent', firstParent);
  617. secondParent.set('T', 'bar');
  618. widgetDict.set('Parent', secondParent);
  619. widgetDict.set('T', 'baz');
  620. var widgetRef = new _primitives.Ref(22, 0);
  621. var xref = new _test_utils.XRefMock([{
  622. ref: widgetRef,
  623. data: widgetDict
  624. }]);
  625. var annotation = _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  626. var data = annotation.data;
  627. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  628. expect(data.fieldName).toEqual('foo.bar.baz');
  629. });
  630. it('should construct the field name if a parent is not a dictionary ' + '(issue 8143)', function () {
  631. var parentDict = new _primitives.Dict();
  632. parentDict.set('Parent', null);
  633. parentDict.set('T', 'foo');
  634. widgetDict.set('Parent', parentDict);
  635. widgetDict.set('T', 'bar');
  636. var widgetRef = new _primitives.Ref(22, 0);
  637. var xref = new _test_utils.XRefMock([{
  638. ref: widgetRef,
  639. data: widgetDict
  640. }]);
  641. var annotation = _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  642. var data = annotation.data;
  643. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  644. expect(data.fieldName).toEqual('foo.bar');
  645. });
  646. });
  647. describe('TextWidgetAnnotation', function () {
  648. var textWidgetDict;
  649. beforeEach(function (done) {
  650. textWidgetDict = new _primitives.Dict();
  651. textWidgetDict.set('Type', _primitives.Name.get('Annot'));
  652. textWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  653. textWidgetDict.set('FT', _primitives.Name.get('Tx'));
  654. done();
  655. });
  656. afterEach(function () {
  657. textWidgetDict = null;
  658. });
  659. it('should handle unknown text alignment, maximum length and flags', function () {
  660. var textWidgetRef = new _primitives.Ref(124, 0);
  661. var xref = new _test_utils.XRefMock([{
  662. ref: textWidgetRef,
  663. data: textWidgetDict
  664. }]);
  665. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  666. var data = annotation.data;
  667. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  668. expect(data.textAlignment).toEqual(null);
  669. expect(data.maxLen).toEqual(null);
  670. expect(data.readOnly).toEqual(false);
  671. expect(data.multiLine).toEqual(false);
  672. expect(data.comb).toEqual(false);
  673. });
  674. it('should not set invalid text alignment, maximum length and flags', function () {
  675. textWidgetDict.set('Q', 'center');
  676. textWidgetDict.set('MaxLen', 'five');
  677. textWidgetDict.set('Ff', 'readonly');
  678. var textWidgetRef = new _primitives.Ref(43, 0);
  679. var xref = new _test_utils.XRefMock([{
  680. ref: textWidgetRef,
  681. data: textWidgetDict
  682. }]);
  683. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  684. var data = annotation.data;
  685. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  686. expect(data.textAlignment).toEqual(null);
  687. expect(data.maxLen).toEqual(null);
  688. expect(data.readOnly).toEqual(false);
  689. expect(data.multiLine).toEqual(false);
  690. expect(data.comb).toEqual(false);
  691. });
  692. it('should set valid text alignment, maximum length and flags', function () {
  693. textWidgetDict.set('Q', 1);
  694. textWidgetDict.set('MaxLen', 20);
  695. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.MULTILINE);
  696. var textWidgetRef = new _primitives.Ref(84, 0);
  697. var xref = new _test_utils.XRefMock([{
  698. ref: textWidgetRef,
  699. data: textWidgetDict
  700. }]);
  701. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  702. var data = annotation.data;
  703. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  704. expect(data.textAlignment).toEqual(1);
  705. expect(data.maxLen).toEqual(20);
  706. expect(data.readOnly).toEqual(true);
  707. expect(data.multiLine).toEqual(true);
  708. });
  709. it('should reject comb fields without a maximum length', function () {
  710. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  711. var textWidgetRef = new _primitives.Ref(46, 0);
  712. var xref = new _test_utils.XRefMock([{
  713. ref: textWidgetRef,
  714. data: textWidgetDict
  715. }]);
  716. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  717. var data = annotation.data;
  718. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  719. expect(data.comb).toEqual(false);
  720. });
  721. it('should accept comb fields with a maximum length', function () {
  722. textWidgetDict.set('MaxLen', 20);
  723. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  724. var textWidgetRef = new _primitives.Ref(46, 0);
  725. var xref = new _test_utils.XRefMock([{
  726. ref: textWidgetRef,
  727. data: textWidgetDict
  728. }]);
  729. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  730. var data = annotation.data;
  731. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  732. expect(data.comb).toEqual(true);
  733. });
  734. it('should only accept comb fields when the flags are valid', function () {
  735. var invalidFieldFlags = [_util.AnnotationFieldFlag.MULTILINE, _util.AnnotationFieldFlag.PASSWORD, _util.AnnotationFieldFlag.FILESELECT];
  736. var flags = _util.AnnotationFieldFlag.COMB + _util.AnnotationFieldFlag.MULTILINE + _util.AnnotationFieldFlag.PASSWORD + _util.AnnotationFieldFlag.FILESELECT;
  737. for (var i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
  738. textWidgetDict.set('MaxLen', 20);
  739. textWidgetDict.set('Ff', flags);
  740. var textWidgetRef = new _primitives.Ref(93, 0);
  741. var xref = new _test_utils.XRefMock([{
  742. ref: textWidgetRef,
  743. data: textWidgetDict
  744. }]);
  745. var annotation = _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  746. var data = annotation.data;
  747. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  748. var valid = invalidFieldFlags.length === 0;
  749. expect(data.comb).toEqual(valid);
  750. if (!valid) {
  751. flags -= invalidFieldFlags.splice(-1, 1);
  752. }
  753. }
  754. });
  755. });
  756. describe('ButtonWidgetAnnotation', function () {
  757. var buttonWidgetDict;
  758. beforeEach(function (done) {
  759. buttonWidgetDict = new _primitives.Dict();
  760. buttonWidgetDict.set('Type', _primitives.Name.get('Annot'));
  761. buttonWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  762. buttonWidgetDict.set('FT', _primitives.Name.get('Btn'));
  763. done();
  764. });
  765. afterEach(function () {
  766. buttonWidgetDict = null;
  767. });
  768. it('should handle checkboxes', function () {
  769. buttonWidgetDict.set('V', _primitives.Name.get('1'));
  770. var buttonWidgetRef = new _primitives.Ref(124, 0);
  771. var xref = new _test_utils.XRefMock([{
  772. ref: buttonWidgetRef,
  773. data: buttonWidgetDict
  774. }]);
  775. var annotation = _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  776. var data = annotation.data;
  777. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  778. expect(data.checkBox).toEqual(true);
  779. expect(data.fieldValue).toEqual('1');
  780. expect(data.radioButton).toEqual(false);
  781. });
  782. it('should handle radio buttons with a field value', function () {
  783. var parentDict = new _primitives.Dict();
  784. parentDict.set('V', _primitives.Name.get('1'));
  785. var normalAppearanceStateDict = new _primitives.Dict();
  786. normalAppearanceStateDict.set('2', null);
  787. var appearanceStatesDict = new _primitives.Dict();
  788. appearanceStatesDict.set('N', normalAppearanceStateDict);
  789. buttonWidgetDict.set('Ff', _util.AnnotationFieldFlag.RADIO);
  790. buttonWidgetDict.set('Parent', parentDict);
  791. buttonWidgetDict.set('AP', appearanceStatesDict);
  792. var buttonWidgetRef = new _primitives.Ref(124, 0);
  793. var xref = new _test_utils.XRefMock([{
  794. ref: buttonWidgetRef,
  795. data: buttonWidgetDict
  796. }]);
  797. var annotation = _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  798. var data = annotation.data;
  799. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  800. expect(data.checkBox).toEqual(false);
  801. expect(data.radioButton).toEqual(true);
  802. expect(data.fieldValue).toEqual('1');
  803. expect(data.buttonValue).toEqual('2');
  804. });
  805. it('should handle radio buttons without a field value', function () {
  806. var normalAppearanceStateDict = new _primitives.Dict();
  807. normalAppearanceStateDict.set('2', null);
  808. var appearanceStatesDict = new _primitives.Dict();
  809. appearanceStatesDict.set('N', normalAppearanceStateDict);
  810. buttonWidgetDict.set('Ff', _util.AnnotationFieldFlag.RADIO);
  811. buttonWidgetDict.set('AP', appearanceStatesDict);
  812. var buttonWidgetRef = new _primitives.Ref(124, 0);
  813. var xref = new _test_utils.XRefMock([{
  814. ref: buttonWidgetRef,
  815. data: buttonWidgetDict
  816. }]);
  817. var annotation = _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  818. var data = annotation.data;
  819. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  820. expect(data.checkBox).toEqual(false);
  821. expect(data.radioButton).toEqual(true);
  822. expect(data.fieldValue).toEqual(null);
  823. expect(data.buttonValue).toEqual('2');
  824. });
  825. });
  826. describe('ChoiceWidgetAnnotation', function () {
  827. var choiceWidgetDict;
  828. beforeEach(function (done) {
  829. choiceWidgetDict = new _primitives.Dict();
  830. choiceWidgetDict.set('Type', _primitives.Name.get('Annot'));
  831. choiceWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  832. choiceWidgetDict.set('FT', _primitives.Name.get('Ch'));
  833. done();
  834. });
  835. afterEach(function () {
  836. choiceWidgetDict = null;
  837. });
  838. it('should handle missing option arrays', function () {
  839. var choiceWidgetRef = new _primitives.Ref(122, 0);
  840. var xref = new _test_utils.XRefMock([{
  841. ref: choiceWidgetRef,
  842. data: choiceWidgetDict
  843. }]);
  844. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  845. var data = annotation.data;
  846. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  847. expect(data.options).toEqual([]);
  848. });
  849. it('should handle option arrays with array elements', function () {
  850. var optionBarRef = new _primitives.Ref(20, 0);
  851. var optionBarStr = 'Bar';
  852. var optionOneRef = new _primitives.Ref(10, 0);
  853. var optionOneArr = ['bar_export', optionBarRef];
  854. var options = [['foo_export', 'Foo'], optionOneRef];
  855. var expected = [{
  856. exportValue: 'foo_export',
  857. displayValue: 'Foo'
  858. }, {
  859. exportValue: 'bar_export',
  860. displayValue: 'Bar'
  861. }];
  862. choiceWidgetDict.set('Opt', options);
  863. var choiceWidgetRef = new _primitives.Ref(123, 0);
  864. var xref = new _test_utils.XRefMock([{
  865. ref: choiceWidgetRef,
  866. data: choiceWidgetDict
  867. }, {
  868. ref: optionBarRef,
  869. data: optionBarStr
  870. }, {
  871. ref: optionOneRef,
  872. data: optionOneArr
  873. }]);
  874. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  875. var data = annotation.data;
  876. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  877. expect(data.options).toEqual(expected);
  878. });
  879. it('should handle option arrays with string elements', function () {
  880. var optionBarRef = new _primitives.Ref(10, 0);
  881. var optionBarStr = 'Bar';
  882. var options = ['Foo', optionBarRef];
  883. var expected = [{
  884. exportValue: 'Foo',
  885. displayValue: 'Foo'
  886. }, {
  887. exportValue: 'Bar',
  888. displayValue: 'Bar'
  889. }];
  890. choiceWidgetDict.set('Opt', options);
  891. var choiceWidgetRef = new _primitives.Ref(981, 0);
  892. var xref = new _test_utils.XRefMock([{
  893. ref: choiceWidgetRef,
  894. data: choiceWidgetDict
  895. }, {
  896. ref: optionBarRef,
  897. data: optionBarStr
  898. }]);
  899. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  900. var data = annotation.data;
  901. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  902. expect(data.options).toEqual(expected);
  903. });
  904. it('should handle inherited option arrays (issue 8094)', function () {
  905. var options = [['Value1', 'Description1'], ['Value2', 'Description2']];
  906. var expected = [{
  907. exportValue: 'Value1',
  908. displayValue: 'Description1'
  909. }, {
  910. exportValue: 'Value2',
  911. displayValue: 'Description2'
  912. }];
  913. var parentDict = new _primitives.Dict();
  914. parentDict.set('Opt', options);
  915. choiceWidgetDict.set('Parent', parentDict);
  916. var choiceWidgetRef = new _primitives.Ref(123, 0);
  917. var xref = new _test_utils.XRefMock([{
  918. ref: choiceWidgetRef,
  919. data: choiceWidgetDict
  920. }]);
  921. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  922. var data = annotation.data;
  923. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  924. expect(data.options).toEqual(expected);
  925. });
  926. it('should sanitize display values in option arrays (issue 8947)', function () {
  927. var options = ['\xFE\xFF\x00F\x00o\x00o'];
  928. var expected = [{
  929. exportValue: '\xFE\xFF\x00F\x00o\x00o',
  930. displayValue: 'Foo'
  931. }];
  932. choiceWidgetDict.set('Opt', options);
  933. var choiceWidgetRef = new _primitives.Ref(984, 0);
  934. var xref = new _test_utils.XRefMock([{
  935. ref: choiceWidgetRef,
  936. data: choiceWidgetDict
  937. }]);
  938. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  939. var data = annotation.data;
  940. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  941. expect(data.options).toEqual(expected);
  942. });
  943. it('should handle array field values', function () {
  944. var fieldValue = ['Foo', 'Bar'];
  945. choiceWidgetDict.set('V', fieldValue);
  946. var choiceWidgetRef = new _primitives.Ref(968, 0);
  947. var xref = new _test_utils.XRefMock([{
  948. ref: choiceWidgetRef,
  949. data: choiceWidgetDict
  950. }]);
  951. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  952. var data = annotation.data;
  953. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  954. expect(data.fieldValue).toEqual(fieldValue);
  955. });
  956. it('should handle string field values', function () {
  957. var fieldValue = 'Foo';
  958. choiceWidgetDict.set('V', fieldValue);
  959. var choiceWidgetRef = new _primitives.Ref(978, 0);
  960. var xref = new _test_utils.XRefMock([{
  961. ref: choiceWidgetRef,
  962. data: choiceWidgetDict
  963. }]);
  964. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  965. var data = annotation.data;
  966. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  967. expect(data.fieldValue).toEqual([fieldValue]);
  968. });
  969. it('should handle unknown flags', function () {
  970. var choiceWidgetRef = new _primitives.Ref(166, 0);
  971. var xref = new _test_utils.XRefMock([{
  972. ref: choiceWidgetRef,
  973. data: choiceWidgetDict
  974. }]);
  975. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  976. var data = annotation.data;
  977. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  978. expect(data.readOnly).toEqual(false);
  979. expect(data.combo).toEqual(false);
  980. expect(data.multiSelect).toEqual(false);
  981. });
  982. it('should not set invalid flags', function () {
  983. choiceWidgetDict.set('Ff', 'readonly');
  984. var choiceWidgetRef = new _primitives.Ref(165, 0);
  985. var xref = new _test_utils.XRefMock([{
  986. ref: choiceWidgetRef,
  987. data: choiceWidgetDict
  988. }]);
  989. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  990. var data = annotation.data;
  991. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  992. expect(data.readOnly).toEqual(false);
  993. expect(data.combo).toEqual(false);
  994. expect(data.multiSelect).toEqual(false);
  995. });
  996. it('should set valid flags', function () {
  997. choiceWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.COMBO + _util.AnnotationFieldFlag.MULTISELECT);
  998. var choiceWidgetRef = new _primitives.Ref(512, 0);
  999. var xref = new _test_utils.XRefMock([{
  1000. ref: choiceWidgetRef,
  1001. data: choiceWidgetDict
  1002. }]);
  1003. var annotation = _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  1004. var data = annotation.data;
  1005. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1006. expect(data.readOnly).toEqual(true);
  1007. expect(data.combo).toEqual(true);
  1008. expect(data.multiSelect).toEqual(true);
  1009. });
  1010. });
  1011. describe('LineAnnotation', function () {
  1012. it('should set the line coordinates', function () {
  1013. var lineDict = new _primitives.Dict();
  1014. lineDict.set('Type', _primitives.Name.get('Annot'));
  1015. lineDict.set('Subtype', _primitives.Name.get('Line'));
  1016. lineDict.set('L', [1, 2, 3, 4]);
  1017. var lineRef = new _primitives.Ref(122, 0);
  1018. var xref = new _test_utils.XRefMock([{
  1019. ref: lineRef,
  1020. data: lineDict
  1021. }]);
  1022. var annotation = _annotation.AnnotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock);
  1023. var data = annotation.data;
  1024. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  1025. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  1026. });
  1027. });
  1028. describe('FileAttachmentAnnotation', function () {
  1029. it('should correctly parse a file attachment', function () {
  1030. var fileStream = new _stream.StringStream('<<\n' + '/Type /EmbeddedFile\n' + '/Subtype /text#2Fplain\n' + '>>\n' + 'stream\n' + 'Test attachment' + 'endstream\n');
  1031. var lexer = new _parser.Lexer(fileStream);
  1032. var parser = new _parser.Parser(lexer, true);
  1033. var fileStreamRef = new _primitives.Ref(18, 0);
  1034. var fileStreamDict = parser.getObj();
  1035. var embeddedFileDict = new _primitives.Dict();
  1036. embeddedFileDict.set('F', fileStreamRef);
  1037. var fileSpecRef = new _primitives.Ref(19, 0);
  1038. var fileSpecDict = new _primitives.Dict();
  1039. fileSpecDict.set('Type', _primitives.Name.get('Filespec'));
  1040. fileSpecDict.set('Desc', '');
  1041. fileSpecDict.set('EF', embeddedFileDict);
  1042. fileSpecDict.set('UF', 'Test.txt');
  1043. var fileAttachmentRef = new _primitives.Ref(20, 0);
  1044. var fileAttachmentDict = new _primitives.Dict();
  1045. fileAttachmentDict.set('Type', _primitives.Name.get('Annot'));
  1046. fileAttachmentDict.set('Subtype', _primitives.Name.get('FileAttachment'));
  1047. fileAttachmentDict.set('FS', fileSpecRef);
  1048. fileAttachmentDict.set('T', 'Topic');
  1049. fileAttachmentDict.set('Contents', 'Test.txt');
  1050. var xref = new _test_utils.XRefMock([{
  1051. ref: fileStreamRef,
  1052. data: fileStreamDict
  1053. }, {
  1054. ref: fileSpecRef,
  1055. data: fileSpecDict
  1056. }, {
  1057. ref: fileAttachmentRef,
  1058. data: fileAttachmentDict
  1059. }]);
  1060. embeddedFileDict.assignXref(xref);
  1061. fileSpecDict.assignXref(xref);
  1062. fileAttachmentDict.assignXref(xref);
  1063. var annotation = _annotation.AnnotationFactory.create(xref, fileAttachmentRef, pdfManagerMock, idFactoryMock);
  1064. var data = annotation.data;
  1065. expect(data.annotationType).toEqual(_util.AnnotationType.FILEATTACHMENT);
  1066. expect(data.file.filename).toEqual('Test.txt');
  1067. expect(data.file.content).toEqual((0, _util.stringToBytes)('Test attachment'));
  1068. });
  1069. });
  1070. describe('PopupAnnotation', function () {
  1071. it('should inherit the parent flags when the Popup is not viewable, ' + 'but the parent is (PR 7352)', function () {
  1072. var parentDict = new _primitives.Dict();
  1073. parentDict.set('Type', _primitives.Name.get('Annot'));
  1074. parentDict.set('Subtype', _primitives.Name.get('Text'));
  1075. parentDict.set('F', 28);
  1076. var popupDict = new _primitives.Dict();
  1077. popupDict.set('Type', _primitives.Name.get('Annot'));
  1078. popupDict.set('Subtype', _primitives.Name.get('Popup'));
  1079. popupDict.set('F', 25);
  1080. popupDict.set('Parent', parentDict);
  1081. var popupRef = new _primitives.Ref(13, 0);
  1082. var xref = new _test_utils.XRefMock([{
  1083. ref: popupRef,
  1084. data: popupDict
  1085. }]);
  1086. var annotation = _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  1087. var data = annotation.data;
  1088. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  1089. expect(data.annotationFlags).toEqual(25);
  1090. expect(annotation.viewable).toEqual(true);
  1091. });
  1092. });
  1093. });