Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/editor/src/plugins/email-theming/extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export function setGlobalCssInjected(editor: Editor, css: string): boolean {
return editor.commands.setGlobalContent('css', css);
}

function isKnownEditorTheme(value: unknown): value is EditorTheme {
return typeof value === 'string' && value in EDITOR_THEMES;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Theme validation is too permissive because in accepts inherited object keys; use an own-key check to avoid accepting non-theme strings.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/editor/src/plugins/email-theming/extension.tsx, line 231:

<comment>Theme validation is too permissive because `in` accepts inherited object keys; use an own-key check to avoid accepting non-theme strings.</comment>

<file context>
@@ -227,6 +227,10 @@ export function setGlobalCssInjected(editor: Editor, css: string): boolean {
 }
 
+function isKnownEditorTheme(value: unknown): value is EditorTheme {
+  return typeof value === 'string' && value in EDITOR_THEMES;
+}
+
</file context>
Suggested change
return typeof value === 'string' && value in EDITOR_THEMES;
return typeof value === 'string' && Object.hasOwn(EDITOR_THEMES, value);

}

function getEmailTheme(editor: Editor): EditorTheme {
const extensionOptions = (
editor.extensionManager.extensions.find(
Expand All @@ -238,12 +242,12 @@ function getEmailTheme(editor: Editor): EditorTheme {
return extensionOptions.extends ?? 'minimal';
}

if (extensionOptions === 'basic' || extensionOptions === 'minimal') {
if (isKnownEditorTheme(extensionOptions)) {
return extensionOptions;
}

const globalTheme = getGlobalContent('theme', editor) as EditorTheme | null;
if (globalTheme === 'basic' || globalTheme === 'minimal') {
const globalTheme = getGlobalContent('theme', editor);
if (isKnownEditorTheme(globalTheme)) {
return globalTheme;
}

Expand Down
75 changes: 74 additions & 1 deletion packages/editor/src/plugins/email-theming/theme-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
parseCssValue,
themeStylesToPanelOverrides,
} from './theme-config';
import { EDITOR_THEMES } from './themes';
import { EDITOR_THEMES, RESET_THEMES } from './themes';
import type { EditorTheme } from './types';

describe('parseCssValue', () => {
it('parses pixel strings', () => {
Expand Down Expand Up @@ -210,3 +211,75 @@ describe('extendTheme', () => {
expect(result.styles).toEqual({ link: { color: '#abc' } });
});
});

describe('new design themes', () => {
const DESIGN_THEMES = [
'barebone',
'matte',
'protocol',
'arcane',
'studio',
] as const satisfies readonly EditorTheme[];

const REQUIRED_PANEL_IDS = [
'body',
'container',
'typography',
'h1',
'h2',
'h3',
'paragraph',
'link',
'image',
'button',
'code-block',
'inline-code',
];

for (const theme of DESIGN_THEMES) {
it(`${theme} is registered in EDITOR_THEMES with all required panels`, () => {
const panels = EDITOR_THEMES[theme];
expect(panels).toBeDefined();
const ids = panels.map((g) => g.id);
for (const id of REQUIRED_PANEL_IDS) {
expect(ids).toContain(id);
}
});

it(`${theme} is registered in RESET_THEMES with heading styles`, () => {
const reset = RESET_THEMES[theme];
expect(reset).toBeDefined();
expect(reset.h1.fontSize).toBeDefined();
expect(reset.h2.fontSize).toBeDefined();
expect(reset.h3.fontSize).toBeDefined();
expect(reset.button.backgroundColor).toBeDefined();
expect(reset.button.color).toBeDefined();
});

it(`${theme} can be used as extendTheme base`, () => {
const result = extendTheme(theme, { link: { color: '#abc' } });
expect(result.extends).toBe(theme);
});
}

it('barebone has a light body background', () => {
const bodyBg = EDITOR_THEMES.barebone
.find((g) => g.id === 'body')
?.inputs.find((i) => i.prop === 'backgroundColor')?.value;
expect(bodyBg).toBe('#F3F4F6');
});

it('protocol is a dark theme', () => {
const containerBg = EDITOR_THEMES.protocol
.find((g) => g.id === 'container')
?.inputs.find((i) => i.prop === 'backgroundColor')?.value;
expect(containerBg).toBe('#131313');
});

it('arcane has a dark maroon container', () => {
const containerBg = EDITOR_THEMES.arcane
.find((g) => g.id === 'container')
?.inputs.find((i) => i.prop === 'backgroundColor')?.value;
expect(containerBg).toBe('#300610');
});
});
Loading
Loading