Skip to content

Commit

Permalink
fix($compile): make order directives w/ same priority deterministic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IgorMinar committed Oct 12, 2013
1 parent d652c31 commit 8231444
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit 8231444

Please sign in to comment.