Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4357da8

Browse files
committed
fix($compile): make order directives w/ same priority deterministic
Array.prototype.sort is speced out to be as potentionally unstable sort, which is how it's implemented in FF and IE. This has caused the order of directives with the same priority to vary between browsers. For consistency sake, we now consider directive name and registration, order when determining the order of directives with the same priority. Note: it is still possible to get into a situation when the directive order is underministic - when source files are loaded asynchronously in non-deterministic order and there are are directives registered with the same name and priority, the order in which they will be applied will depend on the file load order.
1 parent 83fbaa5 commit 4357da8

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/ng/compile.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function $CompileProvider($provide) {
186186
$provide.factory(name + Suffix, ['$injector', '$exceptionHandler',
187187
function($injector, $exceptionHandler) {
188188
var directives = [];
189-
forEach(hasDirectives[name], function(directiveFactory) {
189+
forEach(hasDirectives[name], function(directiveFactory, index) {
190190
try {
191191
var directive = $injector.invoke(directiveFactory);
192192
if (isFunction(directive)) {
@@ -195,6 +195,7 @@ function $CompileProvider($provide) {
195195
directive.compile = valueFn(directive.link);
196196
}
197197
directive.priority = directive.priority || 0;
198+
directive.index = index;
198199
directive.name = directive.name || name;
199200
directive.require = directive.require || (directive.controller && directive.name);
200201
directive.restrict = directive.restrict || 'A';
@@ -1277,7 +1278,10 @@ function $CompileProvider($provide) {
12771278
* Sorting function for bound directives.
12781279
*/
12791280
function byPriority(a, b) {
1280-
return b.priority - a.priority;
1281+
var diff = b.priority - a.priority;
1282+
if (diff !== 0) return diff;
1283+
if (a.name !== b.name) return (a.name < b.name) ? -1 : 1;
1284+
return a.index - b.index;
12811285
}
12821286

12831287

0 commit comments

Comments
 (0)