annotation_spec.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. });
  552. describe('WidgetAnnotation', function () {
  553. var widgetDict;
  554. beforeEach(function (done) {
  555. widgetDict = new _primitives.Dict();
  556. widgetDict.set('Type', _primitives.Name.get('Annot'));
  557. widgetDict.set('Subtype', _primitives.Name.get('Widget'));
  558. done();
  559. });
  560. afterEach(function () {
  561. widgetDict = null;
  562. });
  563. it('should handle unknown field names', function () {
  564. var widgetRef = new _primitives.Ref(20, 0);
  565. var xref = new _test_utils.XRefMock([{
  566. ref: widgetRef,
  567. data: widgetDict
  568. }]);
  569. var annotation = annotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  570. var data = annotation.data;
  571. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  572. expect(data.fieldName).toEqual('');
  573. });
  574. it('should construct the field name when there are no ancestors', function () {
  575. widgetDict.set('T', 'foo');
  576. var widgetRef = new _primitives.Ref(21, 0);
  577. var xref = new _test_utils.XRefMock([{
  578. ref: widgetRef,
  579. data: widgetDict
  580. }]);
  581. var annotation = annotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  582. var data = annotation.data;
  583. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  584. expect(data.fieldName).toEqual('foo');
  585. });
  586. it('should construct the field name when there are ancestors', function () {
  587. var firstParent = new _primitives.Dict();
  588. firstParent.set('T', 'foo');
  589. var secondParent = new _primitives.Dict();
  590. secondParent.set('Parent', firstParent);
  591. secondParent.set('T', 'bar');
  592. widgetDict.set('Parent', secondParent);
  593. widgetDict.set('T', 'baz');
  594. var widgetRef = new _primitives.Ref(22, 0);
  595. var xref = new _test_utils.XRefMock([{
  596. ref: widgetRef,
  597. data: widgetDict
  598. }]);
  599. var annotation = annotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  600. var data = annotation.data;
  601. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  602. expect(data.fieldName).toEqual('foo.bar.baz');
  603. });
  604. it('should construct the field name if a parent is not a dictionary ' + '(issue 8143)', function () {
  605. var parentDict = new _primitives.Dict();
  606. parentDict.set('Parent', null);
  607. parentDict.set('T', 'foo');
  608. widgetDict.set('Parent', parentDict);
  609. widgetDict.set('T', 'bar');
  610. var widgetRef = new _primitives.Ref(22, 0);
  611. var xref = new _test_utils.XRefMock([{
  612. ref: widgetRef,
  613. data: widgetDict
  614. }]);
  615. var annotation = annotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock);
  616. var data = annotation.data;
  617. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  618. expect(data.fieldName).toEqual('foo.bar');
  619. });
  620. });
  621. describe('TextWidgetAnnotation', function () {
  622. var textWidgetDict;
  623. beforeEach(function (done) {
  624. textWidgetDict = new _primitives.Dict();
  625. textWidgetDict.set('Type', _primitives.Name.get('Annot'));
  626. textWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  627. textWidgetDict.set('FT', _primitives.Name.get('Tx'));
  628. done();
  629. });
  630. afterEach(function () {
  631. textWidgetDict = null;
  632. });
  633. it('should handle unknown text alignment, maximum length and flags', function () {
  634. var textWidgetRef = new _primitives.Ref(124, 0);
  635. var xref = new _test_utils.XRefMock([{
  636. ref: textWidgetRef,
  637. data: textWidgetDict
  638. }]);
  639. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  640. var data = annotation.data;
  641. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  642. expect(data.textAlignment).toEqual(null);
  643. expect(data.maxLen).toEqual(null);
  644. expect(data.readOnly).toEqual(false);
  645. expect(data.multiLine).toEqual(false);
  646. expect(data.comb).toEqual(false);
  647. });
  648. it('should not set invalid text alignment, maximum length and flags', function () {
  649. textWidgetDict.set('Q', 'center');
  650. textWidgetDict.set('MaxLen', 'five');
  651. textWidgetDict.set('Ff', 'readonly');
  652. var textWidgetRef = new _primitives.Ref(43, 0);
  653. var xref = new _test_utils.XRefMock([{
  654. ref: textWidgetRef,
  655. data: textWidgetDict
  656. }]);
  657. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  658. var data = annotation.data;
  659. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  660. expect(data.textAlignment).toEqual(null);
  661. expect(data.maxLen).toEqual(null);
  662. expect(data.readOnly).toEqual(false);
  663. expect(data.multiLine).toEqual(false);
  664. expect(data.comb).toEqual(false);
  665. });
  666. it('should set valid text alignment, maximum length and flags', function () {
  667. textWidgetDict.set('Q', 1);
  668. textWidgetDict.set('MaxLen', 20);
  669. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.MULTILINE);
  670. var textWidgetRef = new _primitives.Ref(84, 0);
  671. var xref = new _test_utils.XRefMock([{
  672. ref: textWidgetRef,
  673. data: textWidgetDict
  674. }]);
  675. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  676. var data = annotation.data;
  677. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  678. expect(data.textAlignment).toEqual(1);
  679. expect(data.maxLen).toEqual(20);
  680. expect(data.readOnly).toEqual(true);
  681. expect(data.multiLine).toEqual(true);
  682. });
  683. it('should reject comb fields without a maximum length', function () {
  684. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  685. var textWidgetRef = new _primitives.Ref(46, 0);
  686. var xref = new _test_utils.XRefMock([{
  687. ref: textWidgetRef,
  688. data: textWidgetDict
  689. }]);
  690. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  691. var data = annotation.data;
  692. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  693. expect(data.comb).toEqual(false);
  694. });
  695. it('should accept comb fields with a maximum length', function () {
  696. textWidgetDict.set('MaxLen', 20);
  697. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  698. var textWidgetRef = new _primitives.Ref(46, 0);
  699. var xref = new _test_utils.XRefMock([{
  700. ref: textWidgetRef,
  701. data: textWidgetDict
  702. }]);
  703. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  704. var data = annotation.data;
  705. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  706. expect(data.comb).toEqual(true);
  707. });
  708. it('should only accept comb fields when the flags are valid', function () {
  709. var invalidFieldFlags = [_util.AnnotationFieldFlag.MULTILINE, _util.AnnotationFieldFlag.PASSWORD, _util.AnnotationFieldFlag.FILESELECT];
  710. var flags = _util.AnnotationFieldFlag.COMB + _util.AnnotationFieldFlag.MULTILINE + _util.AnnotationFieldFlag.PASSWORD + _util.AnnotationFieldFlag.FILESELECT;
  711. for (var i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
  712. textWidgetDict.set('MaxLen', 20);
  713. textWidgetDict.set('Ff', flags);
  714. var textWidgetRef = new _primitives.Ref(93, 0);
  715. var xref = new _test_utils.XRefMock([{
  716. ref: textWidgetRef,
  717. data: textWidgetDict
  718. }]);
  719. var annotation = annotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  720. var data = annotation.data;
  721. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  722. var valid = invalidFieldFlags.length === 0;
  723. expect(data.comb).toEqual(valid);
  724. if (!valid) {
  725. flags -= invalidFieldFlags.splice(-1, 1);
  726. }
  727. }
  728. });
  729. });
  730. describe('ButtonWidgetAnnotation', function () {
  731. var buttonWidgetDict;
  732. beforeEach(function (done) {
  733. buttonWidgetDict = new _primitives.Dict();
  734. buttonWidgetDict.set('Type', _primitives.Name.get('Annot'));
  735. buttonWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  736. buttonWidgetDict.set('FT', _primitives.Name.get('Btn'));
  737. done();
  738. });
  739. afterEach(function () {
  740. buttonWidgetDict = null;
  741. });
  742. it('should handle checkboxes', function () {
  743. buttonWidgetDict.set('V', _primitives.Name.get('1'));
  744. var buttonWidgetRef = new _primitives.Ref(124, 0);
  745. var xref = new _test_utils.XRefMock([{
  746. ref: buttonWidgetRef,
  747. data: buttonWidgetDict
  748. }]);
  749. var annotation = annotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  750. var data = annotation.data;
  751. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  752. expect(data.checkBox).toEqual(true);
  753. expect(data.fieldValue).toEqual('1');
  754. expect(data.radioButton).toEqual(false);
  755. });
  756. it('should handle radio buttons with a field value', function () {
  757. var parentDict = new _primitives.Dict();
  758. parentDict.set('V', _primitives.Name.get('1'));
  759. var normalAppearanceStateDict = new _primitives.Dict();
  760. normalAppearanceStateDict.set('2', null);
  761. var appearanceStatesDict = new _primitives.Dict();
  762. appearanceStatesDict.set('N', normalAppearanceStateDict);
  763. buttonWidgetDict.set('Ff', _util.AnnotationFieldFlag.RADIO);
  764. buttonWidgetDict.set('Parent', parentDict);
  765. buttonWidgetDict.set('AP', appearanceStatesDict);
  766. var buttonWidgetRef = new _primitives.Ref(124, 0);
  767. var xref = new _test_utils.XRefMock([{
  768. ref: buttonWidgetRef,
  769. data: buttonWidgetDict
  770. }]);
  771. var annotation = annotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  772. var data = annotation.data;
  773. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  774. expect(data.checkBox).toEqual(false);
  775. expect(data.radioButton).toEqual(true);
  776. expect(data.fieldValue).toEqual('1');
  777. expect(data.buttonValue).toEqual('2');
  778. });
  779. it('should handle radio buttons without a field value', function () {
  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('AP', appearanceStatesDict);
  786. var buttonWidgetRef = new _primitives.Ref(124, 0);
  787. var xref = new _test_utils.XRefMock([{
  788. ref: buttonWidgetRef,
  789. data: buttonWidgetDict
  790. }]);
  791. var annotation = annotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  792. var data = annotation.data;
  793. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  794. expect(data.checkBox).toEqual(false);
  795. expect(data.radioButton).toEqual(true);
  796. expect(data.fieldValue).toEqual(null);
  797. expect(data.buttonValue).toEqual('2');
  798. });
  799. });
  800. describe('ChoiceWidgetAnnotation', function () {
  801. var choiceWidgetDict;
  802. beforeEach(function (done) {
  803. choiceWidgetDict = new _primitives.Dict();
  804. choiceWidgetDict.set('Type', _primitives.Name.get('Annot'));
  805. choiceWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  806. choiceWidgetDict.set('FT', _primitives.Name.get('Ch'));
  807. done();
  808. });
  809. afterEach(function () {
  810. choiceWidgetDict = null;
  811. });
  812. it('should handle missing option arrays', function () {
  813. var choiceWidgetRef = new _primitives.Ref(122, 0);
  814. var xref = new _test_utils.XRefMock([{
  815. ref: choiceWidgetRef,
  816. data: choiceWidgetDict
  817. }]);
  818. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  819. var data = annotation.data;
  820. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  821. expect(data.options).toEqual([]);
  822. });
  823. it('should handle option arrays with array elements', function () {
  824. var optionBarRef = new _primitives.Ref(20, 0);
  825. var optionBarStr = 'Bar';
  826. var optionOneRef = new _primitives.Ref(10, 0);
  827. var optionOneArr = ['bar_export', optionBarRef];
  828. var options = [['foo_export', 'Foo'], optionOneRef];
  829. var expected = [{
  830. exportValue: 'foo_export',
  831. displayValue: 'Foo'
  832. }, {
  833. exportValue: 'bar_export',
  834. displayValue: 'Bar'
  835. }];
  836. choiceWidgetDict.set('Opt', options);
  837. var choiceWidgetRef = new _primitives.Ref(123, 0);
  838. var xref = new _test_utils.XRefMock([{
  839. ref: choiceWidgetRef,
  840. data: choiceWidgetDict
  841. }, {
  842. ref: optionBarRef,
  843. data: optionBarStr
  844. }, {
  845. ref: optionOneRef,
  846. data: optionOneArr
  847. }]);
  848. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  849. var data = annotation.data;
  850. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  851. expect(data.options).toEqual(expected);
  852. });
  853. it('should handle option arrays with string elements', function () {
  854. var optionBarRef = new _primitives.Ref(10, 0);
  855. var optionBarStr = 'Bar';
  856. var options = ['Foo', optionBarRef];
  857. var expected = [{
  858. exportValue: 'Foo',
  859. displayValue: 'Foo'
  860. }, {
  861. exportValue: 'Bar',
  862. displayValue: 'Bar'
  863. }];
  864. choiceWidgetDict.set('Opt', options);
  865. var choiceWidgetRef = new _primitives.Ref(981, 0);
  866. var xref = new _test_utils.XRefMock([{
  867. ref: choiceWidgetRef,
  868. data: choiceWidgetDict
  869. }, {
  870. ref: optionBarRef,
  871. data: optionBarStr
  872. }]);
  873. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  874. var data = annotation.data;
  875. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  876. expect(data.options).toEqual(expected);
  877. });
  878. it('should handle inherited option arrays (issue 8094)', function () {
  879. var options = [['Value1', 'Description1'], ['Value2', 'Description2']];
  880. var expected = [{
  881. exportValue: 'Value1',
  882. displayValue: 'Description1'
  883. }, {
  884. exportValue: 'Value2',
  885. displayValue: 'Description2'
  886. }];
  887. var parentDict = new _primitives.Dict();
  888. parentDict.set('Opt', options);
  889. choiceWidgetDict.set('Parent', parentDict);
  890. var choiceWidgetRef = new _primitives.Ref(123, 0);
  891. var xref = new _test_utils.XRefMock([{
  892. ref: choiceWidgetRef,
  893. data: choiceWidgetDict
  894. }]);
  895. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  896. var data = annotation.data;
  897. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  898. expect(data.options).toEqual(expected);
  899. });
  900. it('should handle array field values', function () {
  901. var fieldValue = ['Foo', 'Bar'];
  902. choiceWidgetDict.set('V', fieldValue);
  903. var choiceWidgetRef = new _primitives.Ref(968, 0);
  904. var xref = new _test_utils.XRefMock([{
  905. ref: choiceWidgetRef,
  906. data: choiceWidgetDict
  907. }]);
  908. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  909. var data = annotation.data;
  910. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  911. expect(data.fieldValue).toEqual(fieldValue);
  912. });
  913. it('should handle string field values', function () {
  914. var fieldValue = 'Foo';
  915. choiceWidgetDict.set('V', fieldValue);
  916. var choiceWidgetRef = new _primitives.Ref(978, 0);
  917. var xref = new _test_utils.XRefMock([{
  918. ref: choiceWidgetRef,
  919. data: choiceWidgetDict
  920. }]);
  921. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  922. var data = annotation.data;
  923. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  924. expect(data.fieldValue).toEqual([fieldValue]);
  925. });
  926. it('should handle unknown flags', function () {
  927. var choiceWidgetRef = new _primitives.Ref(166, 0);
  928. var xref = new _test_utils.XRefMock([{
  929. ref: choiceWidgetRef,
  930. data: choiceWidgetDict
  931. }]);
  932. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  933. var data = annotation.data;
  934. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  935. expect(data.readOnly).toEqual(false);
  936. expect(data.combo).toEqual(false);
  937. expect(data.multiSelect).toEqual(false);
  938. });
  939. it('should not set invalid flags', function () {
  940. choiceWidgetDict.set('Ff', 'readonly');
  941. var choiceWidgetRef = new _primitives.Ref(165, 0);
  942. var xref = new _test_utils.XRefMock([{
  943. ref: choiceWidgetRef,
  944. data: choiceWidgetDict
  945. }]);
  946. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  947. var data = annotation.data;
  948. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  949. expect(data.readOnly).toEqual(false);
  950. expect(data.combo).toEqual(false);
  951. expect(data.multiSelect).toEqual(false);
  952. });
  953. it('should set valid flags', function () {
  954. choiceWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.COMBO + _util.AnnotationFieldFlag.MULTISELECT);
  955. var choiceWidgetRef = new _primitives.Ref(512, 0);
  956. var xref = new _test_utils.XRefMock([{
  957. ref: choiceWidgetRef,
  958. data: choiceWidgetDict
  959. }]);
  960. var annotation = annotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  961. var data = annotation.data;
  962. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  963. expect(data.readOnly).toEqual(true);
  964. expect(data.combo).toEqual(true);
  965. expect(data.multiSelect).toEqual(true);
  966. });
  967. });
  968. describe('LineAnnotation', function () {
  969. it('should set the line coordinates', function () {
  970. var lineDict = new _primitives.Dict();
  971. lineDict.set('Type', _primitives.Name.get('Annot'));
  972. lineDict.set('Subtype', _primitives.Name.get('Line'));
  973. lineDict.set('L', [1, 2, 3, 4]);
  974. var lineRef = new _primitives.Ref(122, 0);
  975. var xref = new _test_utils.XRefMock([{
  976. ref: lineRef,
  977. data: lineDict
  978. }]);
  979. var annotation = annotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock);
  980. var data = annotation.data;
  981. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  982. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  983. });
  984. });
  985. describe('FileAttachmentAnnotation', function () {
  986. it('should correctly parse a file attachment', function () {
  987. var fileStream = new _stream.StringStream('<<\n' + '/Type /EmbeddedFile\n' + '/Subtype /text#2Fplain\n' + '>>\n' + 'stream\n' + 'Test attachment' + 'endstream\n');
  988. var lexer = new _parser.Lexer(fileStream);
  989. var parser = new _parser.Parser(lexer, true);
  990. var fileStreamRef = new _primitives.Ref(18, 0);
  991. var fileStreamDict = parser.getObj();
  992. var embeddedFileDict = new _primitives.Dict();
  993. embeddedFileDict.set('F', fileStreamRef);
  994. var fileSpecRef = new _primitives.Ref(19, 0);
  995. var fileSpecDict = new _primitives.Dict();
  996. fileSpecDict.set('Type', _primitives.Name.get('Filespec'));
  997. fileSpecDict.set('Desc', '');
  998. fileSpecDict.set('EF', embeddedFileDict);
  999. fileSpecDict.set('UF', 'Test.txt');
  1000. var fileAttachmentRef = new _primitives.Ref(20, 0);
  1001. var fileAttachmentDict = new _primitives.Dict();
  1002. fileAttachmentDict.set('Type', _primitives.Name.get('Annot'));
  1003. fileAttachmentDict.set('Subtype', _primitives.Name.get('FileAttachment'));
  1004. fileAttachmentDict.set('FS', fileSpecRef);
  1005. fileAttachmentDict.set('T', 'Topic');
  1006. fileAttachmentDict.set('Contents', 'Test.txt');
  1007. var xref = new _test_utils.XRefMock([{
  1008. ref: fileStreamRef,
  1009. data: fileStreamDict
  1010. }, {
  1011. ref: fileSpecRef,
  1012. data: fileSpecDict
  1013. }, {
  1014. ref: fileAttachmentRef,
  1015. data: fileAttachmentDict
  1016. }]);
  1017. embeddedFileDict.assignXref(xref);
  1018. fileSpecDict.assignXref(xref);
  1019. fileAttachmentDict.assignXref(xref);
  1020. var annotation = annotationFactory.create(xref, fileAttachmentRef, pdfManagerMock, idFactoryMock);
  1021. var data = annotation.data;
  1022. expect(data.annotationType).toEqual(_util.AnnotationType.FILEATTACHMENT);
  1023. expect(data.file.filename).toEqual('Test.txt');
  1024. expect(data.file.content).toEqual((0, _util.stringToBytes)('Test attachment'));
  1025. });
  1026. });
  1027. describe('PopupAnnotation', function () {
  1028. it('should inherit the parent flags when the Popup is not viewable, ' + 'but the parent is (PR 7352)', function () {
  1029. var parentDict = new _primitives.Dict();
  1030. parentDict.set('Type', _primitives.Name.get('Annot'));
  1031. parentDict.set('Subtype', _primitives.Name.get('Text'));
  1032. parentDict.set('F', 28);
  1033. var popupDict = new _primitives.Dict();
  1034. popupDict.set('Type', _primitives.Name.get('Annot'));
  1035. popupDict.set('Subtype', _primitives.Name.get('Popup'));
  1036. popupDict.set('F', 25);
  1037. popupDict.set('Parent', parentDict);
  1038. var popupRef = new _primitives.Ref(13, 0);
  1039. var xref = new _test_utils.XRefMock([{
  1040. ref: popupRef,
  1041. data: popupDict
  1042. }]);
  1043. var annotation = annotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  1044. var data = annotation.data;
  1045. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  1046. expect(data.annotationFlags).toEqual(25);
  1047. expect(annotation.viewable).toEqual(true);
  1048. });
  1049. });
  1050. });