@a11y-oracle/audit-formatter

中文文档
1.3.2发布于 2026/03/12MITTypeScriptES Module近 30 天下载 2498

Converts accessibility findings to axe-core-compatible OracleIssue objects with WCAG rule metadata

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Pure Formatter Functions
  4. OracleAuditor Class
  5. Selector Utilities
  6. Rules
  7. API Reference
  8. Formatter Functions
  9. OracleAuditor
  10. OrchestratorLike Interface
  11. Rule Utilities
  12. Selector Utilities
  13. Types
  14. Exports

@a11y-oracle/audit-formatter

Converts A11y-Oracle findings into axe-core-compatible issue objects (OracleIssue). Pure functions with no CDP dependency — works with any A11yState or TraversalResult regardless of how it was produced.

Output is differentiated from axe-core findings by resultType: 'oracle' and oracle/-prefixed rule IDs (e.g., oracle/focus-not-visible).

Installation

npm install @a11y-oracle/audit-formatter

Note: Most users should use the @a11y-oracle/playwright-plugin or @a11y-oracle/cypress-plugin instead. The audit-formatter is the underlying library that those plugins use. Install it directly if you need to format issues outside a test framework or build a custom integration.

Usage

Pure Formatter Functions

The simplest approach — pass an A11yState and get back any issues:

import { formatFocusIssues, formatTrapIssue } from '@a11y-oracle/audit-formatter';
import type { A11yState } from '@a11y-oracle/core-engine';
import type { TraversalResult } from '@a11y-oracle/focus-analyzer';

const context = { project: 'my-app', specName: 'nav.spec.ts' };

// Check a focused element for focus indicator issues
const focusIssues = formatFocusIssues(state, context);
// Returns: [] if passing, or [OracleIssue] if failing

// Check a traversal result for keyboard traps
const trapIssues = formatTrapIssue(result, '#modal-container', context);
// Returns: [] if not trapped, or [OracleIssue] if trapped

// Convenience: run all state-based checks at once
import { formatAllIssues } from '@a11y-oracle/audit-formatter';
const allIssues = formatAllIssues(state, context);

OracleAuditor Class

Wraps an orchestrator and automatically audits every interaction. Issues accumulate across calls:

import { test, expect } from '@a11y-oracle/playwright-plugin';
import { OracleAuditor } from '@a11y-oracle/audit-formatter';

test('all focus indicators pass oracle rules', async ({ page, a11y }) => {
  await page.goto('/my-page.html');

  const auditor = new OracleAuditor(a11y, {
    project: 'my-app',
    specName: 'navigation.spec.ts',
  });

  // Each pressKey() automatically runs all state-based checks
  await auditor.pressKey('Tab');
  await auditor.pressKey('Tab');
  await auditor.pressKey('Tab');

  // Check a container for keyboard traps
  await auditor.checkTrap('#modal-container');

  // Assert no issues found
  expect(auditor.issueCount).toBe(0);
  expect(auditor.getIssues()).toHaveLength(0);
});

The OracleAuditor accepts any object matching the OrchestratorLike interface, which is satisfied by both A11yOracle (Playwright) and A11yOrchestrator (core engine).

Selector Utilities

Generate CSS selectors and HTML snippets from element data:

import {
  selectorFromFocusedElement,
  htmlSnippetFromFocusedElement,
} from '@a11y-oracle/audit-formatter';

const selector = selectorFromFocusedElement(state.focusedElement);
// "#submit-btn" or "button.primary.large" or "button"

const snippet = htmlSnippetFromFocusedElement(state.focusedElement);
// '<button id="submit-btn" class="primary">Submit</button>'

Rules

A11y-Oracle enforces six WCAG rules:

