Skip to content

Commit

Permalink
feat(axe.d.ts): add nodeSerializer API
Browse files Browse the repository at this point in the history
This API was added in pr dequelabs#4093, but TS definitions were never added.
  • Loading branch information
hamax committed Aug 1, 2024
1 parent cfd2974 commit a0299b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
30 changes: 26 additions & 4 deletions axe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ declare namespace axe {
interface DqElement extends SerialDqElement {
element: Element;
toJSON(): SerialDqElement;
}
interface DqElementConstructor {
new (elm: Element, options?: { absolutePaths?: boolean }): DqElement;
mergeSpecs(
childSpec: SerialDqElement,
parentSpec: SerialDqElement
Expand Down Expand Up @@ -405,6 +408,27 @@ declare namespace axe {
boundingClientRect: DOMRect;
}

interface CustomNodeSerializer {
toSpec: (dqElm: DqElement) => SerialDqElement;
mergeSpecs: (
nodeSpec: SerialDqElement,
parentFrameSpec: SerialDqElement
) => SerialDqElement;
}

interface NodeSerializer {
update: (serializer: CustomNodeSerializer) => void;
toSpec: (node: Element | VirtualNode) => SerialDqElement;
dqElmToSpec: (
dqElm: DqElement | SerialDqElement,
options?: RunOptions
) => SerialDqElement;
mergeSpecs: (
nodeSpec: SerialDqElement,
parentFrameSpec: SerialDqElement
) => SerialDqElement;
}

interface Utils {
getFrameContexts: (
context?: ElementContext,
Expand All @@ -423,15 +447,13 @@ declare namespace axe {
selector: unknown
) => selector is LabelledShadowDomSelector;

DqElement: new (
elm: Element,
options?: { absolutePaths?: boolean }
) => DqElement;
DqElement: DqElementConstructor;
uuid: (
options?: { random?: Uint8Array | Array<number> },
buf?: Uint8Array | Array<number>,
offset?: number
) => string | Uint8Array | Array<number>;
nodeSerializer: NodeSerializer;
}

interface Aria {
Expand Down
13 changes: 13 additions & 0 deletions typings/axe-core/axe-core-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,19 @@ if (axe.utils.isLabelledShadowDomSelector(unknownContext)) {
} else if (axe.utils.isContextSpec(unknownContext)) {
let context: axe.ContextSpec = unknownContext;
}
axe.utils.nodeSerializer.update({
toSpec(dqElm: axe.DqElement) {
return dqElm.toJSON();
},
mergeSpecs(childSpec: axe.SerialDqElement, parentSpec: axe.SerialDqElement) {
return axe.utils.DqElement.mergeSpecs(childSpec, parentSpec);
}
});
const spec2: axe.SerialDqElement = axe.utils.nodeSerializer.toSpec(element);
const spec3: axe.SerialDqElement = axe.utils.nodeSerializer.dqElmToSpec(
dqElement,
options
);

// Commons
axe.commons.aria.getRoleType('img');
Expand Down

0 comments on commit a0299b0

Please sign in to comment.