HTMLAdaptor.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.HTMLAdaptor = void 0;
  30. var DOMAdaptor_js_1 = require("../core/DOMAdaptor.js");
  31. var HTMLAdaptor = (function (_super) {
  32. __extends(HTMLAdaptor, _super);
  33. function HTMLAdaptor(window) {
  34. var _this = _super.call(this, window.document) || this;
  35. _this.window = window;
  36. _this.parser = new window.DOMParser();
  37. return _this;
  38. }
  39. HTMLAdaptor.prototype.parse = function (text, format) {
  40. if (format === void 0) { format = 'text/html'; }
  41. return this.parser.parseFromString(text, format);
  42. };
  43. HTMLAdaptor.prototype.create = function (kind, ns) {
  44. return (ns ?
  45. this.document.createElementNS(ns, kind) :
  46. this.document.createElement(kind));
  47. };
  48. HTMLAdaptor.prototype.text = function (text) {
  49. return this.document.createTextNode(text);
  50. };
  51. HTMLAdaptor.prototype.head = function (doc) {
  52. return doc.head || doc;
  53. };
  54. HTMLAdaptor.prototype.body = function (doc) {
  55. return doc.body || doc;
  56. };
  57. HTMLAdaptor.prototype.root = function (doc) {
  58. return doc.documentElement || doc;
  59. };
  60. HTMLAdaptor.prototype.doctype = function (doc) {
  61. return (doc.doctype ? "<!DOCTYPE ".concat(doc.doctype.name, ">") : '');
  62. };
  63. HTMLAdaptor.prototype.tags = function (node, name, ns) {
  64. if (ns === void 0) { ns = null; }
  65. var nodes = (ns ? node.getElementsByTagNameNS(ns, name) : node.getElementsByTagName(name));
  66. return Array.from(nodes);
  67. };
  68. HTMLAdaptor.prototype.getElements = function (nodes, _document) {
  69. var e_1, _a;
  70. var containers = [];
  71. try {
  72. for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
  73. var node = nodes_1_1.value;
  74. if (typeof (node) === 'string') {
  75. containers = containers.concat(Array.from(this.document.querySelectorAll(node)));
  76. }
  77. else if (Array.isArray(node)) {
  78. containers = containers.concat(Array.from(node));
  79. }
  80. else if (node instanceof this.window.NodeList || node instanceof this.window.HTMLCollection) {
  81. containers = containers.concat(Array.from(node));
  82. }
  83. else {
  84. containers.push(node);
  85. }
  86. }
  87. }
  88. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  89. finally {
  90. try {
  91. if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
  92. }
  93. finally { if (e_1) throw e_1.error; }
  94. }
  95. return containers;
  96. };
  97. HTMLAdaptor.prototype.contains = function (container, node) {
  98. return container.contains(node);
  99. };
  100. HTMLAdaptor.prototype.parent = function (node) {
  101. return node.parentNode;
  102. };
  103. HTMLAdaptor.prototype.append = function (node, child) {
  104. return node.appendChild(child);
  105. };
  106. HTMLAdaptor.prototype.insert = function (nchild, ochild) {
  107. return this.parent(ochild).insertBefore(nchild, ochild);
  108. };
  109. HTMLAdaptor.prototype.remove = function (child) {
  110. return this.parent(child).removeChild(child);
  111. };
  112. HTMLAdaptor.prototype.replace = function (nnode, onode) {
  113. return this.parent(onode).replaceChild(nnode, onode);
  114. };
  115. HTMLAdaptor.prototype.clone = function (node) {
  116. return node.cloneNode(true);
  117. };
  118. HTMLAdaptor.prototype.split = function (node, n) {
  119. return node.splitText(n);
  120. };
  121. HTMLAdaptor.prototype.next = function (node) {
  122. return node.nextSibling;
  123. };
  124. HTMLAdaptor.prototype.previous = function (node) {
  125. return node.previousSibling;
  126. };
  127. HTMLAdaptor.prototype.firstChild = function (node) {
  128. return node.firstChild;
  129. };
  130. HTMLAdaptor.prototype.lastChild = function (node) {
  131. return node.lastChild;
  132. };
  133. HTMLAdaptor.prototype.childNodes = function (node) {
  134. return Array.from(node.childNodes);
  135. };
  136. HTMLAdaptor.prototype.childNode = function (node, i) {
  137. return node.childNodes[i];
  138. };
  139. HTMLAdaptor.prototype.kind = function (node) {
  140. var n = node.nodeType;
  141. return (n === 1 || n === 3 || n === 8 ? node.nodeName.toLowerCase() : '');
  142. };
  143. HTMLAdaptor.prototype.value = function (node) {
  144. return node.nodeValue || '';
  145. };
  146. HTMLAdaptor.prototype.textContent = function (node) {
  147. return node.textContent;
  148. };
  149. HTMLAdaptor.prototype.innerHTML = function (node) {
  150. return node.innerHTML;
  151. };
  152. HTMLAdaptor.prototype.outerHTML = function (node) {
  153. return node.outerHTML;
  154. };
  155. HTMLAdaptor.prototype.serializeXML = function (node) {
  156. var serializer = new this.window.XMLSerializer();
  157. return serializer.serializeToString(node);
  158. };
  159. HTMLAdaptor.prototype.setAttribute = function (node, name, value, ns) {
  160. if (ns === void 0) { ns = null; }
  161. if (!ns) {
  162. return node.setAttribute(name, value);
  163. }
  164. name = ns.replace(/.*\//, '') + ':' + name.replace(/^.*:/, '');
  165. return node.setAttributeNS(ns, name, value);
  166. };
  167. HTMLAdaptor.prototype.getAttribute = function (node, name) {
  168. return node.getAttribute(name);
  169. };
  170. HTMLAdaptor.prototype.removeAttribute = function (node, name) {
  171. return node.removeAttribute(name);
  172. };
  173. HTMLAdaptor.prototype.hasAttribute = function (node, name) {
  174. return node.hasAttribute(name);
  175. };
  176. HTMLAdaptor.prototype.allAttributes = function (node) {
  177. return Array.from(node.attributes).map(function (x) {
  178. return { name: x.name, value: x.value };
  179. });
  180. };
  181. HTMLAdaptor.prototype.addClass = function (node, name) {
  182. if (node.classList) {
  183. node.classList.add(name);
  184. }
  185. else {
  186. node.className = (node.className + ' ' + name).trim();
  187. }
  188. };
  189. HTMLAdaptor.prototype.removeClass = function (node, name) {
  190. if (node.classList) {
  191. node.classList.remove(name);
  192. }
  193. else {
  194. node.className = node.className.split(/ /).filter(function (c) { return c !== name; }).join(' ');
  195. }
  196. };
  197. HTMLAdaptor.prototype.hasClass = function (node, name) {
  198. if (node.classList) {
  199. return node.classList.contains(name);
  200. }
  201. return node.className.split(/ /).indexOf(name) >= 0;
  202. };
  203. HTMLAdaptor.prototype.setStyle = function (node, name, value) {
  204. node.style[name] = value;
  205. };
  206. HTMLAdaptor.prototype.getStyle = function (node, name) {
  207. return node.style[name];
  208. };
  209. HTMLAdaptor.prototype.allStyles = function (node) {
  210. return node.style.cssText;
  211. };
  212. HTMLAdaptor.prototype.insertRules = function (node, rules) {
  213. var e_2, _a;
  214. try {
  215. for (var _b = __values(rules.reverse()), _c = _b.next(); !_c.done; _c = _b.next()) {
  216. var rule = _c.value;
  217. try {
  218. node.sheet.insertRule(rule, 0);
  219. }
  220. catch (e) {
  221. console.warn("MathJax: can't insert css rule '".concat(rule, "': ").concat(e.message));
  222. }
  223. }
  224. }
  225. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  226. finally {
  227. try {
  228. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  229. }
  230. finally { if (e_2) throw e_2.error; }
  231. }
  232. };
  233. HTMLAdaptor.prototype.fontSize = function (node) {
  234. var style = this.window.getComputedStyle(node);
  235. return parseFloat(style.fontSize);
  236. };
  237. HTMLAdaptor.prototype.fontFamily = function (node) {
  238. var style = this.window.getComputedStyle(node);
  239. return style.fontFamily || '';
  240. };
  241. HTMLAdaptor.prototype.nodeSize = function (node, em, local) {
  242. if (em === void 0) { em = 1; }
  243. if (local === void 0) { local = false; }
  244. if (local && node.getBBox) {
  245. var _a = node.getBBox(), width = _a.width, height = _a.height;
  246. return [width / em, height / em];
  247. }
  248. return [node.offsetWidth / em, node.offsetHeight / em];
  249. };
  250. HTMLAdaptor.prototype.nodeBBox = function (node) {
  251. var _a = node.getBoundingClientRect(), left = _a.left, right = _a.right, top = _a.top, bottom = _a.bottom;
  252. return { left: left, right: right, top: top, bottom: bottom };
  253. };
  254. return HTMLAdaptor;
  255. }(DOMAdaptor_js_1.AbstractDOMAdaptor));
  256. exports.HTMLAdaptor = HTMLAdaptor;
  257. //# sourceMappingURL=HTMLAdaptor.js.map