#315798: JavaScript Patch #2: Weight added weights to drupal_add_js() included files, but now files of the same weight can be included in a different order then the order of the calls. This is counter intuitive and we need to fix that.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Status: Active » Needs review
FileSize
886 bytes

And here is a patch.

Of course, we need a test... anyone?

TheRec’s picture

Test, it works as advertised. I used the tests described in #315798: JavaScript Patch #2: Weight by Rob Loach (comment #6)..

<?php
drupal_add_js('(function($){alert("Weight 25 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 25));
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 -50 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -50));
drupal_add_js('(function($){alert("Weight -50 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -50));
drupal_add_js('(function($){alert("Weight -50 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -50));
drupal_add_js('(function($){alert("Weight -50 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -50));
drupal_add_js('(function($){alert("Weight 25 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 25));
drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
?>

Alerts are showing in this order :

Weight -50 #1
Weight -50 #2
Weight -50 #3
Weight -50 #4
Weight 0 #1
Weight 0 #2
Weight 0 #3
Weight 25 #1
Weight 25 #2

Which is the expected order. I guess we do not need to test all the other scopes and types since they use the same logic for weight, but if needed just ask and maybe give samples of what should be tested ?

RobLoach’s picture

Status: Needs review » Reviewed & tested by the community

Thanks DamZ! Good find.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Tests, please!

RobLoach’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +JavaScript, +weight
FileSize
3.15 KB

Regex FTW.

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Deal.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed to HEAD!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

donquixote’s picture

The micro-weight does not work in some cases.

Follow-up in
#1670450: drupal_get_js() stable sort / drupal_add_js automatic weight