diff --git a/includes/common.inc b/includes/common.inc index 348390e3377128ca20afc95080709e0d44829729..bc211bfeb8b94b478723f2b0289a4e6ca521eb58 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2793,6 +2793,10 @@ function drupal_add_js($data = NULL, $options = NULL) { // Preprocess can only be set if caching is enabled. $options['preprocess'] = $options['cache'] ? $options['preprocess'] : FALSE; + // Tweak the weight so that files of the same weight are included in the + // order of the calls to drupal_add_js(). + $options['weight'] += count($javascript) / 1000; + if (isset($data)) { // Add jquery.js and drupal.js, as well as the basePath setting, the // first time a Javascript file is added. diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 229b91c2120afb4a1771c19c8f288cf1e372ceaf..1892de1b5f73b61a5b0244f375efffca45a964d8 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -559,6 +559,46 @@ class JavaScriptTestCase extends DrupalWebTestCase { $this->assertEqual($javascript['misc/collapse.js']['weight'], JS_THEME, t('Adding a JavaScript file with a different weight caches the given weight.')); } + /** + * Test JavaScript ordering. + */ + function testRenderOrder() { + // Add a bunch of JavaScript in strange ordering. + drupal_add_js('(function($){alert("Weight 5 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); + drupal_add_js('(function($){alert("Weight 0 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); + drupal_add_js('(function($){alert("Weight 0 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); + drupal_add_js('(function($){alert("Weight -8 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); + drupal_add_js('(function($){alert("Weight -8 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); + drupal_add_js('(function($){alert("Weight -8 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); + drupal_add_js('(function($){alert("Weight -8 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); + drupal_add_js('(function($){alert("Weight 5 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); + drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); + + // Construct the expected result from the regex. + $expected = array( + "-8 #1", + "-8 #2", + "-8 #3", + "-8 #4", + "0 #1", + "0 #2", + "0 #3", + "5 #1", + "5 #2", + ); + + // Retrieve the rendered JavaScript and test against the regex. + $js = drupal_get_js('footer'); + $matches = array(); + if (preg_match_all('/Weight\s([-0-9]+\s[#0-9]+)/', $js, $matches)) { + $result = $matches[1]; + } + else { + $result = array(); + } + $this->assertIdentical($result, $expected, t('JavaScript is added in the expected weight order.')); + } + /** * Test rendering the JavaScript with a file's weight above jQuery's. */