From e874af28cea31ac551d5070aea66e42dbeaaa233 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Thu, 25 Sep 2008 06:29:28 +0000 Subject: [PATCH] MDL-16583 Make require_js accept library names like lib/javascript-static.js without the caller having to fiddle around with $CFG->wwwroot themselves. --- lib/ajax/ajaxlib.php | 12 +++--- lib/ajax/simpletest/testajaxlib.php | 61 +++++++++++++++++++++++++++++ lib/weblib.php | 35 +++++++++-------- 3 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 lib/ajax/simpletest/testajaxlib.php diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index 0636b82e2a..a14c9dfdda 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -9,7 +9,6 @@ * @return string */ function ajax_get_lib($libname) { - global $CFG; $libpath = ''; $external_yui = false; @@ -66,18 +65,21 @@ function ajax_get_lib($libname) { include($CFG->libdir.'/yui/version.php'); $libpath = 'http://yui.yahooapis.com/'.$yuiversion.'/build/'.substr($translatelist[$libname], 9); } else { - $libpath = $CFG->wwwroot . $translatelist[$libname]; + $libpath = $CFG->httpswwwroot . $translatelist[$libname]; } - } else { + } else if (preg_match('/^https?:/', $libname)) { $libpath = $libname; + + } else { + $libpath = $CFG->httpswwwroot . '/' . $libname; } // Make sure the file exists if it is local. if ($external_yui === false) { - $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath); + $testpath = str_replace($CFG->httpswwwroot, $CFG->dirroot, $libpath); if (!file_exists($testpath)) { - error('require_js: '.$libpath.' - file not found.'); + throw new moodle_exception('unknownjsinrequirejs', '', '', $libpath); } } diff --git a/lib/ajax/simpletest/testajaxlib.php b/lib/ajax/simpletest/testajaxlib.php new file mode 100644 index 0000000000..3014d96742 --- /dev/null +++ b/lib/ajax/simpletest/testajaxlib.php @@ -0,0 +1,61 @@ +libdir . '/ajax/ajaxlib.php'); + +/** + * Unit tests of mathslib wrapper and underlying EvalMath library. + * + * @author Petr Skoda (skodak) + * @version $Id$ + */ +class ajaxlib_test extends MoodleUnitTestCase { + function test_ajax_get_lib() { + global $CFG; + $cases = array( + 'yui_yahoo' => $CFG->wwwroot . '/lib/yui/yahoo/yahoo-min.js', + 'lib/javascript-static.js' => $CFG->wwwroot . '/lib/javascript-static.js', + $CFG->wwwroot . '/lib/javascript-static.js' => $CFG->wwwroot . '/lib/javascript-static.js', + ); + foreach ($cases as $arg => $result) { + $this->assertEqual(ajax_get_lib($arg), $result); + } + $this->expectException(); + ajax_get_lib('a_file_that_does_not_exist.js'); + } +} + +?> \ No newline at end of file diff --git a/lib/weblib.php b/lib/weblib.php index 97d31df383..3b2d44ab1e 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2004,7 +2004,6 @@ function replace_smilies(&$text) { } } - if (empty($img[$lang])) { /// After the first time this is not run again $e[$lang] = array(); $img[$lang] = array(); @@ -2554,28 +2553,30 @@ function print_header ($title='', $heading='', $navigation='', $focus='', * Used to include JavaScript libraries. * * When the $lib parameter is given, the function will ensure that the - * named library is loaded onto the page - either in the HTML , - * just after the header, or at an arbitrary later point in the page, - * depending on where this function is called. + * named library or libraries is loaded onto the page - either in the + * HTML , just after the header, or at an arbitrary later point in + * the page, depending on where this function is called. * * Libraries will not be included more than once, so this works like * require_once in PHP. * - * There are two special-case calls to this function which are both used only - * by weblib print_header: + * There are two special-case calls to this function from print_header which are + * internal to weblib and use the second $extracthtmlparameter: * $extracthtml = 1: this is used before printing the header. - * It returns the script tag code that should go inside the . + * It returns the script tag code that should go inside the . * $extracthtml = 2: this is used after printing the header and handles any - * require_js calls that occurred within the header itself. - * - * @param mixed $lib - string or array of strings - * string(s) should be the shortname for the library or the - * full URL (which will probably start with $CFG->wwwroot) to the library file. - * @param int $extracthtml Do not set this parameter usually (leave 0), only - * weblib should set this to 1 or 2 in print_header function. - * @return mixed No return value, except when using $extracthtml it returns the html code. - */ -function require_js($lib,$extracthtml=0) { + * require_js calls that occurred within the header itself. + * + * @param mixed $lib The library or libraries to load (a string or array of strings) + * There are three way to specify the library: + * 1. a shorname like 'yui_yahoo'. The list of recognised values is in lib/ajax/ajaxlib.php + * 2. the path to the library relative to wwwroot, for example 'lib/javascript-static.js' + * 3. (legacy) a full URL like $CFG->wwwroot . '/lib/javascript-static.js'. + * @param int $extracthtml Private. For internal weblib use only. + * @return mixed No return value (except when doing the internal $extracthtml + * calls, when it returns html code). + */ +function require_js($lib, $extracthtml = 0) { global $CFG; static $loadlibs = array(); -- 2.39.5