1

我可以通过以下方式检测 MathML 支持:

var e = document.createElement('div');
    e.innerHTML = '<math></math>';
    var mathMLSupported = e.firstChild && "namespaceURI" in e.firstChild && e.firstChild.namespaceURI == 'http://www.w3.org/1998/Math/MathML';

但是如何检测对<mfrac>and 的支持<mtable>

4

4 回答 4

4

Element#getBoundingClientRect

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mfrac><mn>1</mn><mn>2</mn></mfrac></math>' +
                  '<math><mn>1</mn></math>';
  document.body.appendChild(div);
  return div.firstElementChild.firstElementChild.getBoundingClientRect().height > div.lastElementChild.firstElementChild.getBoundingClientRect().height + 1;
}

console.log(hasMathMLSupport());

window.getComputedStyle

(不适用于夜间模式下的小米浏览器,因为它会将颜色更改为 rgba(255, 255, 255, 0.5))

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mrow mathcolor=\"red\"><mn>1</mn></mrow></math>';
  document.body.appendChild(div);
  return window.getComputedStyle(div.firstChild.firstChild, null).color === "rgb(255, 0, 0)";
}

console.log(hasMathMLSupport());

使用Element.querySelector(":link"): (Safari 10+, Firefox ?+)

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mrow href=\"https://ya.ru\"><mn>1</mn></mrow></math>';
  document.body.appendChild(div);
  return !!div.querySelector(":link");
}

console.log(hasMathMLSupport());

使用 window.MathMLElement != null (该接口已添加到 MathML 4 规范中)

于 2016-08-23T16:11:31.523 回答
3

jqmath中,我构造了一个隐藏<mfrac>元素并将其计算高度与非分数的高度进行比较。有关实际代码,请参阅 jqmath-0.1.js 中的 M.checkMathML 函数。通过尝试使用或不使用 XML 命名空间(取决于浏览器)并允许 Internet Explorer 的 MathPlayer 插件的命名空间前缀,这有点复杂。

于 2011-03-19T23:55:24.833 回答
1

遵循本文档的浏览器必须为 DOM 中的特定 MathML 元素实现几个属性(也称为绑定)。因此,您可以简单地创建一个 MathML mtable 元素并检查浏览器是否添加了以下rowalign属性:

var tmp = document.createElementNS('http://www.w3.org/1998/Math/MathML',
                                   'mtable');
if (tmp.hasOwnProperty('rowalign')) {
  return true;
} else {
  return false;
}
于 2011-01-28T10:20:34.773 回答
1

这似乎仍然不是直截了当的。

http://www.w3.org/TR/MathML2/chapter8.html

可以通过使用测试字符串“org.w3c.dom.mathml”调用 DOMImplementation::hasFeature 方法来查询对 MathML 文档对象模型的支持

这意味着一个简单的测试,但是 Chrome 和 IE 通过插件支持这一点,但即使没有插件,Chrome 也会返回true

我的解决方案是使用 w3c 规范,但适用于浏览器 [chrome] 必须做出相反响应的情况。然后我可以在必要时使用 MathJax,除了 Firefox 之外,它总是如此。脚本进入 html <head> 部分

<script type="text/javascript">

  //Browser detector for Chrome
  //returns true if the Browser is Chrome
  function isChrome(){
    var regex = /Chrome\/[0-9]{1,2}\.[0-9]/
    var matches = navigator.userAgent.match(regex)
    //console.log( matches )
    return (matches!=null && matches.length==1)
  }

  /*
   *  Feature Detect for MathML as w3c specification
   *  <returns>boolean: true if mathML is supported in browser
   */
  function hasFeatureMathML(){
    MATHML_FEATURE = "org.w3c.dom.mathml"     //as per w3c specification
    MATHML_FEATURE_VERSION = "2.0"            //Any version number appears to work
    if(isChrome()) return false               //Opps Chrome not natively supported yet
    return document.implementation.hasFeature(MATHML_FEATURE, MATHML_FEATURE_VERSION )
  }

  /*
   * init MathML use MathJax according to
   *     http://docs.mathjax.org/en/latest/dynamic.html
   * with additional test to confirm necessity
   * <returns>boolean: true if mathML is supported in browser
   */
  function initMathML(){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";

    //doctorBob added test on next line, return if has native support for MathML
    if( hasFeatureMathML() ) return true

    document.getElementsByTagName("head")[0].appendChild(script)
    return false
  }

  //initialize in html <head> incase MathJax is required
  var browserHasMathML = initMathML()
  if( !browserHasMathML )console.log("No Native MathML using MathJax")

</script>

我并没有真正考虑安装浏览器插件,因为不是每个人都安装了它们。这适用于 IE 8、Chrome 39、Firefox 38、Komodo Edit 6

于 2015-03-03T15:24:19.813 回答