Download & Extend

JS files of the same weight are not included in the order of the calls

Project:Drupal core
Version:7.x-dev
Component:base system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)
Issue tags:JavaScript, weight

Issue Summary

#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.

Comments

#1

Status:active» needs review

And here is a patch.

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

AttachmentSizeStatusTest resultOperations
505528-js-files-wrong-order.patch886 bytesIdlePassed: 11567 passes, 0 fails, 0 exceptionsView details

#2

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 ?

#3

Status:needs review» reviewed & tested by the community

Thanks DamZ! Good find.

#4

Status:reviewed & tested by the community» needs work

Tests, please!

#5

Status:needs work» needs review
Issue tags:-Needs tests+JavaScript, +weight

Regex FTW.

AttachmentSizeStatusTest resultOperations
505528.patch3.15 KBIdlePassed: 11567 passes, 0 fails, 0 exceptionsView details

#6

Status:needs review» reviewed & tested by the community

Deal.

#7

Status:reviewed & tested by the community» fixed

Thanks, committed to HEAD!

#8

Status:fixed» closed (fixed)

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