dom_utils_spec.js 4.3 KB

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