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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization: Avoid costly getImplicitRole when explicitRole.role exists #1489

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export function getComputedRole(
version: ARIAVersion,
assumeSingleNode = false,
): ComputedRole {
const implicitRole = getImplicitRole(specs, el, version);
let lazyImplicitRole: ComputedRole | undefined;
const explicitRole = getExplicitRole(specs, el, version);
const computedRole = explicitRole.role
? explicitRole
: {
...implicitRole,
...(lazyImplicitRole = getImplicitRole(specs, el, version)),
errorType: explicitRole.errorType === 'NO_EXPLICIT' ? undefined : explicitRole.errorType,
};

Expand Down Expand Up @@ -145,6 +145,8 @@ export function getComputedRole(
return computedRole;
}

const implicitRole = lazyImplicitRole ?? getImplicitRole(specs, el, version);

/**
* > If an element is focusable,
* > user agents MUST ignore the presentation/none role
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ function getClosestNonPresentationalDescendants(
): ComputedRole[] {
const owned: ComputedRole[] = [];
for (const child of el.children) {
const implicitRole = getImplicitRole(specs, child, version);
const explicitRole = getExplicitRole(specs, child, version);
const computed = explicitRole.role ? explicitRole : implicitRole;
const computed = explicitRole.role ? explicitRole : getImplicitRole(specs, child, version);
if (isPresentational(computed.role?.name)) {
owned.push(...getClosestNonPresentationalDescendants(child, specs, version));
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { getImplicitRoleName } from '@markuplint/ml-spec';
export const checkingImplicitRole: AttrChecker<boolean, Options> =
({ attr }) =>
t => {
const tokens = attr.tokenList?.allTokens();
if (!tokens) {
return;
}
const implicitRole = getImplicitRoleName(
attr.ownerElement,
attr.rule.options.version,
attr.ownerMLDocument.specs,
);
const tokens = attr.tokenList?.allTokens();
if (!tokens) {
return;
}
for (const token of tokens) {
if (implicitRole === token.raw) {
return {
Expand Down