diff --git a/filter/mathjaxloader/db/upgrade.php b/filter/mathjaxloader/db/upgrade.php index f4b62c2c3bd..ef222ea505c 100644 --- a/filter/mathjaxloader/db/upgrade.php +++ b/filter/mathjaxloader/db/upgrade.php @@ -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; } diff --git a/filter/mathjaxloader/db/upgradelib.php b/filter/mathjaxloader/db/upgradelib.php index 295d4b3d9a8..813075698a8 100644 --- a/filter/mathjaxloader/db/upgradelib.php +++ b/filter/mathjaxloader/db/upgradelib.php @@ -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)); +} diff --git a/filter/mathjaxloader/readme_moodle.txt b/filter/mathjaxloader/readme_moodle.txt index 7ace4042d7d..e7e666f1095 100644 --- a/filter/mathjaxloader/readme_moodle.txt +++ b/filter/mathjaxloader/readme_moodle.txt @@ -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. diff --git a/filter/mathjaxloader/settings.php b/filter/mathjaxloader/settings.php index a911788d89b..783282b997a 100644 --- a/filter/mathjaxloader/settings.php +++ b/filter/mathjaxloader/settings.php @@ -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" diff --git a/filter/mathjaxloader/tests/upgradelib_test.php b/filter/mathjaxloader/tests/upgradelib_test.php index 01d99088671..092f293b858 100644 --- a/filter/mathjaxloader/tests/upgradelib_test.php +++ b/filter/mathjaxloader/tests/upgradelib_test.php @@ -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)); + } } diff --git a/filter/mathjaxloader/version.php b/filter/mathjaxloader/version.php index 2db22c7adfb..d8213c89599 100644 --- a/filter/mathjaxloader/version.php +++ b/filter/mathjaxloader/version.php @@ -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';