From 3976fd00004665b1b57eb7b90142d644ed271eb1 Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Tue, 14 Jul 2015 17:28:04 -0400 Subject: [PATCH] Fix failing test on Firefox due to non-deterministic attr ordering --- src/js/parsers/dom.js | 28 +++++++++++++++++++++++++++- tests/unit/parsers/dom-test.js | 6 +++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/js/parsers/dom.js b/src/js/parsers/dom.js index db41f8a88..5db82ac67 100644 --- a/src/js/parsers/dom.js +++ b/src/js/parsers/dom.js @@ -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; @@ -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 = [ diff --git a/tests/unit/parsers/dom-test.js b/tests/unit/parsers/dom-test.js index 42ffcbe07..8ee581a42 100644 --- a/tests/unit/parsers/dom-test.js +++ b/tests/unit/parsers/dom-test.js @@ -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('

Link to google.com

')); + const href = 'http://google.com'; + const rel = 'nofollow'; + const post = parser.parse(buildDOM(`

Link to google.com

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