Skip to content

Commit

Permalink
chore: consistent node sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Jun 11, 2021
1 parent 88147ad commit cafc413
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 9 additions & 2 deletions lib/core/utils/merge-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,21 @@ function mergeResults(frameResults, options) {
mergedResult.forEach(result => {
if (result.nodes) {
result.nodes.sort((nodeA, nodeB) => {
return nodeIndexSort(nodeA.node.nodeIndexes, nodeB.node.nodeIndexes);
return nodeIndexSort(
nodeA.node.nodeIndexes,
nodeB.node.nodeIndexes
);
});
}
});
return mergedResult;
}

function nodeIndexSort(nodeIndexesA, nodeIndexesB) {
function nodeIndexSort(nodeIndexesA = [], nodeIndexesB = []) {
if (!Array.isArray(nodeIndexesB) || nodeIndexesB.length === 0 ) {
return -1
}

for (let i = 0; i <= nodeIndexesA?.length; i++) {
const indexA = nodeIndexesA[i];
const indexB = nodeIndexesB?.[i];
Expand Down
16 changes: 8 additions & 8 deletions test/core/utils/merge-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ describe('axe.utils.mergeResults', function() {
});
// Order of "nill" varies between JS engines
assert.deepEqual(ids, [
'nill',
'nill',
'nill',
'h1',
'iframe1 >> h2',
'h3'
'h3',
'nill',
'nill',
'nill'
]);
});

Expand Down Expand Up @@ -327,12 +327,12 @@ describe('axe.utils.mergeResults', function() {
});
// Order of "nill" varies between JS engines
assert.deepEqual(ids, [
'nill',
'nill',
'nill',
'h1',
'iframe1 >> h2',
'h3'
'h3',
'nill',
'nill',
'nill'
]);
});
});

0 comments on commit cafc413

Please sign in to comment.