enrich_attr.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EnrichAttributes = exports.Attribute = void 0;
  4. exports.makeIdList = makeIdList;
  5. exports.setAttributes = setAttributes;
  6. exports.removeAttributePrefix = removeAttributePrefix;
  7. exports.addPrefix = addPrefix;
  8. exports.addMrow = addMrow;
  9. const DomUtil = require("../common/dom_util.js");
  10. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  11. const Prefix = 'data-semantic-';
  12. var Attribute;
  13. (function (Attribute) {
  14. Attribute["ADDED"] = "data-semantic-added";
  15. Attribute["ALTERNATIVE"] = "data-semantic-alternative";
  16. Attribute["CHILDREN"] = "data-semantic-children";
  17. Attribute["COLLAPSED"] = "data-semantic-collapsed";
  18. Attribute["CONTENT"] = "data-semantic-content";
  19. Attribute["EMBELLISHED"] = "data-semantic-embellished";
  20. Attribute["FENCEPOINTER"] = "data-semantic-fencepointer";
  21. Attribute["FONT"] = "data-semantic-font";
  22. Attribute["ID"] = "data-semantic-id";
  23. Attribute["ANNOTATION"] = "data-semantic-annotation";
  24. Attribute["ATTRIBUTES"] = "data-semantic-attributes";
  25. Attribute["OPERATOR"] = "data-semantic-operator";
  26. Attribute["OWNS"] = "data-semantic-owns";
  27. Attribute["PARENT"] = "data-semantic-parent";
  28. Attribute["POSTFIX"] = "data-semantic-postfix";
  29. Attribute["PREFIX"] = "data-semantic-prefix";
  30. Attribute["ROLE"] = "data-semantic-role";
  31. Attribute["SPEECH"] = "data-semantic-speech";
  32. Attribute["STRUCTURE"] = "data-semantic-structure";
  33. Attribute["SUMMARY"] = "data-semantic-summary";
  34. Attribute["TYPE"] = "data-semantic-type";
  35. })(Attribute || (exports.Attribute = Attribute = {}));
  36. exports.EnrichAttributes = [
  37. Attribute.ADDED,
  38. Attribute.ALTERNATIVE,
  39. Attribute.CHILDREN,
  40. Attribute.COLLAPSED,
  41. Attribute.CONTENT,
  42. Attribute.EMBELLISHED,
  43. Attribute.FENCEPOINTER,
  44. Attribute.FONT,
  45. Attribute.ID,
  46. Attribute.ANNOTATION,
  47. Attribute.ATTRIBUTES,
  48. Attribute.OPERATOR,
  49. Attribute.OWNS,
  50. Attribute.PARENT,
  51. Attribute.POSTFIX,
  52. Attribute.PREFIX,
  53. Attribute.ROLE,
  54. Attribute.SPEECH,
  55. Attribute.STRUCTURE,
  56. Attribute.SUMMARY,
  57. Attribute.TYPE
  58. ];
  59. function makeIdList(nodes) {
  60. return nodes
  61. .map(function (node) {
  62. return node.id;
  63. })
  64. .join(',');
  65. }
  66. function setAttributes(mml, semantic) {
  67. mml.setAttribute(Attribute.TYPE, semantic.type);
  68. const attributes = semantic.allAttributes();
  69. for (let i = 0, attr; (attr = attributes[i]); i++) {
  70. mml.setAttribute(Prefix + attr[0].toLowerCase(), attr[1]);
  71. }
  72. if (semantic.childNodes.length) {
  73. mml.setAttribute(Attribute.CHILDREN, makeIdList(semantic.childNodes));
  74. }
  75. if (semantic.contentNodes.length) {
  76. mml.setAttribute(Attribute.CONTENT, makeIdList(semantic.contentNodes));
  77. }
  78. if (semantic.parent) {
  79. mml.setAttribute(Attribute.PARENT, semantic.parent.id.toString());
  80. }
  81. const external = semantic.attributesXml();
  82. if (external) {
  83. mml.setAttribute(Attribute.ATTRIBUTES, external);
  84. }
  85. setPostfix(mml, semantic);
  86. }
  87. function setPostfix(mml, semantic) {
  88. const postfix = [];
  89. if (semantic.role === semantic_meaning_js_1.SemanticRole.MGLYPH) {
  90. postfix.push('image');
  91. }
  92. if (semantic.attributes['href']) {
  93. postfix.push('link');
  94. }
  95. if (postfix.length) {
  96. mml.setAttribute(Attribute.POSTFIX, postfix.join(' '));
  97. }
  98. }
  99. function removeAttributePrefix(mml) {
  100. return mml.toString().replace(new RegExp(Prefix, 'g'), '');
  101. }
  102. function addPrefix(attr) {
  103. return (Prefix + attr);
  104. }
  105. function addMrow() {
  106. const mrow = DomUtil.createElement('mrow');
  107. mrow.setAttribute(Attribute.ADDED, 'true');
  108. return mrow;
  109. }