HtmlMethods.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-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 Methods for the Html package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import TexParser from '../TexParser.js';
  23. import {ParseMethod} from '../Types.js';
  24. import NodeUtil from '../NodeUtil.js';
  25. import {MmlNode} from '../../../core/MmlTree/MmlNode.js';
  26. // Namespace
  27. let HtmlMethods: Record<string, ParseMethod> = {};
  28. /**
  29. * Implements \href{url}{math}
  30. * @param {TexParser} parser The calling parser.
  31. * @param {string} name The macro name.
  32. */
  33. HtmlMethods.Href = function(parser: TexParser, name: string) {
  34. const url = parser.GetArgument(name);
  35. const arg = GetArgumentMML(parser, name);
  36. NodeUtil.setAttribute(arg, 'href', url);
  37. parser.Push(arg);
  38. };
  39. /**
  40. * Implements \class{name}{math}
  41. * @param {TexParser} parser The calling parser.
  42. * @param {string} name The macro name.
  43. */
  44. HtmlMethods.Class = function(parser: TexParser, name: string) {
  45. let CLASS = parser.GetArgument(name);
  46. const arg = GetArgumentMML(parser, name);
  47. let oldClass = NodeUtil.getAttribute(arg, 'class');
  48. if (oldClass) {
  49. CLASS = oldClass + ' ' + CLASS;
  50. }
  51. NodeUtil.setAttribute(arg, 'class', CLASS);
  52. parser.Push(arg);
  53. };
  54. /**
  55. * Implements \style{style-string}{math}
  56. * @param {TexParser} parser The calling parser.
  57. * @param {string} name The macro name.
  58. */
  59. HtmlMethods.Style = function(parser: TexParser, name: string) {
  60. let style = parser.GetArgument(name);
  61. const arg = GetArgumentMML(parser, name);
  62. // check that it looks like a style string
  63. let oldStyle = NodeUtil.getAttribute(arg, 'style');
  64. if (oldStyle) {
  65. if (style.charAt(style.length - 1) !== ';') {
  66. style += ';';
  67. }
  68. style = oldStyle + ' ' + style;
  69. }
  70. NodeUtil.setAttribute(arg, 'style', style);
  71. parser.Push(arg);
  72. };
  73. /**
  74. * Implements \cssId{id}{math}
  75. * @param {TexParser} parser The calling parser.
  76. * @param {string} name The macro name.
  77. */
  78. HtmlMethods.Id = function(parser: TexParser, name: string) {
  79. const ID = parser.GetArgument(name);
  80. const arg = GetArgumentMML(parser, name);
  81. NodeUtil.setAttribute(arg, 'id', ID);
  82. parser.Push(arg);
  83. };
  84. /**
  85. * Parses the math argument of the above commands and returns it as single
  86. * node (in an mrow if necessary). The HTML attributes are then
  87. * attached to this element.
  88. * @param {TexParser} parser The calling parser.
  89. * @param {string} name The calling macro name.
  90. * @return {MmlNode} The math node.
  91. */
  92. let GetArgumentMML = function(parser: TexParser, name: string): MmlNode {
  93. let arg = parser.ParseArg(name);
  94. if (!NodeUtil.isInferred(arg)) {
  95. return arg;
  96. }
  97. let children = NodeUtil.getChildren(arg);
  98. if (children.length === 1) {
  99. return children[0];
  100. }
  101. const mrow = parser.create('node', 'mrow');
  102. NodeUtil.copyChildren(arg, mrow);
  103. NodeUtil.copyAttributes(arg, mrow);
  104. return mrow;
  105. };
  106. export default HtmlMethods;