StyleList.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 CssStyles class for handling stylesheets
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. /**
  23. * The data for a selector
  24. */
  25. export type StyleData = {
  26. [property: string]: string | number;
  27. };
  28. /**
  29. * A list of selectors and their data (basically a stylesheet)
  30. */
  31. export type StyleList = {
  32. [selector: string]: StyleData;
  33. };
  34. /******************************************************************************/
  35. /**
  36. * The CssStyles class (for managing a collection of CSS style definitions)
  37. */
  38. export class CssStyles {
  39. /**
  40. * The styles as they currently stand
  41. */
  42. protected styles: StyleList = {};
  43. /**
  44. * @return {string} The styles as a CSS string
  45. */
  46. get cssText(): string {
  47. return this.getStyleString();
  48. }
  49. /**
  50. * @param {StyleList} styles The initial styles to use, if any
  51. * @constructor
  52. */
  53. constructor(styles: StyleList = null) {
  54. this.addStyles(styles);
  55. }
  56. /**
  57. * @param {StyleList} styles The styles to combine with the existing ones
  58. */
  59. public addStyles(styles: StyleList) {
  60. if (!styles) return;
  61. for (const style of Object.keys(styles)) {
  62. if (!this.styles[style]) {
  63. this.styles[style] = {};
  64. }
  65. Object.assign(this.styles[style], styles[style]);
  66. }
  67. }
  68. /**
  69. * @param {string[]} selectors The selectors for the styles to remove
  70. */
  71. public removeStyles(...selectors: string[]) {
  72. for (const selector of selectors) {
  73. delete this.styles[selector];
  74. }
  75. }
  76. /**
  77. * Clear all the styles
  78. */
  79. public clear() {
  80. this.styles = {};
  81. }
  82. /**
  83. * @return {string} The CSS string for the style list
  84. */
  85. public getStyleString(): string {
  86. return this.getStyleRules().join('\n\n');
  87. }
  88. /**
  89. * @return {string[]} An array of rule strings for the style list
  90. */
  91. public getStyleRules(): string[] {
  92. const selectors = Object.keys(this.styles);
  93. const defs: string[] = new Array(selectors.length);
  94. let i = 0;
  95. for (const selector of selectors) {
  96. defs[i++] = selector + ' {\n' + this.getStyleDefString(this.styles[selector]) + '\n}';
  97. }
  98. return defs;
  99. }
  100. /**
  101. * @param {StyleData} styles The style data to be stringified
  102. * @return {string} The CSS string for the given data
  103. */
  104. public getStyleDefString(styles: StyleData): string {
  105. const properties = Object.keys(styles);
  106. const values: string[] = new Array(properties.length);
  107. let i = 0;
  108. for (const property of properties) {
  109. values[i++] = ' ' + property + ': ' + styles[property] + ';';
  110. }
  111. return values.join('\n');
  112. }
  113. }