Skip to content
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

refactor: add util function to normalize node argument #3998

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 6 additions & 10 deletions lib/commons/aria/arialabel-text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Get the text value of aria-label, if any
Expand All @@ -8,14 +7,11 @@ import { getNodeFromTree } from '../../core/utils';
* @param {VirtualNode|Element} element
* @return {string} ARIA label
*/
function arialabelText(vNode) {
if (!(vNode instanceof AbstractVirtualNode)) {
if (vNode.nodeType !== 1) {
return '';
}
vNode = getNodeFromTree(vNode);
export default function arialabelText(element) {
const { vNode } = normalizeNode(element);
Copy link
Contributor

Choose a reason for hiding this comment

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

Bummer decorators aren't available yet. I'd love to have this work:

Suggested change
export default function arialabelText(element) {
const { vNode } = normalizeNode(element);
@nodeLookup
export default function arialabelText({ vNode }) {

if (vNode?.props.nodeType !== 1) {
return '';
}

return vNode.attr('aria-label') || '';
}

export default arialabelText;
15 changes: 6 additions & 9 deletions lib/commons/aria/arialabelledby-text.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import idrefs from '../dom/idrefs';
import accessibleText from '../text/accessible-text';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Get the accessible name based on aria-labelledby
*
* @deprecated Do not use Element directly. Pass VirtualNode instead
* @param {VirtualNode} element
* @param {VirtualNode|Element} element
* @param {Object} context
* @property {Bool} inLabelledByContext Whether or not the lookup is part of aria-labelledby reference
* @property {Bool} inControlContext Whether or not the lookup is part of a native label reference
* @property {Element} startNode First node in accessible name computation
* @property {Bool} debug Enable logging for formControlValue
* @return {string} Concatenated text value for referenced elements
*/
function arialabelledbyText(vNode, context = {}) {
if (!(vNode instanceof AbstractVirtualNode)) {
if (vNode.nodeType !== 1) {
return '';
}
vNode = getNodeFromTree(vNode);
function arialabelledbyText(element, context = {}) {
const { vNode } = normalizeNode(element);
if (vNode?.props.nodeType !== 1) {
return '';
}

/**
Expand Down
6 changes: 2 additions & 4 deletions lib/commons/aria/get-element-unallowed-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import isValidRole from './is-valid-role';
import getImplicitRole from './implicit-role';
import getRoleType from './get-role-type';
import isAriaRoleAllowedOnElement from './is-aria-role-allowed-on-element';
import { tokenList, isHtmlElement, getNodeFromTree } from '../../core/utils';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { tokenList, isHtmlElement, normalizeNode } from '../../core/utils';

// dpub roles which are subclassing roles that are implicit on some native
// HTML elements (img, link, etc.)
Expand Down Expand Up @@ -54,8 +53,7 @@ function getRoleSegments(vNode) {
* @return {Array<String>} retruns an array of roles that are not allowed on the given node
*/
function getElementUnallowedRoles(node, allowImplicit = true) {
const vNode =
node instanceof AbstractVirtualNode ? node : getNodeFromTree(node);
const { vNode } = normalizeNode(node);
// by pass custom elements
if (!isHtmlElement(vNode)) {
return [];
Expand Down
6 changes: 2 additions & 4 deletions lib/commons/aria/get-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import getExplicitRole from './get-explicit-role';
import getImplicitRole from './implicit-role';
import getGlobalAriaAttrs from '../standards/get-global-aria-attrs';
import isFocusable from '../dom/is-focusable';
import { getNodeFromTree } from '../../core/utils';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { normalizeNode } from '../../core/utils';

// when an element inherits the presentational role from a parent
// is not defined in the spec, but through testing it seems to be
Expand Down Expand Up @@ -125,8 +124,7 @@ function hasConflictResolution(vNode) {
* @deprecated noImplicit option is deprecated. Use aria.getExplicitRole instead.
*/
function resolveRole(node, { noImplicit, ...roleOptions } = {}) {
const vNode =
node instanceof AbstractVirtualNode ? node : getNodeFromTree(node);
const { vNode } = normalizeNode(node);
if (vNode.props.nodeType !== 1) {
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/focus-disabled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';
import isHiddenForEveryone from './is-hidden-for-everyone';
import isInert from './is-inert';

Expand All @@ -26,7 +25,7 @@ function isDisabledAttrAllowed(nodeName) {
* @return {Boolean} Whether focusing has been disabled on an element.
*/
function focusDisabled(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);
const { vNode } = normalizeNode(el);

if (
(isDisabledAttrAllowed(vNode.props.nodeName) &&
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-focusable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import focusDisabled from './focus-disabled';
import isNativelyFocusable from './is-natively-focusable';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Determines if an element is keyboard or programmatically focusable.
Expand All @@ -12,7 +11,7 @@ import { getNodeFromTree } from '../../core/utils';
* @return {Boolean} The element's focusability status
*/
export default function isFocusable(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);
const { vNode } = normalizeNode(el);

if (vNode.props.nodeType !== 1) {
return false;
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-hidden-for-everyone.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';
import memoize from '../../core/utils/memoize';
import {
nativelyHidden,
Expand Down Expand Up @@ -30,7 +29,7 @@ export default function isHiddenForEveryone(
vNode,
{ skipAncestors, isAncestor = false } = {}
) {
vNode = vNode instanceof AbstractVirtualNode ? vNode : getNodeFromTree(vNode);
vNode = normalizeNode(vNode).vNode;

if (skipAncestors) {
return isHiddenSelf(vNode, isAncestor);
Expand Down
13 changes: 5 additions & 8 deletions lib/commons/dom/is-hidden-with-css.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import getComposedParent from './get-composed-parent';
import { getNodeFromTree } from '../../core/utils';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { normalizeNode } from '../../core/utils';

/**
* Determine whether an element is hidden based on css
Expand All @@ -12,17 +11,15 @@ import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-n
* @return {Boolean} the element's hidden status
* @deprecated use isHiddenForEveryone
*/
function isHiddenWithCSS(node, descendentVisibilityValue) {
const vNode =
node instanceof AbstractVirtualNode ? node : getNodeFromTree(node);
const el = node instanceof window.Node ? node : vNode?.actualNode;
function isHiddenWithCSS(el, descendentVisibilityValue) {
const { vNode, node } = normalizeNode(el);

if (!vNode) {
return _isHiddenWithCSS(el, descendentVisibilityValue);
return _isHiddenWithCSS(node, descendentVisibilityValue);
}

if (vNode._isHiddenWithCSS === void 0) {
vNode._isHiddenWithCSS = _isHiddenWithCSS(el, descendentVisibilityValue);
vNode._isHiddenWithCSS = _isHiddenWithCSS(node, descendentVisibilityValue);
}

return vNode._isHiddenWithCSS;
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-in-tab-order.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';
import isFocusable from './is-focusable';

/**
Expand All @@ -11,7 +10,7 @@ import isFocusable from './is-focusable';
* @return {Boolean} The element's tabindex status
*/
export default function isInTabOrder(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);
const { vNode } = normalizeNode(el);

if (vNode.props.nodeType !== 1) {
return false;
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-natively-focusable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree, querySelectorAll } from '../../core/utils';
import { normalizeNode, querySelectorAll } from '../../core/utils';
import focusDisabled from './focus-disabled';

/**
Expand All @@ -12,7 +11,7 @@ import focusDisabled from './focus-disabled';
* if its tabindex were removed. Else, false.
*/
function isNativelyFocusable(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);
const { vNode } = normalizeNode(el);

if (!vNode || focusDisabled(vNode)) {
return false;
Expand Down
14 changes: 6 additions & 8 deletions lib/commons/dom/is-offscreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getComposedParent from './get-composed-parent';
import getElementCoordinates from './get-element-coordinates';
import getViewportSize from './get-viewport-size';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { normalizeNode } from '../../core/utils';

function noParentScrolled(element, offset) {
element = getComposedParent(element);
Expand Down Expand Up @@ -32,25 +32,23 @@ function isOffscreen(element, { isAncestor } = {}) {
return false;
}

element =
element instanceof AbstractVirtualNode ? element.actualNode : element;

if (!element) {
const { node } = normalizeNode(element);
if (!node) {
return undefined;
}

let leftBoundary;
const docElement = document.documentElement;
const styl = window.getComputedStyle(element);
const styl = window.getComputedStyle(node);
const dir = window
.getComputedStyle(document.body || docElement)
.getPropertyValue('direction');
const coords = getElementCoordinates(element);
const coords = getElementCoordinates(node);

// bottom edge beyond
if (
coords.bottom < 0 &&
(noParentScrolled(element, coords.bottom) || styl.position === 'absolute')
(noParentScrolled(node, coords.bottom) || styl.position === 'absolute')
) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-visible-on-screen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';
import memoize from '../../core/utils/memoize';
import isHiddenForEveryone from './is-hidden-for-everyone';
import {
Expand Down Expand Up @@ -27,7 +26,7 @@ const hiddenMethods = [
* @return {Boolean} True if the element is visible on screen
*/
export default function isVisibleOnScreen(vNode) {
vNode = vNode instanceof AbstractVirtualNode ? vNode : getNodeFromTree(vNode);
vNode = normalizeNode(vNode).vNode;
return isVisibleOnScreenVirtual(vNode);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-visible-to-screenreader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';
import memoize from '../../core/utils/memoize';
import isHiddenForEveryone from './is-hidden-for-everyone';
import { ariaHidden, areaHidden } from './visibility-methods';
Expand All @@ -13,7 +12,7 @@ import isInert from './is-inert';
* @return {Boolean} True if the element is visible to a screen reader
*/
export default function isVisibleToScreenReaders(vNode) {
vNode = vNode instanceof AbstractVirtualNode ? vNode : getNodeFromTree(vNode);
vNode = normalizeNode(vNode).vNode;
return isVisibleToScreenReadersVirtual(vNode);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/commons/dom/is-visual-content.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getNodeFromTree } from '../../core/utils';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { normalizeNode } from '../../core/utils';

const visualRoles = [
'checkbox',
Expand All @@ -22,7 +21,7 @@ const visualRoles = [
* @return {Boolean}
*/
function isVisualContent(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);
const { vNode } = normalizeNode(el);
const role = axe.commons.aria.getExplicitRole(vNode);
if (role) {
return visualRoles.indexOf(role) !== -1;
Expand Down
7 changes: 2 additions & 5 deletions lib/commons/matches/attributes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fromFunction from './from-function';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Check if a virtual node matches some attribute(s)
Expand All @@ -23,9 +22,7 @@ import { getNodeFromTree } from '../../core/utils';
* @returns {Boolean}
*/
function attributes(vNode, matcher) {
if (!(vNode instanceof AbstractVirtualNode)) {
vNode = getNodeFromTree(vNode);
}
vNode = normalizeNode(vNode).vNode;
return fromFunction(attrName => vNode.attr(attrName), matcher);
}

Expand Down
7 changes: 2 additions & 5 deletions lib/commons/matches/from-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import implicitRole from './implicit-role';
import nodeName from './node-name';
import properties from './properties';
import semanticRole from './semantic-role';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree, matches } from '../../core/utils';
import { normalizeNode, matches } from '../../core/utils';

const matchers = {
hasAccessibleName,
Expand Down Expand Up @@ -44,9 +43,7 @@ const matchers = {
* @returns {Boolean}
*/
function fromDefinition(vNode, definition) {
if (!(vNode instanceof AbstractVirtualNode)) {
vNode = getNodeFromTree(vNode);
}
vNode = normalizeNode(vNode).vNode;

if (Array.isArray(definition)) {
return definition.some(definitionItem =>
Expand Down
7 changes: 2 additions & 5 deletions lib/commons/matches/node-name.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fromPrimative from './from-primative';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Check if the nodeName of a virtual node matches some value.
Expand All @@ -20,9 +19,7 @@ import { getNodeFromTree } from '../../core/utils';
* @returns {Boolean}
*/
function nodeName(vNode, matcher) {
if (!(vNode instanceof AbstractVirtualNode)) {
vNode = getNodeFromTree(vNode);
}
vNode = normalizeNode(vNode).vNode;
return fromPrimative(vNode.props.nodeName, matcher);
}

Expand Down
7 changes: 2 additions & 5 deletions lib/commons/matches/properties.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fromFunction from './from-function';
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node';
import { getNodeFromTree } from '../../core/utils';
import { normalizeNode } from '../../core/utils';

/**
* Check if a virtual node matches some attribute(s)
Expand All @@ -23,9 +22,7 @@ import { getNodeFromTree } from '../../core/utils';
* @returns {Boolean}
*/
function properties(vNode, matcher) {
if (!(vNode instanceof AbstractVirtualNode)) {
vNode = getNodeFromTree(vNode);
}
vNode = normalizeNode(vNode).vNode;
return fromFunction(propName => vNode.props[propName], matcher);
}

Expand Down
Loading