From cd651a0713fa2f4b307cc7fc2be033f8636b40d2 Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Mon, 3 Feb 2020 21:26:05 +0100 Subject: [PATCH] fix(color-contrast): support IE extension context (#2008) msElementsFromPoint in IE extensions can return null instead of an empty array. --- lib/commons/dom/shadow-elements-from-point.js | 3 ++- test/commons/dom/shadow-elements-from-point.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/commons/dom/shadow-elements-from-point.js b/lib/commons/dom/shadow-elements-from-point.js index 39a0f9535d..168fd0cc0b 100644 --- a/lib/commons/dom/shadow-elements-from-point.js +++ b/lib/commons/dom/shadow-elements-from-point.js @@ -14,7 +14,8 @@ dom.shadowElementsFromPoint = function(nodeX, nodeY, root = document, i = 0) { throw new Error('Infinite loop detected'); } return ( - Array.from(root.elementsFromPoint(nodeX, nodeY)) + // IE can return null when there are no elements + Array.from(root.elementsFromPoint(nodeX, nodeY) || []) // As of Chrome 66, elementFromPoint will return elements from parent trees. // We only want to touch each tree once, so we're filtering out nodes on other trees. .filter(nodes => dom.getRootNode(nodes) === root) diff --git a/test/commons/dom/shadow-elements-from-point.js b/test/commons/dom/shadow-elements-from-point.js index f11a9d860c..ac8af13cb6 100644 --- a/test/commons/dom/shadow-elements-from-point.js +++ b/test/commons/dom/shadow-elements-from-point.js @@ -43,4 +43,17 @@ describe('dom.shadowElementsFromPoint', function() { assert.notInclude(result2, shadowSpan); } ); + + it('does not throw when elementsFromPoints returns null', function() { + var mockDocument = { + elementsFromPoint: function() { + return null; + } + }; + var out; + assert.doesNotThrow(function() { + out = axe.commons.dom.shadowElementsFromPoint(10, 10, mockDocument); + }); + assert.deepEqual(out, []); + }); });