dom_utils_spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 _dom_utils = require('../../display/dom_utils');
  17. var _util = require('../../shared/util');
  18. var _global = require('../../display/global');
  19. describe('dom_utils', function () {
  20. describe('DOMSVGFactory', function () {
  21. var svgFactory = void 0;
  22. beforeAll(function (done) {
  23. svgFactory = new _dom_utils.DOMSVGFactory();
  24. done();
  25. });
  26. afterAll(function () {
  27. svgFactory = null;
  28. });
  29. it('`create` should throw an error if the dimensions are invalid', function () {
  30. expect(function () {
  31. return svgFactory.create(-1, 0);
  32. }).toThrow(new Error('Invalid SVG dimensions'));
  33. expect(function () {
  34. return svgFactory.create(0, -1);
  35. }).toThrow(new Error('Invalid SVG dimensions'));
  36. });
  37. it('`create` should return an SVG element if the dimensions are valid', function () {
  38. if ((0, _util.isNodeJS)()) {
  39. pending('Document is not supported in Node.js.');
  40. }
  41. var svg = svgFactory.create(20, 40);
  42. expect(svg instanceof SVGSVGElement).toBe(true);
  43. expect(svg.getAttribute('version')).toBe('1.1');
  44. expect(svg.getAttribute('width')).toBe('20px');
  45. expect(svg.getAttribute('height')).toBe('40px');
  46. expect(svg.getAttribute('preserveAspectRatio')).toBe('none');
  47. expect(svg.getAttribute('viewBox')).toBe('0 0 20 40');
  48. });
  49. it('`createElement` should throw an error if the type is not a string', function () {
  50. expect(function () {
  51. return svgFactory.createElement(true);
  52. }).toThrow(new Error('Invalid SVG element type'));
  53. });
  54. it('`createElement` should return an SVG element if the type is valid', function () {
  55. if ((0, _util.isNodeJS)()) {
  56. pending('Document is not supported in Node.js.');
  57. }
  58. var svg = svgFactory.createElement('svg:rect');
  59. expect(svg instanceof SVGRectElement).toBe(true);
  60. });
  61. });
  62. describe('getFilenameFromUrl', function () {
  63. it('should get the filename from an absolute URL', function () {
  64. var url = 'http://server.org/filename.pdf';
  65. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  66. var expected = 'filename.pdf';
  67. expect(result).toEqual(expected);
  68. });
  69. it('should get the filename from a relative URL', function () {
  70. var url = '../../filename.pdf';
  71. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  72. var expected = 'filename.pdf';
  73. expect(result).toEqual(expected);
  74. });
  75. });
  76. describe('isExternalLinkTargetSet', function () {
  77. var savedExternalLinkTarget;
  78. beforeAll(function (done) {
  79. savedExternalLinkTarget = _global.PDFJS.externalLinkTarget;
  80. done();
  81. });
  82. afterAll(function () {
  83. _global.PDFJS.externalLinkTarget = savedExternalLinkTarget;
  84. });
  85. it('handles the predefined LinkTargets', function () {
  86. for (var key in _dom_utils.LinkTarget) {
  87. var linkTarget = _dom_utils.LinkTarget[key];
  88. _global.PDFJS.externalLinkTarget = linkTarget;
  89. expect((0, _dom_utils.isExternalLinkTargetSet)()).toEqual(!!linkTarget);
  90. }
  91. });
  92. it('handles incorrect LinkTargets', function () {
  93. var targets = [true, '', false, -1, '_blank', null];
  94. for (var i = 0, ii = targets.length; i < ii; i++) {
  95. var linkTarget = targets[i];
  96. _global.PDFJS.externalLinkTarget = linkTarget;
  97. expect((0, _dom_utils.isExternalLinkTargetSet)()).toEqual(false);
  98. }
  99. });
  100. });
  101. });