From 77774f6a83bc632522de23d33d9708b878ee5a6d Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Mon, 18 Jan 2010 20:17:43 +0000 Subject: [PATCH] MDL-21240 move script tag generation into html_writer + improving api a bit --- lib/ajax/ajaxlib.php | 46 +++++++++++++---------------- lib/ajax/simpletest/testajaxlib.php | 13 -------- lib/outputcomponents.php | 14 +++++++++ 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index 9aa970192c2..6508c5bec09 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -186,7 +186,7 @@ class page_requirements_manager { * @param moodle_page $page * @param core_renderer $output */ - protected function setup_core_javascript(moodle_page $page, core_renderer $output) { + protected function init_requirements_data(moodle_page $page, core_renderer $output) { global $CFG; // JavaScript should always work with $CFG->httpswwwroot rather than $CFG->wwwroot. @@ -685,17 +685,28 @@ class page_requirements_manager { public function get_head_code(moodle_page $page, core_renderer $output) { // note: the $page and $output are not stored here because it would // create circular references in memory which prevents garbage collection - $this->setup_core_javascript($page, $output); + $this->init_requirements_data($page, $output); + // yui3 JS and CSS is always loaded first - it is cached in browser $output = $this->get_yui3lib_headcode(); - // BC: load basic YUI2 for now, yui2 things should be loaded as yui3 modules + + // BC: load basic YUI2 for now, all yui2 things should be loaded via Y.use('yui2-oldmodulename') $output .= $this->get_yui2lib_code(); - // now theme CSS, it must be loaded before the + + // now theme CSS + custom CSS in this specific order $output .= $this->get_css_code(); + + // all the other linked things from HEAD - there should be as few as possible + // because we need to minimise number of http requests, + // all core stuff should go into /lib/javascript-static.js $output .= $this->get_linked_resources_code(self::WHEN_IN_HEAD); - $js = $this->get_javascript_code(self::WHEN_IN_HEAD); - $output .= ajax_generate_script_tag($js); + + // finally all JS that should go directly into head tag - mostly global config + $output .= html_writer::script($this->get_javascript_code(self::WHEN_IN_HEAD)); + + // mark head sending done, it is not possible to anything there $this->headdone = true; + return $output; } @@ -711,7 +722,7 @@ class page_requirements_manager { $output = ''; $js = "document.body.className += ' jsenabled';\n"; $js .= $this->get_javascript_code(self::WHEN_TOP_OF_BODY); - $output .= ajax_generate_script_tag($js); + $output .= html_writer::script($js); $this->topofbodydone = true; return $output; } @@ -756,7 +767,7 @@ class page_requirements_manager { }); EOD; - $output .= ajax_generate_script_tag($js); + $output .= html_writer::script($js); return $output; } @@ -1067,8 +1078,7 @@ abstract class required_js_code extends requirement_base { if ($this->is_done()) { return ''; } - $js = $this->get_js_code(); - $output = ajax_generate_script_tag($js); + $output = html_writer::script($this->get_js_code()); $this->mark_done(); return $output; } @@ -1266,22 +1276,6 @@ class required_event_handler extends required_js_code { } } -/** - * Generate a script tag containing the the specified code. - * - * @param string $js the JavaScript code - * @return string HTML, the code wrapped in \n"; - } else { - return ''; - } -} - - /** * Return the HTML required to link to a JavaScript file. * @param $url the URL of a JavaScript file. diff --git a/lib/ajax/simpletest/testajaxlib.php b/lib/ajax/simpletest/testajaxlib.php index 4b1047910d4..cd32a602260 100644 --- a/lib/ajax/simpletest/testajaxlib.php +++ b/lib/ajax/simpletest/testajaxlib.php @@ -384,19 +384,6 @@ class page_requirements_manager_test extends ajaxlib_unit_test_base { */ class ajax_test extends ajaxlib_unit_test_base { - function test_ajax_generate_script_tag() { - $html = ajax_generate_script_tag('var x = 1;'); - $this->assertContains($html, ''); - } - - function test_ajax_generate_script_tag_no_output_when_no_code() { - $html = ajax_generate_script_tag(''); - $this->assertEqual($html, ''); - } - var $user_agents = array( 'MSIE' => array( '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'), diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index aeb141e44df..28be41f1bee 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -536,6 +536,20 @@ class html_writer { } return $output; } + + /** + * Generate a script tag containing the the specified code. + * + * @param string $js the JavaScript code + * @return string HTML, the code wrapped in \n"; + } else { + return ''; + } + } }