Skip to content

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
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/commons/dom/get-root-vnodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { nodeLookup } from '../../core/utils';

/**
* Return the vNode(s)
* @method getRootVNodes
* @memberof axe.commons.dom
* @instance
* @param {Element|VirtualNode} node
* @returns {VirtualNode[]}
*/
export default function getRootVNodes(node) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming-wise, I'd suggest something like getRootChildren instead - getRootVNodes makes me think it'd be returning a virtual document fragment node, since that's the "root" as the DOM uses that term, but like you commented down on L28, we don't track vnodes for those document fragments 😞

const { vNode } = nodeLookup(node);

if (vNode._rootNodes) {
return vNode._rootNodes;
}

// top of tree
if (vNode.parent === null) {
return [vNode];
}

// disconnected tree
if (!vNode.parent) {
return undefined;
}

// 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
if (vNode.shadowId !== vNode.parent.shadowId) {
return [...vNode.parent.children];
}

vNode._rootNodes = getRootVNodes(vNode.parent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than needing to create new properties on every vnode, would it be lower overhead cache a map from shadowId to root children?

return vNode._rootNodes;
}
52 changes: 37 additions & 15 deletions lib/commons/dom/idrefs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import getRootNode from './get-root-node';
import { tokenList } from '../../core/utils';
import getRootVNodes from './get-root-vnodes';
import {
tokenList,
nodeLookup,
querySelectorAll,
getRootNode
} from '../../core/utils';

/**
* Get elements referenced via a space-separated token attribute;
Expand All @@ -18,24 +23,41 @@ import { tokenList } from '../../core/utils';
*
*/
function idrefs(node, attr) {
node = node.actualNode || node;
const { domNode, vNode } = nodeLookup(node);
const results = [];
const attrValue = vNode ? vNode.attr(attr) : node.getAttribute(attr);

if (!attrValue) {
return results;
}

try {
const doc = getRootNode(node);
const result = [];
let attrValue = node.getAttribute(attr);

if (attrValue) {
attrValue = tokenList(attrValue);
for (let index = 0; index < attrValue.length; index++) {
result.push(doc.getElementById(attrValue[index]));
}
const root = getRootNode(domNode);
for (const token of tokenList(attrValue)) {
results.push(root.getElementById(token));
}

return result;
} catch {
throw new TypeError('Cannot resolve id references for non-DOM nodes');
const rootVNodes = getRootVNodes(vNode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is closer than previous attempts, but I think it's going to do the wrong thing with slotted elements; those appear in the virtual tree under the shadowId of the thing they're slotted into, but for idrefs purposes their id counts as being scoped to the root in which they're defined, not the one they're slotted into:

  <!-- Behavior is consistent in current chrome/firefox/safari -->
  <label for="slotted-input-a">out-of-shadow label</label>
  <my-custom-elm>
    <template shadowrootmode="open">
      <label for="slotted-input-a">in-shadow label competing with out-of-shadow label</label>
      <label for="slotted-input-b">in-shadow-only label</label>
      <slot></slot>
    </template>
    <!-- Accessible name is "out-of-shadow label" -->
    <input id="slotted-input-a"></input>
    <!-- Accessible name is empty -->
    <input id="slotted-input-b"></input>
  </my-custom-elm>

I'm not sure there's enough information in the virtual tree right now to distinguish that case correctly; it might be necessary for us to start tracking information about the "source shadow ID" or something for slotted elements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When thinking what to track, consider also this test case:

  <label for="slotted-input">out-of-shadow label</label>
  <level-a-custom-elm>
    <template shadowrootmode="open">
      <label for="slotted-input">shadow level A label</label>
      <level-b-custom-elm>
        <template shadowrootmode="open">
          <label for="slotted-input">shadow level B label</label>
          <slot></slot>
        </template>
        <slot></slot>
      </level-b-custom-elm>
    </template>
    <!-- Accessible name is "out-of-shadow label" -->
    <input id="slotted-input"></input>
  </level-a-custom-elm>

if (!rootVNodes) {
throw new TypeError('Cannot resolve id references for non-DOM nodes');
}

for (const token of tokenList(attrValue)) {
let result = null;

for (const root of rootVNodes) {
const foundNode = querySelectorAll(root, `#${token}`)[0];
Copy link
Contributor

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, but getNodesMatchingExpression is going to early exit from the cached path anytime root is not the document root)

if (foundNode) {
result = foundNode;
break;
}
}

results.push(result);
}
}

return results;
}

export default idrefs;
1 change: 1 addition & 0 deletions lib/commons/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { default as getElementStack } from './get-element-stack';
export { default as getModalDialog } from './get-modal-dialog';
export { default as getOverflowHiddenAncestors } from './get-overflow-hidden-ancestors';
export { default as getRootNode } from './get-root-node';
export { default as getRootVNodes } from './get-root-vnodes';
export { default as getScrollOffset } from './get-scroll-offset';
export { default as getTabbableElements } from './get-tabbable-elements';
export { default as getTargetRects } from './get-target-rects';
Expand Down
6 changes: 3 additions & 3 deletions lib/commons/text/accessible-text.js
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()
Expand All @@ -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
Copy link
Contributor Author

@straker straker Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not true as getNodeFromTree does not throw, just returns null if the node isn't found.

return accessibleTextVirtual(virtualNode, context);
const { vNode } = nodeLookup(element);
return accessibleTextVirtual(vNode, context);
}

export default accessibleText;
27 changes: 27 additions & 0 deletions test/commons/dom/get-root-vnodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe('dom.getRootVNodes', () => {
const getRootVNodes = axe.commons.dom.getRootVNodes;
const fixture = document.querySelector('#fixture');
const queryShadowFixture = axe.testUtils.queryShadowFixture;

it('should return the root vNode of complete tree', () => {
axe.setup();
const expected = [axe.utils.getNodeFromTree(document.documentElement)];
assert.deepEqual(getRootVNodes(fixture), expected);
});

it('should return undefined for disconnected tree', () => {
axe.setup();
axe.utils.getNodeFromTree(document.documentElement).parent = undefined;
assert.isUndefined(getRootVNodes(fixture));
});

it('should return each child of a shadow DOM host', () => {
const target = queryShadowFixture(
'<div id="shadow"></div>',
'<div id="target">Hello World</div><div id="child1"></div><div id="child2"></div>'
);

const expected = target.parent.children;
assert.deepEqual(getRootVNodes(target), expected);
});
});
Loading