enrich_attr.js 3.3 KB

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