Options.js (4930B)
1 /** 2 * This file contains information about the options that the Parser carries 3 * around with it while parsing. Data is held in an `Options` object, and when 4 * recursing, a new `Options` object can be created with the `.with*` and 5 * `.reset` functions. 6 */ 7 8 /** 9 * This is the main options class. It contains the style, size, color, and font 10 * of the current parse level. It also contains the style and size of the parent 11 * parse level, so size changes can be handled efficiently. 12 * 13 * Each of the `.with*` and `.reset` functions passes its current style and size 14 * as the parentStyle and parentSize of the new options class, so parent 15 * handling is taken care of automatically. 16 */ 17 function Options(data) { 18 this.style = data.style; 19 this.color = data.color; 20 this.size = data.size; 21 this.phantom = data.phantom; 22 this.font = data.font; 23 24 if (data.parentStyle === undefined) { 25 this.parentStyle = data.style; 26 } else { 27 this.parentStyle = data.parentStyle; 28 } 29 30 if (data.parentSize === undefined) { 31 this.parentSize = data.size; 32 } else { 33 this.parentSize = data.parentSize; 34 } 35 } 36 37 /** 38 * Returns a new options object with the same properties as "this". Properties 39 * from "extension" will be copied to the new options object. 40 */ 41 Options.prototype.extend = function(extension) { 42 const data = { 43 style: this.style, 44 size: this.size, 45 color: this.color, 46 parentStyle: this.style, 47 parentSize: this.size, 48 phantom: this.phantom, 49 font: this.font, 50 }; 51 52 for (const key in extension) { 53 if (extension.hasOwnProperty(key)) { 54 data[key] = extension[key]; 55 } 56 } 57 58 return new Options(data); 59 }; 60 61 /** 62 * Create a new options object with the given style. 63 */ 64 Options.prototype.withStyle = function(style) { 65 return this.extend({ 66 style: style, 67 }); 68 }; 69 70 /** 71 * Create a new options object with the given size. 72 */ 73 Options.prototype.withSize = function(size) { 74 return this.extend({ 75 size: size, 76 }); 77 }; 78 79 /** 80 * Create a new options object with the given color. 81 */ 82 Options.prototype.withColor = function(color) { 83 return this.extend({ 84 color: color, 85 }); 86 }; 87 88 /** 89 * Create a new options object with "phantom" set to true. 90 */ 91 Options.prototype.withPhantom = function() { 92 return this.extend({ 93 phantom: true, 94 }); 95 }; 96 97 /** 98 * Create a new options objects with the give font. 99 */ 100 Options.prototype.withFont = function(font) { 101 return this.extend({ 102 font: font || this.font, 103 }); 104 }; 105 106 /** 107 * Create a new options object with the same style, size, and color. This is 108 * used so that parent style and size changes are handled correctly. 109 */ 110 Options.prototype.reset = function() { 111 return this.extend({}); 112 }; 113 114 /** 115 * A map of color names to CSS colors. 116 * TODO(emily): Remove this when we have real macros 117 */ 118 const colorMap = { 119 "katex-blue": "#6495ed", 120 "katex-orange": "#ffa500", 121 "katex-pink": "#ff00af", 122 "katex-red": "#df0030", 123 "katex-green": "#28ae7b", 124 "katex-gray": "gray", 125 "katex-purple": "#9d38bd", 126 "katex-blueA": "#ccfaff", 127 "katex-blueB": "#80f6ff", 128 "katex-blueC": "#63d9ea", 129 "katex-blueD": "#11accd", 130 "katex-blueE": "#0c7f99", 131 "katex-tealA": "#94fff5", 132 "katex-tealB": "#26edd5", 133 "katex-tealC": "#01d1c1", 134 "katex-tealD": "#01a995", 135 "katex-tealE": "#208170", 136 "katex-greenA": "#b6ffb0", 137 "katex-greenB": "#8af281", 138 "katex-greenC": "#74cf70", 139 "katex-greenD": "#1fab54", 140 "katex-greenE": "#0d923f", 141 "katex-goldA": "#ffd0a9", 142 "katex-goldB": "#ffbb71", 143 "katex-goldC": "#ff9c39", 144 "katex-goldD": "#e07d10", 145 "katex-goldE": "#a75a05", 146 "katex-redA": "#fca9a9", 147 "katex-redB": "#ff8482", 148 "katex-redC": "#f9685d", 149 "katex-redD": "#e84d39", 150 "katex-redE": "#bc2612", 151 "katex-maroonA": "#ffbde0", 152 "katex-maroonB": "#ff92c6", 153 "katex-maroonC": "#ed5fa6", 154 "katex-maroonD": "#ca337c", 155 "katex-maroonE": "#9e034e", 156 "katex-purpleA": "#ddd7ff", 157 "katex-purpleB": "#c6b9fc", 158 "katex-purpleC": "#aa87ff", 159 "katex-purpleD": "#7854ab", 160 "katex-purpleE": "#543b78", 161 "katex-mintA": "#f5f9e8", 162 "katex-mintB": "#edf2df", 163 "katex-mintC": "#e0e5cc", 164 "katex-grayA": "#f6f7f7", 165 "katex-grayB": "#f0f1f2", 166 "katex-grayC": "#e3e5e6", 167 "katex-grayD": "#d6d8da", 168 "katex-grayE": "#babec2", 169 "katex-grayF": "#888d93", 170 "katex-grayG": "#626569", 171 "katex-grayH": "#3b3e40", 172 "katex-grayI": "#21242c", 173 "katex-kaBlue": "#314453", 174 "katex-kaGreen": "#71B307", 175 }; 176 177 /** 178 * Gets the CSS color of the current options object, accounting for the 179 * `colorMap`. 180 */ 181 Options.prototype.getColor = function() { 182 if (this.phantom) { 183 return "transparent"; 184 } else { 185 return colorMap[this.color] || this.color; 186 } 187 }; 188 189 module.exports = Options;