Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

10.2 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { ILogService, LocaleService } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { BrowserClipboardService } from '../clipboard-interface.service';
import { supportClipboardAPI } from '../clipboard-utils';

vi.mock('../clipboard-utils', () => ({
    supportClipboardAPI: vi.fn(),
}));

class MockClipboardItem {
    constructor(public readonly items: Record<string, Blob>) {}
}

function createService() {
    const localeService = {
        t: vi.fn((key: string) => key),
    } as unknown as LocaleService;
    const logService = {
        error: vi.fn(),
    } as unknown as ILogService;
    const notificationService = {
        show: vi.fn(),
    };

    const service = new BrowserClipboardService(localeService, logService, notificationService as any);

    return { service, localeService, logService, notificationService };
}

describe('BrowserClipboardService', () => {
    beforeEach(() => {
        vi.mocked(supportClipboardAPI).mockReturnValue(true);
        vi.stubGlobal('ClipboardItem', MockClipboardItem);
        Object.defineProperty(navigator, 'clipboard', {
            configurable: true,
            value: {
                write: vi.fn().mockResolvedValue(undefined),
                writeText: vi.fn().mockResolvedValue(undefined),
                read: vi.fn().mockResolvedValue([]),
                readText: vi.fn().mockResolvedValue('clipboard-text'),
            },
        });
        (document as any).execCommand = vi.fn(() => true);
    });

    afterEach(() => {
        vi.restoreAllMocks();
        vi.unstubAllGlobals();
    });

    it('should expose clipboard support from utility', () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(true);
        const { service } = createService();
        expect(service.supportClipboard).toBe(true);
    });

    it('should write plain text and html with modern clipboard api', async () => {
        const { service } = createService();

        await service.write('plain', '<b>plain</b>');

        expect(navigator.clipboard.write).toHaveBeenCalledTimes(1);
    });

    it('should write text with modern clipboard api', async () => {
        const { service } = createService();

        await service.writeText('hello');
        expect(navigator.clipboard.writeText).toHaveBeenCalledWith('hello');
    });

    it('should fallback to legacy copy when clipboard api is unsupported', async () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(false);
        const { service } = createService();

        const input = document.createElement('input');
        document.body.appendChild(input);
        input.focus();

        await service.writeText('legacy-text');
        await service.write('legacy', '<i>legacy</i>');

        expect(document.execCommand).toHaveBeenCalledWith('copy');
    });

    it('should fallback to legacy copy when ClipboardItem is unavailable', async () => {
        vi.stubGlobal('ClipboardItem', undefined);
        const { service } = createService();

        await service.write('legacy', '<i>legacy</i>');

        expect(document.execCommand).toHaveBeenCalledWith('copy');
        expect(navigator.clipboard.write).not.toHaveBeenCalled();
    });

    it('should write raw sanitized html during legacy copy event', async () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(false);
        const { service } = createService();
        const clipboardData = {
            setData: vi.fn(),
        };

        vi.mocked(document.execCommand).mockImplementation(() => {
            const event = new Event('copy', { cancelable: true }) as ClipboardEvent;
            Object.defineProperty(event, 'clipboardData', {
                value: clipboardData,
            });
            document.dispatchEvent(event);
            return true;
        });

        await service.write('plain', '<div onclick="alert(1)" style="color:red">rich</div>');

        expect(clipboardData.setData).toHaveBeenCalledWith('text/plain', 'plain');
        expect(clipboardData.setData).toHaveBeenCalledWith('text/html', '<div style="color: red">rich</div>');
        expect(document.body.lastElementChild?.textContent).not.toBe('rich');
    });

    it('should preserve Univer Excel metadata while sanitizing legacy html', async () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(false);
        const { service } = createService();
        const clipboardData = {
            setData: vi.fn(),
        };

        vi.mocked(document.execCommand).mockImplementation(() => {
            const event = new Event('copy', { cancelable: true }) as ClipboardEvent;
            Object.defineProperty(event, 'clipboardData', {
                value: clipboardData,
            });
            document.dispatchEvent(event);
            return true;
        });

        await service.write(
            'merged',
            '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Excel.Sheet"><meta name="Generator" content="Univer"><script>alert(1)</script></head><body><!--StartFragment--><table data-copy-id="copy-id"><tbody><tr><td rowspan="3" onclick="alert(1)">merged</td></tr></tbody></table><!--EndFragment--></body></html>'
        );

        const copiedHtml = clipboardData.setData.mock.calls.find(([type]) => type === 'text/html')?.[1];

        expect(copiedHtml).toContain('xmlns:x="urn:schemas-microsoft-com:office:excel"');
        expect(copiedHtml).toContain('<meta name="ProgId" content="Excel.Sheet">');
        expect(copiedHtml).toContain('<!--StartFragment--><table data-copy-id="copy-id">');
        expect(copiedHtml).toContain('<td rowspan="3">merged</td>');
        expect(copiedHtml).toContain('</table><!--EndFragment-->');
        expect(copiedHtml).not.toContain('<script');
        expect(copiedHtml).not.toContain('onclick');
    });

    it('should sanitize html before legacy copy', async () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(false);
        const { service } = createService();
        let copiedHtml = '';

        vi.mocked(document.execCommand).mockImplementation(() => {
            copiedHtml = document.body.lastElementChild?.innerHTML ?? '';
            return true;
        });

        await service.write(
            'legacy',
            '<div data-copy-id="copy-id" onclick="alert(1)"><a href="javascript:alert(1)">unsafe</a><img src="data:image/png;base64,ZmFrZQ==" onerror="alert(1)" /><span style="color: red; background-image: url(javascript:alert(1))">safe</span><script>alert(1)</script></div>'
        );

        const copiedDocument = new DOMParser().parseFromString(copiedHtml, 'text/html');
        const wrapper = copiedDocument.body.firstElementChild as HTMLDivElement | null;
        const link = copiedDocument.querySelector('a');
        const image = copiedDocument.querySelector('img');
        const span = copiedDocument.querySelector('span');

        expect(wrapper?.getAttribute('data-copy-id')).toBe('copy-id');
        expect(wrapper?.hasAttribute('onclick')).toBe(false);
        expect(copiedDocument.querySelector('script')).toBeNull();
        expect(link?.hasAttribute('href')).toBe(false);
        expect(image?.getAttribute('src')).toBe('data:image/png;base64,ZmFrZQ==');
        expect(image?.hasAttribute('onerror')).toBe(false);
        expect(span?.getAttribute('style')).toContain('color: red');
        expect(span?.getAttribute('style')).not.toContain('url(');
    });

    it('should show notification when write/writeText fails', async () => {
        const { service, logService, notificationService } = createService();
        vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('writeText failed'));
        vi.mocked(navigator.clipboard.write).mockRejectedValueOnce(new Error('write failed'));

        await service.writeText('failed');
        await service.write('failed', '<b>failed</b>');

        expect(logService.error).toHaveBeenCalledTimes(2);
        expect(notificationService.show).toHaveBeenCalledWith({
            type: 'warning',
            title: 'ui.clipboard.authentication.title',
            content: 'ui.clipboard.authentication.content',
        });
    });

    it('should throw for read/readText when clipboard api is unsupported', async () => {
        vi.mocked(supportClipboardAPI).mockReturnValue(false);
        const { service } = createService();

        await expect(service.read()).rejects.toThrow('[BrowserClipboardService] read() is not supported on this platform.');
        await expect(service.readText()).rejects.toThrow('[BrowserClipboardService] read() is not supported on this platform.');
    });

    it('should read clipboard data when supported', async () => {
        const { service } = createService();

        await expect(service.read()).resolves.toEqual([]);
        await expect(service.readText()).resolves.toBe('clipboard-text');
    });

    it('should return fallback values and notify when read/readText fails', async () => {
        const { service, logService, notificationService } = createService();
        vi.mocked(navigator.clipboard.read).mockImplementationOnce(() => {
            throw new Error('read failed');
        });
        vi.mocked(navigator.clipboard.readText).mockRejectedValueOnce(new Error('readText failed'));

        await expect(service.read()).resolves.toEqual([]);
        await expect(service.readText()).resolves.toBe('');
        expect(logService.error).toHaveBeenCalledTimes(2);
        expect(notificationService.show).toHaveBeenCalled();
    });
});