unit_util.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.unitMultipliers = unitMultipliers;
  4. exports.oneLeft = oneLeft;
  5. const auditory_description_js_1 = require("../audio/auditory_description.js");
  6. const XpathUtil = require("../common/xpath_util.js");
  7. const locale_js_1 = require("../l10n/locale.js");
  8. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  9. function unitMultipliers(nodes, _context) {
  10. const children = nodes;
  11. let counter = 0;
  12. return function () {
  13. const descr = auditory_description_js_1.AuditoryDescription.create({
  14. text: rightMostUnit(children[counter]) &&
  15. leftMostUnit(children[counter + 1])
  16. ? locale_js_1.LOCALE.MESSAGES.unitTimes
  17. : ''
  18. }, {});
  19. counter++;
  20. return [descr];
  21. };
  22. }
  23. const SCRIPT_ELEMENTS = [
  24. semantic_meaning_js_1.SemanticType.SUPERSCRIPT,
  25. semantic_meaning_js_1.SemanticType.SUBSCRIPT,
  26. semantic_meaning_js_1.SemanticType.OVERSCORE,
  27. semantic_meaning_js_1.SemanticType.UNDERSCORE
  28. ];
  29. function rightMostUnit(node) {
  30. while (node) {
  31. if (node.getAttribute('role') === 'unit') {
  32. return true;
  33. }
  34. const tag = node.tagName;
  35. const children = XpathUtil.evalXPath('children/*', node);
  36. node = (SCRIPT_ELEMENTS.indexOf(tag) !== -1
  37. ? children[0]
  38. : children[children.length - 1]);
  39. }
  40. return false;
  41. }
  42. function leftMostUnit(node) {
  43. while (node) {
  44. if (node.getAttribute('role') === 'unit') {
  45. return true;
  46. }
  47. const children = XpathUtil.evalXPath('children/*', node);
  48. node = children[0];
  49. }
  50. return false;
  51. }
  52. function oneLeft(node) {
  53. while (node) {
  54. if (node.tagName === 'number' && node.textContent === '1') {
  55. return [node];
  56. }
  57. if (node.tagName !== 'infixop' ||
  58. (node.getAttribute('role') !== 'multiplication' &&
  59. node.getAttribute('role') !== 'implicit')) {
  60. return [];
  61. }
  62. node = XpathUtil.evalXPath('children/*', node)[0];
  63. }
  64. return [];
  65. }