diff --git a/admin/src/components/form/fields/CodeEditorField.vue b/admin/src/components/form/fields/CodeEditorField.vue index 47d7133b..d5b09238 100644 --- a/admin/src/components/form/fields/CodeEditorField.vue +++ b/admin/src/components/form/fields/CodeEditorField.vue @@ -1,7 +1,9 @@ @@ -207,9 +214,17 @@ defineExpose({ insert }) class="flex flex-wrap items-center gap-1 border-b border-border bg-subtle p-1" data-codeeditor-toolbar > - - +
+
diff --git a/admin/src/components/form/fields/ColorPickerField.vue b/admin/src/components/form/fields/ColorPickerField.vue index 4ddf3043..a53eb101 100644 --- a/admin/src/components/form/fields/ColorPickerField.vue +++ b/admin/src/components/form/fields/ColorPickerField.vue @@ -1,11 +1,63 @@ diff --git a/admin/src/components/form/fields/MailImageField.vue b/admin/src/components/form/fields/MailImageField.vue new file mode 100644 index 00000000..d7a818e9 --- /dev/null +++ b/admin/src/components/form/fields/MailImageField.vue @@ -0,0 +1,94 @@ + + + diff --git a/admin/src/components/form/fields/MarkdownField.vue b/admin/src/components/form/fields/MarkdownField.vue index ca75f6ce..bf5011b7 100644 --- a/admin/src/components/form/fields/MarkdownField.vue +++ b/admin/src/components/form/fields/MarkdownField.vue @@ -1,6 +1,10 @@ + + diff --git a/admin/src/components/mail/insertImage.ts b/admin/src/components/mail/insertImage.ts index 0f8f1a71..1eb7ee1d 100644 --- a/admin/src/components/mail/insertImage.ts +++ b/admin/src/components/mail/insertImage.ts @@ -1,7 +1,34 @@ -// Stub for TDD RED (35-03 Task 5): real escaping lands in GREEN. +// Pure snippet builder for mail image insertion (UI-SPEC C4, D-19/D-20). +// The SPA never builds image URLs; callers pass the absolute URL the server returned. export type InsertImageLanguage = 'markdown' | 'html' | 'css' | 'text' -/** Builds the snippet inserted into a mail body or code editor. */ -export function insertImage(_language: InsertImageLanguage, _url: string, _alt: string): string { - return '' +function escapeMarkdownAlt(alt: string): string { + return alt.replace(/\\/g, '\\\\').replace(/\[/g, '\\[').replace(/\]/g, '\\]') +} + +/** Percent-encode spaces and parentheses in Markdown URLs (research Pitfall 4). */ +function encodeMarkdownUrl(url: string): string { + return url.replace(/[ ()]/g, (ch) => encodeURIComponent(ch)) +} + +function escapeHtmlAttr(value: string): string { + return value + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(//g, '>') +} + +/** Builds the snippet inserted into a mail body or code editor. */ +export function insertImage(language: InsertImageLanguage, url: string, alt: string): string { + switch (language) { + case 'markdown': + return `![${escapeMarkdownAlt(alt)}](${encodeMarkdownUrl(url)})` + case 'html': + return `${escapeHtmlAttr(alt)}` + case 'css': + return `url("${url.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}")` + case 'text': + return url + } } diff --git a/admin/tests/form/mail-smoke.test.ts b/admin/tests/form/mail-smoke.test.ts index 69d4dac4..f6c7199d 100644 --- a/admin/tests/form/mail-smoke.test.ts +++ b/admin/tests/form/mail-smoke.test.ts @@ -298,17 +298,38 @@ describe('insertImage', () => { }) }) +const pickerBundle = { + ...mailPreviewBundle, + ...codeeditorBundle, + 'backend::lang.colorpicker.pick': { other: 'Pick a colour for “:field”' }, + 'backend::lang.colorpicker.not_set': { other: 'Not set' }, + 'backend::lang.mail.picker.insert_button': { other: 'Insert image' }, + 'backend::lang.mail.picker.insert_title': { other: 'Insert an image from the library' }, + 'backend::lang.mail.picker.choose_title': { other: 'Choose an image' }, + 'backend::lang.mail.picker.search': { other: 'Search images' }, + 'backend::lang.mail.picker.insert': { other: 'Insert image' }, + 'backend::lang.mail.picker.use': { other: 'Use this image' }, + 'backend::lang.mail.picker.upload': { other: 'Upload image' }, + 'backend::lang.mail.picker.uploading': { other: 'Uploading…' }, + 'backend::lang.mail.picker.empty_title': { other: 'No images yet' }, + 'backend::lang.mail.picker.empty_upload': { other: 'Upload an image to use it in mail.' }, + 'backend::lang.mail.picker.empty_pick_only': { other: 'Images are added in Settings.' }, + 'backend::lang.mail.picker.no_results': { other: 'No images match “:query”.' }, + 'backend::lang.mail.picker.load_failed': { other: 'The image library could not be loaded.' }, + 'backend::lang.mail.picker.alt': { other: 'Alternative text' }, + 'backend::lang.mail.picker.alt_comment': { other: 'Describes the image.' }, + 'backend::lang.mailimage.empty': { other: 'No image chosen' }, + 'backend::lang.mailimage.choose': { other: 'Choose image' }, + 'backend::lang.mailimage.change': { other: 'Change image' }, + 'backend::lang.mailimage.remove': { other: 'Remove image' }, + 'backend::lang.mailimage.missing': { other: 'This image is no longer in the library. Choose another one.' }, + 'backend::lang.form.cancel': { other: 'Cancel' }, +} + describe('ColorPickerField', () => { it('syncs a typed #rrggbb into the swatch and keeps the last valid on partial', async () => { const Field = (await import('../../src/components/form/fields/ColorPickerField.vue')).default - setBundle( - { - ...mailPreviewBundle, - 'backend::lang.colorpicker.pick': { other: 'Pick a colour for “:field”' }, - 'backend::lang.colorpicker.not_set': { other: 'Not set' }, - }, - 'en', - ) + setBundle(pickerBundle, 'en') const wrapper = mount(Field, { props: { field: { name: 'body_bg', label: 'Body background', type: 'colorpicker' } as FormField, @@ -328,17 +349,105 @@ describe('ColorPickerField', () => { }) }) +describe('MailImagePicker and MarkdownField mailImages', () => { + beforeEach(() => { + setBundle(pickerBundle, 'en') + }) + + it('shows the upload row only when meta.canUpload is true', async () => { + const Picker = (await import('../../src/components/mail/MailImagePicker.vue')).default + mockApi({ + [`GET ${API}/mail/images`]: { + body: { data: [], meta: { canUpload: false, total: 0, page: 1, perPage: 40 } }, + }, + }) + const wrapper = mount(Picker, { + props: { open: true, mode: 'insert' }, + attachTo: document.body, + }) + await flushPromises() + expect(document.querySelector('[data-mail-image-upload]')).toBeNull() + + mockApi({ + [`GET ${API}/mail/images`]: { + body: { data: [], meta: { canUpload: true, total: 0, page: 1, perPage: 40 } }, + }, + }) + await wrapper.setProps({ open: false }) + await flushPromises() + await wrapper.setProps({ open: true }) + await flushPromises() + expect(document.querySelector('[data-mail-image-upload]')).not.toBeNull() + }) + + it('hides Preview and inserts markdown at the caret when mailImages is set', async () => { + const Field = (await import('../../src/components/form/fields/MarkdownField.vue')).default + mockApi({ + [`GET ${API}/mail/images`]: { + body: { + data: [{ id: 9, fileName: 'logo.png', title: 'Logo', url: 'https://cdn.example.com/logo.png' }], + meta: { canUpload: false, total: 1, page: 1, perPage: 40 }, + }, + }, + }) + const wrapper = mount(Field, { + props: { + field: { name: 'body', label: 'Body', type: 'markdown', mailImages: true } as FormField, + modelValue: 'Hello ', + controlId: 'f-body', + }, + attachTo: document.body, + }) + expect(wrapper.find('[data-markdown-preview]').exists()).toBe(false) + expect(wrapper.find('[data-markdown-insert-image]').exists()).toBe(true) + await wrapper.find('[data-markdown-insert-image]').trigger('click') + await flushPromises() + const tile = document.querySelector('[data-mail-image-id="9"]') + expect(tile).not.toBeNull() + tile!.click() + await flushPromises() + document.querySelector('[data-mail-image-confirm]')!.click() + await flushPromises() + expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['Hello ![Logo](https://cdn.example.com/logo.png)']) + }) +}) + +describe('MailImageField', () => { + it('shows the missing line when the stored id is gone', async () => { + ;(await import('../../src/app/i18n')).setBundle(pickerBundle, 'en') + const Field = (await import('../../src/components/form/fields/MailImageField.vue')).default + mockApi({ + [`GET ${API}/mail/images/3`]: { status: 404, body: { error: { code: 'not_found', message: 'gone', details: {} } } }, + }) + const wrapper = mount(Field, { + props: { + field: { name: 'logo', label: 'Logo', type: 'mailimage' } as FormField, + modelValue: 3, + controlId: 'f-logo', + }, + attachTo: document.body, + }) + await flushPromises() + expect(wrapper.find('[data-mailimage-missing]').exists()).toBe(true) + expect(wrapper.find('[data-mailimage-missing]').text()).toContain('no longer in the library') + }) +}) + // Last in the file: it replaces the editor chunk for the rest of the run. describe('CodeEditorField chunk failure', () => { afterEach(() => { vi.doUnmock('../../src/components/form/codeeditor/editor') + vi.resetModules() }) it('shows the load_failed note and a monospace textarea that still emits', async () => { vi.resetModules() - vi.doMock('../../src/components/form/codeeditor/editor', () => { - throw new Error('chunk failed') - }) + vi.unmock('../../src/components/form/codeeditor/editor') + vi.doMock('../../src/components/form/codeeditor/editor', () => ({ + createCodeEditor: () => { + throw new Error('chunk failed') + }, + })) ;(await import('../../src/app/i18n')).setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en') const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default const wrapper = mount(Field, {