Rule IDWCAG CriterionSinceLevelTagImpactDescription
oracle/focus-not-visible2.4.7 Focus Visible2.0AAwcag2aaseriousFocused element has no visible focus indicator
oracle/focus-low-contrast2.4.12 Focus Appearance2.2AAwcag22aamoderateFocus indicator contrast ratio is below 3:1
oracle/keyboard-trap2.1.2 No Keyboard Trap2.0Awcag2acriticalKeyboard focus is trapped within a container
oracle/focus-missing-name4.1.2 Name, Role, Value2.0Awcag2aseriousFocused element has no accessible name
oracle/focus-generic-role4.1.2 Name, Role, Value2.0Awcag2aseriousFocused element has a generic or presentational role
oracle/positive-tabindex2.4.3 Focus Order2.0Awcag2aseriousElement uses a positive tabindex value

focus-not-visible takes priority over focus-low-contrast — if no indicator exists, only the visibility rule fires (not both). focus-generic-role and focus-missing-name are mutually exclusive — generic roles trigger only the role check.

For detailed remediation guidance on each rule, see the Remediation Guide.

API Reference

Formatter Functions

formatFocusIssues(state, context)

Check an A11yState for focus indicator issues.

  • Parameters:
    • state: A11yState — unified accessibility state from pressKey() or getState()
    • context: AuditContext — { project, specName, wcagLevel?, disabledRules? }
  • Returns: OracleIssue[] — 0 or 1 issues
  • Behavior: Returns oracle/focus-not-visible if isVisible === false. Returns oracle/focus-low-contrast if visible but meetsWCAG_AA === false. Returns [] if passing or no focused element.

formatTrapIssue(result, containerSelector, context)

Check a TraversalResult for keyboard traps.

  • Parameters:
    • result: TraversalResult — result from traverseSubTree()
    • containerSelector: string — CSS selector for the container (used in the issue)
    • context: AuditContext — { project: string, specName: string }
  • Returns: OracleIssue[] — 0 or 1 issues

formatNameIssues(state, context)

Check an A11yState for missing accessible name issues.

  • Parameters: Same as formatFocusIssues
  • Returns: OracleIssue[] — 0 or 1 issues
  • Behavior: Returns oracle/focus-missing-name if the focused element has no computed name. Skips elements with generic/presentational roles (those fire formatRoleIssues instead).

formatRoleIssues(state, context)

Check an A11yState for generic/presentational role issues.

  • Parameters: Same as formatFocusIssues
  • Returns: OracleIssue[] — 0 or 1 issues
  • Behavior: Returns oracle/focus-generic-role if the focused element has a generic, none, or presentation role.

formatTabIndexIssues(state, context)

Check an A11yState for positive tabindex issues.

  • Parameters: Same as formatFocusIssues
  • Returns: OracleIssue[] — 0 or 1 issues
  • Behavior: Returns oracle/positive-tabindex if the focused element has tabIndex > 0.

formatAllIssues(state, context)

Convenience wrapper that runs all state-based checks: focus indicator, name, role, and tabindex.

  • Parameters: Same as formatFocusIssues
  • Returns: OracleIssue[]
  • Behavior: Applies wcagLevel filtering (default 'aa') and disabledRules suppression from context.

matchesWcagLevel(rule, level)

Check if a rule applies at a given WCAG conformance level.

  • Parameters:
    • rule: OracleRule — rule metadata object
    • level: WcagLevel — 'a' or 'aa'
  • Returns: boolean — true if the rule applies at the given level
  • Behavior: 'aa' includes all rules. 'a' includes only rules tagged with wcag2a.

OracleAuditor

constructor(orchestrator, context)

  • orchestrator: OrchestratorLike — any object with pressKey(), getState(), and traverseSubTree() methods
  • context: AuditContext — { project, specName, wcagLevel?, disabledRules? }

pressKey(key, modifiers?): Promise<A11yState>

Dispatch a key press and automatically audit the resulting state for all state-based rules.

getState(): Promise<A11yState>

Read the current state and audit it for all state-based rules.

checkTrap(selector, maxTabs?): Promise<TraversalResult>

Run keyboard trap detection on a container and audit the result.

getIssues(): ReadonlyArray<OracleIssue>

