Skip to content

Commit

Permalink
Fix failing test on Firefox due to non-deterministic attr ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 14, 2015
1 parent 3bc230a commit 3976fd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/js/parsers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ function isEmptyTextNode(node) {
return node.nodeType === TEXT_NODE && trim(node.textContent) === '';
}

// FIXME we need sorted attributes for deterministic tests. This is not
// a particularly elegant method, since it loops at least 3 times.
function sortAttributes(attributes) {
let keyValueAttributes = [];
let currentKey;
attributes.forEach((keyOrValue, index) => {
if (index % 2 === 0) {
currentKey = keyOrValue;
} else {
keyValueAttributes.push({key:currentKey, value:keyOrValue});
}
});
keyValueAttributes.sort((a,b) => {
return a.key === b.key ? 0 :
a.key > b.key ? 1 : - 1;
});
let sortedAttributes = [];
keyValueAttributes.forEach(({key, value}) => {
sortedAttributes.push(key, value);
});
return sortedAttributes;
}

// FIXME: should probably always return an array
function readAttributes(node) {
var attributes = null;
Expand All @@ -26,9 +49,12 @@ function readAttributes(node) {
}
if (attributes.length === 0) {
return null;
} else {
return sortAttributes(attributes);
}
}
return attributes;

return null;
}

const VALID_MARKER_ELEMENTS = [
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/parsers/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ test('markup: consistent order', (assert) => {
*/

test('attributes', (assert) => {
var href = 'http://google.com';
var rel = 'nofollow';
const post = parser.parse(buildDOM('<p><a href="'+href+'" rel="'+rel+'">Link to google.com</a></p>'));
const href = 'http://google.com';
const rel = 'nofollow';
const post = parser.parse(buildDOM(`<p><a href="${href}" rel="${rel}">Link to google.com</a></p>`));

let expectedFirst = builder.generateSection('P');
expectedFirst.markers.push(builder.generateMarker([
Expand Down

0 comments on commit 3976fd0

Please sign in to comment.