From 7795c390f3b972be1392b4ec91fb3ffaab5a6564 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Tue, 1 Dec 2026 17:27:43 +0100 Subject: [PATCH 1/2] fix: omit undefined-valued object keys passed to native --- .../__tests__/normalizeHtmlStyle.test.ts | 124 ++++++++++++++++++ src/utils/expandMentionStylesForIndicators.ts | 9 +- src/utils/normalizeHtmlStyle.ts | 7 +- src/utils/omitUndefined.ts | 18 +++ 4 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 src/utils/__tests__/normalizeHtmlStyle.test.ts create mode 100644 src/utils/omitUndefined.ts diff --git a/src/utils/__tests__/normalizeHtmlStyle.test.ts b/src/utils/__tests__/normalizeHtmlStyle.test.ts new file mode 100644 index 000000000..b4a268bf0 --- /dev/null +++ b/src/utils/__tests__/normalizeHtmlStyle.test.ts @@ -0,0 +1,124 @@ +import type { EnrichedTextHtmlStyle, HtmlStyle } from '../../types'; +import { + DEFAULT_ENRICHED_TEXT_STYLE, + DEFAULT_HTML_STYLE, +} from '../defaultHtmlStyle'; +import { + normalizeEnrichedTextHtmlStyle, + normalizeHtmlStyle, +} from '../normalizeHtmlStyle'; + +// Keep colors as given, so the results can be compared with the defaults. +jest.mock('react-native', () => ({ + processColor: (color: unknown) => color, +})); + +const DEFAULT_MENTION = DEFAULT_HTML_STYLE.mention as Record; +const DEFAULT_ENRICHED_TEXT_MENTION = + DEFAULT_ENRICHED_TEXT_STYLE.mention as Record; + +// The internal style types `mention` as an opaque codegen value. +const mentionsOf = (style: { mention?: unknown }) => + style.mention as Record>; + +describe('normalizeHtmlStyle', () => { + describe('undefined values fall back to the defaults', () => { + it('in the flat form of mention', () => { + const style = { + mention: { borderRadius: undefined, color: undefined }, + } as HtmlStyle; + + const mention = mentionsOf(normalizeHtmlStyle(style, ['@', '#'])); + + for (const indicator of ['@', '#']) { + expect(mention[indicator]).toEqual(DEFAULT_MENTION); + } + }); + + it('in the per-indicator record form of mention', () => { + const style = { + mention: { + '@': { borderRadius: undefined, color: 'red' }, + '#': { color: undefined, borderRadius: 6 }, + }, + } as HtmlStyle; + + const mention = mentionsOf(normalizeHtmlStyle(style, ['@', '#'])); + + expect(mention['@']).toEqual({ ...DEFAULT_MENTION, color: 'red' }); + expect(mention['#']).toEqual({ ...DEFAULT_MENTION, borderRadius: 6 }); + }); + + it('in the default entry of the record form of mention', () => { + const style = { + mention: { default: { borderRadius: undefined, color: undefined } }, + } as HtmlStyle; + + const mention = mentionsOf(normalizeHtmlStyle(style, ['@'])); + + expect(mention['@']).toEqual(DEFAULT_MENTION); + }); + + it('in the other tags', () => { + const style = { + codeblock: { borderRadius: undefined, color: undefined }, + h1: { fontSize: undefined }, + } as HtmlStyle; + + const { codeblock, h1 } = normalizeHtmlStyle(style, []); + + expect(codeblock).toEqual(DEFAULT_HTML_STYLE.codeblock); + expect(h1).toEqual(DEFAULT_HTML_STYLE.h1); + }); + }); + + it('keeps defined values', () => { + const style: HtmlStyle = { + mention: { borderRadius: 6, color: 'red' }, + codeblock: { borderRadius: 4 }, + }; + + const normalized = normalizeHtmlStyle(style, ['@']); + const mention = mentionsOf(normalized); + const { codeblock } = normalized; + + expect(mention['@']).toEqual({ + ...DEFAULT_MENTION, + borderRadius: 6, + color: 'red', + }); + expect(codeblock).toEqual({ + ...DEFAULT_HTML_STYLE.codeblock, + borderRadius: 4, + }); + }); +}); + +describe('normalizeEnrichedTextHtmlStyle', () => { + it('falls back to the defaults for undefined values in the flat form of mention', () => { + const style = { + mention: { borderRadius: undefined, color: undefined }, + } as EnrichedTextHtmlStyle; + + const mention = mentionsOf(normalizeEnrichedTextHtmlStyle(style)); + + expect(mention._default).toEqual(DEFAULT_ENRICHED_TEXT_MENTION); + }); + + it('falls back to the defaults for undefined values in the record form of mention', () => { + const style = { + mention: { + '@': { borderRadius: undefined, color: 'red' }, + '#': { color: undefined }, + }, + } as EnrichedTextHtmlStyle; + + const mention = mentionsOf(normalizeEnrichedTextHtmlStyle(style)); + + expect(mention['@']).toEqual({ + ...DEFAULT_ENRICHED_TEXT_MENTION, + color: 'red', + }); + expect(mention['#']).toEqual(DEFAULT_ENRICHED_TEXT_MENTION); + }); +}); diff --git a/src/utils/expandMentionStylesForIndicators.ts b/src/utils/expandMentionStylesForIndicators.ts index 5ea356b7f..53583145b 100644 --- a/src/utils/expandMentionStylesForIndicators.ts +++ b/src/utils/expandMentionStylesForIndicators.ts @@ -1,5 +1,6 @@ import type { HtmlStyle, MentionStyleProperties } from '../types'; import { isMentionStyleRecord } from './isMentionStyleRecord'; +import { omitUndefined } from './omitUndefined'; export function expandMentionStylesForIndicators( mention: HtmlStyle['mention'] | undefined, @@ -10,9 +11,11 @@ export function expandMentionStylesForIndicators( for (const indicator of indicators) { out[indicator] = { ...htmlStyleToMergeWith.mention, - ...(isMentionStyleRecord(mention) - ? (mention[indicator] ?? mention.default ?? {}) - : mention), + ...omitUndefined( + isMentionStyleRecord(mention) + ? (mention[indicator] ?? mention.default) + : mention + ), }; } return out; diff --git a/src/utils/normalizeHtmlStyle.ts b/src/utils/normalizeHtmlStyle.ts index 81be317e7..b82dd2d38 100644 --- a/src/utils/normalizeHtmlStyle.ts +++ b/src/utils/normalizeHtmlStyle.ts @@ -11,6 +11,7 @@ import { DEFAULT_ENRICHED_TEXT_STYLE, } from './defaultHtmlStyle'; import { expandMentionStylesForIndicators } from './expandMentionStylesForIndicators'; +import { omitUndefined } from './omitUndefined'; const MENTION_DEFAULT_KEY = '_default'; @@ -74,12 +75,12 @@ const convertToEnrichedTextHtmlStyleInternal = ( if (typeof value === 'object' && value !== null) { mentionStyles[key] = { ...DEFAULT_ENRICHED_TEXT_STYLE.mention, - ...(value as MentionStyleProperties), + ...omitUndefined(value as MentionStyleProperties), }; } else { mentionStyles[MENTION_DEFAULT_KEY] = { ...DEFAULT_ENRICHED_TEXT_STYLE.mention, - ...(mention as MentionStyleProperties), + ...omitUndefined(mention as MentionStyleProperties), }; } } @@ -115,7 +116,7 @@ const assignDefaultValues = >( merged[key] = { ...(base[key] ?? {}), - ...(style[key as keyof typeof style] as object), + ...omitUndefined(style[key as keyof typeof style] as object), }; } diff --git a/src/utils/omitUndefined.ts b/src/utils/omitUndefined.ts new file mode 100644 index 000000000..833753dc7 --- /dev/null +++ b/src/utils/omitUndefined.ts @@ -0,0 +1,18 @@ +/** + * Returns a shallow copy of `obj` without the keys whose value is `undefined`. + */ +export function omitUndefined( + obj: T | null | undefined +): Partial { + const result: Partial = {}; + if (!obj) { + return result; + } + + for (const key of Object.keys(obj) as (keyof T)[]) { + if (obj[key] !== undefined) { + result[key] = obj[key]; + } + } + return result; +} From 3b877562b3b0343d295aa8834813fee5d3158d61 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Tue, 1 Dec 2026 17:38:35 +0100 Subject: [PATCH 2/2] fix: remove unneeded jest test --- .../__tests__/normalizeHtmlStyle.test.ts | 124 ------------------ 1 file changed, 124 deletions(-) delete mode 100644 src/utils/__tests__/normalizeHtmlStyle.test.ts diff --git a/src/utils/__tests__/normalizeHtmlStyle.test.ts b/src/utils/__tests__/normalizeHtmlStyle.test.ts deleted file mode 100644 index b4a268bf0..000000000 --- a/src/utils/__tests__/normalizeHtmlStyle.test.ts +++ /dev/null @@ -1,124 +0,0 @@ -import type { EnrichedTextHtmlStyle, HtmlStyle } from '../../types'; -import { - DEFAULT_ENRICHED_TEXT_STYLE, - DEFAULT_HTML_STYLE, -} from '../defaultHtmlStyle'; -import { - normalizeEnrichedTextHtmlStyle, - normalizeHtmlStyle, -} from '../normalizeHtmlStyle'; - -// Keep colors as given, so the results can be compared with the defaults. -jest.mock('react-native', () => ({ - processColor: (color: unknown) => color, -})); - -const DEFAULT_MENTION = DEFAULT_HTML_STYLE.mention as Record; -const DEFAULT_ENRICHED_TEXT_MENTION = - DEFAULT_ENRICHED_TEXT_STYLE.mention as Record; - -// The internal style types `mention` as an opaque codegen value. -const mentionsOf = (style: { mention?: unknown }) => - style.mention as Record>; - -describe('normalizeHtmlStyle', () => { - describe('undefined values fall back to the defaults', () => { - it('in the flat form of mention', () => { - const style = { - mention: { borderRadius: undefined, color: undefined }, - } as HtmlStyle; - - const mention = mentionsOf(normalizeHtmlStyle(style, ['@', '#'])); - - for (const indicator of ['@', '#']) { - expect(mention[indicator]).toEqual(DEFAULT_MENTION); - } - }); - - it('in the per-indicator record form of mention', () => { - const style = { - mention: { - '@': { borderRadius: undefined, color: 'red' }, - '#': { color: undefined, borderRadius: 6 }, - }, - } as HtmlStyle; - - const mention = mentionsOf(normalizeHtmlStyle(style, ['@', '#'])); - - expect(mention['@']).toEqual({ ...DEFAULT_MENTION, color: 'red' }); - expect(mention['#']).toEqual({ ...DEFAULT_MENTION, borderRadius: 6 }); - }); - - it('in the default entry of the record form of mention', () => { - const style = { - mention: { default: { borderRadius: undefined, color: undefined } }, - } as HtmlStyle; - - const mention = mentionsOf(normalizeHtmlStyle(style, ['@'])); - - expect(mention['@']).toEqual(DEFAULT_MENTION); - }); - - it('in the other tags', () => { - const style = { - codeblock: { borderRadius: undefined, color: undefined }, - h1: { fontSize: undefined }, - } as HtmlStyle; - - const { codeblock, h1 } = normalizeHtmlStyle(style, []); - - expect(codeblock).toEqual(DEFAULT_HTML_STYLE.codeblock); - expect(h1).toEqual(DEFAULT_HTML_STYLE.h1); - }); - }); - - it('keeps defined values', () => { - const style: HtmlStyle = { - mention: { borderRadius: 6, color: 'red' }, - codeblock: { borderRadius: 4 }, - }; - - const normalized = normalizeHtmlStyle(style, ['@']); - const mention = mentionsOf(normalized); - const { codeblock } = normalized; - - expect(mention['@']).toEqual({ - ...DEFAULT_MENTION, - borderRadius: 6, - color: 'red', - }); - expect(codeblock).toEqual({ - ...DEFAULT_HTML_STYLE.codeblock, - borderRadius: 4, - }); - }); -}); - -describe('normalizeEnrichedTextHtmlStyle', () => { - it('falls back to the defaults for undefined values in the flat form of mention', () => { - const style = { - mention: { borderRadius: undefined, color: undefined }, - } as EnrichedTextHtmlStyle; - - const mention = mentionsOf(normalizeEnrichedTextHtmlStyle(style)); - - expect(mention._default).toEqual(DEFAULT_ENRICHED_TEXT_MENTION); - }); - - it('falls back to the defaults for undefined values in the record form of mention', () => { - const style = { - mention: { - '@': { borderRadius: undefined, color: 'red' }, - '#': { color: undefined }, - }, - } as EnrichedTextHtmlStyle; - - const mention = mentionsOf(normalizeEnrichedTextHtmlStyle(style)); - - expect(mention['@']).toEqual({ - ...DEFAULT_ENRICHED_TEXT_MENTION, - color: 'red', - }); - expect(mention['#']).toEqual(DEFAULT_ENRICHED_TEXT_MENTION); - }); -});