Return a copy of all accumulated issues.

clear(): void

Reset the accumulated issues list.

issueCount: number

The number of accumulated issues (getter).

OrchestratorLike Interface

interface OrchestratorLike {
  pressKey(key: string, modifiers?: Record<string, boolean>): Promise<A11yState>;
  getState(): Promise<A11yState>;
  traverseSubTree(selector: string, maxTabs?: number): Promise<TraversalResult>;
}

Satisfied by A11yOracle (playwright-plugin) and A11yOrchestrator (core-engine).

Rule Utilities

RULES: Record<string, OracleRule>

All defined rule objects keyed by rule ID.

RULE_IDS: string[]

Array of all rule IDs.

getRule(ruleId): OracleRule

Get a rule by ID, or throw if unknown.

Selector Utilities

selectorFromFocusedElement(el): string

Generate a CSS selector from an A11yFocusedElement. Priority: #id > tag.class1.class2 > tag.

selectorFromTabOrderEntry(entry): string

Generate a CSS selector from a TabOrderEntry. Priority: #id > tag[role="..."] > tag.

htmlSnippetFromFocusedElement(el): string

Generate a minimal HTML snippet from an A11yFocusedElement.

htmlSnippetFromTabOrderEntry(entry): string

Generate a minimal HTML snippet from a TabOrderEntry.

Types

import type {
  OracleIssue,         // Main issue object
  OracleNode,          // Axe-compatible node result
  OracleCheck,         // Individual check within a node
  OracleImpact,        // 'minor' | 'moderate' | 'serious' | 'critical'
  OracleResultType,    // 'violation' | 'incomplete' | 'oracle'
  OracleRule,          // Rule metadata
  AuditContext,        // { project, specName, wcagLevel?, disabledRules? }
  WcagLevel,           // 'wcag2a' | 'wcag2aa' | 'wcag21a' | 'wcag21aa' | 'wcag22a' | 'wcag22aa'
} from '@a11y-oracle/audit-formatter';

Exports

// Types
export type { OracleIssue, OracleNode, OracleCheck, OracleImpact, OracleResultType, OracleRule, AuditContext, WcagLevel };

// Rule definitions
export { RULES, RULE_IDS, getRule, matchesWcagLevel };

// Pure formatter functions
export { formatFocusIssues, formatTrapIssue, formatNameIssues, formatRoleIssues, formatTabIndexIssues, formatAllIssues };

// Selector utilities
export { selectorFromFocusedElement, selectorFromTabOrderEntry, htmlSnippetFromFocusedElement, htmlSnippetFromTabOrderEntry };

// Convenience class
export { OracleAuditor };
export type { OrchestratorLike };

版本与发布记录

@a11y-oracle/audit-formatter 共发布 10 个版本,首次发布于 2026/03/05;过去 12 个月发布了 10 个版本。

版本发布日期距今
1.3.2latest2026/03/127 个月前
1.3.12026/03/117 个月前
1.3.02026/03/068 个月前
1.2.02026/03/068 个月前
1.1.42026/03/068 个月前
1.1.32026/03/068 个月前

关于 @a11y-oracle/audit-formatter 的常见问题

@a11y-oracle/audit-formatter 是什么?

Converts accessibility findings to axe-core-compatible OracleIssue objects with WCAG rule metadata

如何安装 @a11y-oracle/audit-formatter?

在项目目录运行 npm install @a11y-oracle/audit-formatter,或使用 pnpm add / yarn add / bun add。

@a11y-oracle/audit-formatter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@a11y-oracle/audit-formatter 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@a11y-oracle/audit-formatter 可以商用吗?

@a11y-oracle/audit-formatter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@a11y-oracle/audit-formatter 的下载量有多少?

根据 npm 官方下载统计,@a11y-oracle/audit-formatter 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,498 次,日均约 83 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@a11y-oracle/audit-formatter 还在维护吗?

最新版本 1.3.2 发布于 2026/03/12(7 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。