Skip to content

Commit

Permalink
feat: fieldset check shadow DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers authored and marcysutton committed Jul 21, 2017
1 parent e2a9642 commit da148d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
19 changes: 13 additions & 6 deletions lib/checks/forms/fieldset.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,28 @@ function spliceCurrentNode(nodes, current) {
}

function runCheck(element) {
var name = axe.commons.utils.escapeSelector(node.name);
var matchingNodes = document.querySelectorAll('input[type="' +
const name = axe.commons.utils.escapeSelector(node.name);
const root = axe.commons.dom.getRootNode(node);
const matchingNodes = root.querySelectorAll('input[type="' +
axe.commons.utils.escapeSelector(node.type) + '"][name="' + name + '"]');

if (matchingNodes.length < 2) {
return true;
}
var fieldset = axe.commons.dom.findUp(element, 'fieldset');
var group = axe.commons.dom.findUp(element, '[role="group"]' + (node.type === 'radio' ? ',[role="radiogroup"]' : ''));
const fieldset = axe.commons.dom.findUp(element, 'fieldset');
const group = axe.commons.dom.findUp(element, '[role="group"]' +
(node.type === 'radio' ? ',[role="radiogroup"]' : ''));

if (!group && !fieldset) {
failureCode = 'no-group';
self.relatedNodes(spliceCurrentNode(matchingNodes, element));
return false;
}
return fieldset ? checkFieldset(fieldset, name) : checkARIAGroup(group, name);

} else if (fieldset) {
return checkFieldset(fieldset, name);
} else {
return checkARIAGroup(group, name);
}
}

var data = {
Expand Down
36 changes: 33 additions & 3 deletions test/checks/forms/fieldset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
describe('fieldset', function () {
'use strict';
var fixture = document.getElementById('fixture');
var shadowSupport = axe.testUtils.shadowSupport;
var fixtureSetup = axe.testUtils.fixtureSetup;

var checkContext = {
_data: null,
data: function (d) {
Expand Down Expand Up @@ -158,7 +160,6 @@ describe('fieldset', function () {
});
});


it('should return true if a properly labelled-by ARIA group contains only the right elements - special characters', function () {
fixtureSetup('<div id="grouplabel">Label</div>' +
'<div role="group" aria-labelledby="grouplabel">' +
Expand All @@ -182,15 +183,13 @@ describe('fieldset', function () {
assert.isTrue(checks.fieldset.evaluate.call(checkContext, node));
});


it('should ignore hidden inputs', function () {
fixtureSetup('<fieldset><legend>Legendary</legend>' +
'<input type="' + type + '" id="target" name="uniqueyname">' +
'<input type="' + type + '" name="uniqueyname"></div>' +
'<input type="hidden" name="things"></fieldset>');
var node = fixture.querySelector('#target');
assert.isTrue(checks.fieldset.evaluate.call(checkContext, node));

});

it('should allow elements to be contained in 2 or more fieldsets', function () {
Expand Down Expand Up @@ -218,6 +217,36 @@ describe('fieldset', function () {
var node = fixture.querySelector('#target');
assert.isTrue(checks.fieldset.evaluate.call(checkContext, node));
});

(shadowSupport ? it : xit)('should find fieldsets outside a shadow tree', function () {
var fieldset = document.createElement('fieldset');
fieldset.innerHTML = '<legend>Legendary</legend> <div></div>';

var div = fieldset.querySelector('div');
var shadow = div.attachShadow({ mode: 'open' });
shadow.innerHTML =
'<input type="' + type + '" id="target" name="sharedname">' +
'<input type="' + type + '" name="sharedname">';
fixtureSetup(fieldset);

var node = shadow.querySelector('#target');
assert.isTrue(checks.fieldset.evaluate.call(checkContext, node));
});

(shadowSupport ? it : xit)('should find fieldsets inside a shadow tree', function () {
var div = document.createElement('div');
div.innerHTML =
'<input type="' + type + '" id="target" name="sharedname">' +
'<input type="' + type + '" name="sharedname">';

var shadow = div.attachShadow({ mode: 'open' });
shadow.innerHTML = '<fieldset><legend>Legendary</legend>' +
'<slot></slot></fieldset>';
fixtureSetup(div);

var node = div.querySelector('#target');
assert.isTrue(checks.fieldset.evaluate.call(checkContext, node));
});
}


Expand Down Expand Up @@ -249,4 +278,5 @@ describe('fieldset', function () {
assert.isFalse(checks.fieldset.evaluate.call(checkContext, node));
});
});

});

0 comments on commit da148d3

Please sign in to comment.