-
Notifications
You must be signed in to change notification settings - Fork 839
fix(idref): fallback to qsa #4844
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
Draft
straker
wants to merge
6
commits into
develop
Choose a base branch
from
idref
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { nodeLookup } from '../../core/utils'; | ||
import cache from '../../core/base/cache'; | ||
|
||
/** | ||
* Return the child virtual nodes of the root node | ||
* @method getRootChildren | ||
* @memberof axe.commons.dom | ||
* @instance | ||
* @param {Element|VirtualNode} node | ||
* @returns {VirtualNode[]|undefined} | ||
*/ | ||
export default function getRootChildren(node) { | ||
const { vNode } = nodeLookup(node); | ||
const { shadowId } = vNode; | ||
|
||
const childrenMap = cache.get('getRootChildrenMap', () => ({})); | ||
if (childrenMap[shadowId]) { | ||
return childrenMap[shadowId]; | ||
} | ||
|
||
// top of tree | ||
if (vNode.parent === null) { | ||
childrenMap[shadowId] = [...vNode.children]; | ||
return childrenMap[shadowId]; | ||
} | ||
|
||
// disconnected tree | ||
if (!vNode.parent) { | ||
childrenMap[shadowId] = undefined; | ||
return childrenMap[shadowId]; | ||
} | ||
|
||
// since the virtual tree does not have a #shadowRoot element the root virtual | ||
// node is the shadow host element. however the shadow host element is not inside | ||
// the shadow DOM tree so we return the children of the shadow host element in | ||
// order to not cross shadow DOM boundaries. | ||
// | ||
// TODO: slotted elements share the shadowId of the shadow tree it is attached to | ||
// but should not be used to find id's inside the shadow tree. throw an error | ||
// until we resolve this | ||
if (vNode.shadowId !== vNode.parent.shadowId) { | ||
throw new Error( | ||
'Getting root children of shadow DOM elements is not supported' | ||
); | ||
} | ||
|
||
return getRootChildren(vNode.parent); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import accessibleTextVirtual from './accessible-text-virtual'; | ||
import { getNodeFromTree } from '../../core/utils'; | ||
import { nodeLookup } from '../../core/utils'; | ||
|
||
/** | ||
* Finds virtual node and calls accessibleTextVirtual() | ||
|
@@ -12,8 +12,8 @@ import { getNodeFromTree } from '../../core/utils'; | |
* @return {string} | ||
*/ | ||
function accessibleText(element, context) { | ||
const virtualNode = getNodeFromTree(element); // throws an exception on purpose if axe._tree not correct | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is not true as |
||
return accessibleTextVirtual(virtualNode, context); | ||
const { vNode } = nodeLookup(element); | ||
return accessibleTextVirtual(vNode, context); | ||
} | ||
|
||
export default accessibleText; |
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,30 @@ | ||
describe('dom.getRootChildren', () => { | ||
const getRootChildren = axe.commons.dom.getRootChildren; | ||
const fixture = document.querySelector('#fixture'); | ||
const queryShadowFixture = axe.testUtils.queryShadowFixture; | ||
|
||
it('should return the children of the root node of a complete tree', () => { | ||
axe.setup(); | ||
const expected = axe.utils.getNodeFromTree( | ||
document.documentElement | ||
).children; | ||
assert.deepEqual(getRootChildren(fixture), expected); | ||
}); | ||
|
||
it('should return undefined for disconnected tree', () => { | ||
axe.setup(); | ||
axe.utils.getNodeFromTree(document.documentElement).parent = undefined; | ||
assert.isUndefined(getRootChildren(fixture)); | ||
}); | ||
|
||
it('should throw for shadow DOM', () => { | ||
const target = queryShadowFixture( | ||
'<div id="shadow"></div>', | ||
'<div id="target">Hello World</div><div id="child1"></div><div id="child2"></div>' | ||
); | ||
|
||
assert.throws(() => { | ||
getRootChildren(target); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For shadow root cases, this is going to essentially bypass QSA's selector-cache behavior that would otherwise make the ID lookup fast; we might want to consider making it smarter about that (
findMatchingNodes
looks like it's meant to understand how to do it, butgetNodesMatchingExpression
is going to early exit from the cached path anytimeroot
is not the document root)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya. We originally did the cache on the root to improve rule node selection. We cached the information on the root node since it was the only part that would have a complete picture of the entire tree. We could work around that limit for just id lookups, but it would require somehow passing the desired
shadowId
for thegetNodesMatchingExpression
query instead of grabbing it from the root node. Maybe we create agetElementById
like function that allows you do this this? Something like: