Skip to content

Commit

Permalink
MDL-21240 move script tag generation into html_writer + improving api…
Browse files Browse the repository at this point in the history
… a bit
  • Loading branch information
skodak committed Jan 18, 2010
1 parent c046747 commit 77774f6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
46 changes: 20 additions & 26 deletions lib/ajax/ajaxlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function add_yui2_modules() {
* @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.
Expand Down Expand Up @@ -685,17 +685,28 @@ protected function get_css_code() {
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;
}

Expand All @@ -711,7 +722,7 @@ public function get_top_of_body_code() {
$output = '<div class="skiplinks">' . $this->get_linked_resources_code(self::WHEN_TOP_OF_BODY) . '</div>';
$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;
}
Expand Down Expand Up @@ -756,7 +767,7 @@ public function get_end_code() {
});
EOD;

$output .= ajax_generate_script_tag($js);
$output .= html_writer::script($js);

return $output;
}
Expand Down Expand Up @@ -1067,8 +1078,7 @@ public function now() {
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;
}
Expand Down Expand Up @@ -1266,22 +1276,6 @@ public function get_js_code() {
}
}

/**
* Generate a script tag containing the the specified code.
*
* @param string $js the JavaScript code
* @return string HTML, the code wrapped in <script> tags.
*/
function ajax_generate_script_tag($js) {
if ($js) {
return '<script type="text/javascript">' . "\n//<![CDATA[\n" .
$js . "//]]>\n</script>\n";
} else {
return '';
}
}


/**
* Return the HTML required to link to a JavaScript file.
* @param $url the URL of a JavaScript file.
Expand Down
13 changes: 0 additions & 13 deletions lib/ajax/simpletest/testajaxlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,6 @@ public function test_requiring_js_function_call_on_dom_ready() {
*/
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, '<script type="text/javascript">');
$this->assertContains($html, '<![CDATA[');
$this->assertContains($html, ']]>');
$this->assertContains($html, '</script>');
}

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)'),
Expand Down
14 changes: 14 additions & 0 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ public static function input_hidden_params(moodle_url $url, array $exclude = nul
}
return $output;
}

/**
* Generate a script tag containing the the specified code.
*
* @param string $js the JavaScript code
* @return string HTML, the code wrapped in <script> tags.
*/
public static function script($jscode) {
if ($jscode) {
return "<script type=\"text/javascript\">\n//<![CDATA[\n$jscode\n//]]>\n</script>\n";
} else {
return '';
}
}
}


Expand Down

0 comments on commit 77774f6

Please sign in to comment.