HTMLMathItem.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 HTMLMathItem class
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AbstractMathItem, Location, STATE} from '../../core/MathItem.js';
  23. import {InputJax} from '../../core/InputJax.js';
  24. import {HTMLDocument} from './HTMLDocument.js';
  25. /*****************************************************************/
  26. /**
  27. * Implements the HTMLMathItem class (extends AbstractMathItem)
  28. *
  29. * @template N The HTMLElement node class
  30. * @template T The Text node class
  31. * @template D The Document class
  32. */
  33. export class HTMLMathItem<N, T, D> extends AbstractMathItem<N, T, D> {
  34. /**
  35. * Easy access to DOM adaptor
  36. */
  37. get adaptor() {
  38. return this.inputJax.adaptor;
  39. }
  40. /**
  41. * @override
  42. */
  43. constructor(math: string, jax: InputJax<N, T, D>, display: boolean = true,
  44. start: Location<N, T> = {node: null, n: 0, delim: ''},
  45. end: Location<N, T> = {node: null, n: 0, delim: ''}) {
  46. super(math, jax, display, start, end);
  47. }
  48. /**
  49. * Insert the typeset MathItem into the document at the right location
  50. * If the starting and ending nodes are the same:
  51. * Split the text to isolate the math and its delimiters
  52. * Replace the math by the typeset version
  53. * Otherewise (spread over several nodes)
  54. * Split the start node, if needed
  55. * Remove nodes until we reach the end node
  56. * Insert the math before the end node
  57. * Split the end node, if needed
  58. * Remove the end node
  59. *
  60. * @override
  61. */
  62. public updateDocument(_html: HTMLDocument<N, T, D>) {
  63. if (this.state() < STATE.INSERTED) {
  64. if (this.inputJax.processStrings) {
  65. let node = this.start.node as T;
  66. if (node === this.end.node) {
  67. if (this.end.n && this.end.n < this.adaptor.value(this.end.node).length) {
  68. this.adaptor.split(this.end.node, this.end.n);
  69. }
  70. if (this.start.n) {
  71. node = this.adaptor.split(this.start.node as T, this.start.n);
  72. }
  73. this.adaptor.replace(this.typesetRoot, node);
  74. } else {
  75. if (this.start.n) {
  76. node = this.adaptor.split(node, this.start.n);
  77. }
  78. while (node !== this.end.node) {
  79. let next = this.adaptor.next(node) as T;
  80. this.adaptor.remove(node);
  81. node = next;
  82. }
  83. this.adaptor.insert(this.typesetRoot, node);
  84. if (this.end.n < this.adaptor.value(node).length) {
  85. this.adaptor.split(node, this.end.n);
  86. }
  87. this.adaptor.remove(node);
  88. }
  89. } else {
  90. this.adaptor.replace(this.typesetRoot, this.start.node);
  91. }
  92. this.start.node = this.end.node = this.typesetRoot;
  93. this.start.n = this.end.n = 0;
  94. this.state(STATE.INSERTED);
  95. }
  96. }
  97. /**
  98. * Update the style sheet for any changes due to rerendering
  99. *
  100. * @param {HTMLDocument} document The document whose styles are to be updated
  101. */
  102. public updateStyleSheet(document: HTMLDocument<N, T, D>) {
  103. document.addStyleSheet();
  104. }
  105. /**
  106. * Remove the typeset math from the document, and put back the original
  107. * expression and its delimiters, if requested.
  108. *
  109. * @override
  110. */
  111. public removeFromDocument(restore: boolean = false) {
  112. if (this.state() >= STATE.TYPESET) {
  113. const adaptor = this.adaptor;
  114. let node = this.start.node;
  115. let math: N | T = adaptor.text('');
  116. if (restore) {
  117. let text = this.start.delim + this.math + this.end.delim;
  118. if (this.inputJax.processStrings) {
  119. math = adaptor.text(text);
  120. } else {
  121. const doc = adaptor.parse(text, 'text/html');
  122. math = adaptor.firstChild(adaptor.body(doc));
  123. }
  124. }
  125. if (adaptor.parent(node)) {
  126. adaptor.replace(math, node);
  127. }
  128. this.start.node = this.end.node = math;
  129. this.start.n = this.end.n = 0;
  130. }
  131. }
  132. }