mpadded.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 CHTMLmpadded wrapper for the MmlMpadded object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {CHTMLWrapper, CHTMLConstructor, StringMap} from '../Wrapper.js';
  23. import {CommonMpaddedMixin} from '../../common/Wrappers/mpadded.js';
  24. import {MmlMpadded} from '../../../core/MmlTree/MmlNodes/mpadded.js';
  25. import {StyleList} from '../../../util/StyleList.js';
  26. /*****************************************************************/
  27. /**
  28. * The CHTMLmpadded wrapper for the MmlMpadded object
  29. *
  30. * @template N The HTMLElement node class
  31. * @template T The Text node class
  32. * @template D The Document class
  33. */
  34. // @ts-ignore
  35. export class CHTMLmpadded<N, T, D> extends
  36. CommonMpaddedMixin<CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
  37. /**
  38. * The mpadded wrapper
  39. */
  40. public static kind = MmlMpadded.prototype.kind;
  41. /**
  42. * @override
  43. */
  44. public static styles: StyleList = {
  45. 'mjx-mpadded': {
  46. display: 'inline-block'
  47. },
  48. 'mjx-rbox': {
  49. display: 'inline-block',
  50. position: 'relative'
  51. }
  52. };
  53. /**
  54. * @override
  55. */
  56. public toCHTML(parent: N) {
  57. let chtml = this.standardCHTMLnode(parent);
  58. const content: N[] = [];
  59. const style: StringMap = {};
  60. const [ , , W, dh, dd, dw, x, y, dx] = this.getDimens();
  61. //
  62. // If the width changed, set the width explicitly
  63. //
  64. if (dw) {
  65. style.width = this.em(W + dw);
  66. }
  67. //
  68. // If the height or depth changed, use margin to make the change
  69. //
  70. if (dh || dd) {
  71. style.margin = this.em(dh) + ' 0 ' + this.em(dd);
  72. }
  73. //
  74. // If there is a horizontal or vertical shift,
  75. // use relative positioning to move the contents
  76. //
  77. if (x + dx || y) {
  78. style.position = 'relative';
  79. const rbox = this.html('mjx-rbox', {
  80. style: {left: this.em(x + dx), top: this.em(-y), 'max-width': style.width}
  81. });
  82. if (x + dx && this.childNodes[0].getBBox().pwidth) {
  83. this.adaptor.setAttribute(rbox, 'width', 'full');
  84. this.adaptor.setStyle(rbox, 'left', this.em(x));
  85. }
  86. content.push(rbox);
  87. }
  88. //
  89. // Create the HTML with the proper styles and content
  90. //
  91. chtml = this.adaptor.append(chtml, this.html('mjx-block', {style: style}, content)) as N;
  92. for (const child of this.childNodes) {
  93. child.toCHTML(content[0] || chtml);
  94. }
  95. }
  96. }