From 48879277f243f86e2c04584ba29482f75c20866c Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Wed, 12 Jul 2017 14:08:53 +0200 Subject: [PATCH] feat: Add findElmsInContext method --- lib/commons/dom/find-elms-in-context.js | 26 ++++++++++++ test/commons/.jshintrc | 1 + test/commons/dom/find-elms-in-context.js | 53 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 lib/commons/dom/find-elms-in-context.js create mode 100644 test/commons/dom/find-elms-in-context.js diff --git a/lib/commons/dom/find-elms-in-context.js b/lib/commons/dom/find-elms-in-context.js new file mode 100644 index 0000000000..a71b659744 --- /dev/null +++ b/lib/commons/dom/find-elms-in-context.js @@ -0,0 +1,26 @@ +/* global axe, dom */ +/** + * Find a elements reference from a given context + * + * @param object { + * context: Node | virtual node Element in the same context + * value: String attribute value to search for + * attr: String attribute name to search for + * elm: String ndoeName to search for (optional) + * } + * @return Array[Node] + */ +dom.findElmsInContext = function ({ context, value, attr, elm = '' }) { + let root; + context = context.actualNode || context; + const escapedValue = axe.utils.escapeSelector(value); + + if (context.nodeType === 9 || context.nodeType === 11) { // It's already root + root = context; + } else { + root = dom.getRootNode(context); + } + return Array.from( + root.querySelectorAll(elm + '[' + attr + '=' + escapedValue + ']') + ); +}; diff --git a/test/commons/.jshintrc b/test/commons/.jshintrc index 1c4d868423..7cca133389 100644 --- a/test/commons/.jshintrc +++ b/test/commons/.jshintrc @@ -3,6 +3,7 @@ "globals": { "describe": true, "it": true, + "xit": true, "before": true, "beforeEach": true, "after": true, diff --git a/test/commons/dom/find-elms-in-context.js b/test/commons/dom/find-elms-in-context.js new file mode 100644 index 0000000000..ca5470b0e8 --- /dev/null +++ b/test/commons/dom/find-elms-in-context.js @@ -0,0 +1,53 @@ +describe('dom.findElmsInContext', function () { + 'use strict'; + + var shadowSupport = axe.testUtils.shadowSupport; + var fixtureSetup = axe.testUtils.fixtureSetup; + var findElmsInContext = axe.commons.dom.findElmsInContext; + + afterEach(function () { + fixtureSetup(''); + }); + + it('returns an array or elements in the same context', function () { + var fixture = fixtureSetup( + '1' + + '2' + + '3' + + '4'); + + assert.deepEqual( + findElmsInContext({ elm: 'b', attr: 'name', value: 'foo', context: fixture }), + Array.from( document.querySelectorAll('b[name=foo]') ) + ); + }); + + (shadowSupport.v1 ? it : xit)('ignores elements inside shadow tree', function () { + var node = document.createElement('div'); + node.innerHTML = '1'; + var shadow = node.attachShadow({ mode: 'open' }); + shadow.innerHTML = '2 '; + var fixture = fixtureSetup(node); + + var result = findElmsInContext({ + elm: 'b', attr: 'name', value: 'foo', context: fixture + }); + assert.lengthOf(result, 1); + assert.equal(result[0].innerText, '1'); + }); + + (shadowSupport.v1 ? it : xit)('can search elements limited to the shadow tree', function () { + var node = document.createElement('div'); + node.innerHTML = '1'; + var shadow = node.attachShadow({ mode: 'open' }); + shadow.innerHTML = '2'; + fixtureSetup(node); + + var result = findElmsInContext({ + elm: 'b', attr: 'name', value: 'foo', context: shadow + }); + + assert.lengthOf(result, 1); + assert.equal(result[0].innerText, '2'); + }); +});