annotation_spec.js 47 KB

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