Skip to content

Commit 7053373

Browse files
committed
suggestions
1 parent a8f1abd commit 7053373

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

lib/commons/aria/get-aria-value.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ export default function getAriaValue(node, attrName, options = {}) {
3535
return null;
3636
}
3737

38+
const { vNode, domNode } = nodeLookup(node);
3839
const { type } = attrStandard;
3940
const { lowercase } = options;
4041

4142
for (const { source, getValue } of sources) {
42-
let value = getValue(node, attrStandard, attrName);
43+
let value = getValue(vNode, domNode, attrStandard, attrName);
4344
if (value === null || value === undefined) {
4445
continue;
4546
}
@@ -68,8 +69,7 @@ export default function getAriaValue(node, attrName, options = {}) {
6869
return null;
6970
}
7071

71-
function getAttributeValue(node, attrStandard, attrName) {
72-
const { vNode, domNode } = nodeLookup(node);
72+
function getAttributeValue(vNode, domNode, attrStandard, attrName) {
7373
const { type } = attrStandard;
7474
const value = vNode ? vNode.attr(attrName) : domNode.getAttribute(attrName);
7575

@@ -85,24 +85,28 @@ function getAttributeValue(node, attrStandard, attrName) {
8585
}
8686

8787
// return null for the attribute if the value is empty but the idref(s) prop is not
88-
const propValue = getPropertyValue(node, attrStandard);
88+
const propValue = getPropertyValue(vNode, domNode, attrStandard);
8989
const propEmpty = Array.isArray(propValue) ? propValue.length : !!propValue;
9090
return propEmpty ? null : value;
9191
}
9292

93-
function getPropertyValue(node, attrStandard) {
94-
const { domNode } = nodeLookup(node);
93+
function getPropertyValue(vNode, domNode, attrStandard) {
9594
const { prop } = attrStandard;
9695
return prop && domNode ? domNode[prop] : null;
9796
}
9897

99-
function getInternalValue(node, attrStandard) {
100-
const { vNode, domNode } = nodeLookup(node);
98+
function getInternalValue(vNode, domNode, attrStandard) {
10199
const { prop } = attrStandard;
102100
if (!prop) {
103101
return null;
104102
}
105103

104+
// feature flag to enable internals. uses globalThis.axe as it can be run outside of axe context
105+
// TODO: remove when feature is fully enabled
106+
if (!axe._enableElementInternals) {
107+
return null;
108+
}
109+
106110
const internals = vNode
107111
? vNode.elementInternals
108112
: getElementInternals(domNode);

lib/commons/aria/has-aria-value.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,37 @@ export default function hasAriaValue(node, attrName) {
1616
throw new TypeError(`Attribute ${attrName} is not an ARIA attribute`);
1717
}
1818

19+
const { vNode, domNode } = nodeLookup(node);
20+
1921
return (
20-
hasAttributeValue(node, attrName) ||
21-
hasPropertyValue(node, attrStandard) ||
22-
hasInternalValue(node, attrStandard)
22+
hasAttributeValue(vNode, domNode, attrName) ||
23+
hasPropertyValue(vNode, domNode, attrStandard) ||
24+
hasInternalValue(vNode, domNode, attrStandard)
2325
);
2426
}
2527

26-
function hasAttributeValue(node, attrName) {
27-
const { vNode, domNode } = nodeLookup(node);
28+
function hasAttributeValue(vNode, domNode, attrName) {
2829
return vNode ? vNode.hasAttr(attrName) : domNode.hasAttribute(attrName);
2930
}
3031

31-
function hasPropertyValue(node, attrStandard) {
32+
function hasPropertyValue(vNode, domNode, attrStandard) {
3233
const { prop } = attrStandard;
33-
const { domNode } = nodeLookup(node);
3434
if (prop && domNode) {
3535
const propValue = domNode[prop];
3636
return propValue !== null && propValue !== undefined;
3737
}
3838
return false;
3939
}
4040

41-
function hasInternalValue(node, attrStandard) {
42-
const { vNode, domNode } = nodeLookup(node);
41+
function hasInternalValue(vNode, domNode, attrStandard) {
4342
const { prop } = attrStandard;
43+
44+
// feature flag to enable internals. uses globalThis.axe as it can be run outside of axe context
45+
// TODO: remove when feature is fully enabled
46+
if (!axe._enableElementInternals) {
47+
return false;
48+
}
49+
4450
const internals = vNode
4551
? vNode.elementInternals
4652
: getElementInternals(domNode);

test/commons/aria/get-aria-value.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ describe('aria.getAriaValue', () => {
33
const getAriaValue = axe.commons.aria.getAriaValue;
44
const SerialVirtualNode = axe.SerialVirtualNode;
55

6+
afterEach(() => {
7+
axe._enableElementInternals = true;
8+
});
9+
610
it('returns the aria attribute value', () => {
711
const vNode = queryFixture(
812
html`<div id="target" aria-label="hello"></div>`
@@ -173,6 +177,7 @@ describe('aria.getAriaValue', () => {
173177
});
174178

175179
it('returns element internals from an element not in the tree', () => {
180+
axe._elemen;
176181
fixture.innerHTML = html`<testutils-element
177182
id="target"
178183
with-aria-label="hello"
@@ -186,6 +191,17 @@ describe('aria.getAriaValue', () => {
186191
});
187192
});
188193

194+
it('does not return element internals from an element not in the tree when feature flag is off', () => {
195+
axe._enableElementInternals = false;
196+
fixture.innerHTML = html`<testutils-element
197+
id="target"
198+
with-aria-label="hello"
199+
></testutils-element>`;
200+
const node = fixture.querySelector('#target');
201+
202+
assert.isNull(getAriaValue(node, 'aria-label'));
203+
});
204+
189205
describe('idref', () => {
190206
it('returns the attribute value over the property value', () => {
191207
const vNode = queryFixture(

test/commons/aria/has-aria-value.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ describe('aria.hasAriaValue', () => {
33
const hasAriaValue = axe.commons.aria.hasAriaValue;
44
const SerialVirtualNode = axe.SerialVirtualNode;
55

6+
afterEach(() => {
7+
axe._enableElementInternals = true;
8+
});
9+
610
it('returns true if element has attribute', () => {
711
const vNode = queryFixture(
812
html`<div id="target" aria-label="hello"></div>`
@@ -95,6 +99,17 @@ describe('aria.hasAriaValue', () => {
9599
assert.isTrue(hasAriaValue(node, 'aria-label'));
96100
});
97101

102+
it('returns false if element not in the tree has elementInternals but feature flag is off', () => {
103+
axe._enableElementInternals = false;
104+
fixture.innerHTML = html`<testutils-element
105+
id="target"
106+
with-aria-label="hello"
107+
></testutils-element>`;
108+
const node = fixture.querySelector('#target');
109+
110+
assert.isFalse(hasAriaValue(node, 'aria-label'));
111+
});
112+
98113
describe('SerialVirtualNode', () => {
99114
it('returns true if element has attribute', () => {
100115
// SerialVirtualNode will not support `props` or `elementInternals` so everything must be part of the `attributes` property

0 commit comments

Comments
 (0)