2
0

dom_utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.SimpleXMLParser = 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 SimpleDOMNode = function () {
  158. function SimpleDOMNode(nodeName, nodeValue) {
  159. _classCallCheck(this, SimpleDOMNode);
  160. this.nodeName = nodeName;
  161. this.nodeValue = nodeValue;
  162. Object.defineProperty(this, 'parentNode', {
  163. value: null,
  164. writable: true
  165. });
  166. }
  167. _createClass(SimpleDOMNode, [{
  168. key: 'hasChildNodes',
  169. value: function hasChildNodes() {
  170. return this.childNodes && this.childNodes.length > 0;
  171. }
  172. }, {
  173. key: 'firstChild',
  174. get: function get() {
  175. return this.childNodes[0];
  176. }
  177. }, {
  178. key: 'nextSibling',
  179. get: function get() {
  180. var index = this.parentNode.childNodes.indexOf(this);
  181. return this.parentNode.childNodes[index + 1];
  182. }
  183. }, {
  184. key: 'textContent',
  185. get: function get() {
  186. if (!this.childNodes) {
  187. return this.nodeValue || '';
  188. }
  189. return this.childNodes.map(function (child) {
  190. return child.textContent;
  191. }).join('');
  192. }
  193. }]);
  194. return SimpleDOMNode;
  195. }();
  196. var SimpleXMLParser = function () {
  197. function SimpleXMLParser() {
  198. _classCallCheck(this, SimpleXMLParser);
  199. }
  200. _createClass(SimpleXMLParser, [{
  201. key: 'parseFromString',
  202. value: function parseFromString(data) {
  203. var _this2 = this;
  204. var nodes = [];
  205. data = data.replace(/<\?[\s\S]*?\?>|<!--[\s\S]*?-->/g, '').trim();
  206. data = data.replace(/<!DOCTYPE[^>\[]+(\[[^\]]+)?[^>]+>/g, '').trim();
  207. data = data.replace(/>([^<][\s\S]*?)</g, function (all, text) {
  208. var length = nodes.length;
  209. var node = new SimpleDOMNode('#text', _this2._decodeXML(text));
  210. nodes.push(node);
  211. if (node.textContent.trim().length === 0) {
  212. return '><';
  213. }
  214. return '>' + length + ',<';
  215. });
  216. data = data.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, function (all, text) {
  217. var length = nodes.length;
  218. var node = new SimpleDOMNode('#text', text);
  219. nodes.push(node);
  220. return length + ',';
  221. });
  222. var regex = /<([\w\:]+)((?:[\s\w:=]|'[^']*'|"[^"]*")*)(?:\/>|>([\d,]*)<\/[^>]+>)/g;
  223. var lastLength = void 0;
  224. do {
  225. lastLength = nodes.length;
  226. data = data.replace(regex, function (all, name, attrs, data) {
  227. var length = nodes.length;
  228. var node = new SimpleDOMNode(name);
  229. var children = [];
  230. if (data) {
  231. data = data.split(',');
  232. data.pop();
  233. data.forEach(function (child) {
  234. var childNode = nodes[+child];
  235. childNode.parentNode = node;
  236. children.push(childNode);
  237. });
  238. }
  239. node.childNodes = children;
  240. nodes.push(node);
  241. return length + ',';
  242. });
  243. } while (lastLength < nodes.length);
  244. return { documentElement: nodes.pop() };
  245. }
  246. }, {
  247. key: '_decodeXML',
  248. value: function _decodeXML(text) {
  249. if (!text.includes('&')) {
  250. return text;
  251. }
  252. return text.replace(/&(#(x[0-9a-f]+|\d+)|\w+);/gi, function (all, entityName, number) {
  253. if (number) {
  254. if (number[0] === 'x') {
  255. number = parseInt(number.substring(1), 16);
  256. } else {
  257. number = +number;
  258. }
  259. return String.fromCharCode(number);
  260. }
  261. switch (entityName) {
  262. case 'amp':
  263. return '&';
  264. case 'lt':
  265. return '<';
  266. case 'gt':
  267. return '>';
  268. case 'quot':
  269. return '\"';
  270. case 'apos':
  271. return '\'';
  272. }
  273. return '&' + entityName + ';';
  274. });
  275. }
  276. }]);
  277. return SimpleXMLParser;
  278. }();
  279. var RenderingCancelledException = function RenderingCancelledException() {
  280. function RenderingCancelledException(msg, type) {
  281. this.message = msg;
  282. this.type = type;
  283. }
  284. RenderingCancelledException.prototype = new Error();
  285. RenderingCancelledException.prototype.name = 'RenderingCancelledException';
  286. RenderingCancelledException.constructor = RenderingCancelledException;
  287. return RenderingCancelledException;
  288. }();
  289. var LinkTarget = {
  290. NONE: 0,
  291. SELF: 1,
  292. BLANK: 2,
  293. PARENT: 3,
  294. TOP: 4
  295. };
  296. var LinkTargetStringMap = ['', '_self', '_blank', '_parent', '_top'];
  297. function addLinkAttributes(link) {
  298. var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  299. url = _ref3.url,
  300. target = _ref3.target,
  301. rel = _ref3.rel;
  302. link.href = link.title = url ? (0, _util.removeNullCharacters)(url) : '';
  303. if (url) {
  304. var LinkTargetValues = Object.values(LinkTarget);
  305. var targetIndex = LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
  306. link.target = LinkTargetStringMap[targetIndex];
  307. link.rel = typeof rel === 'string' ? rel : DEFAULT_LINK_REL;
  308. }
  309. }
  310. function getFilenameFromUrl(url) {
  311. var anchor = url.indexOf('#');
  312. var query = url.indexOf('?');
  313. var end = Math.min(anchor > 0 ? anchor : url.length, query > 0 ? query : url.length);
  314. return url.substring(url.lastIndexOf('/', end) + 1, end);
  315. }
  316. var StatTimer = function () {
  317. function StatTimer() {
  318. var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  319. _classCallCheck(this, StatTimer);
  320. this.enabled = !!enable;
  321. this.started = Object.create(null);
  322. this.times = [];
  323. }
  324. _createClass(StatTimer, [{
  325. key: 'time',
  326. value: function time(name) {
  327. if (!this.enabled) {
  328. return;
  329. }
  330. if (name in this.started) {
  331. (0, _util.warn)('Timer is already running for ' + name);
  332. }
  333. this.started[name] = Date.now();
  334. }
  335. }, {
  336. key: 'timeEnd',
  337. value: function timeEnd(name) {
  338. if (!this.enabled) {
  339. return;
  340. }
  341. if (!(name in this.started)) {
  342. (0, _util.warn)('Timer has not been started for ' + name);
  343. }
  344. this.times.push({
  345. 'name': name,
  346. 'start': this.started[name],
  347. 'end': Date.now()
  348. });
  349. delete this.started[name];
  350. }
  351. }, {
  352. key: 'toString',
  353. value: function toString() {
  354. var times = this.times;
  355. var out = '',
  356. longest = 0;
  357. for (var i = 0, ii = times.length; i < ii; ++i) {
  358. var name = times[i]['name'];
  359. if (name.length > longest) {
  360. longest = name.length;
  361. }
  362. }
  363. for (var _i = 0, _ii = times.length; _i < _ii; ++_i) {
  364. var span = times[_i];
  365. var duration = span.end - span.start;
  366. out += span['name'].padEnd(longest) + ' ' + duration + 'ms\n';
  367. }
  368. return out;
  369. }
  370. }]);
  371. return StatTimer;
  372. }();
  373. var DummyStatTimer = function () {
  374. function DummyStatTimer() {
  375. _classCallCheck(this, DummyStatTimer);
  376. (0, _util.unreachable)('Cannot initialize DummyStatTimer.');
  377. }
  378. _createClass(DummyStatTimer, null, [{
  379. key: 'time',
  380. value: function time(name) {}
  381. }, {
  382. key: 'timeEnd',
  383. value: function timeEnd(name) {}
  384. }, {
  385. key: 'toString',
  386. value: function toString() {
  387. return '';
  388. }
  389. }]);
  390. return DummyStatTimer;
  391. }();
  392. exports.RenderingCancelledException = RenderingCancelledException;
  393. exports.addLinkAttributes = addLinkAttributes;
  394. exports.getFilenameFromUrl = getFilenameFromUrl;
  395. exports.LinkTarget = LinkTarget;
  396. exports.DEFAULT_LINK_REL = DEFAULT_LINK_REL;
  397. exports.DOMCanvasFactory = DOMCanvasFactory;
  398. exports.DOMCMapReaderFactory = DOMCMapReaderFactory;
  399. exports.DOMSVGFactory = DOMSVGFactory;
  400. exports.SimpleXMLParser = SimpleXMLParser;
  401. exports.StatTimer = StatTimer;
  402. exports.DummyStatTimer = DummyStatTimer;