-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make region check work with shadow DOM
- Loading branch information
1 parent
02daad1
commit ecd222f
Showing
6 changed files
with
179 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,51 @@ | ||
//jshint latedef: false | ||
const { dom, aria } = axe.commons; | ||
function getSkiplink (virtualNode) { | ||
const firstLink = axe.utils.querySelectorAll(virtualNode, 'a[href]')[0]; | ||
if (firstLink && axe.commons.dom.getElementByReference(firstLink.actualNode, 'href')) { | ||
return firstLink.actualNode; | ||
} | ||
} | ||
|
||
var landmarkRoles = axe.commons.aria.getRolesByType('landmark'), | ||
firstLink = node.querySelector('a[href]'); | ||
const skipLink = getSkiplink(virtualNode); | ||
const landmarkRoles = aria.getRolesByType('landmark'); | ||
const implicitLandmarks = landmarkRoles | ||
.reduce((arr, role) => arr.concat(aria.implicitNodes(role)), []) | ||
.filter(r => r !== null).map(r => r.toUpperCase()); | ||
|
||
function isSkipLink(n) { | ||
return firstLink && | ||
axe.commons.dom.isFocusable(axe.commons.dom.getElementByReference(firstLink, 'href')) && | ||
firstLink === n; | ||
// Check if the current element it the skiplink | ||
function isSkipLink (node) { | ||
return skipLink && skipLink === node; | ||
} | ||
|
||
function isLandmark(n) { | ||
var role = n.getAttribute('role'); | ||
return role && (landmarkRoles.indexOf(role) !== -1); | ||
// Check if the current element is a landmark | ||
function isLandmark (node) { | ||
const nodeName = node.nodeName.toUpperCase(); | ||
return (landmarkRoles.includes(node.getAttribute('role')) || | ||
implicitLandmarks.includes(nodeName)); | ||
} | ||
|
||
function checkRegion(n) { | ||
if (isLandmark(n)) { return null; } | ||
if (isSkipLink(n)) { return getViolatingChildren(n); } | ||
if (axe.commons.dom.isVisible(n, true) && | ||
(axe.commons.text.visible(n, true, true) || axe.commons.dom.isVisualContent(n))) { return n; } | ||
return getViolatingChildren(n); | ||
} | ||
function getViolatingChildren(n) { | ||
var children = axe.commons.utils.toArray(n.children); | ||
if (children.length === 0) { return []; } | ||
return children.map(checkRegion) | ||
.filter(function (c) { return c !== null; }) | ||
.reduce(function (a, b) { return a.concat(b); }, []); | ||
/** | ||
* Find all visible elements not wrapped inside a landmark or skiplink | ||
*/ | ||
function findRegionlessElms (virtualNode) { | ||
const node = virtualNode.actualNode; | ||
// End recursion if the element a landmark, skiplink, or hidden content | ||
if (isLandmark(node) || isSkipLink(node) || !dom.isVisible(node, true)) { | ||
return []; | ||
|
||
// Return the node is a content element | ||
} else if (dom.hasContent(node, /* noRecursion: */ true)) { | ||
return [node]; | ||
|
||
// Recursively look at all child elements | ||
} else { | ||
return virtualNode.children.filter(({ actualNode }) => actualNode.nodeType === 1) | ||
.map(findRegionlessElms) | ||
.reduce((a, b) => a.concat(b), []); // flatten the results | ||
} | ||
} | ||
|
||
var v = getViolatingChildren(node); | ||
this.relatedNodes(v); | ||
return !v.length; | ||
var regionlessNodes = findRegionlessElms(virtualNode); | ||
this.relatedNodes(regionlessNodes); | ||
|
||
return regionlessNodes.length === 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
/*global dom */ | ||
|
||
dom.getElementByReference = function (node, attr) { | ||
'use strict'; | ||
|
||
var candidate, | ||
fragment = node.getAttribute(attr), | ||
doc = document; | ||
let fragment = node.getAttribute(attr); | ||
|
||
if (fragment && fragment.charAt(0) === '#') { | ||
fragment = fragment.substring(1); | ||
|
||
candidate = doc.getElementById(fragment); | ||
let candidate = document.getElementById(fragment); | ||
if (candidate) { | ||
return candidate; | ||
} | ||
|
||
candidate = doc.getElementsByName(fragment); | ||
candidate = document.getElementsByName(fragment); | ||
if (candidate.length) { | ||
return candidate[0]; | ||
} | ||
|
||
} | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters