From 0ec008e53b2cdeed06c7a831be592ae6942f85be Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Mon, 30 Oct 2023 13:56:40 +0100 Subject: [PATCH] Improve pasted meta-data detection. Fix #301 --- src/js/components/cite-tools.jsx | 3 ++- test/translate.test.jsx | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/js/components/cite-tools.jsx b/src/js/components/cite-tools.jsx index 4b00d29..e500214 100644 --- a/src/js/components/cite-tools.jsx +++ b/src/js/components/cite-tools.jsx @@ -31,8 +31,9 @@ const CiteTools = ({ identifier, isTranslating, onEditorOpen, onTranslationCance const handlePaste = useCallback((ev) => { const clipboardData = ev.clipboardData || window.clipboardData; const pastedData = clipboardData.getData('Text'); + const isMultiLineData = pastedData.split('\n').filter(line => line.trim().length > 0).length > 1; - if(!pastedData.includes('\n')) { + if (!isMultiLineData) { return; } diff --git a/test/translate.test.jsx b/test/translate.test.jsx index 373cb12..25b2f1f 100644 --- a/test/translate.test.jsx +++ b/test/translate.test.jsx @@ -4,7 +4,7 @@ import { setupServer } from 'msw/node' import { getAllByRole, getByRole, getByText, screen, waitFor, queryByRole, findByText, fireEvent } from '@testing-library/react' import userEvent from '@testing-library/user-event' -import { applyAdditionalJestTweaks } from './utils/common'; +import { applyAdditionalJestTweaks, waitForPosition } from './utils/common'; import Container from '../src/js/components/container'; import { renderWithProviders } from './utils/render'; import modernLanguageAssociationStyle from './fixtures/modern-language-association.xml'; @@ -396,4 +396,30 @@ describe('Translate', () => { expect(await findByText(newItemSection, /Understanding Dogs/)).toBeInTheDocument(); expect(hasTranslated).toBe(true); }); + + test("Trailing newline is ignored in pasted data", async () => { + let hasTranslated = false; + server.use( + rest.post('http://localhost/search', async (req, res, ctx) => { + expect(await req.text()).toBe('978-1979837125'); + hasTranslated = true; + // delayed to make sure input becomes readonly + return res(ctx.delay(100), ctx.json(responseTranslateIdentifier)); + }) + ); + renderWithProviders(); + const input = await screen.findByRole( + 'searchbox', { name: 'Enter a URL, ISBN, DOI, PMID, arXiv ID, or title' } + ); + expect(input).toHaveFocus(); + fireEvent.paste(input, { clipboardData: { getData: () => "978-1979837125\n " } }); + + // should ignore this paste event and do nothing + await waitForPosition(); + expect(screen.getByRole( + 'searchbox', { name: 'Enter a URL, ISBN, DOI, PMID, arXiv ID, or title' } + )).not.toHaveAttribute('readonly') + + expect(hasTranslated).toBe(false); + }); });