Skip to content

Commit

Permalink
Improve pasted meta-data detection. Fix #301
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Oct 30, 2023
1 parent fe8b531 commit 0ec008e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/js/components/cite-tools.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
28 changes: 27 additions & 1 deletion test/translate.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(<Container />);
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);
});
});

0 comments on commit 0ec008e

Please sign in to comment.