Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit a2854e3

Browse files
fix: filter text node and color inherit for contrast checker
1 parent d2ebcb7 commit a2854e3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

studio/src/app/utils/editor/contrast.utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {extractRgb, extractRgba} from '@deckdeckgo/utils';
33
export class ContrastUtils {
44
static async calculateContrastRatio(bgColor: string | undefined, color: string | undefined): Promise<number> {
55
const bgColorWithDefault: string = bgColor === undefined || bgColor === '' ? `rgb(255, 255, 255)` : bgColor;
6-
const colorWithDefault: string = color === undefined || color === '' ? `rgb(0, 0, 0)` : color;
6+
const colorWithDefault: string = color === undefined || color === '' || color === 'initial' ? `rgb(0, 0, 0)` : color;
77

88
// The text color may or may not be semi-transparent, but that doesn't matter
99
const bgRgba: number[] | undefined = extractRgba(bgColorWithDefault);

studio/src/app/utils/editor/node.utils.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ export class NodeUtils {
2020
return Array.from(elements).reduce((acc: HTMLElement[], slot: HTMLElement) => {
2121
const children: NodeListOf<HTMLElement> = slot.querySelectorAll('*');
2222

23-
if (children && children.length > 0) {
24-
acc.push(...Array.from(children));
23+
const filteredChildren: HTMLElement[] | undefined = Array.from(children).filter((child) =>
24+
Array.from(child.childNodes).some((element) => element.nodeType === Node.TEXT_NODE)
25+
);
26+
27+
if (filteredChildren && filteredChildren.length > 0) {
28+
acc.push(...Array.from(filteredChildren));
2529
}
2630

2731
return acc;
@@ -48,7 +52,8 @@ export class NodeUtils {
4852

4953
const styleAttr: string = color === 'background' ? 'background-color' : 'color';
5054

51-
if (node.style[styleAttr] !== '' && node.style[styleAttr] !== 'initial') {
55+
// initial act for background as inherit
56+
if (node.style[styleAttr] !== '' && ((color === 'background' && node.style[styleAttr] !== 'initial') || color === 'color')) {
5257
return node.style[styleAttr];
5358
}
5459

0 commit comments

Comments
 (0)