FindMathML.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the MathML version of the FindMath object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AbstractFindMath} from '../../core/FindMath.js';
  23. import {DOMAdaptor} from '../../core/DOMAdaptor.js';
  24. import {OptionList} from '../../util/Options.js';
  25. import {ProtoItem} from '../../core/MathItem.js';
  26. /**
  27. * The MathML namespace
  28. */
  29. const NAMESPACE = 'http://www.w3.org/1998/Math/MathML';
  30. /*****************************************************************/
  31. /**
  32. * Implements the FindMathML object (extends AbstractFindMath)
  33. *
  34. * @template N The HTMLElement node class
  35. * @template T The Text node class
  36. * @template D The Document class
  37. */
  38. export class FindMathML<N, T, D> extends AbstractFindMath<N, T, D> {
  39. /**
  40. * @override
  41. */
  42. public static OPTIONS: OptionList = {};
  43. /**
  44. * The DOMAdaptor for the document being processed
  45. */
  46. public adaptor: DOMAdaptor<N, T, D>;
  47. /**
  48. * Locates math nodes, possibly with namespace prefixes.
  49. * Store them in a set so that if found more than once, they will only
  50. * appear in the list once.
  51. *
  52. * @override
  53. */
  54. public findMath(node: N) {
  55. let set = new Set<N>();
  56. this.findMathNodes(node, set);
  57. this.findMathPrefixed(node, set);
  58. const html = this.adaptor.root(this.adaptor.document);
  59. if (this.adaptor.kind(html) === 'html' && set.size === 0) {
  60. this.findMathNS(node, set);
  61. }
  62. return this.processMath(set);
  63. }
  64. /**
  65. * Find plain <math> tags
  66. *
  67. * @param {N} node The container to seaerch for math
  68. * @param {Set<N>} set The set in which to store the math nodes
  69. */
  70. protected findMathNodes(node: N, set: Set<N>) {
  71. for (const math of this.adaptor.tags(node, 'math')) {
  72. set.add(math);
  73. }
  74. }
  75. /**
  76. * Find <m:math> tags (or whatever prefixes there are)
  77. *
  78. * @param {N} node The container to seaerch for math
  79. * @param {NodeSet} set The set in which to store the math nodes
  80. */
  81. protected findMathPrefixed(node: N, set: Set<N>) {
  82. let html = this.adaptor.root(this.adaptor.document);
  83. for (const attr of this.adaptor.allAttributes(html)) {
  84. if (attr.name.substr(0, 6) === 'xmlns:' && attr.value === NAMESPACE) {
  85. let prefix = attr.name.substr(6);
  86. for (const math of this.adaptor.tags(node, prefix + ':math')) {
  87. set.add(math);
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * Find namespaced math in XHTML documents (is this really needed?)
  94. *
  95. * @param {N} node The container to seaerch for math
  96. * @param {NodeSet} set The set in which to store the math nodes
  97. */
  98. protected findMathNS(node: N, set: Set<N>) {
  99. for (const math of this.adaptor.tags(node, 'math', NAMESPACE)) {
  100. set.add(math);
  101. }
  102. }
  103. /**
  104. * Produce the array of proto math items from the node set
  105. */
  106. protected processMath(set: Set<N>) {
  107. let math: ProtoItem<N, T>[] = [];
  108. for (const mml of Array.from(set)) {
  109. let display = (this.adaptor.getAttribute(mml, 'display') === 'block' ||
  110. this.adaptor.getAttribute(mml, 'mode') === 'display');
  111. let start = {node: mml, n: 0, delim: ''};
  112. let end = {node: mml, n: 0, delim: ''};
  113. math.push({math: this.adaptor.outerHTML(mml), start, end, display});
  114. }
  115. return math;
  116. }
  117. }