ms.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 CommonMs wrapper mixin for the MmlMs object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. /*****************************************************************/
  24. /**
  25. * The CommonMs interface
  26. */
  27. export interface CommonMs extends AnyWrapper {
  28. /**
  29. * Create a text wrapper with the given text;
  30. *
  31. * @param {string} text The text for the wrapped element
  32. * @return {CommonWrapper} The wrapped text node
  33. */
  34. createText(text: string): AnyWrapper;
  35. }
  36. /**
  37. * Shorthand for the CommonMs constructor
  38. */
  39. export type MsConstructor = Constructor<CommonMs>;
  40. /*****************************************************************/
  41. /**
  42. * The CommonMs wrapper mixin for the MmlMs object
  43. *
  44. * @template T The Wrapper class constructor type
  45. */
  46. export function CommonMsMixin<T extends WrapperConstructor>(Base: T): MsConstructor & T {
  47. return class extends Base {
  48. /**
  49. * Add the quote characters to the wrapper children so they will be output
  50. *
  51. * @override
  52. */
  53. constructor(...args: any[]) {
  54. super(...args);
  55. const attributes = this.node.attributes;
  56. let quotes = attributes.getList('lquote', 'rquote');
  57. if (this.variant !== 'monospace') {
  58. if (!attributes.isSet('lquote') && quotes.lquote === '"') quotes.lquote = '\u201C';
  59. if (!attributes.isSet('rquote') && quotes.rquote === '"') quotes.rquote = '\u201D';
  60. }
  61. this.childNodes.unshift(this.createText(quotes.lquote as string));
  62. this.childNodes.push(this.createText(quotes.rquote as string));
  63. }
  64. /**
  65. * Create a text wrapper with the given text;
  66. *
  67. * @param {string} text The text for the wrapped element
  68. * @return {AnyWrapper} The wrapped text node
  69. */
  70. public createText(text: string): AnyWrapper {
  71. const node = this.wrap(this.mmlText(text));
  72. node.parent = this;
  73. return node;
  74. }
  75. };
  76. }