-
Notifications
You must be signed in to change notification settings - Fork 920
feat(matches): add inSectioningContent, hasChild, and isSummaryForDetails matches #5262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
456c051
feat(matches): add hasAncestor, hasDescendant, and firstOfType matches
straker c7745db
Merge branch 'develop' into matches
straker c6416e8
move to summaryForDetails
straker 742166e
name is isSummaryForDetails
straker b5d9d14
alpha order
straker 9c7c7ec
fix typo
straker ed730d7
change hasAncestor to inSectioningContent
straker 7a67d68
forgot test
straker 97c339e
suggetsions
straker 2979f4c
add test
straker fa8ed5c
comment
straker ceafb7e
find and replce fail
straker ef121b9
undo
straker 217149c
Update lib/commons/matches/is-summary-for-details.js
straker bd998c6
throw more
straker d571aed
Merge branch 'matches' of https://github.com/dequelabs/axe-core into …
straker d9a22b3
Merge branch 'develop' into matches
straker 5a86146
suggestions
straker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { matches } from '../../core/utils'; | ||
|
|
||
| /** | ||
| * Check if a virtual node has a direct child that matches the selector | ||
| * | ||
| * Note: matches.hasChild(vNode, selector) can be indirectly used through | ||
| * matches(vNode, { hasChild: selector }) | ||
| * | ||
| * Example: | ||
| * ```js | ||
| * matches.hasChild(vNode, 'main'); | ||
| * matches.hasChild(vNode, 'button:not([role])'); | ||
| * ``` | ||
| * | ||
| * @param {VirtualNode} vNode | ||
| * @param {String} selector | ||
| * @returns {Boolean} | ||
| */ | ||
| export default function hasChild(vNode, selector) { | ||
| return vNode.children.some(node => matches(node, selector)); | ||
| } | ||
|
straker marked this conversation as resolved.
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import getElementsByContentType from '../standards/get-elements-by-content-type'; | ||
| import cache from '../../core/base/cache'; | ||
| import getExplicitRole from '../aria/get-explicit-role'; | ||
| import { hasConflictResolution } from '../aria/get-role'; | ||
| import fromPrimative from './from-primative'; | ||
|
|
||
| // sectioning roles are not the same as landmark roles so this is a hard coded list | ||
| const sectioningRoles = [ | ||
| 'article', | ||
| 'complementary', | ||
| 'main', | ||
| 'navigation', | ||
| 'region' | ||
| ]; | ||
|
|
||
| /** | ||
| * Check if a virtual node is a descendant of sectioning content | ||
| * | ||
| * Note: matches.inSectioningContent(vNode) can be indirectly used through | ||
| * matches(vNode, { inSectioningContent: boolean }) | ||
| * | ||
| * Example: | ||
| * ```js | ||
| * matches.inSectioningContent(vNode, true); | ||
| * matches.inSectioningContent(vNode, false); | ||
| * ``` | ||
| * | ||
| * @param {VirtualNode} vNode | ||
| * @param {Object} matcher | ||
| * @returns {Boolean} | ||
| */ | ||
| export default function inSectioningContent(vNode, matcher) { | ||
| // @see https://html.spec.whatwg.org/multipage/dom.html#sectioning-content | ||
| // main is not considered sectioning content so we need to add it | ||
| const sectioningElms = cache.get('sectioningElms', () => | ||
| getElementsByContentType('sectioning').concat('main') | ||
| ); | ||
|
|
||
| // the top node of the tree will have parent === null, so a undefined parent means | ||
| // we are in a disconnected tree | ||
| if (typeof vNode.parent === 'undefined') { | ||
| throw new TypeError('Cannot resolve parent for non-DOM nodes'); | ||
| } | ||
|
|
||
| vNode = vNode.parent; | ||
| while (vNode) { | ||
|
straker marked this conversation as resolved.
|
||
| const { nodeName } = vNode.props; | ||
|
|
||
| /* | ||
| avoid calling into getRole (for now) in order to avoid an infinite loop of | ||
| calling into the html-elms spec. for example, | ||
| <section aria-labelledby="foo"><header id="foo"></header></section>, if using | ||
| getRole, would trigger looking at the accessible name through ariaLabelledby, | ||
| which would then look at the header which would try to get the role and see it | ||
| needs to be in sectioning content (when implemented), which would then loop to | ||
| checking the accessible name of the parent section, ad infinitum. this is solved | ||
| by the change suggested in https://github.com/dequelabs/axe-core/issues/5263. | ||
|
|
||
| unfortunately this means we need to handle element internals and conflict resolution | ||
| ourselves | ||
| */ | ||
| let role = getExplicitRole(vNode); | ||
| if ( | ||
| ['presentation', 'none'].includes(role) && | ||
| hasConflictResolution(vNode) | ||
| ) { | ||
| role = null; | ||
| } | ||
| if (!role && vNode.elementInternals?.role) { | ||
| role = vNode.elementInternals.role; | ||
| } | ||
|
|
||
| if ( | ||
| (!role && sectioningElms.includes(nodeName)) || | ||
| sectioningRoles.includes(role) | ||
| ) { | ||
| return fromPrimative(true, matcher); | ||
| } | ||
|
|
||
| if (typeof vNode.parent === 'undefined') { | ||
| throw new TypeError('Cannot resolve parent for non-DOM nodes'); | ||
| } | ||
|
|
||
| vNode = vNode.parent; | ||
| } | ||
|
|
||
| return fromPrimative(false, matcher); | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import fromPrimative from './from-primative'; | ||
|
|
||
| /** | ||
| * Check if a virtual node is the first summary child of a details element | ||
| * | ||
| * Note: matches.isSummaryForDetails(vNode) can be indirectly used through | ||
| * matches(vNode, { isSummaryForDetails: boolean }) | ||
| * | ||
| * Example: | ||
| * ```js | ||
| * matches.isSummaryForDetails(vNode, true); | ||
| * matches.isSummaryForDetails(vNode, false); | ||
| * ``` | ||
| * | ||
| * @param {VirtualNode} vNode | ||
| * @param {Object} matcher | ||
| * @returns {Boolean} | ||
| */ | ||
| export default function isSummaryForDetails(vNode, matcher) { | ||
| // the top node of the tree will have parent === null, so a undefined parent means | ||
| // we are in a disconnected tree | ||
| if (typeof vNode.parent === 'undefined') { | ||
| throw new TypeError('Cannot resolve parent for non-DOM nodes'); | ||
| } | ||
|
|
||
| if (!vNode.parent || vNode.parent.props.nodeName !== 'details') { | ||
| return fromPrimative(false, matcher); | ||
| } | ||
|
straker marked this conversation as resolved.
|
||
|
|
||
| const firstMatch = vNode.parent.children.find( | ||
| node => node.props.nodeName === 'summary' | ||
| ); | ||
| return fromPrimative(firstMatch === vNode, matcher); | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| describe('matches.hasChild', () => { | ||
| const hasChild = axe.commons.matches.hasChild; | ||
| const { html, queryFixture } = axe.testUtils; | ||
|
|
||
| it('returns true if has child', () => { | ||
| const vNode = queryFixture(html`<div id="target"><span></span></div>`); | ||
| assert.isTrue(hasChild(vNode, 'span')); | ||
| }); | ||
|
|
||
| it('returns true if has child with complex selector', () => { | ||
| const vNode = queryFixture( | ||
| html`<div id="target"> | ||
| <span></span> | ||
| <button></button> | ||
| </div>` | ||
| ); | ||
| assert.isTrue(hasChild(vNode, 'button:not([role])')); | ||
| }); | ||
|
|
||
| it('returns false if child does not match', () => { | ||
| const vNode = queryFixture(html`<div id="target"><span></span></div>`); | ||
| assert.isFalse(hasChild(vNode, 'button')); | ||
| }); | ||
|
|
||
| it('returns false for descendant', () => { | ||
| const vNode = queryFixture( | ||
| html`<div id="target"> | ||
| <div><span></span></div> | ||
| </div>` | ||
| ); | ||
| assert.isFalse(hasChild(vNode, 'span')); | ||
| }); | ||
|
|
||
| it('works with SerialVirtualNode', () => { | ||
| const serialNode = new axe.SerialVirtualNode({ | ||
| nodeName: 'div' | ||
| }); | ||
| const childNode = new axe.SerialVirtualNode({ | ||
| nodeName: 'span' | ||
| }); | ||
|
|
||
| childNode.parent = serialNode; | ||
| serialNode.children = [childNode]; | ||
| assert.isTrue(hasChild(serialNode, 'span')); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.