NodeUtil.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var __read = (this && this.__read) || function (o, n) {
  14. var m = typeof Symbol === "function" && o[Symbol.iterator];
  15. if (!m) return o;
  16. var i = m.call(o), r, ar = [], e;
  17. try {
  18. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  19. }
  20. catch (error) { e = { error: error }; }
  21. finally {
  22. try {
  23. if (r && !r.done && (m = i["return"])) m.call(i);
  24. }
  25. finally { if (e) throw e.error; }
  26. }
  27. return ar;
  28. };
  29. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  30. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  31. if (ar || !(i in from)) {
  32. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  33. ar[i] = from[i];
  34. }
  35. }
  36. return to.concat(ar || Array.prototype.slice.call(from));
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. var MmlNode_js_1 = require("../../core/MmlTree/MmlNode.js");
  40. var mo_js_1 = require("../../core/MmlTree/MmlNodes/mo.js");
  41. var NodeUtil;
  42. (function (NodeUtil) {
  43. var attrs = new Map([
  44. ['autoOP', true],
  45. ['fnOP', true],
  46. ['movesupsub', true],
  47. ['subsupOK', true],
  48. ['texprimestyle', true],
  49. ['useHeight', true],
  50. ['variantForm', true],
  51. ['withDelims', true],
  52. ['mathaccent', true],
  53. ['open', true],
  54. ['close', true]
  55. ]);
  56. function createEntity(code) {
  57. return String.fromCodePoint(parseInt(code, 16));
  58. }
  59. NodeUtil.createEntity = createEntity;
  60. function getChildren(node) {
  61. return node.childNodes;
  62. }
  63. NodeUtil.getChildren = getChildren;
  64. function getText(node) {
  65. return node.getText();
  66. }
  67. NodeUtil.getText = getText;
  68. function appendChildren(node, children) {
  69. var e_1, _a;
  70. try {
  71. for (var children_1 = __values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
  72. var child = children_1_1.value;
  73. node.appendChild(child);
  74. }
  75. }
  76. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  77. finally {
  78. try {
  79. if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
  80. }
  81. finally { if (e_1) throw e_1.error; }
  82. }
  83. }
  84. NodeUtil.appendChildren = appendChildren;
  85. function setAttribute(node, attribute, value) {
  86. node.attributes.set(attribute, value);
  87. }
  88. NodeUtil.setAttribute = setAttribute;
  89. function setProperty(node, property, value) {
  90. node.setProperty(property, value);
  91. }
  92. NodeUtil.setProperty = setProperty;
  93. function setProperties(node, properties) {
  94. var e_2, _a;
  95. try {
  96. for (var _b = __values(Object.keys(properties)), _c = _b.next(); !_c.done; _c = _b.next()) {
  97. var name_1 = _c.value;
  98. var value = properties[name_1];
  99. if (name_1 === 'texClass') {
  100. node.texClass = value;
  101. node.setProperty(name_1, value);
  102. }
  103. else if (name_1 === 'movablelimits') {
  104. node.setProperty('movablelimits', value);
  105. if (node.isKind('mo') || node.isKind('mstyle')) {
  106. node.attributes.set('movablelimits', value);
  107. }
  108. }
  109. else if (name_1 === 'inferred') {
  110. }
  111. else if (attrs.has(name_1)) {
  112. node.setProperty(name_1, value);
  113. }
  114. else {
  115. node.attributes.set(name_1, value);
  116. }
  117. }
  118. }
  119. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  120. finally {
  121. try {
  122. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  123. }
  124. finally { if (e_2) throw e_2.error; }
  125. }
  126. }
  127. NodeUtil.setProperties = setProperties;
  128. function getProperty(node, property) {
  129. return node.getProperty(property);
  130. }
  131. NodeUtil.getProperty = getProperty;
  132. function getAttribute(node, attr) {
  133. return node.attributes.get(attr);
  134. }
  135. NodeUtil.getAttribute = getAttribute;
  136. function removeProperties(node) {
  137. var properties = [];
  138. for (var _i = 1; _i < arguments.length; _i++) {
  139. properties[_i - 1] = arguments[_i];
  140. }
  141. node.removeProperty.apply(node, __spreadArray([], __read(properties), false));
  142. }
  143. NodeUtil.removeProperties = removeProperties;
  144. function getChildAt(node, position) {
  145. return node.childNodes[position];
  146. }
  147. NodeUtil.getChildAt = getChildAt;
  148. function setChild(node, position, child) {
  149. var children = node.childNodes;
  150. children[position] = child;
  151. if (child) {
  152. child.parent = node;
  153. }
  154. }
  155. NodeUtil.setChild = setChild;
  156. function copyChildren(oldNode, newNode) {
  157. var children = oldNode.childNodes;
  158. for (var i = 0; i < children.length; i++) {
  159. setChild(newNode, i, children[i]);
  160. }
  161. }
  162. NodeUtil.copyChildren = copyChildren;
  163. function copyAttributes(oldNode, newNode) {
  164. newNode.attributes = oldNode.attributes;
  165. setProperties(newNode, oldNode.getAllProperties());
  166. }
  167. NodeUtil.copyAttributes = copyAttributes;
  168. function isType(node, kind) {
  169. return node.isKind(kind);
  170. }
  171. NodeUtil.isType = isType;
  172. function isEmbellished(node) {
  173. return node.isEmbellished;
  174. }
  175. NodeUtil.isEmbellished = isEmbellished;
  176. function getTexClass(node) {
  177. return node.texClass;
  178. }
  179. NodeUtil.getTexClass = getTexClass;
  180. function getCoreMO(node) {
  181. return node.coreMO();
  182. }
  183. NodeUtil.getCoreMO = getCoreMO;
  184. function isNode(item) {
  185. return item instanceof MmlNode_js_1.AbstractMmlNode || item instanceof MmlNode_js_1.AbstractMmlEmptyNode;
  186. }
  187. NodeUtil.isNode = isNode;
  188. function isInferred(node) {
  189. return node.isInferred;
  190. }
  191. NodeUtil.isInferred = isInferred;
  192. function getForm(node) {
  193. var e_3, _a;
  194. if (!isType(node, 'mo')) {
  195. return null;
  196. }
  197. var mo = node;
  198. var forms = mo.getForms();
  199. try {
  200. for (var forms_1 = __values(forms), forms_1_1 = forms_1.next(); !forms_1_1.done; forms_1_1 = forms_1.next()) {
  201. var form = forms_1_1.value;
  202. var symbol = mo_js_1.MmlMo.OPTABLE[form][mo.getText()];
  203. if (symbol) {
  204. return symbol;
  205. }
  206. }
  207. }
  208. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  209. finally {
  210. try {
  211. if (forms_1_1 && !forms_1_1.done && (_a = forms_1.return)) _a.call(forms_1);
  212. }
  213. finally { if (e_3) throw e_3.error; }
  214. }
  215. return null;
  216. }
  217. NodeUtil.getForm = getForm;
  218. })(NodeUtil || (NodeUtil = {}));
  219. exports.default = NodeUtil;
  220. //# sourceMappingURL=NodeUtil.js.map