dom_utils.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DummyStatTimer = exports.StatTimer = exports.DOMSVGFactory = exports.DOMCMapReaderFactory = exports.DOMCanvasFactory = exports.DEFAULT_LINK_REL = exports.LinkTarget = exports.getFilenameFromUrl = exports.addLinkAttributes = exports.RenderingCancelledException = undefined;
  27. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  28. var _util = require('../shared/util');
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. var DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
  31. var SVG_NS = 'http://www.w3.org/2000/svg';
  32. var DOMCanvasFactory = function () {
  33. function DOMCanvasFactory() {
  34. _classCallCheck(this, DOMCanvasFactory);
  35. }
  36. _createClass(DOMCanvasFactory, [{
  37. key: 'create',
  38. value: function create(width, height) {
  39. if (width <= 0 || height <= 0) {
  40. throw new Error('invalid canvas size');
  41. }
  42. var canvas = document.createElement('canvas');
  43. var context = canvas.getContext('2d');
  44. canvas.width = width;
  45. canvas.height = height;
  46. return {
  47. canvas: canvas,
  48. context: context
  49. };
  50. }
  51. }, {
  52. key: 'reset',
  53. value: function reset(canvasAndContext, width, height) {
  54. if (!canvasAndContext.canvas) {
  55. throw new Error('canvas is not specified');
  56. }
  57. if (width <= 0 || height <= 0) {
  58. throw new Error('invalid canvas size');
  59. }
  60. canvasAndContext.canvas.width = width;
  61. canvasAndContext.canvas.height = height;
  62. }
  63. }, {
  64. key: 'destroy',
  65. value: function destroy(canvasAndContext) {
  66. if (!canvasAndContext.canvas) {
  67. throw new Error('canvas is not specified');
  68. }
  69. canvasAndContext.canvas.width = 0;
  70. canvasAndContext.canvas.height = 0;
  71. canvasAndContext.canvas = null;
  72. canvasAndContext.context = null;
  73. }
  74. }]);
  75. return DOMCanvasFactory;
  76. }();
  77. var DOMCMapReaderFactory = function () {
  78. function DOMCMapReaderFactory(_ref) {
  79. var _ref$baseUrl = _ref.baseUrl,
  80. baseUrl = _ref$baseUrl === undefined ? null : _ref$baseUrl,
  81. _ref$isCompressed = _ref.isCompressed,
  82. isCompressed = _ref$isCompressed === undefined ? false : _ref$isCompressed;
  83. _classCallCheck(this, DOMCMapReaderFactory);
  84. this.baseUrl = baseUrl;
  85. this.isCompressed = isCompressed;
  86. }
  87. _createClass(DOMCMapReaderFactory, [{
  88. key: 'fetch',
  89. value: function fetch(_ref2) {
  90. var _this = this;
  91. var name = _ref2.name;
  92. if (!this.baseUrl) {
  93. return Promise.reject(new Error('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
  94. }
  95. if (!name) {
  96. return Promise.reject(new Error('CMap name must be specified.'));
  97. }
  98. return new Promise(function (resolve, reject) {
  99. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  100. var request = new XMLHttpRequest();
  101. request.open('GET', url, true);
  102. if (_this.isCompressed) {
  103. request.responseType = 'arraybuffer';
  104. }
  105. request.onreadystatechange = function () {
  106. if (request.readyState !== XMLHttpRequest.DONE) {
  107. return;
  108. }
  109. if (request.status === 200 || request.status === 0) {
  110. var data = void 0;
  111. if (_this.isCompressed && request.response) {
  112. data = new Uint8Array(request.response);
  113. } else if (!_this.isCompressed && request.responseText) {
  114. data = (0, _util.stringToBytes)(request.responseText);
  115. }
  116. if (data) {
  117. resolve({
  118. cMapData: data,
  119. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  120. });
  121. return;
  122. }
  123. }
  124. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  125. };
  126. request.send(null);
  127. });
  128. }
  129. }]);
  130. return DOMCMapReaderFactory;
  131. }();
  132. var DOMSVGFactory = function () {
  133. function DOMSVGFactory() {
  134. _classCallCheck(this, DOMSVGFactory);
  135. }
  136. _createClass(DOMSVGFactory, [{
  137. key: 'create',
  138. value: function create(width, height) {
  139. (0, _util.assert)(width > 0 && height > 0, 'Invalid SVG dimensions');
  140. var svg = document.createElementNS(SVG_NS, 'svg:svg');
  141. svg.setAttribute('version', '1.1');
  142. svg.setAttribute('width', width + 'px');
  143. svg.setAttribute('height', height + 'px');
  144. svg.setAttribute('preserveAspectRatio', 'none');
  145. svg.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
  146. return svg;
  147. }
  148. }, {
  149. key: 'createElement',
  150. value: function createElement(type) {
  151. (0, _util.assert)(typeof type === 'string', 'Invalid SVG element type');
  152. return document.createElementNS(SVG_NS, type);
  153. }
  154. }]);
  155. return DOMSVGFactory;
  156. }();
  157. var RenderingCancelledException = function RenderingCancelledException() {
  158. function RenderingCancelledException(msg, type) {
  159. this.message = msg;
  160. this.type = type;
  161. }
  162. RenderingCancelledException.prototype = new Error();
  163. RenderingCancelledException.prototype.name = 'RenderingCancelledException';
  164. RenderingCancelledException.constructor = RenderingCancelledException;
  165. return RenderingCancelledException;
  166. }();
  167. var LinkTarget = {
  168. NONE: 0,
  169. SELF: 1,
  170. BLANK: 2,
  171. PARENT: 3,
  172. TOP: 4
  173. };
  174. var LinkTargetStringMap = ['', '_self', '_blank', '_parent', '_top'];
  175. function addLinkAttributes(link) {
  176. var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  177. url = _ref3.url,
  178. target = _ref3.target,
  179. rel = _ref3.rel;
  180. link.href = link.title = url ? (0, _util.removeNullCharacters)(url) : '';
  181. if (url) {
  182. var LinkTargetValues = Object.values(LinkTarget);
  183. var targetIndex = LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
  184. link.target = LinkTargetStringMap[targetIndex];
  185. link.rel = typeof rel === 'string' ? rel : DEFAULT_LINK_REL;
  186. }
  187. }
  188. function getFilenameFromUrl(url) {
  189. var anchor = url.indexOf('#');
  190. var query = url.indexOf('?');
  191. var end = Math.min(anchor > 0 ? anchor : url.length, query > 0 ? query : url.length);
  192. return url.substring(url.lastIndexOf('/', end) + 1, end);
  193. }
  194. var StatTimer = function () {
  195. function StatTimer() {
  196. var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  197. _classCallCheck(this, StatTimer);
  198. this.enabled = !!enable;
  199. this.started = Object.create(null);
  200. this.times = [];
  201. }
  202. _createClass(StatTimer, [{
  203. key: 'time',
  204. value: function time(name) {
  205. if (!this.enabled) {
  206. return;
  207. }
  208. if (name in this.started) {
  209. (0, _util.warn)('Timer is already running for ' + name);
  210. }
  211. this.started[name] = Date.now();
  212. }
  213. }, {
  214. key: 'timeEnd',
  215. value: function timeEnd(name) {
  216. if (!this.enabled) {
  217. return;
  218. }
  219. if (!(name in this.started)) {
  220. (0, _util.warn)('Timer has not been started for ' + name);
  221. }
  222. this.times.push({
  223. 'name': name,
  224. 'start': this.started[name],
  225. 'end': Date.now()
  226. });
  227. delete this.started[name];
  228. }
  229. }, {
  230. key: 'toString',
  231. value: function toString() {
  232. var times = this.times;
  233. var out = '',
  234. longest = 0;
  235. for (var i = 0, ii = times.length; i < ii; ++i) {
  236. var name = times[i]['name'];
  237. if (name.length > longest) {
  238. longest = name.length;
  239. }
  240. }
  241. for (var _i = 0, _ii = times.length; _i < _ii; ++_i) {
  242. var span = times[_i];
  243. var duration = span.end - span.start;
  244. out += span['name'].padEnd(longest) + ' ' + duration + 'ms\n';
  245. }
  246. return out;
  247. }
  248. }]);
  249. return StatTimer;
  250. }();
  251. var DummyStatTimer = function () {
  252. function DummyStatTimer() {
  253. _classCallCheck(this, DummyStatTimer);
  254. (0, _util.unreachable)('Cannot initialize DummyStatTimer.');
  255. }
  256. _createClass(DummyStatTimer, null, [{
  257. key: 'time',
  258. value: function time(name) {}
  259. }, {
  260. key: 'timeEnd',
  261. value: function timeEnd(name) {}
  262. }, {
  263. key: 'toString',
  264. value: function toString() {
  265. return '';
  266. }
  267. }]);
  268. return DummyStatTimer;
  269. }();
  270. exports.RenderingCancelledException = RenderingCancelledException;
  271. exports.addLinkAttributes = addLinkAttributes;
  272. exports.getFilenameFromUrl = getFilenameFromUrl;
  273. exports.LinkTarget = LinkTarget;
  274. exports.DEFAULT_LINK_REL = DEFAULT_LINK_REL;
  275. exports.DOMCanvasFactory = DOMCanvasFactory;
  276. exports.DOMCMapReaderFactory = DOMCMapReaderFactory;
  277. exports.DOMSVGFactory = DOMSVGFactory;
  278. exports.StatTimer = StatTimer;
  279. exports.DummyStatTimer = DummyStatTimer;