1
0
mirror of https://github.com/moodle/moodle.git synced 2025-03-22 16:40:07 +01:00

MDL-60209 mathjax: Do not load broken accessibility extensions

The Accessible.js config loads accessibility-menu extension which in
turn loads mathjax-sre.js library. There is a bug in this library
https://github.com/zorkow/speech-rule-engine/issues/182 that makes it
fail in RequireJS environments. This has been reported upstream as
https://github.com/mathjax/MathJax/issues/1854

As an immediate solution for now, this patch changes the default MathJax
configuration so that it does not load accessibility extensions at all.
This commit is contained in:
David Mudrák 2017-10-12 15:03:36 +02:00
parent b75eb8442f
commit 9e6df843a0
6 changed files with 101 additions and 3 deletions

@ -140,5 +140,34 @@ function xmldb_filter_mathjaxloader_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2017100900, 'filter', 'mathjaxloader');
}
if ($oldversion < 2017101200) {
// Update default MathJax configuration so that it does not use the Accessible.js config (causes JS errors due to upstream bug).
$previousdefault = '
MathJax.Hub.Config({
config: ["Accessible.js", "Safe.js"],
errorSettings: { message: ["!"] },
skipStartupTypeset: true,
messageStyle: "none"
});
';
$newdefault = '
MathJax.Hub.Config({
config: ["default.js", "MMLorHTML.js", "Safe.js"],
errorSettings: { message: ["!"] },
skipStartupTypeset: true,
messageStyle: "none"
});
';
$mathjaxconfig = get_config('filter_mathjaxloader', 'mathjaxconfig');
if (empty($mathjaxconfig) || filter_mathjaxloader_upgrade_mathjaxconfig_equal($mathjaxconfig, $previousdefault)) {
set_config('mathjaxconfig', $newdefault, 'filter_mathjaxloader');
}
upgrade_plugin_savepoint(true, 2017101200, 'filter', 'mathjaxloader');
}
return true;
}

@ -64,3 +64,32 @@ function filter_mathjaxloader_upgrade_cdn_cloudflare($mathjaxurl, $httponly = fa
return $newcdnurl;
}
/**
* Compares two values of the 'mathjaxconfig' config option.
*
* This is used during the upgrade to see if the two text values of the 'mathjaxconfig' config option should be
* considered equal of different. The strings are normalized so that EOL characters and whitespace is not significant.
*
* @param string $val1 value
* @param string $val2 value
* @return bool true if the texts should be considered equals, false otherwise
*/
function filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2) {
$val1lines = preg_split("/[\r\n]/", $val1);
$val2lines = preg_split("/[\r\n]/", $val2);
$val1lines = array_map('trim', $val1lines);
$val2lines = array_map('trim', $val2lines);
$val1lines = array_filter($val1lines, function($value) {
return $value !== '';
});
$val2lines = array_filter($val2lines, function($value) {
return $value !== '';
});
return (implode(' ', $val1lines) === implode(' ', $val2lines));
}

@ -17,3 +17,10 @@ Upgrading the default MathJax version
previous default.
3. Check and eventually update the list of language mappings in filter.php.
Also see the unit test for the language mappings.
Changes
-------
* The MathJax 2.7.2 seems to have a bug causing the accessibility extensions
fail in web apps using RequireJS (such as Moodle). We had to stop using the
Accessible.js config for that reason. See MDL-60209 for details.

@ -45,7 +45,7 @@ if ($ADMIN->fulltree) {
$default = '
MathJax.Hub.Config({
config: ["Accessible.js", "Safe.js"],
config: ["default.js", "MMLorHTML.js", "Safe.js"],
errorSettings: { message: ["!"] },
skipStartupTypeset: true,
messageStyle: "none"

@ -37,7 +37,10 @@ require_once($CFG->dirroot . '/filter/mathjaxloader/db/upgradelib.php');
*/
class filter_mathjax_upgradelib_testcase extends advanced_testcase {
public function test_upgradelib() {
/**
* Tests for {@link filter_mathjaxloader_upgrade_cdn_cloudflare()} function.
*/
public function test_filter_mathjaxloader_upgrade_cdn_cloudflare() {
$current = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...';
$expected = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?...';
$this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current));
@ -87,5 +90,35 @@ class filter_mathjax_upgradelib_testcase extends advanced_testcase {
$expected = 'https://cdn.mathjax.org/mathjax/2.7-latest';
$this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current));
}
/**
* Tests for {@link filter_mathjaxloader_upgrade_mathjaxconfig_equal()} function.
*/
public function test_filter_mathjaxloader_upgrade_mathjaxconfig_equal() {
$val1 = '';
$val2 = "\n \r \r\n \t ";
$this->assertTrue(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
$val1 = '0';
$val2 = '';
$this->assertFalse(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
$val1 = 'Hello Unittest, my old friend '.PHP_EOL."I've come to play with you again \r\n\r\n \t ";
$val2 = ' Hello Unittest, my old friend '."\r\n\r\n"." I've come to play with you again \n\n\n ";
$this->assertTrue(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
$val1 = "\n".'MathJax.Hub.Config({'."\n".' config: ["Accessible.js", "Safe.js"]'."\n".'});'."\n";
$val2 = 'MathJax.Hub.Config({'."\r".'config: ["Accessible.js", "Safe.js"]'."\r".'});';
$this->assertTrue(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
$val1 = "\r\n\t".'MathJax.Hub.Config({'."\r\n\t".' config: ["Accessible.js", "Safe.js"]'."\r\n".'}); '."\r\n\r\n";
$val2 = 'MathJax.Hub.Config({'."\n".'config: ["Accessible.js", "Safe.js"]'."\r".'});';
$this->assertTrue(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
$val2 = 'MathJax.Hub.Config({'."\n".'config: ["Significant.js"]'."\n".'});';
$val2 = 'MathJax.Hub.Config({'."\n".'config: ["Signi ficant.js", "Safe.js"]'."\n".'});';
$this->assertFalse(filter_mathjaxloader_upgrade_mathjaxconfig_equal($val1, $val2));
}
}

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017100900;
$plugin->version = 2017101200;
$plugin->requires = 2017050500; // Requires this Moodle version.
$plugin->component= 'filter_mathjaxloader';