Skip to content

Commit 91d3b2d

Browse files
committed
Chore(dom): DOM - helpers refactor (#165)
1 parent adfc2ee commit 91d3b2d

4 files changed

Lines changed: 39 additions & 39 deletions

File tree

packages/dom/src/lib/ElementAssertion.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Assertion, AssertionError } from "@assertive-ts/core";
22
import equal from "fast-deep-equal";
33

4-
import { getExpectedAndReceivedStyles, getAccessibleDescription, isElementEmpty } from "./helpers/helpers";
4+
import { getAccessibleDescription } from "./helpers/accessibility";
5+
import { isElementEmpty } from "./helpers/dom";
6+
import { getExpectedAndReceivedStyles } from "./helpers/styles";
57

68
export class ElementAssertion<T extends Element> extends Assertion<T> {
79

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function normalizeText(text: string): string {
2+
return text.replace(/\s+/g, " ").trim();
3+
}
4+
5+
export function getAccessibleDescription(actual: Element): string {
6+
const ariaDescribedBy = actual.getAttribute("aria-describedby");
7+
8+
if (!ariaDescribedBy) {
9+
return "";
10+
}
11+
12+
const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean);
13+
14+
const getElementText = (id: string): string | null => {
15+
const element = actual.ownerDocument.getElementById(id);
16+
17+
if (!element || !element.textContent) {
18+
return null;
19+
}
20+
21+
return element.textContent;
22+
};
23+
24+
const combinedText = descriptionIds
25+
.map(getElementText)
26+
.filter((text): text is string => text !== null)
27+
.join(" ");
28+
29+
return normalizeText(combinedText);
30+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const COMMENT_NODE_TYPE = 8;
2+
3+
export function isElementEmpty (element: Element): boolean {
4+
const nonCommentChildNodes = [...element.childNodes].filter(child => child.nodeType !== COMMENT_NODE_TYPE);
5+
return nonCommentChildNodes.length === 0;
6+
}
Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ interface StyleDeclaration extends Record<string, string> {
33
value: string;
44
}
55

6-
const COMMENT_NODE_TYPE = 8;
7-
86
function normalizeStyles(css: Partial<CSSStyleDeclaration>): StyleDeclaration {
97
const normalizer = document.createElement("div");
108
document.body.appendChild(normalizer);
@@ -75,39 +73,3 @@ export function getExpectedAndReceivedStyles
7573
elementProcessedStyle,
7674
];
7775
}
78-
79-
export function isElementEmpty (element: Element): boolean {
80-
const nonCommentChildNodes = [...element.childNodes].filter(child => child.nodeType !== COMMENT_NODE_TYPE);
81-
return nonCommentChildNodes.length === 0;
82-
}
83-
84-
function normalizeText(text: string): string {
85-
return text.replace(/\s+/g, " ").trim();
86-
}
87-
88-
export function getAccessibleDescription(actual: Element): string {
89-
const ariaDescribedBy = actual.getAttribute("aria-describedby");
90-
91-
if (!ariaDescribedBy) {
92-
return "";
93-
}
94-
95-
const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean);
96-
97-
const getElementText = (id: string): string | null => {
98-
const element = actual.ownerDocument.getElementById(id);
99-
100-
if (!element || !element.textContent) {
101-
return null;
102-
}
103-
104-
return element.textContent;
105-
};
106-
107-
const combinedText = descriptionIds
108-
.map(getElementText)
109-
.filter((text): text is string => text !== null)
110-
.join(" ");
111-
112-
return normalizeText(combinedText);
113-
}

0 commit comments

Comments
 (0)