annotation_spec.js 48 KB

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