| 1 |
/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ |
| 2 |
/* vim: set ts=2 et sw=2 tw=80: */ |
| 3 |
|
| 4 |
/************************************************************* |
| 5 |
* |
| 6 |
* MathJax/extensions/Safe.js |
| 7 |
* |
| 8 |
* Implements a "Safe" mode that disables features that could be |
| 9 |
* misused in a shared environment (such as href's to javascript URL's). |
| 10 |
* See the CONFIG variable below for configuration options. |
| 11 |
* |
| 12 |
* --------------------------------------------------------------------- |
| 13 |
* |
| 14 |
* Copyright (c) 2013-2020 The MathJax Consortium |
| 15 |
* |
| 16 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 |
* you may not use this file except in compliance with the License. |
| 18 |
* You may obtain a copy of the License at |
| 19 |
* |
| 20 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 21 |
* |
| 22 |
* Unless required by applicable law or agreed to in writing, software |
| 23 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 24 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 |
* See the License for the specific language governing permissions and |
| 26 |
* limitations under the License. |
| 27 |
*/ |
| 28 |
|
| 29 |
(function (HUB,AJAX) { |
| 30 |
var VERSION = "2.7.9"; |
| 31 |
|
| 32 |
var CONFIG = MathJax.Hub.CombineConfig("Safe",{ |
| 33 |
allow: { |
| 34 |
// |
| 35 |
// Values can be "all", "safe", or "none" |
| 36 |
// |
| 37 |
URLs: "safe", // safe are in safeProtocols below |
| 38 |
classes: "safe", // safe start with MJX- |
| 39 |
cssIDs: "safe", // safe start with MJX- |
| 40 |
styles: "safe", // safe are in safeStyles below |
| 41 |
fontsize: "all", // safe are between sizeMin and sizeMax em's |
| 42 |
require: "safe" // safe are in safeRequire below |
| 43 |
}, |
| 44 |
sizeMin: .7, // \scriptsize |
| 45 |
sizeMax: 1.44, // \large |
| 46 |
lengthMax: 3, // largest padding/border/margin, etc. in em's |
| 47 |
safeProtocols: { |
| 48 |
http: true, |
| 49 |
https: true, |
| 50 |
file: true, |
| 51 |
javascript: false |
| 52 |
}, |
| 53 |
safeStyles: { |
| 54 |
color: true, |
| 55 |
backgroundColor: true, |
| 56 |
border: true, |
| 57 |
cursor: true, |
| 58 |
margin: true, |
| 59 |
padding: true, |
| 60 |
textShadow: true, |
| 61 |
fontFamily: true, |
| 62 |
fontSize: true, |
| 63 |
fontStyle: true, |
| 64 |
fontWeight: true, |
| 65 |
opacity: true, |
| 66 |
outline: true |
| 67 |
}, |
| 68 |
safeRequire: { |
| 69 |
action: true, |
| 70 |
amscd: true, |
| 71 |
amsmath: true, |
| 72 |
amssymbols: true, |
| 73 |
autobold: false, |
| 74 |
"autoload-all": false, |
| 75 |
bbox: true, |
| 76 |
begingroup: true, |
| 77 |
boldsymbol: true, |
| 78 |
cancel: true, |
| 79 |
color: true, |
| 80 |
enclose: true, |
| 81 |
extpfeil: true, |
| 82 |
HTML: true, |
| 83 |
mathchoice: true, |
| 84 |
mhchem: true, |
| 85 |
newcommand: true, |
| 86 |
noErrors: false, |
| 87 |
noUndefined: false, |
| 88 |
unicode: true, |
| 89 |
verb: true |
| 90 |
}, |
| 91 |
// |
| 92 |
// CSS styles that have Top/Right/Bottom/Left versions |
| 93 |
// |
| 94 |
styleParts: { |
| 95 |
border: true, |
| 96 |
padding: true, |
| 97 |
margin: true, |
| 98 |
outline: true |
| 99 |
}, |
| 100 |
// |
| 101 |
// CSS styles that are lengths needing max/min testing |
| 102 |
// A string value means test that style value; |
| 103 |
// An array gives [min,max] in em's |
| 104 |
// Otherwise use [-lengthMax,lengthMax] from above |
| 105 |
// |
| 106 |
styleLengths: { |
| 107 |
borderTop: "borderTopWidth", |
| 108 |
borderRight: "borderRightWidth", |
| 109 |
borderBottom: "borderBottomWidth", |
| 110 |
borderLeft: "borderLeftWidth", |
| 111 |
paddingTop: true, |
| 112 |
paddingRight: true, |
| 113 |
paddingBottom: true, |
| 114 |
paddingLeft: true, |
| 115 |
marginTop: true, |
| 116 |
marginRight: true, |
| 117 |
marginBottom: true, |
| 118 |
marginLeft: true, |
| 119 |
outlineTop: true, |
| 120 |
outlineRight: true, |
| 121 |
outlineBottom: true, |
| 122 |
outlineLeft: true, |
| 123 |
fontSize: [.7,1.44] |
| 124 |
} |
| 125 |
}); |
| 126 |
|
| 127 |
var ALLOW = CONFIG.allow; |
| 128 |
if (ALLOW.fontsize !== "all") {CONFIG.safeStyles.fontSize = false} |
| 129 |
|
| 130 |
var SAFE = MathJax.Extension.Safe = { |
| 131 |
version: VERSION, |
| 132 |
config: CONFIG, |
| 133 |
div1: document.createElement("div"), // for CSS processing |
| 134 |
div2: document.createElement("div"), |
| 135 |
|
| 136 |
// |
| 137 |
// Methods called for MathML attribute processing |
| 138 |
// |
| 139 |
filter: { |
| 140 |
href: "filterURL", |
| 141 |
src: "filterURL", |
| 142 |
altimg: "filterURL", |
| 143 |
"class": "filterClass", |
| 144 |
style: "filterStyles", |
| 145 |
id: "filterID", |
| 146 |
fontsize: "filterFontSize", |
| 147 |
mathsize: "filterFontSize", |
| 148 |
scriptminsize: "filterFontSize", |
| 149 |
scriptsizemultiplier: "filterSizeMultiplier", |
| 150 |
scriptlevel: "filterScriptLevel" |
| 151 |
}, |
| 152 |
|
| 153 |
// |
| 154 |
// Filter HREF URL's |
| 155 |
// |
| 156 |
filterURL: function (url) { |
| 157 |
var protocol = (url.match(/^\s*([a-z]+):/i)||[null,""])[1].toLowerCase(); |
| 158 |
if (ALLOW.URLs === "none" || |
| 159 |
(ALLOW.URLs !== "all" && !CONFIG.safeProtocols[protocol])) {url = null} |
| 160 |
return url; |
| 161 |
}, |
| 162 |
|
| 163 |
// |
| 164 |
// Filter class names and css ID's |
| 165 |
// |
| 166 |
filterClass: function (CLASS) { |
| 167 |
if (ALLOW.classes === "none" || |
| 168 |
(ALLOW.classes !== "all" && !CLASS.match(/^MJX-[-a-zA-Z0-9_.]+$/))) {CLASS = null} |
| 169 |
return CLASS; |
| 170 |
}, |
| 171 |
filterID: function (id) { |
| 172 |
if (ALLOW.cssIDs === "none" || |
| 173 |
(ALLOW.cssIDs !== "all" && !id.match(/^MJX-[-a-zA-Z0-9_.]+$/))) {id = null} |
| 174 |
return id; |
| 175 |
}, |
| 176 |
|
| 177 |
// |
| 178 |
// Filter style strings |
| 179 |
// |
| 180 |
filterStyles: function (styles) { |
| 181 |
if (ALLOW.styles === "all") {return styles} |
| 182 |
if (ALLOW.styles === "none") {return null} |
| 183 |
try { |
| 184 |
// |
| 185 |
// Set the div1 styles to the given styles, and clear div2 |
| 186 |
// |
| 187 |
var STYLE1 = this.div1.style, STYLE2 = this.div2.style, value; |
| 188 |
STYLE1.cssText = styles; STYLE2.cssText = ""; |
| 189 |
// |
| 190 |
// Check each allowed style and transfer OK ones to div2 |
| 191 |
// If the style has Top/Right/Bottom/Left, look at all four separately |
| 192 |
// |
| 193 |
for (var name in CONFIG.safeStyles) {if (CONFIG.safeStyles.hasOwnProperty(name)) { |
| 194 |
if (CONFIG.styleParts[name]) { |
| 195 |
for (var i = 0; i < 4; i++) { |
| 196 |
var NAME = name+["Top","Right","Bottom","Left"][i] |
| 197 |
value = this.filterStyle(NAME,STYLE1); |
| 198 |
if (value) {STYLE2[NAME] = value} |
| 199 |
} |
| 200 |
} else { |
| 201 |
value = this.filterStyle(name,STYLE1); |
| 202 |
if (value) {STYLE2[name] = value} |
| 203 |
} |
| 204 |
}} |
| 205 |
// |
| 206 |
// Return the div2 style string |
| 207 |
// |
| 208 |
styles = STYLE2.cssText; |
| 209 |
} catch (e) {styles = null} |
| 210 |
return styles; |
| 211 |
}, |
| 212 |
// |
| 213 |
// Filter an individual name:value style pair |
| 214 |
// |
| 215 |
filterStyle: function (name,styles) { |
| 216 |
var value = styles[name]; |
| 217 |
if (typeof value !== "string" || value === "") {return null} |
| 218 |
if (value.match(/^\s*expression/)) {return null} |
| 219 |
if (value.match(/javascript:/)) {return null} |
| 220 |
var NAME = name.replace(/Top|Right|Left|Bottom/,""); |
| 221 |
if (!CONFIG.safeStyles[name] && !CONFIG.safeStyles[NAME]) {return null} |
| 222 |
if (!CONFIG.styleLengths[name]) {return value} |
| 223 |
return (this.filterStyleLength(name,value,styles) ? value : null); |
| 224 |
}, |
| 225 |
filterStyleLength: function (name,value,styles) { |
| 226 |
if (typeof CONFIG.styleLengths[name] === "string") value = styles[CONFIG.styleLengths[name]]; |
| 227 |
value = this.length2em(value); |
| 228 |
if (value == null) return false; |
| 229 |
var mM = [-CONFIG.lengthMax,CONFIG.lengthMax]; |
| 230 |
if (MathJax.Object.isArray(CONFIG.styleLengths[name])) mM = CONFIG.styleLengths[name]; |
| 231 |
return (value >= mM[0] && value <= mM[1]); |
| 232 |
}, |
| 233 |
// |
| 234 |
// Conversion of units to em's |
| 235 |
// |
| 236 |
unit2em: { |
| 237 |
em: 1, |
| 238 |
ex: .5, // assume 1ex = .5em |
| 239 |
ch: .5, // assume 1ch = .5em |
| 240 |
rem: 1, // assume 1rem = 1em |
| 241 |
px: 1/16, // assume 1em = 16px |
| 242 |
mm: 96/25.4/16, // 25.4mm = 96px |
| 243 |
cm: 96/2.54/16, // 2.54cm = 96px |
| 244 |
'in': 96/16, // 1in = 96px |
| 245 |
pt: 96/72/16, // 72pt = 1in |
| 246 |
pc: 96/6/16 // 1pc = 12pt |
| 247 |
}, |
| 248 |
length2em: function (value) { |
| 249 |
var match = value.match(/(.+)(em|ex|ch|rem|px|mm|cm|in|pt|pc)/); |
| 250 |
if (!match) return null; |
| 251 |
return parseFloat(match[1])*this.unit2em[match[2]]; |
| 252 |
}, |
| 253 |
|
| 254 |
// |
| 255 |
// Filter TeX font size values (in em's) |
| 256 |
// |
| 257 |
filterSize: function (size) { |
| 258 |
if (ALLOW.fontsize === "none") {return null} |
| 259 |
if (ALLOW.fontsize !== "all") |
| 260 |
{size = Math.min(Math.max(size,CONFIG.sizeMin),CONFIG.sizeMax)} |
| 261 |
return size; |
| 262 |
}, |
| 263 |
filterFontSize: function (size) { |
| 264 |
return (ALLOW.fontsize === "all" ? size: null); |
| 265 |
}, |
| 266 |
|
| 267 |
// |
| 268 |
// Filter scriptsizemultiplier |
| 269 |
// |
| 270 |
filterSizeMultiplier: function (size) { |
| 271 |
if (ALLOW.fontsize === "none") {size = null} |
| 272 |
else if (ALLOW.fontsize !== "all") {size = Math.min(1,Math.max(.6,size)).toString()} |
| 273 |
return size; |
| 274 |
}, |
| 275 |
// |
| 276 |
// Filter scriptLevel |
| 277 |
// |
| 278 |
filterScriptLevel: function (level) { |
| 279 |
if (ALLOW.fontsize === "none") {level = null} |
| 280 |
else if (ALLOW.fontsize !== "all") {level = Math.max(0,level).toString()} |
| 281 |
return level; |
| 282 |
}, |
| 283 |
|
| 284 |
// |
| 285 |
// Filter TeX extension names |
| 286 |
// |
| 287 |
filterRequire: function (name) { |
| 288 |
if (ALLOW.require === "none" || |
| 289 |
(ALLOW.require !== "all" && !CONFIG.safeRequire[name.toLowerCase()])) |
| 290 |
{name = null} |
| 291 |
return name; |
| 292 |
} |
| 293 |
|
| 294 |
}; |
| 295 |
|
| 296 |
HUB.Register.StartupHook("TeX HTML Ready",function () { |
| 297 |
var TEX = MathJax.InputJax.TeX; |
| 298 |
|
| 299 |
TEX.Parse.Augment({ |
| 300 |
|
| 301 |
// |
| 302 |
// Implements \href{url}{math} with URL filter |
| 303 |
// |
| 304 |
HREF_attribute: function (name) { |
| 305 |
var url = SAFE.filterURL(this.GetArgument(name)), |
| 306 |
arg = this.GetArgumentMML(name); |
| 307 |
if (url) {arg.With({href:url})} |
| 308 |
this.Push(arg); |
| 309 |
}, |
| 310 |
|
| 311 |
// |
| 312 |
// Implements \class{name}{math} with class-name filter |
| 313 |
// |
| 314 |
CLASS_attribute: function (name) { |
| 315 |
var CLASS = SAFE.filterClass(this.GetArgument(name)), |
| 316 |
arg = this.GetArgumentMML(name); |
| 317 |
if (CLASS) { |
| 318 |
if (arg["class"] != null) {CLASS = arg["class"] + " " + CLASS} |
| 319 |
arg.With({"class":CLASS}); |
| 320 |
} |
| 321 |
this.Push(arg); |
| 322 |
}, |
| 323 |
|
| 324 |
// |
| 325 |
// Implements \style{style-string}{math} with style filter |
| 326 |
// |
| 327 |
STYLE_attribute: function (name) { |
| 328 |
var style = SAFE.filterStyles(this.GetArgument(name)), |
| 329 |
arg = this.GetArgumentMML(name); |
| 330 |
if (style) { |
| 331 |
if (arg.style != null) { |
| 332 |
if (style.charAt(style.length-1) !== ";") {style += ";"} |
| 333 |
style = arg.style + " " + style; |
| 334 |
} |
| 335 |
arg.With({style: style}); |
| 336 |
} |
| 337 |
this.Push(arg); |
| 338 |
}, |
| 339 |
|
| 340 |
// |
| 341 |
// Implements \cssId{id}{math} with ID filter |
| 342 |
// |
| 343 |
ID_attribute: function (name) { |
| 344 |
var ID = SAFE.filterID(this.GetArgument(name)), |
| 345 |
arg = this.GetArgumentMML(name); |
| 346 |
if (ID) {arg.With({id:ID})} |
| 347 |
this.Push(arg); |
| 348 |
} |
| 349 |
|
| 350 |
}); |
| 351 |
|
| 352 |
}); |
| 353 |
|
| 354 |
HUB.Register.StartupHook("TeX Jax Ready",function () { |
| 355 |
var TEX = MathJax.InputJax.TeX, |
| 356 |
PARSE = TEX.Parse, METHOD = SAFE.filter; |
| 357 |
|
| 358 |
PARSE.Augment({ |
| 359 |
|
| 360 |
// |
| 361 |
// Implements \require{name} with filtering |
| 362 |
// |
| 363 |
Require: function (name) { |
| 364 |
var file = this.GetArgument(name).replace(/.*\//,"").replace(/[^a-z0-9_.-]/ig,""); |
| 365 |
file = SAFE.filterRequire(file); |
| 366 |
if (file) {this.Extension(null,file)} |
| 367 |
}, |
| 368 |
|
| 369 |
// |
| 370 |
// Controls \mmlToken attributes |
| 371 |
// |
| 372 |
MmlFilterAttribute: function (name,value) { |
| 373 |
if (METHOD[name]) {value = SAFE[METHOD[name]](value)} |
| 374 |
return value; |
| 375 |
}, |
| 376 |
|
| 377 |
// |
| 378 |
// Handles font size macros with filtering |
| 379 |
// |
| 380 |
SetSize: function (name,size) { |
| 381 |
size = SAFE.filterSize(size); |
| 382 |
if (size) { |
| 383 |
this.stack.env.size = size; |
| 384 |
this.Push(TEX.Stack.Item.style().With({styles: {mathsize: size+"em"}})); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
}); |
| 389 |
}); |
| 390 |
|
| 391 |
HUB.Register.StartupHook("TeX bbox Ready",function () { |
| 392 |
var TEX = MathJax.InputJax.TeX; |
| 393 |
|
| 394 |
// |
| 395 |
// Filter the styles for \bbox |
| 396 |
// |
| 397 |
TEX.Parse.Augment({ |
| 398 |
BBoxStyle: function (styles) {return SAFE.filterStyles(styles)}, |
| 399 |
BBoxPadding: function (pad) { |
| 400 |
var styles = SAFE.filterStyles("padding: "+pad); |
| 401 |
return (styles ? pad : 0); |
| 402 |
} |
| 403 |
}); |
| 404 |
|
| 405 |
}); |
| 406 |
|
| 407 |
HUB.Register.StartupHook("MathML Jax Ready",function () { |
| 408 |
var PARSE = MathJax.InputJax.MathML.Parse, |
| 409 |
METHOD = SAFE.filter; |
| 410 |
|
| 411 |
// |
| 412 |
// Filter MathML attributes |
| 413 |
// |
| 414 |
PARSE.Augment({ |
| 415 |
filterAttribute: function (name,value) { |
| 416 |
if (METHOD[name]) {value = SAFE[METHOD[name]](value)} |
| 417 |
return value; |
| 418 |
} |
| 419 |
}); |
| 420 |
|
| 421 |
}); |
| 422 |
|
| 423 |
// MathML input (href, style, fontsize, class, id) |
| 424 |
|
| 425 |
HUB.Startup.signal.Post("Safe Extension Ready"); |
| 426 |
AJAX.loadComplete("[MathJax]/extensions/Safe.js"); |
| 427 |
|
| 428 |
})(MathJax.Hub,MathJax.Ajax); |