2
0

annotation_spec.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _annotation = require("../../core/annotation");
  24. var _util = require("../../shared/util");
  25. var _primitives = require("../../core/primitives");
  26. var _parser = require("../../core/parser");
  27. var _stream = require("../../core/stream");
  28. var _test_utils = require("./test_utils");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. describe('annotation', function () {
  33. var PDFManagerMock =
  34. /*#__PURE__*/
  35. function () {
  36. function PDFManagerMock(params) {
  37. _classCallCheck(this, PDFManagerMock);
  38. this.docBaseUrl = params.docBaseUrl || null;
  39. }
  40. _createClass(PDFManagerMock, [{
  41. key: "ensure",
  42. value: function ensure(obj, prop, args) {
  43. return new Promise(function (resolve) {
  44. var value = obj[prop];
  45. if (typeof value === 'function') {
  46. resolve(value.apply(obj, args));
  47. } else {
  48. resolve(value);
  49. }
  50. });
  51. }
  52. }]);
  53. return PDFManagerMock;
  54. }();
  55. var IdFactoryMock =
  56. /*#__PURE__*/
  57. function () {
  58. function IdFactoryMock(params) {
  59. _classCallCheck(this, IdFactoryMock);
  60. this.uniquePrefix = params.prefix || 'p0_';
  61. this.idCounters = {
  62. obj: params.startObjId || 0
  63. };
  64. }
  65. _createClass(IdFactoryMock, [{
  66. key: "createObjId",
  67. value: function createObjId() {
  68. return this.uniquePrefix + ++this.idCounters.obj;
  69. }
  70. }]);
  71. return IdFactoryMock;
  72. }();
  73. var pdfManagerMock, idFactoryMock;
  74. beforeAll(function (done) {
  75. pdfManagerMock = new PDFManagerMock({
  76. docBaseUrl: null
  77. });
  78. idFactoryMock = new IdFactoryMock({});
  79. done();
  80. });
  81. afterAll(function () {
  82. pdfManagerMock = null;
  83. idFactoryMock = null;
  84. });
  85. describe('AnnotationFactory', function () {
  86. it('should get id for annotation', function (done) {
  87. var annotationDict = new _primitives.Dict();
  88. annotationDict.set('Type', _primitives.Name.get('Annot'));
  89. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  90. var annotationRef = new _primitives.Ref(10, 0);
  91. var xref = new _test_utils.XRefMock([{
  92. ref: annotationRef,
  93. data: annotationDict
  94. }]);
  95. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref) {
  96. var data = _ref.data;
  97. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  98. expect(data.id).toEqual('10R');
  99. done();
  100. }, done.fail);
  101. });
  102. it('should handle, and get fallback IDs for, annotations that are not ' + 'indirect objects (issue 7569)', function (done) {
  103. var annotationDict = new _primitives.Dict();
  104. annotationDict.set('Type', _primitives.Name.get('Annot'));
  105. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  106. var xref = new _test_utils.XRefMock();
  107. var idFactory = new IdFactoryMock({
  108. prefix: 'p0_',
  109. startObjId: 0
  110. });
  111. var annotation1 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory).then(function (_ref2) {
  112. var data = _ref2.data;
  113. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  114. expect(data.id).toEqual('annot_p0_1');
  115. });
  116. var annotation2 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory).then(function (_ref3) {
  117. var data = _ref3.data;
  118. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  119. expect(data.id).toEqual('annot_p0_2');
  120. });
  121. Promise.all([annotation1, annotation2]).then(done, done.fail);
  122. });
  123. it('should handle missing /Subtype', function (done) {
  124. var annotationDict = new _primitives.Dict();
  125. annotationDict.set('Type', _primitives.Name.get('Annot'));
  126. var annotationRef = new _primitives.Ref(1, 0);
  127. var xref = new _test_utils.XRefMock([{
  128. ref: annotationRef,
  129. data: annotationDict
  130. }]);
  131. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref4) {
  132. var data = _ref4.data;
  133. expect(data.annotationType).toBeUndefined();
  134. done();
  135. }, done.fail);
  136. });
  137. });
  138. describe('Annotation', function () {
  139. var dict, ref;
  140. beforeAll(function (done) {
  141. dict = new _primitives.Dict();
  142. ref = new _primitives.Ref(1, 0);
  143. done();
  144. });
  145. afterAll(function () {
  146. dict = ref = null;
  147. });
  148. it('should set and get flags', function () {
  149. var annotation = new _annotation.Annotation({
  150. dict: dict,
  151. ref: ref
  152. });
  153. annotation.setFlags(13);
  154. expect(annotation.hasFlag(_util.AnnotationFlag.INVISIBLE)).toEqual(true);
  155. expect(annotation.hasFlag(_util.AnnotationFlag.NOZOOM)).toEqual(true);
  156. expect(annotation.hasFlag(_util.AnnotationFlag.PRINT)).toEqual(true);
  157. expect(annotation.hasFlag(_util.AnnotationFlag.READONLY)).toEqual(false);
  158. });
  159. it('should be viewable and not printable by default', function () {
  160. var annotation = new _annotation.Annotation({
  161. dict: dict,
  162. ref: ref
  163. });
  164. expect(annotation.viewable).toEqual(true);
  165. expect(annotation.printable).toEqual(false);
  166. });
  167. it('should set and get a valid rectangle', function () {
  168. var annotation = new _annotation.Annotation({
  169. dict: dict,
  170. ref: ref
  171. });
  172. annotation.setRectangle([117, 694, 164.298, 720]);
  173. expect(annotation.rectangle).toEqual([117, 694, 164.298, 720]);
  174. });
  175. it('should not set and get an invalid rectangle', function () {
  176. var annotation = new _annotation.Annotation({
  177. dict: dict,
  178. ref: ref
  179. });
  180. annotation.setRectangle([117, 694, 164.298]);
  181. expect(annotation.rectangle).toEqual([0, 0, 0, 0]);
  182. });
  183. it('should reject a color if it is not an array', function () {
  184. var annotation = new _annotation.Annotation({
  185. dict: dict,
  186. ref: ref
  187. });
  188. annotation.setColor('red');
  189. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
  190. });
  191. it('should set and get a transparent color', function () {
  192. var annotation = new _annotation.Annotation({
  193. dict: dict,
  194. ref: ref
  195. });
  196. annotation.setColor([]);
  197. expect(annotation.color).toEqual(null);
  198. });
  199. it('should set and get a grayscale color', function () {
  200. var annotation = new _annotation.Annotation({
  201. dict: dict,
  202. ref: ref
  203. });
  204. annotation.setColor([0.4]);
  205. expect(annotation.color).toEqual(new Uint8ClampedArray([102, 102, 102]));
  206. });
  207. it('should set and get an RGB color', function () {
  208. var annotation = new _annotation.Annotation({
  209. dict: dict,
  210. ref: ref
  211. });
  212. annotation.setColor([0, 0, 1]);
  213. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  214. });
  215. it('should set and get a CMYK color', function () {
  216. var annotation = new _annotation.Annotation({
  217. dict: dict,
  218. ref: ref
  219. });
  220. annotation.setColor([0.1, 0.92, 0.84, 0.02]);
  221. expect(annotation.color).toEqual(new Uint8ClampedArray([234, 59, 48]));
  222. });
  223. it('should not set and get an invalid color', function () {
  224. var annotation = new _annotation.Annotation({
  225. dict: dict,
  226. ref: ref
  227. });
  228. annotation.setColor([0.4, 0.6]);
  229. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
  230. });
  231. });
  232. describe('AnnotationBorderStyle', function () {
  233. it('should set and get a valid width', function () {
  234. var borderStyle = new _annotation.AnnotationBorderStyle();
  235. borderStyle.setWidth(3);
  236. expect(borderStyle.width).toEqual(3);
  237. });
  238. it('should not set and get an invalid width', function () {
  239. var borderStyle = new _annotation.AnnotationBorderStyle();
  240. borderStyle.setWidth('three');
  241. expect(borderStyle.width).toEqual(1);
  242. });
  243. it('should set the width to zero, when the input is a `Name` (issue 10385)', function () {
  244. var borderStyleZero = new _annotation.AnnotationBorderStyle();
  245. borderStyleZero.setWidth(_primitives.Name.get('0'));
  246. var borderStyleFive = new _annotation.AnnotationBorderStyle();
  247. borderStyleFive.setWidth(_primitives.Name.get('5'));
  248. expect(borderStyleZero.width).toEqual(0);
  249. expect(borderStyleFive.width).toEqual(0);
  250. });
  251. it('should set and get a valid style', function () {
  252. var borderStyle = new _annotation.AnnotationBorderStyle();
  253. borderStyle.setStyle(_primitives.Name.get('D'));
  254. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.DASHED);
  255. });
  256. it('should not set and get an invalid style', function () {
  257. var borderStyle = new _annotation.AnnotationBorderStyle();
  258. borderStyle.setStyle('Dashed');
  259. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.SOLID);
  260. });
  261. it('should set and get a valid dash array', function () {
  262. var borderStyle = new _annotation.AnnotationBorderStyle();
  263. borderStyle.setDashArray([1, 2, 3]);
  264. expect(borderStyle.dashArray).toEqual([1, 2, 3]);
  265. });
  266. it('should not set and get an invalid dash array', function () {
  267. var borderStyle = new _annotation.AnnotationBorderStyle();
  268. borderStyle.setDashArray([0, 0]);
  269. expect(borderStyle.dashArray).toEqual([3]);
  270. });
  271. it('should set and get a valid horizontal corner radius', function () {
  272. var borderStyle = new _annotation.AnnotationBorderStyle();
  273. borderStyle.setHorizontalCornerRadius(3);
  274. expect(borderStyle.horizontalCornerRadius).toEqual(3);
  275. });
  276. it('should not set and get an invalid horizontal corner radius', function () {
  277. var borderStyle = new _annotation.AnnotationBorderStyle();
  278. borderStyle.setHorizontalCornerRadius('three');
  279. expect(borderStyle.horizontalCornerRadius).toEqual(0);
  280. });
  281. it('should set and get a valid vertical corner radius', function () {
  282. var borderStyle = new _annotation.AnnotationBorderStyle();
  283. borderStyle.setVerticalCornerRadius(3);
  284. expect(borderStyle.verticalCornerRadius).toEqual(3);
  285. });
  286. it('should not set and get an invalid vertical corner radius', function () {
  287. var borderStyle = new _annotation.AnnotationBorderStyle();
  288. borderStyle.setVerticalCornerRadius('three');
  289. expect(borderStyle.verticalCornerRadius).toEqual(0);
  290. });
  291. });
  292. describe('LinkAnnotation', function () {
  293. it('should correctly parse a URI action', function (done) {
  294. var actionDict = new _primitives.Dict();
  295. actionDict.set('Type', _primitives.Name.get('Action'));
  296. actionDict.set('S', _primitives.Name.get('URI'));
  297. actionDict.set('URI', 'http://www.ctan.org/tex-archive/info/lshort');
  298. var annotationDict = new _primitives.Dict();
  299. annotationDict.set('Type', _primitives.Name.get('Annot'));
  300. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  301. annotationDict.set('A', actionDict);
  302. var annotationRef = new _primitives.Ref(820, 0);
  303. var xref = new _test_utils.XRefMock([{
  304. ref: annotationRef,
  305. data: annotationDict
  306. }]);
  307. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref5) {
  308. var data = _ref5.data;
  309. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  310. expect(data.url).toEqual('http://www.ctan.org/tex-archive/info/lshort');
  311. expect(data.unsafeUrl).toEqual('http://www.ctan.org/tex-archive/info/lshort');
  312. expect(data.dest).toBeUndefined();
  313. done();
  314. }, done.fail);
  315. });
  316. it('should correctly parse a URI action, where the URI entry ' + 'is missing a protocol', function (done) {
  317. var actionDict = new _primitives.Dict();
  318. actionDict.set('Type', _primitives.Name.get('Action'));
  319. actionDict.set('S', _primitives.Name.get('URI'));
  320. actionDict.set('URI', 'www.hmrc.gov.uk');
  321. var annotationDict = new _primitives.Dict();
  322. annotationDict.set('Type', _primitives.Name.get('Annot'));
  323. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  324. annotationDict.set('A', actionDict);
  325. var annotationRef = new _primitives.Ref(353, 0);
  326. var xref = new _test_utils.XRefMock([{
  327. ref: annotationRef,
  328. data: annotationDict
  329. }]);
  330. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref6) {
  331. var data = _ref6.data;
  332. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  333. expect(data.url).toEqual('http://www.hmrc.gov.uk/');
  334. expect(data.unsafeUrl).toEqual('http://www.hmrc.gov.uk');
  335. expect(data.dest).toBeUndefined();
  336. done();
  337. }, done.fail);
  338. });
  339. it('should correctly parse a URI action, where the URI entry ' + 'has an incorrect encoding (bug 1122280)', function (done) {
  340. 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');
  341. var lexer = new _parser.Lexer(actionStream);
  342. var parser = new _parser.Parser(lexer);
  343. var actionDict = parser.getObj();
  344. var annotationDict = new _primitives.Dict();
  345. annotationDict.set('Type', _primitives.Name.get('Annot'));
  346. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  347. annotationDict.set('A', actionDict);
  348. var annotationRef = new _primitives.Ref(8, 0);
  349. var xref = new _test_utils.XRefMock([{
  350. ref: annotationRef,
  351. data: annotationDict
  352. }]);
  353. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref7) {
  354. var data = _ref7.data;
  355. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  356. expect(data.url).toEqual(new URL((0, _util.stringToUTF8String)('http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4')).href);
  357. expect(data.unsafeUrl).toEqual((0, _util.stringToUTF8String)('http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4'));
  358. expect(data.dest).toBeUndefined();
  359. done();
  360. }, done.fail);
  361. });
  362. it('should correctly parse a GoTo action', function (done) {
  363. var actionDict = new _primitives.Dict();
  364. actionDict.set('Type', _primitives.Name.get('Action'));
  365. actionDict.set('S', _primitives.Name.get('GoTo'));
  366. actionDict.set('D', 'page.157');
  367. var annotationDict = new _primitives.Dict();
  368. annotationDict.set('Type', _primitives.Name.get('Annot'));
  369. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  370. annotationDict.set('A', actionDict);
  371. var annotationRef = new _primitives.Ref(798, 0);
  372. var xref = new _test_utils.XRefMock([{
  373. ref: annotationRef,
  374. data: annotationDict
  375. }]);
  376. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref8) {
  377. var data = _ref8.data;
  378. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  379. expect(data.url).toBeUndefined();
  380. expect(data.unsafeUrl).toBeUndefined();
  381. expect(data.dest).toEqual('page.157');
  382. done();
  383. }, done.fail);
  384. });
  385. it('should correctly parse a GoToR action, where the FileSpec entry ' + 'is a string containing a relative URL', function (done) {
  386. var actionDict = new _primitives.Dict();
  387. actionDict.set('Type', _primitives.Name.get('Action'));
  388. actionDict.set('S', _primitives.Name.get('GoToR'));
  389. actionDict.set('F', '../../0013/001346/134685E.pdf');
  390. actionDict.set('D', '4.3');
  391. actionDict.set('NewWindow', true);
  392. var annotationDict = new _primitives.Dict();
  393. annotationDict.set('Type', _primitives.Name.get('Annot'));
  394. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  395. annotationDict.set('A', actionDict);
  396. var annotationRef = new _primitives.Ref(489, 0);
  397. var xref = new _test_utils.XRefMock([{
  398. ref: annotationRef,
  399. data: annotationDict
  400. }]);
  401. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref9) {
  402. var data = _ref9.data;
  403. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  404. expect(data.url).toBeUndefined();
  405. expect(data.unsafeUrl).toEqual('../../0013/001346/134685E.pdf#4.3');
  406. expect(data.dest).toBeUndefined();
  407. expect(data.newWindow).toEqual(true);
  408. done();
  409. }, done.fail);
  410. });
  411. it('should correctly parse a GoToR action, containing a relative URL, ' + 'with the "docBaseUrl" parameter specified', function (done) {
  412. var actionDict = new _primitives.Dict();
  413. actionDict.set('Type', _primitives.Name.get('Action'));
  414. actionDict.set('S', _primitives.Name.get('GoToR'));
  415. actionDict.set('F', '../../0013/001346/134685E.pdf');
  416. actionDict.set('D', '4.3');
  417. var annotationDict = new _primitives.Dict();
  418. annotationDict.set('Type', _primitives.Name.get('Annot'));
  419. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  420. annotationDict.set('A', actionDict);
  421. var annotationRef = new _primitives.Ref(489, 0);
  422. var xref = new _test_utils.XRefMock([{
  423. ref: annotationRef,
  424. data: annotationDict
  425. }]);
  426. var pdfManager = new PDFManagerMock({
  427. docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf'
  428. });
  429. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock).then(function (_ref10) {
  430. var data = _ref10.data;
  431. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  432. expect(data.url).toEqual('http://www.example.com/0013/001346/134685E.pdf#4.3');
  433. expect(data.unsafeUrl).toEqual('../../0013/001346/134685E.pdf#4.3');
  434. expect(data.dest).toBeUndefined();
  435. done();
  436. }, done.fail);
  437. });
  438. it('should correctly parse a GoToR action, with named destination', function (done) {
  439. var actionDict = new _primitives.Dict();
  440. actionDict.set('Type', _primitives.Name.get('Action'));
  441. actionDict.set('S', _primitives.Name.get('GoToR'));
  442. actionDict.set('F', 'http://www.example.com/test.pdf');
  443. actionDict.set('D', '15');
  444. var annotationDict = new _primitives.Dict();
  445. annotationDict.set('Type', _primitives.Name.get('Annot'));
  446. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  447. annotationDict.set('A', actionDict);
  448. var annotationRef = new _primitives.Ref(495, 0);
  449. var xref = new _test_utils.XRefMock([{
  450. ref: annotationRef,
  451. data: annotationDict
  452. }]);
  453. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref11) {
  454. var data = _ref11.data;
  455. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  456. expect(data.url).toEqual('http://www.example.com/test.pdf#15');
  457. expect(data.unsafeUrl).toEqual('http://www.example.com/test.pdf#15');
  458. expect(data.dest).toBeUndefined();
  459. expect(data.newWindow).toBeFalsy();
  460. done();
  461. }, done.fail);
  462. });
  463. it('should correctly parse a GoToR action, with explicit destination array', function (done) {
  464. var actionDict = new _primitives.Dict();
  465. actionDict.set('Type', _primitives.Name.get('Action'));
  466. actionDict.set('S', _primitives.Name.get('GoToR'));
  467. actionDict.set('F', 'http://www.example.com/test.pdf');
  468. actionDict.set('D', [14, _primitives.Name.get('XYZ'), null, 298.043, null]);
  469. var annotationDict = new _primitives.Dict();
  470. annotationDict.set('Type', _primitives.Name.get('Annot'));
  471. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  472. annotationDict.set('A', actionDict);
  473. var annotationRef = new _primitives.Ref(489, 0);
  474. var xref = new _test_utils.XRefMock([{
  475. ref: annotationRef,
  476. data: annotationDict
  477. }]);
  478. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref12) {
  479. var data = _ref12.data;
  480. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  481. expect(data.url).toEqual(new URL('http://www.example.com/test.pdf#' + '[14,{"name":"XYZ"},null,298.043,null]').href);
  482. expect(data.unsafeUrl).toEqual('http://www.example.com/test.pdf#' + '[14,{"name":"XYZ"},null,298.043,null]');
  483. expect(data.dest).toBeUndefined();
  484. expect(data.newWindow).toBeFalsy();
  485. done();
  486. }, done.fail);
  487. });
  488. it('should correctly parse a Launch action, where the FileSpec dict ' + 'contains a relative URL, with the "docBaseUrl" parameter specified', function (done) {
  489. var fileSpecDict = new _primitives.Dict();
  490. fileSpecDict.set('Type', _primitives.Name.get('FileSpec'));
  491. fileSpecDict.set('F', 'Part II/Part II.pdf');
  492. fileSpecDict.set('UF', 'Part II/Part II.pdf');
  493. var actionDict = new _primitives.Dict();
  494. actionDict.set('Type', _primitives.Name.get('Action'));
  495. actionDict.set('S', _primitives.Name.get('Launch'));
  496. actionDict.set('F', fileSpecDict);
  497. actionDict.set('NewWindow', true);
  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(88, 0);
  503. var xref = new _test_utils.XRefMock([{
  504. ref: annotationRef,
  505. data: annotationDict
  506. }]);
  507. var pdfManager = new PDFManagerMock({
  508. docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf'
  509. });
  510. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock).then(function (_ref13) {
  511. var data = _ref13.data;
  512. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  513. expect(data.url).toEqual(new URL('http://www.example.com/test/pdfs/Part II/Part II.pdf').href);
  514. expect(data.unsafeUrl).toEqual('Part II/Part II.pdf');
  515. expect(data.dest).toBeUndefined();
  516. expect(data.newWindow).toEqual(true);
  517. done();
  518. }, done.fail);
  519. });
  520. it('should recover valid URLs from JavaScript actions having certain ' + 'white-listed formats', function (done) {
  521. function checkJsAction(params) {
  522. var jsEntry = params.jsEntry;
  523. var expectedUrl = params.expectedUrl;
  524. var expectedUnsafeUrl = params.expectedUnsafeUrl;
  525. var expectedNewWindow = params.expectedNewWindow;
  526. var actionDict = new _primitives.Dict();
  527. actionDict.set('Type', _primitives.Name.get('Action'));
  528. actionDict.set('S', _primitives.Name.get('JavaScript'));
  529. actionDict.set('JS', jsEntry);
  530. var annotationDict = new _primitives.Dict();
  531. annotationDict.set('Type', _primitives.Name.get('Annot'));
  532. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  533. annotationDict.set('A', actionDict);
  534. var annotationRef = new _primitives.Ref(46, 0);
  535. var xref = new _test_utils.XRefMock([{
  536. ref: annotationRef,
  537. data: annotationDict
  538. }]);
  539. return _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref14) {
  540. var data = _ref14.data;
  541. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  542. expect(data.url).toEqual(expectedUrl);
  543. expect(data.unsafeUrl).toEqual(expectedUnsafeUrl);
  544. expect(data.dest).toBeUndefined();
  545. expect(data.newWindow).toEqual(expectedNewWindow);
  546. });
  547. }
  548. var annotation1 = checkJsAction({
  549. jsEntry: 'function someFun() { return "qwerty"; } someFun();',
  550. expectedUrl: undefined,
  551. expectedUnsafeUrl: undefined,
  552. expectedNewWindow: undefined
  553. });
  554. var annotation2 = checkJsAction({
  555. jsEntry: 'window.open(\'http://www.example.com/test.pdf\')',
  556. expectedUrl: new URL('http://www.example.com/test.pdf').href,
  557. expectedUnsafeUrl: 'http://www.example.com/test.pdf',
  558. expectedNewWindow: undefined
  559. });
  560. var annotation3 = checkJsAction({
  561. jsEntry: new _stream.StringStream('app.launchURL("http://www.example.com/test.pdf", true)'),
  562. expectedUrl: new URL('http://www.example.com/test.pdf').href,
  563. expectedUnsafeUrl: 'http://www.example.com/test.pdf',
  564. expectedNewWindow: true
  565. });
  566. Promise.all([annotation1, annotation2, annotation3]).then(done, done.fail);
  567. });
  568. it('should correctly parse a Named action', function (done) {
  569. var actionDict = new _primitives.Dict();
  570. actionDict.set('Type', _primitives.Name.get('Action'));
  571. actionDict.set('S', _primitives.Name.get('Named'));
  572. actionDict.set('N', _primitives.Name.get('GoToPage'));
  573. var annotationDict = new _primitives.Dict();
  574. annotationDict.set('Type', _primitives.Name.get('Annot'));
  575. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  576. annotationDict.set('A', actionDict);
  577. var annotationRef = new _primitives.Ref(12, 0);
  578. var xref = new _test_utils.XRefMock([{
  579. ref: annotationRef,
  580. data: annotationDict
  581. }]);
  582. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref15) {
  583. var data = _ref15.data;
  584. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  585. expect(data.url).toBeUndefined();
  586. expect(data.unsafeUrl).toBeUndefined();
  587. expect(data.action).toEqual('GoToPage');
  588. done();
  589. }, done.fail);
  590. });
  591. it('should correctly parse a simple Dest', function (done) {
  592. var annotationDict = new _primitives.Dict();
  593. annotationDict.set('Type', _primitives.Name.get('Annot'));
  594. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  595. annotationDict.set('Dest', _primitives.Name.get('LI0'));
  596. var annotationRef = new _primitives.Ref(583, 0);
  597. var xref = new _test_utils.XRefMock([{
  598. ref: annotationRef,
  599. data: annotationDict
  600. }]);
  601. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref16) {
  602. var data = _ref16.data;
  603. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  604. expect(data.url).toBeUndefined();
  605. expect(data.unsafeUrl).toBeUndefined();
  606. expect(data.dest).toEqual('LI0');
  607. done();
  608. }, done.fail);
  609. });
  610. it('should correctly parse a simple Dest, with explicit destination array', function (done) {
  611. var annotationDict = new _primitives.Dict();
  612. annotationDict.set('Type', _primitives.Name.get('Annot'));
  613. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  614. annotationDict.set('Dest', [new _primitives.Ref(17, 0), _primitives.Name.get('XYZ'), 0, 841.89, null]);
  615. var annotationRef = new _primitives.Ref(10, 0);
  616. var xref = new _test_utils.XRefMock([{
  617. ref: annotationRef,
  618. data: annotationDict
  619. }]);
  620. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref17) {
  621. var data = _ref17.data;
  622. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  623. expect(data.url).toBeUndefined();
  624. expect(data.unsafeUrl).toBeUndefined();
  625. expect(data.dest).toEqual([{
  626. num: 17,
  627. gen: 0
  628. }, {
  629. name: 'XYZ'
  630. }, 0, 841.89, null]);
  631. done();
  632. }, done.fail);
  633. });
  634. it('should correctly parse a Dest, which violates the specification ' + 'by containing a dictionary', function (done) {
  635. var destDict = new _primitives.Dict();
  636. destDict.set('Type', _primitives.Name.get('Action'));
  637. destDict.set('S', _primitives.Name.get('GoTo'));
  638. destDict.set('D', 'page.157');
  639. var annotationDict = new _primitives.Dict();
  640. annotationDict.set('Type', _primitives.Name.get('Annot'));
  641. annotationDict.set('Subtype', _primitives.Name.get('Link'));
  642. annotationDict.set('Dest', destDict);
  643. var annotationRef = new _primitives.Ref(798, 0);
  644. var xref = new _test_utils.XRefMock([{
  645. ref: annotationRef,
  646. data: annotationDict
  647. }]);
  648. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(function (_ref18) {
  649. var data = _ref18.data;
  650. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  651. expect(data.url).toBeUndefined();
  652. expect(data.unsafeUrl).toBeUndefined();
  653. expect(data.dest).toEqual('page.157');
  654. done();
  655. }, done.fail);
  656. });
  657. });
  658. describe('WidgetAnnotation', function () {
  659. var widgetDict;
  660. beforeEach(function (done) {
  661. widgetDict = new _primitives.Dict();
  662. widgetDict.set('Type', _primitives.Name.get('Annot'));
  663. widgetDict.set('Subtype', _primitives.Name.get('Widget'));
  664. done();
  665. });
  666. afterEach(function () {
  667. widgetDict = null;
  668. });
  669. it('should handle unknown field names', function (done) {
  670. var widgetRef = new _primitives.Ref(20, 0);
  671. var xref = new _test_utils.XRefMock([{
  672. ref: widgetRef,
  673. data: widgetDict
  674. }]);
  675. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(function (_ref19) {
  676. var data = _ref19.data;
  677. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  678. expect(data.fieldName).toEqual('');
  679. done();
  680. }, done.fail);
  681. });
  682. it('should construct the field name when there are no ancestors', function (done) {
  683. widgetDict.set('T', 'foo');
  684. var widgetRef = new _primitives.Ref(21, 0);
  685. var xref = new _test_utils.XRefMock([{
  686. ref: widgetRef,
  687. data: widgetDict
  688. }]);
  689. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(function (_ref20) {
  690. var data = _ref20.data;
  691. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  692. expect(data.fieldName).toEqual('foo');
  693. done();
  694. }, done.fail);
  695. });
  696. it('should construct the field name when there are ancestors', function (done) {
  697. var firstParent = new _primitives.Dict();
  698. firstParent.set('T', 'foo');
  699. var secondParent = new _primitives.Dict();
  700. secondParent.set('Parent', firstParent);
  701. secondParent.set('T', 'bar');
  702. widgetDict.set('Parent', secondParent);
  703. widgetDict.set('T', 'baz');
  704. var widgetRef = new _primitives.Ref(22, 0);
  705. var xref = new _test_utils.XRefMock([{
  706. ref: widgetRef,
  707. data: widgetDict
  708. }]);
  709. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(function (_ref21) {
  710. var data = _ref21.data;
  711. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  712. expect(data.fieldName).toEqual('foo.bar.baz');
  713. done();
  714. }, done.fail);
  715. });
  716. it('should construct the field name if a parent is not a dictionary ' + '(issue 8143)', function (done) {
  717. var parentDict = new _primitives.Dict();
  718. parentDict.set('Parent', null);
  719. parentDict.set('T', 'foo');
  720. widgetDict.set('Parent', parentDict);
  721. widgetDict.set('T', 'bar');
  722. var widgetRef = new _primitives.Ref(22, 0);
  723. var xref = new _test_utils.XRefMock([{
  724. ref: widgetRef,
  725. data: widgetDict
  726. }]);
  727. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(function (_ref22) {
  728. var data = _ref22.data;
  729. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  730. expect(data.fieldName).toEqual('foo.bar');
  731. done();
  732. }, done.fail);
  733. });
  734. });
  735. describe('TextWidgetAnnotation', function () {
  736. var textWidgetDict;
  737. beforeEach(function (done) {
  738. textWidgetDict = new _primitives.Dict();
  739. textWidgetDict.set('Type', _primitives.Name.get('Annot'));
  740. textWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  741. textWidgetDict.set('FT', _primitives.Name.get('Tx'));
  742. done();
  743. });
  744. afterEach(function () {
  745. textWidgetDict = null;
  746. });
  747. it('should handle unknown text alignment, maximum length and flags', function (done) {
  748. var textWidgetRef = new _primitives.Ref(124, 0);
  749. var xref = new _test_utils.XRefMock([{
  750. ref: textWidgetRef,
  751. data: textWidgetDict
  752. }]);
  753. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref23) {
  754. var data = _ref23.data;
  755. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  756. expect(data.textAlignment).toEqual(null);
  757. expect(data.maxLen).toEqual(null);
  758. expect(data.readOnly).toEqual(false);
  759. expect(data.multiLine).toEqual(false);
  760. expect(data.comb).toEqual(false);
  761. done();
  762. }, done.fail);
  763. });
  764. it('should not set invalid text alignment, maximum length and flags', function (done) {
  765. textWidgetDict.set('Q', 'center');
  766. textWidgetDict.set('MaxLen', 'five');
  767. textWidgetDict.set('Ff', 'readonly');
  768. var textWidgetRef = new _primitives.Ref(43, 0);
  769. var xref = new _test_utils.XRefMock([{
  770. ref: textWidgetRef,
  771. data: textWidgetDict
  772. }]);
  773. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref24) {
  774. var data = _ref24.data;
  775. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  776. expect(data.textAlignment).toEqual(null);
  777. expect(data.maxLen).toEqual(null);
  778. expect(data.readOnly).toEqual(false);
  779. expect(data.multiLine).toEqual(false);
  780. expect(data.comb).toEqual(false);
  781. done();
  782. }, done.fail);
  783. });
  784. it('should set valid text alignment, maximum length and flags', function (done) {
  785. textWidgetDict.set('Q', 1);
  786. textWidgetDict.set('MaxLen', 20);
  787. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.MULTILINE);
  788. var textWidgetRef = new _primitives.Ref(84, 0);
  789. var xref = new _test_utils.XRefMock([{
  790. ref: textWidgetRef,
  791. data: textWidgetDict
  792. }]);
  793. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref25) {
  794. var data = _ref25.data;
  795. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  796. expect(data.textAlignment).toEqual(1);
  797. expect(data.maxLen).toEqual(20);
  798. expect(data.readOnly).toEqual(true);
  799. expect(data.multiLine).toEqual(true);
  800. done();
  801. }, done.fail);
  802. });
  803. it('should reject comb fields without a maximum length', function (done) {
  804. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  805. var textWidgetRef = new _primitives.Ref(46, 0);
  806. var xref = new _test_utils.XRefMock([{
  807. ref: textWidgetRef,
  808. data: textWidgetDict
  809. }]);
  810. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref26) {
  811. var data = _ref26.data;
  812. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  813. expect(data.comb).toEqual(false);
  814. done();
  815. }, done.fail);
  816. });
  817. it('should accept comb fields with a maximum length', function (done) {
  818. textWidgetDict.set('MaxLen', 20);
  819. textWidgetDict.set('Ff', _util.AnnotationFieldFlag.COMB);
  820. var textWidgetRef = new _primitives.Ref(46, 0);
  821. var xref = new _test_utils.XRefMock([{
  822. ref: textWidgetRef,
  823. data: textWidgetDict
  824. }]);
  825. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref27) {
  826. var data = _ref27.data;
  827. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  828. expect(data.comb).toEqual(true);
  829. done();
  830. }, done.fail);
  831. });
  832. it('should only accept comb fields when the flags are valid', function (done) {
  833. var invalidFieldFlags = [_util.AnnotationFieldFlag.MULTILINE, _util.AnnotationFieldFlag.PASSWORD, _util.AnnotationFieldFlag.FILESELECT];
  834. var flags = _util.AnnotationFieldFlag.COMB + _util.AnnotationFieldFlag.MULTILINE + _util.AnnotationFieldFlag.PASSWORD + _util.AnnotationFieldFlag.FILESELECT;
  835. var promise = Promise.resolve();
  836. for (var i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
  837. promise = promise.then(function () {
  838. textWidgetDict.set('MaxLen', 20);
  839. textWidgetDict.set('Ff', flags);
  840. var textWidgetRef = new _primitives.Ref(93, 0);
  841. var xref = new _test_utils.XRefMock([{
  842. ref: textWidgetRef,
  843. data: textWidgetDict
  844. }]);
  845. return _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref28) {
  846. var data = _ref28.data;
  847. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  848. var valid = invalidFieldFlags.length === 0;
  849. expect(data.comb).toEqual(valid);
  850. if (!valid) {
  851. flags -= invalidFieldFlags.pop();
  852. }
  853. });
  854. });
  855. }
  856. promise.then(done, done.fail);
  857. });
  858. });
  859. describe('ButtonWidgetAnnotation', function () {
  860. var buttonWidgetDict;
  861. beforeEach(function (done) {
  862. buttonWidgetDict = new _primitives.Dict();
  863. buttonWidgetDict.set('Type', _primitives.Name.get('Annot'));
  864. buttonWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  865. buttonWidgetDict.set('FT', _primitives.Name.get('Btn'));
  866. done();
  867. });
  868. afterEach(function () {
  869. buttonWidgetDict = null;
  870. });
  871. it('should handle checkboxes with export value', function (done) {
  872. buttonWidgetDict.set('V', _primitives.Name.get('1'));
  873. var appearanceStatesDict = new _primitives.Dict();
  874. var exportValueOptionsDict = new _primitives.Dict();
  875. exportValueOptionsDict.set('Off', 0);
  876. exportValueOptionsDict.set('Checked', 1);
  877. appearanceStatesDict.set('D', exportValueOptionsDict);
  878. buttonWidgetDict.set('AP', appearanceStatesDict);
  879. var buttonWidgetRef = new _primitives.Ref(124, 0);
  880. var xref = new _test_utils.XRefMock([{
  881. ref: buttonWidgetRef,
  882. data: buttonWidgetDict
  883. }]);
  884. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref29) {
  885. var data = _ref29.data;
  886. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  887. expect(data.checkBox).toEqual(true);
  888. expect(data.fieldValue).toEqual('1');
  889. expect(data.radioButton).toEqual(false);
  890. expect(data.exportValue).toEqual('Checked');
  891. done();
  892. }, done.fail);
  893. });
  894. it('should handle checkboxes without export value', function (done) {
  895. buttonWidgetDict.set('V', _primitives.Name.get('1'));
  896. var buttonWidgetRef = new _primitives.Ref(124, 0);
  897. var xref = new _test_utils.XRefMock([{
  898. ref: buttonWidgetRef,
  899. data: buttonWidgetDict
  900. }]);
  901. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref30) {
  902. var data = _ref30.data;
  903. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  904. expect(data.checkBox).toEqual(true);
  905. expect(data.fieldValue).toEqual('1');
  906. expect(data.radioButton).toEqual(false);
  907. done();
  908. }, done.fail);
  909. });
  910. it('should handle radio buttons with a field value', function (done) {
  911. var parentDict = new _primitives.Dict();
  912. parentDict.set('V', _primitives.Name.get('1'));
  913. var normalAppearanceStateDict = new _primitives.Dict();
  914. normalAppearanceStateDict.set('2', null);
  915. var appearanceStatesDict = new _primitives.Dict();
  916. appearanceStatesDict.set('N', normalAppearanceStateDict);
  917. buttonWidgetDict.set('Ff', _util.AnnotationFieldFlag.RADIO);
  918. buttonWidgetDict.set('Parent', parentDict);
  919. buttonWidgetDict.set('AP', appearanceStatesDict);
  920. var buttonWidgetRef = new _primitives.Ref(124, 0);
  921. var xref = new _test_utils.XRefMock([{
  922. ref: buttonWidgetRef,
  923. data: buttonWidgetDict
  924. }]);
  925. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref31) {
  926. var data = _ref31.data;
  927. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  928. expect(data.checkBox).toEqual(false);
  929. expect(data.radioButton).toEqual(true);
  930. expect(data.fieldValue).toEqual('1');
  931. expect(data.buttonValue).toEqual('2');
  932. done();
  933. }, done.fail);
  934. });
  935. it('should handle radio buttons without a field value', function (done) {
  936. var normalAppearanceStateDict = new _primitives.Dict();
  937. normalAppearanceStateDict.set('2', null);
  938. var appearanceStatesDict = new _primitives.Dict();
  939. appearanceStatesDict.set('N', normalAppearanceStateDict);
  940. buttonWidgetDict.set('Ff', _util.AnnotationFieldFlag.RADIO);
  941. buttonWidgetDict.set('AP', appearanceStatesDict);
  942. var buttonWidgetRef = new _primitives.Ref(124, 0);
  943. var xref = new _test_utils.XRefMock([{
  944. ref: buttonWidgetRef,
  945. data: buttonWidgetDict
  946. }]);
  947. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref32) {
  948. var data = _ref32.data;
  949. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  950. expect(data.checkBox).toEqual(false);
  951. expect(data.radioButton).toEqual(true);
  952. expect(data.fieldValue).toEqual(null);
  953. expect(data.buttonValue).toEqual('2');
  954. done();
  955. }, done.fail);
  956. });
  957. });
  958. describe('ChoiceWidgetAnnotation', function () {
  959. var choiceWidgetDict;
  960. beforeEach(function (done) {
  961. choiceWidgetDict = new _primitives.Dict();
  962. choiceWidgetDict.set('Type', _primitives.Name.get('Annot'));
  963. choiceWidgetDict.set('Subtype', _primitives.Name.get('Widget'));
  964. choiceWidgetDict.set('FT', _primitives.Name.get('Ch'));
  965. done();
  966. });
  967. afterEach(function () {
  968. choiceWidgetDict = null;
  969. });
  970. it('should handle missing option arrays', function (done) {
  971. var choiceWidgetRef = new _primitives.Ref(122, 0);
  972. var xref = new _test_utils.XRefMock([{
  973. ref: choiceWidgetRef,
  974. data: choiceWidgetDict
  975. }]);
  976. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref33) {
  977. var data = _ref33.data;
  978. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  979. expect(data.options).toEqual([]);
  980. done();
  981. }, done.fail);
  982. });
  983. it('should handle option arrays with array elements', function (done) {
  984. var optionBarRef = new _primitives.Ref(20, 0);
  985. var optionBarStr = 'Bar';
  986. var optionOneRef = new _primitives.Ref(10, 0);
  987. var optionOneArr = ['bar_export', optionBarRef];
  988. var options = [['foo_export', 'Foo'], optionOneRef];
  989. var expected = [{
  990. exportValue: 'foo_export',
  991. displayValue: 'Foo'
  992. }, {
  993. exportValue: 'bar_export',
  994. displayValue: 'Bar'
  995. }];
  996. choiceWidgetDict.set('Opt', options);
  997. var choiceWidgetRef = new _primitives.Ref(123, 0);
  998. var xref = new _test_utils.XRefMock([{
  999. ref: choiceWidgetRef,
  1000. data: choiceWidgetDict
  1001. }, {
  1002. ref: optionBarRef,
  1003. data: optionBarStr
  1004. }, {
  1005. ref: optionOneRef,
  1006. data: optionOneArr
  1007. }]);
  1008. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref34) {
  1009. var data = _ref34.data;
  1010. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1011. expect(data.options).toEqual(expected);
  1012. done();
  1013. }, done.fail);
  1014. });
  1015. it('should handle option arrays with string elements', function (done) {
  1016. var optionBarRef = new _primitives.Ref(10, 0);
  1017. var optionBarStr = 'Bar';
  1018. var options = ['Foo', optionBarRef];
  1019. var expected = [{
  1020. exportValue: 'Foo',
  1021. displayValue: 'Foo'
  1022. }, {
  1023. exportValue: 'Bar',
  1024. displayValue: 'Bar'
  1025. }];
  1026. choiceWidgetDict.set('Opt', options);
  1027. var choiceWidgetRef = new _primitives.Ref(981, 0);
  1028. var xref = new _test_utils.XRefMock([{
  1029. ref: choiceWidgetRef,
  1030. data: choiceWidgetDict
  1031. }, {
  1032. ref: optionBarRef,
  1033. data: optionBarStr
  1034. }]);
  1035. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref35) {
  1036. var data = _ref35.data;
  1037. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1038. expect(data.options).toEqual(expected);
  1039. done();
  1040. }, done.fail);
  1041. });
  1042. it('should handle inherited option arrays (issue 8094)', function (done) {
  1043. var options = [['Value1', 'Description1'], ['Value2', 'Description2']];
  1044. var expected = [{
  1045. exportValue: 'Value1',
  1046. displayValue: 'Description1'
  1047. }, {
  1048. exportValue: 'Value2',
  1049. displayValue: 'Description2'
  1050. }];
  1051. var parentDict = new _primitives.Dict();
  1052. parentDict.set('Opt', options);
  1053. choiceWidgetDict.set('Parent', parentDict);
  1054. var choiceWidgetRef = new _primitives.Ref(123, 0);
  1055. var xref = new _test_utils.XRefMock([{
  1056. ref: choiceWidgetRef,
  1057. data: choiceWidgetDict
  1058. }]);
  1059. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref36) {
  1060. var data = _ref36.data;
  1061. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1062. expect(data.options).toEqual(expected);
  1063. done();
  1064. }, done.fail);
  1065. });
  1066. it('should sanitize display values in option arrays (issue 8947)', function (done) {
  1067. var options = ['\xFE\xFF\x00F\x00o\x00o'];
  1068. var expected = [{
  1069. exportValue: '\xFE\xFF\x00F\x00o\x00o',
  1070. displayValue: 'Foo'
  1071. }];
  1072. choiceWidgetDict.set('Opt', options);
  1073. var choiceWidgetRef = new _primitives.Ref(984, 0);
  1074. var xref = new _test_utils.XRefMock([{
  1075. ref: choiceWidgetRef,
  1076. data: choiceWidgetDict
  1077. }]);
  1078. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref37) {
  1079. var data = _ref37.data;
  1080. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1081. expect(data.options).toEqual(expected);
  1082. done();
  1083. }, done.fail);
  1084. });
  1085. it('should handle array field values', function (done) {
  1086. var fieldValue = ['Foo', 'Bar'];
  1087. choiceWidgetDict.set('V', fieldValue);
  1088. var choiceWidgetRef = new _primitives.Ref(968, 0);
  1089. var xref = new _test_utils.XRefMock([{
  1090. ref: choiceWidgetRef,
  1091. data: choiceWidgetDict
  1092. }]);
  1093. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref38) {
  1094. var data = _ref38.data;
  1095. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1096. expect(data.fieldValue).toEqual(fieldValue);
  1097. done();
  1098. }, done.fail);
  1099. });
  1100. it('should handle string field values', function (done) {
  1101. var fieldValue = 'Foo';
  1102. choiceWidgetDict.set('V', fieldValue);
  1103. var choiceWidgetRef = new _primitives.Ref(978, 0);
  1104. var xref = new _test_utils.XRefMock([{
  1105. ref: choiceWidgetRef,
  1106. data: choiceWidgetDict
  1107. }]);
  1108. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref39) {
  1109. var data = _ref39.data;
  1110. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1111. expect(data.fieldValue).toEqual([fieldValue]);
  1112. done();
  1113. }, done.fail);
  1114. });
  1115. it('should handle unknown flags', function (done) {
  1116. var choiceWidgetRef = new _primitives.Ref(166, 0);
  1117. var xref = new _test_utils.XRefMock([{
  1118. ref: choiceWidgetRef,
  1119. data: choiceWidgetDict
  1120. }]);
  1121. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref40) {
  1122. var data = _ref40.data;
  1123. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1124. expect(data.readOnly).toEqual(false);
  1125. expect(data.combo).toEqual(false);
  1126. expect(data.multiSelect).toEqual(false);
  1127. done();
  1128. }, done.fail);
  1129. });
  1130. it('should not set invalid flags', function (done) {
  1131. choiceWidgetDict.set('Ff', 'readonly');
  1132. var choiceWidgetRef = new _primitives.Ref(165, 0);
  1133. var xref = new _test_utils.XRefMock([{
  1134. ref: choiceWidgetRef,
  1135. data: choiceWidgetDict
  1136. }]);
  1137. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref41) {
  1138. var data = _ref41.data;
  1139. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1140. expect(data.readOnly).toEqual(false);
  1141. expect(data.combo).toEqual(false);
  1142. expect(data.multiSelect).toEqual(false);
  1143. done();
  1144. }, done.fail);
  1145. });
  1146. it('should set valid flags', function (done) {
  1147. choiceWidgetDict.set('Ff', _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.COMBO + _util.AnnotationFieldFlag.MULTISELECT);
  1148. var choiceWidgetRef = new _primitives.Ref(512, 0);
  1149. var xref = new _test_utils.XRefMock([{
  1150. ref: choiceWidgetRef,
  1151. data: choiceWidgetDict
  1152. }]);
  1153. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(function (_ref42) {
  1154. var data = _ref42.data;
  1155. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1156. expect(data.readOnly).toEqual(true);
  1157. expect(data.combo).toEqual(true);
  1158. expect(data.multiSelect).toEqual(true);
  1159. done();
  1160. }, done.fail);
  1161. });
  1162. });
  1163. describe('LineAnnotation', function () {
  1164. it('should set the line coordinates', function (done) {
  1165. var lineDict = new _primitives.Dict();
  1166. lineDict.set('Type', _primitives.Name.get('Annot'));
  1167. lineDict.set('Subtype', _primitives.Name.get('Line'));
  1168. lineDict.set('L', [1, 2, 3, 4]);
  1169. var lineRef = new _primitives.Ref(122, 0);
  1170. var xref = new _test_utils.XRefMock([{
  1171. ref: lineRef,
  1172. data: lineDict
  1173. }]);
  1174. _annotation.AnnotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock).then(function (_ref43) {
  1175. var data = _ref43.data;
  1176. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  1177. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  1178. done();
  1179. }, done.fail);
  1180. });
  1181. });
  1182. describe('FileAttachmentAnnotation', function () {
  1183. it('should correctly parse a file attachment', function (done) {
  1184. var fileStream = new _stream.StringStream('<<\n' + '/Type /EmbeddedFile\n' + '/Subtype /text#2Fplain\n' + '>>\n' + 'stream\n' + 'Test attachment' + 'endstream\n');
  1185. var lexer = new _parser.Lexer(fileStream);
  1186. var parser = new _parser.Parser(lexer, true);
  1187. var fileStreamRef = new _primitives.Ref(18, 0);
  1188. var fileStreamDict = parser.getObj();
  1189. var embeddedFileDict = new _primitives.Dict();
  1190. embeddedFileDict.set('F', fileStreamRef);
  1191. var fileSpecRef = new _primitives.Ref(19, 0);
  1192. var fileSpecDict = new _primitives.Dict();
  1193. fileSpecDict.set('Type', _primitives.Name.get('Filespec'));
  1194. fileSpecDict.set('Desc', '');
  1195. fileSpecDict.set('EF', embeddedFileDict);
  1196. fileSpecDict.set('UF', 'Test.txt');
  1197. var fileAttachmentRef = new _primitives.Ref(20, 0);
  1198. var fileAttachmentDict = new _primitives.Dict();
  1199. fileAttachmentDict.set('Type', _primitives.Name.get('Annot'));
  1200. fileAttachmentDict.set('Subtype', _primitives.Name.get('FileAttachment'));
  1201. fileAttachmentDict.set('FS', fileSpecRef);
  1202. fileAttachmentDict.set('T', 'Topic');
  1203. fileAttachmentDict.set('Contents', 'Test.txt');
  1204. var xref = new _test_utils.XRefMock([{
  1205. ref: fileStreamRef,
  1206. data: fileStreamDict
  1207. }, {
  1208. ref: fileSpecRef,
  1209. data: fileSpecDict
  1210. }, {
  1211. ref: fileAttachmentRef,
  1212. data: fileAttachmentDict
  1213. }]);
  1214. embeddedFileDict.assignXref(xref);
  1215. fileSpecDict.assignXref(xref);
  1216. fileAttachmentDict.assignXref(xref);
  1217. _annotation.AnnotationFactory.create(xref, fileAttachmentRef, pdfManagerMock, idFactoryMock).then(function (_ref44) {
  1218. var data = _ref44.data;
  1219. expect(data.annotationType).toEqual(_util.AnnotationType.FILEATTACHMENT);
  1220. expect(data.file.filename).toEqual('Test.txt');
  1221. expect(data.file.content).toEqual((0, _util.stringToBytes)('Test attachment'));
  1222. done();
  1223. }, done.fail);
  1224. });
  1225. });
  1226. describe('PopupAnnotation', function () {
  1227. it('should inherit the parent flags when the Popup is not viewable, ' + 'but the parent is (PR 7352)', function (done) {
  1228. var parentDict = new _primitives.Dict();
  1229. parentDict.set('Type', _primitives.Name.get('Annot'));
  1230. parentDict.set('Subtype', _primitives.Name.get('Text'));
  1231. parentDict.set('F', 28);
  1232. var popupDict = new _primitives.Dict();
  1233. popupDict.set('Type', _primitives.Name.get('Annot'));
  1234. popupDict.set('Subtype', _primitives.Name.get('Popup'));
  1235. popupDict.set('F', 25);
  1236. popupDict.set('Parent', parentDict);
  1237. var popupRef = new _primitives.Ref(13, 0);
  1238. var xref = new _test_utils.XRefMock([{
  1239. ref: popupRef,
  1240. data: popupDict
  1241. }]);
  1242. _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock).then(function (_ref45) {
  1243. var data = _ref45.data,
  1244. viewable = _ref45.viewable;
  1245. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  1246. expect(data.annotationFlags).toEqual(25);
  1247. expect(viewable).toEqual(true);
  1248. done();
  1249. }, done.fail);
  1250. });
  1251. });
  1252. describe('InkAnnotation', function () {
  1253. it('should handle a single ink list', function (done) {
  1254. var inkDict = new _primitives.Dict();
  1255. inkDict.set('Type', _primitives.Name.get('Annot'));
  1256. inkDict.set('Subtype', _primitives.Name.get('Ink'));
  1257. inkDict.set('InkList', [[1, 1, 1, 2, 2, 2, 3, 3]]);
  1258. var inkRef = new _primitives.Ref(142, 0);
  1259. var xref = new _test_utils.XRefMock([{
  1260. ref: inkRef,
  1261. data: inkDict
  1262. }]);
  1263. _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock).then(function (_ref46) {
  1264. var data = _ref46.data;
  1265. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  1266. expect(data.inkLists.length).toEqual(1);
  1267. expect(data.inkLists[0]).toEqual([{
  1268. x: 1,
  1269. y: 1
  1270. }, {
  1271. x: 1,
  1272. y: 2
  1273. }, {
  1274. x: 2,
  1275. y: 2
  1276. }, {
  1277. x: 3,
  1278. y: 3
  1279. }]);
  1280. done();
  1281. }, done.fail);
  1282. });
  1283. it('should handle multiple ink lists', function (done) {
  1284. var inkDict = new _primitives.Dict();
  1285. inkDict.set('Type', _primitives.Name.get('Annot'));
  1286. inkDict.set('Subtype', _primitives.Name.get('Ink'));
  1287. inkDict.set('InkList', [[1, 1, 1, 2], [3, 3, 4, 5]]);
  1288. var inkRef = new _primitives.Ref(143, 0);
  1289. var xref = new _test_utils.XRefMock([{
  1290. ref: inkRef,
  1291. data: inkDict
  1292. }]);
  1293. _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock).then(function (_ref47) {
  1294. var data = _ref47.data;
  1295. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  1296. expect(data.inkLists.length).toEqual(2);
  1297. expect(data.inkLists[0]).toEqual([{
  1298. x: 1,
  1299. y: 1
  1300. }, {
  1301. x: 1,
  1302. y: 2
  1303. }]);
  1304. expect(data.inkLists[1]).toEqual([{
  1305. x: 3,
  1306. y: 3
  1307. }, {
  1308. x: 4,
  1309. y: 5
  1310. }]);
  1311. done();
  1312. }, done.fail);
  1313. });
  1314. });
  1315. });