From cb6492df33173e820a9c7021f99cf45c5b0d527c Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Sun, 25 Nov 2018 22:01:51 +0100 Subject: [PATCH] fix(commons): Allow any node in aria.getRole --- lib/commons/aria/get-role.js | 4 ++++ test/commons/aria/get-role.js | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/commons/aria/get-role.js b/lib/commons/aria/get-role.js index 1ab0bae5d8..a828253f5c 100644 --- a/lib/commons/aria/get-role.js +++ b/lib/commons/aria/get-role.js @@ -18,6 +18,10 @@ aria.getRole = function getRole( node, { noImplicit, fallback, abstracts, dpub } = {} ) { + node = node.actualNode || node; + if (node.nodeType !== 1) { + return null; + } const roleAttr = (node.getAttribute('role') || '').trim().toLowerCase(); const roleList = fallback ? axe.utils.tokenList(roleAttr) : [roleAttr]; diff --git a/test/commons/aria/get-role.js b/test/commons/aria/get-role.js index 68a32ed2bf..83a1180801 100644 --- a/test/commons/aria/get-role.js +++ b/test/commons/aria/get-role.js @@ -53,6 +53,17 @@ describe('aria.getRole', function() { assert.isNull(aria.getRole(node)); }); + it('accepts virtualNode objects', function() { + var node = document.createElement('div'); + node.setAttribute('role', 'button'); + assert.equal(aria.getRole({ actualNode: node }), 'button'); + }); + + it('returns null if the node is not an element', function() { + var node = document.createTextNode('foo bar baz'); + assert.isNull(aria.getRole(node)); + }); + describe('noImplicit', function() { it('returns the implicit role by default', function() { var node = document.createElement('li');