2
0

dom_utils_spec.js 4.1 KB

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