From 4357da857587d3c28790e7dc654664bec5808768 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 11 Oct 2013 16:46:32 -0700 Subject: 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. --- src/ng/compile.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/ng/compile.js') diff --git a/src/ng/compile.js b/src/ng/compile.js index ff0ae99d..a13be3b8 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -186,7 +186,7 @@ function $CompileProvider($provide) { $provide.factory(name + Suffix, ['$injector', '$exceptionHandler', function($injector, $exceptionHandler) { var directives = []; - forEach(hasDirectives[name], function(directiveFactory) { + forEach(hasDirectives[name], function(directiveFactory, index) { try { var directive = $injector.invoke(directiveFactory); if (isFunction(directive)) { @@ -195,6 +195,7 @@ function $CompileProvider($provide) { directive.compile = valueFn(directive.link); } directive.priority = directive.priority || 0; + directive.index = index; directive.name = directive.name || name; directive.require = directive.require || (directive.controller && directive.name); directive.restrict = directive.restrict || 'A'; @@ -1277,7 +1278,10 @@ function $CompileProvider($provide) { * Sorting function for bound directives. */ function byPriority(a, b) { - return b.priority - a.priority; + var diff = b.priority - a.priority; + if (diff !== 0) return diff; + if (a.name !== b.name) return (a.name < b.name) ? -1 : 1; + return a.index - b.index; } -- cgit v1.2.3