semantic_skeleton.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import * as BaseUtil from '../common/base_util.js';
  2. import { Engine } from '../common/engine.js';
  3. import * as XpathUtil from '../common/xpath_util.js';
  4. import { Attribute as EnrichAttribute } from '../enrich_mathml/enrich_attr.js';
  5. import { SemanticType } from './semantic_meaning.js';
  6. const Options = {
  7. tree: false
  8. };
  9. export class SemanticSkeleton {
  10. static fromTree(tree) {
  11. return SemanticSkeleton.fromNode(tree.root);
  12. }
  13. static fromNode(node) {
  14. return new SemanticSkeleton(SemanticSkeleton.fromNode_(node));
  15. }
  16. static fromString(skel) {
  17. return new SemanticSkeleton(SemanticSkeleton.fromString_(skel));
  18. }
  19. static simpleCollapseStructure(strct) {
  20. return typeof strct === 'number';
  21. }
  22. static contentCollapseStructure(strct) {
  23. return (!!strct &&
  24. !SemanticSkeleton.simpleCollapseStructure(strct) &&
  25. strct[0] === 'c');
  26. }
  27. static interleaveIds(first, second) {
  28. return BaseUtil.interleaveLists(SemanticSkeleton.collapsedLeafs(first), SemanticSkeleton.collapsedLeafs(second));
  29. }
  30. static collapsedLeafs(...args) {
  31. const collapseStructure = (coll) => {
  32. if (SemanticSkeleton.simpleCollapseStructure(coll)) {
  33. return [coll];
  34. }
  35. coll = coll;
  36. return SemanticSkeleton.contentCollapseStructure(coll[1])
  37. ? coll.slice(2)
  38. : coll.slice(1);
  39. };
  40. return args.reduce((x, y) => x.concat(collapseStructure(y)), []);
  41. }
  42. static fromStructure(mml, tree) {
  43. return new SemanticSkeleton(SemanticSkeleton.tree_(mml, tree.root));
  44. }
  45. static combineContentChildren(type, _role, content, children) {
  46. switch (type) {
  47. case SemanticType.RELSEQ:
  48. case SemanticType.INFIXOP:
  49. case SemanticType.MULTIREL:
  50. return BaseUtil.interleaveLists(children, content);
  51. case SemanticType.PREFIXOP:
  52. return content.concat(children);
  53. case SemanticType.POSTFIXOP:
  54. return children.concat(content);
  55. case SemanticType.MATRIX:
  56. case SemanticType.VECTOR:
  57. case SemanticType.FENCED:
  58. children.unshift(content[0]);
  59. children.push(content[1]);
  60. return children;
  61. case SemanticType.CASES:
  62. children.unshift(content[0]);
  63. return children;
  64. case SemanticType.APPL:
  65. return [children[0], content[0], children[1]];
  66. case SemanticType.ROOT:
  67. return [children[0], children[1]];
  68. case SemanticType.ROW:
  69. case SemanticType.LINE:
  70. if (content.length) {
  71. children.unshift(content[0]);
  72. }
  73. return children;
  74. default:
  75. return children;
  76. }
  77. }
  78. static makeSexp_(struct) {
  79. if (SemanticSkeleton.simpleCollapseStructure(struct)) {
  80. return struct.toString();
  81. }
  82. if (SemanticSkeleton.contentCollapseStructure(struct)) {
  83. return ('(' +
  84. 'c ' +
  85. struct.slice(1).map(SemanticSkeleton.makeSexp_).join(' ') +
  86. ')');
  87. }
  88. return ('(' + struct.map(SemanticSkeleton.makeSexp_).join(' ') + ')');
  89. }
  90. static fromString_(skeleton) {
  91. let str = skeleton.replace(/\(/g, '[');
  92. str = str.replace(/\)/g, ']');
  93. str = str.replace(/ /g, ',');
  94. str = str.replace(/c/g, '"c"');
  95. return JSON.parse(str);
  96. }
  97. static fromNode_(node) {
  98. if (!node) {
  99. return [];
  100. }
  101. const content = node.contentNodes;
  102. let contentStructure;
  103. if (content.length) {
  104. contentStructure = content.map(SemanticSkeleton.fromNode_);
  105. contentStructure.unshift('c');
  106. }
  107. const children = node.childNodes;
  108. if (!children.length) {
  109. return content.length ? [node.id, contentStructure] : node.id;
  110. }
  111. const structure = children.map(SemanticSkeleton.fromNode_);
  112. if (content.length) {
  113. structure.unshift(contentStructure);
  114. }
  115. structure.unshift(node.id);
  116. return structure;
  117. }
  118. static tree_(mml, node, level = 0, posinset = 1, setsize = 1) {
  119. if (!node) {
  120. return [];
  121. }
  122. const id = node.id;
  123. const skeleton = [id];
  124. XpathUtil.updateEvaluator(mml);
  125. const mmlChild = XpathUtil.evalXPath(`.//self::*[@${EnrichAttribute.ID}=${id}]`, mml)[0];
  126. if (!node.childNodes.length) {
  127. SemanticSkeleton.addAria(mmlChild, level, posinset, setsize);
  128. return node.id;
  129. }
  130. const children = SemanticSkeleton.combineContentChildren(node.type, node.role, node.contentNodes.map(function (x) {
  131. return x;
  132. }), node.childNodes.map(function (x) {
  133. return x;
  134. }));
  135. if (mmlChild) {
  136. SemanticSkeleton.addOwns_(mmlChild, children);
  137. }
  138. for (let i = 0, l = children.length, child; (child = children[i]); i++) {
  139. skeleton.push(SemanticSkeleton.tree_(mml, child, level + 1, i + 1, l));
  140. }
  141. SemanticSkeleton.addAria(mmlChild, level, posinset, setsize, !Options.tree ? 'treeitem' : level ? 'group' : 'tree');
  142. return skeleton;
  143. }
  144. static addAria(node, level, posinset, setsize, role = !Options.tree ? 'treeitem' : level ? 'treeitem' : 'tree') {
  145. if (!Engine.getInstance().aria || !node) {
  146. return;
  147. }
  148. node.setAttribute('aria-level', level.toString());
  149. node.setAttribute('aria-posinset', posinset.toString());
  150. node.setAttribute('aria-setsize', setsize.toString());
  151. node.setAttribute('role', role);
  152. if (node.hasAttribute(EnrichAttribute.OWNS)) {
  153. node.setAttribute('aria-owns', node.getAttribute(EnrichAttribute.OWNS));
  154. }
  155. }
  156. static addOwns_(node, children) {
  157. const collapsed = node.getAttribute(EnrichAttribute.COLLAPSED);
  158. const leafs = collapsed
  159. ? SemanticSkeleton.realLeafs_(SemanticSkeleton.fromString(collapsed).array)
  160. : children.map((x) => x.id);
  161. node.setAttribute(EnrichAttribute.OWNS, leafs.join(' '));
  162. }
  163. static realLeafs_(sexp) {
  164. if (SemanticSkeleton.simpleCollapseStructure(sexp)) {
  165. return [sexp];
  166. }
  167. if (SemanticSkeleton.contentCollapseStructure(sexp)) {
  168. return [];
  169. }
  170. sexp = sexp;
  171. let result = [];
  172. for (let i = 1; i < sexp.length; i++) {
  173. result = result.concat(SemanticSkeleton.realLeafs_(sexp[i]));
  174. }
  175. return result;
  176. }
  177. constructor(skeleton) {
  178. this.parents = null;
  179. this.levelsMap = null;
  180. skeleton = skeleton === 0 ? skeleton : skeleton || [];
  181. this.array = skeleton;
  182. }
  183. populate() {
  184. if (this.parents && this.levelsMap) {
  185. return;
  186. }
  187. this.parents = {};
  188. this.levelsMap = {};
  189. this.populate_(this.array, this.array, []);
  190. }
  191. toString() {
  192. return SemanticSkeleton.makeSexp_(this.array);
  193. }
  194. populate_(element, layer, parents) {
  195. if (SemanticSkeleton.simpleCollapseStructure(element)) {
  196. element = element;
  197. this.levelsMap[element] = layer;
  198. this.parents[element] =
  199. element === parents[0] ? parents.slice(1) : parents;
  200. return;
  201. }
  202. const newElement = SemanticSkeleton.contentCollapseStructure(element)
  203. ? element.slice(1)
  204. : element;
  205. const newParents = [newElement[0]].concat(parents);
  206. for (let i = 0, l = newElement.length; i < l; i++) {
  207. const current = newElement[i];
  208. this.populate_(current, element, newParents);
  209. }
  210. }
  211. isRoot(id) {
  212. const level = this.levelsMap[id];
  213. return id === level[0];
  214. }
  215. directChildren(id) {
  216. if (!this.isRoot(id)) {
  217. return [];
  218. }
  219. const level = this.levelsMap[id];
  220. return level.slice(1).map((child) => {
  221. if (SemanticSkeleton.simpleCollapseStructure(child)) {
  222. return child;
  223. }
  224. if (SemanticSkeleton.contentCollapseStructure(child)) {
  225. return child[1];
  226. }
  227. return child[0];
  228. });
  229. }
  230. subtreeNodes(id) {
  231. if (!this.isRoot(id)) {
  232. return [];
  233. }
  234. const subtreeNodes_ = (tree, nodes) => {
  235. if (SemanticSkeleton.simpleCollapseStructure(tree)) {
  236. nodes.push(tree);
  237. return;
  238. }
  239. tree = tree;
  240. if (SemanticSkeleton.contentCollapseStructure(tree)) {
  241. tree = tree.slice(1);
  242. }
  243. tree.forEach((x) => subtreeNodes_(x, nodes));
  244. };
  245. const level = this.levelsMap[id];
  246. const subtree = [];
  247. subtreeNodes_(level.slice(1), subtree);
  248. return subtree;
  249. }
  250. }