Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: directive sort is not stable on Chrome #3149

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,40 @@ function linkAndCapture (linker, vm) {
var originalDirCount = vm._directives.length
linker()
var dirs = vm._directives.slice(originalDirCount)
dirs.sort(directiveComparator)
sortDirectives(dirs)
for (var i = 0, l = dirs.length; i < l; i++) {
dirs[i]._bind()
}
return dirs
}

/**
* Directive priority sort comparator
* sort directives by priority (stable sort)
*
* @param {Object} a
* @param {Object} b
* @param {Array} dirs
*/
function sortDirectives (dirs) {
if (dirs.length === 0) return

var groupedMap = {}
for (var i = 0, j = dirs.length; i < j; i++) {
var dir = dirs[i]
var priority = dir.descriptor.def.priority || DEFAULT_PRIORITY
var array = groupedMap[priority]
if (!array) {
array = groupedMap[priority] = []
}
array.push(dir)
}

function directiveComparator (a, b) {
a = a.descriptor.def.priority || DEFAULT_PRIORITY
b = b.descriptor.def.priority || DEFAULT_PRIORITY
return a > b ? -1 : a === b ? 0 : 1
var index = 0
Object.keys(groupedMap).sort(function (a, b) {
return a > b ? -1 : a === b ? 0 : 1
}).forEach(function (priority) {
groupedMap[priority].forEach(function (item) {
dirs[index++] = item
})
})
}

/**
Expand Down