UNPKG

mathjax

Version:

Beautiful and accessible math in all browsers. MathJax is an open-source JavaScript display engine for LaTeX, MathML, and AsciiMath notation that works in all browsers. This package includes the packaged components (install mathjax-full to get the source

1 lines 8.68 kB
(()=>{"use strict";var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})}};t.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),t.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),t.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var e={};t.r(e),t.d(e,{FindMathML:()=>u});var s={};t.r(s),t.d(s,{MathMLCompile:()=>g});var r={};t.r(r),t.d(r,{MathML:()=>N});const a=("undefined"!=typeof window?window:t.g).MathJax._.components.global,i=(a.GLOBAL,a.isObject,a.combineConfig,a.combineDefaults,a.combineWithMathJax),o=(a.MathJax,MathJax._.core.InputJax.AbstractInputJax),n=MathJax._.util.Options,l=(n.isObject,n.APPEND,n.REMOVE,n.OPTIONS,n.Expandable,n.expandable,n.makeArray,n.keys,n.copy,n.insert,n.defaultOptions),h=n.userOptions,d=(n.selectOptions,n.selectOptionsFromKeys,n.separateOptions),c=(n.lookup,MathJax._.util.FunctionList.FunctionList),p=MathJax._.core.FindMath.AbstractFindMath,m="http://www.w3.org/1998/Math/MathML";class u extends p{findMath(t){const e=new Set;this.findMathNodes(t,e),this.findMathPrefixed(t,e);const s=this.adaptor.root(this.adaptor.document);return"html"===this.adaptor.kind(s)&&0===e.size&&this.findMathNS(t,e),this.processMath(e)}findMathNodes(t,e){for(const s of this.adaptor.tags(t,"math"))e.add(s)}findMathPrefixed(t,e){const s=this.adaptor.root(this.adaptor.document);for(const r of this.adaptor.allAttributes(s))if("xmlns:"===r.name.substring(0,6)&&r.value===m){const s=r.name.substring(6);for(const r of this.adaptor.tags(t,s+":math"))e.add(r)}}findMathNS(t,e){for(const s of this.adaptor.tags(t,"math",m))e.add(s)}processMath(t){const e=this.adaptor,s=[];for(const r of t.values()){if("mjx-assistive-mml"===e.kind(e.parent(r)))continue;const t="block"===e.getAttribute(r,"display")||"display"===e.getAttribute(r,"mode"),a={node:r,n:0,delim:""},i={node:r,n:0,delim:""};s.push({math:e.outerHTML(r),start:a,end:i,display:t})}return s}}u.OPTIONS={};const M=MathJax._.core.MmlTree.MmlNode,f=M.TEXCLASS,b=(M.TEXCLASSNAMES,M.MATHVARIANTS,M.indentAttributes,M.AbstractMmlNode),x=(M.AbstractMmlTokenNode,M.AbstractMmlLayoutNode,M.AbstractMmlBaseNode,M.AbstractMmlEmptyNode,M.TextNode,M.XMLNode,MathJax._.util.Entities),y=(x.options,x.entities,x.add,x.remove,x.translate);x.numeric;class g{constructor(t={}){const e=this.constructor;this.options=h(l({},e.OPTIONS),t)}setMmlFactory(t){this.factory=t}compile(t){const e=this.makeNode(t);return e.verifyTree(this.options.verify),e.setInheritedAttributes({},!1,0,!1),e.walkTree(this.markMrows),e}makeNode(t){const e=this.adaptor;let s=!1;const r=e.kind(t).replace(/^.*:/,"");let a=e.getAttribute(t,"data-mjx-texclass")||"";a&&(a=this.filterAttribute("data-mjx-texclass",a)||"");let i=a&&"mrow"===r?"TeXAtom":r;for(const o of this.filterClassList(e.allClasses(t)))o.match(/^MJX-TeXAtom-/)&&"mrow"===r?(a=o.substring(12),i="TeXAtom"):"MJX-fixedlimits"===o&&(s=!0);return this.factory.getNodeClass(i)?this.createMml(i,t,a,s):this.unknownNode(i,t)}createMml(t,e,s,r){const a=this.factory.create(t);return"TeXAtom"!==t||"OP"!==s||r||(a.setProperty("movesupsub",!0),a.attributes.setInherited("movablelimits",!0)),s&&(a.texClass=f[s],a.setProperty("texClass",a.texClass)),this.addAttributes(a,e),this.checkClass(a,e),this.addChildren(a,e),a}unknownNode(t,e){return this.factory.getNodeClass("html")&&this.options.allowHtmlInTokenNodes?this.factory.create("html").setHTML(e,this.adaptor):(this.error('Unknown node type "'+t+'"'),null)}addAttributes(t,e){let s=!1;for(const r of this.adaptor.allAttributes(e)){const e=r.name,a=this.filterAttribute(e,r.value);if(null!==a&&"xmlns"!==e)if("data-mjx-"===e.substring(0,9))switch(e.substring(9)){case"alternate":t.setProperty("variantForm",!0);break;case"variant":t.attributes.set("mathvariant",a),t.setProperty("ignore-variant",!0),s=!0;break;case"smallmatrix":t.setProperty("smallmatrix",!0),t.setProperty("useHeight",!1);break;case"mathaccent":t.setProperty("mathaccent","true"===a);break;case"auto-op":t.setProperty("autoOP","true"===a);break;case"script-align":t.setProperty("scriptalign",a);break;case"vbox":t.setProperty("vbox",a);break;default:t.attributes.set(e,a)}else if("class"!==e){const r=a.toLowerCase();"true"===r||"false"===r?t.attributes.set(e,"true"===r):s&&"mathvariant"===e||t.attributes.set(e,a)}}}filterAttribute(t,e){return e}filterClassList(t){return t}addChildren(t,e){if(0===t.arity)return;const s=this.adaptor;for(const r of s.childNodes(e)){const e=s.kind(r);if("#comment"!==e)if("#text"===e)this.addText(t,r);else if(t.isKind("annotation-xml"))t.appendChild(this.factory.create("XML").setXML(r,s));else{const e=t.appendChild(this.makeNode(r));0===e.arity&&s.childNodes(r).length&&!e.isKind("html")&&(this.options.fixMisplacedChildren?this.addChildren(t,r):e.mError("There should not be children for "+e.kind+" nodes",this.options.verify,!0))}}t.isToken&&this.trimSpace(t)}addText(t,e){let s=this.adaptor.value(e);(t.isToken||t.getProperty("isChars"))&&t.arity?(t.isToken&&(s=y(s),s=this.normalizeSpace(s)),t.appendChild(this.factory.create("text").setText(s))):s.match(/\S/)&&this.error('Unexpected text node "'+s+'"')}checkClass(t,e){const s=[];for(const r of this.filterClassList(this.adaptor.allClasses(e)))"MJX-"===r.substring(0,4)?"MJX-variant"===r?t.setProperty("variantForm",!0):"MJX-TeXAtom"!==r.substring(0,11)&&t.attributes.set("mathvariant",this.fixCalligraphic(r.substring(3))):s.push(r);s.length&&t.attributes.set("class",s.join(" "))}fixCalligraphic(t){return t.replace(/caligraphic/,"calligraphic")}markMrows(t){if(t.isKind("mrow")&&!t.isInferred&&t.childNodes.length>=2){const e=t.childNodes[0],s=t.childNodes[t.childNodes.length-1];e.isKind("mo")&&e.attributes.get("fence")&&e.attributes.get("stretchy")&&s.isKind("mo")&&s.attributes.get("fence")&&s.attributes.get("stretchy")&&(e.childNodes.length&&t.setProperty("open",e.getText()),s.childNodes.length&&t.setProperty("close",s.getText()))}}normalizeSpace(t){return t.replace(/[\t\n\r]/g," ").replace(/ +/g," ")}trimSpace(t){let e=t.childNodes[0];e&&(e.isKind("text")&&e.setText(e.getText().replace(/^ +/,"")),e=t.childNodes[t.childNodes.length-1],e.isKind("text")&&e.setText(e.getText().replace(/ +$/,"")))}error(t){throw new Error(t)}}g.OPTIONS={MmlFactory:null,allowHtmlInTokenNodes:!1,fixMisplacedChildren:!0,verify:Object.assign({},b.verifyDefaults),translateEntities:!0};class N extends o{constructor(t={}){const[e,s,r]=d(t,u.OPTIONS,g.OPTIONS);super(e),this.findMathML=this.options.FindMathML||new u(s),this.mathml=this.options.MathMLCompile||new g(r),this.mmlFilters=new c(this.options.mmlFilters)}setAdaptor(t){super.setAdaptor(t),this.findMathML.adaptor=t,this.mathml.adaptor=t}setMmlFactory(t){super.setMmlFactory(t),this.mathml.setMmlFactory(t)}get processStrings(){return!1}compile(t,e){let s=t.start.node;if(!s||!t.end.node||this.options.forceReparse||"#text"===this.adaptor.kind(s)){let r=this.executeFilters(this.preFilters,t,e,(t.math||"<math></math>").trim());"html"===this.options.parseAs&&(r=`<html><head></head><body>${r}</body></html>`);const a=this.checkForErrors(this.adaptor.parse(r,"text/"+this.options.parseAs)),i=this.adaptor.body(a);1!==this.adaptor.childNodes(i).length&&this.error("MathML must consist of a single element"),s=this.adaptor.remove(this.adaptor.firstChild(i)),"math"!==this.adaptor.kind(s).replace(/^[a-z]+:/,"")&&this.error("MathML must be formed by a <math> element, not <"+this.adaptor.kind(s)+">")}s=this.executeFilters(this.mmlFilters,t,e,s);let r=this.mathml.compile(s);return r=this.executeFilters(this.postFilters,t,e,r),t.display="block"===r.attributes.get("display"),r}checkForErrors(t){const e=this.adaptor.tags(this.adaptor.body(t),"parsererror")[0];return e&&(""===this.adaptor.textContent(e)&&this.error("Error processing MathML"),this.options.parseError.call(this,e)),t}error(t){throw new Error(t)}findMath(t){return this.findMathML.findMath(t)}}N.NAME="MathML",N.OPTIONS=l({parseAs:"html",forceReparse:!1,mmlFilters:[],FindMathML:null,MathMLCompile:null,parseError:function(t){this.error(this.adaptor.textContent(t).replace(/\n.*/g,""))}},o.OPTIONS),MathJax.loader&&MathJax.loader.checkVersion("input/mml","4.1.0","input"),i({_:{input:{mathml_ts:r,mathml:{FindMathML:e,MathMLCompile:s}}}}),MathJax.loader&&MathJax.loader.pathFilters.add((t=>(t.name=t.name.replace(/\/util\/entities\/.*?\.js/,"/input/mml/entities.js"),!0))),MathJax.startup&&(MathJax.startup.registerConstructor("mml",N),MathJax.startup.useInput("mml"))})();