From ebd32d626f6fca8ac768c2d78cab2d58fada6a77 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sun, 1 Sep 2024 17:15:29 +0200 Subject: [PATCH 1/2] Improve textarea paste (part of gitea#31948) When pasting a URL over another URL, replace the URL instead of creating a useless `[url](url)`. (Partially picked from commit 55502265716f000a2c61c98fa093244e1310d97f) Signed-off-by: Gergely Nagy --- web_src/js/features/comp/Paste.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/features/comp/Paste.js b/web_src/js/features/comp/Paste.js index aab6997ad5..7e4ecbbeda 100644 --- a/web_src/js/features/comp/Paste.js +++ b/web_src/js/features/comp/Paste.js @@ -116,7 +116,7 @@ function handleClipboardText(textarea, text, e) { if (_shiftDown) return; const selectedText = value.substring(selectionStart, selectionEnd); const trimmedText = text.trim(); - if (selectedText && isUrl(trimmedText)) { + if (selectedText && isUrl(trimmedText) && !isUrl(selectedText)) { e.stopPropagation(); e.preventDefault(); replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`); From 3ff15d03f3d56a499cb4f05b685306051cdcc6ab Mon Sep 17 00:00:00 2001 From: Otto Richter Date: Wed, 11 Sep 2024 21:19:13 +0200 Subject: [PATCH 2/2] tests(e2e): assert URL paste behaviour --- tests/e2e/edit-comment.test.e2e.js | 31 -------------- tests/e2e/issue-comment.test.e2e.js | 63 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 31 deletions(-) delete mode 100644 tests/e2e/edit-comment.test.e2e.js create mode 100644 tests/e2e/issue-comment.test.e2e.js diff --git a/tests/e2e/edit-comment.test.e2e.js b/tests/e2e/edit-comment.test.e2e.js deleted file mode 100644 index ad6e028821..0000000000 --- a/tests/e2e/edit-comment.test.e2e.js +++ /dev/null @@ -1,31 +0,0 @@ -// @ts-check -import {expect} from '@playwright/test'; -import {test, login_user, load_logged_in_context} from './utils_e2e.js'; - -test.beforeAll(async ({browser}, workerInfo) => { - await login_user(browser, workerInfo, 'user2'); -}); - -test('Always focus edit tab first on edit', async ({browser}, workerInfo) => { - const context = await load_logged_in_context(browser, workerInfo, 'user2'); - const page = await context.newPage(); - const response = await page.goto('/user2/repo1/issues/1'); - await expect(response?.status()).toBe(200); - - // Switch to preview tab and save - await page.click('#issue-1 .comment-container .context-menu'); - await page.click('#issue-1 .comment-container .menu>.edit-content'); - await page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]').click(); - await page.click('#issue-1 .comment-container .save'); - - await page.waitForLoadState('networkidle'); - - // Edit again and assert that edit tab should be active (and not preview tab) - await page.click('#issue-1 .comment-container .context-menu'); - await page.click('#issue-1 .comment-container .menu>.edit-content'); - const editTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-writer]'); - const previewTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]'); - - await expect(editTab).toHaveClass(/active/); - await expect(previewTab).not.toHaveClass(/active/); -}); diff --git a/tests/e2e/issue-comment.test.e2e.js b/tests/e2e/issue-comment.test.e2e.js new file mode 100644 index 0000000000..ee2e3a4c89 --- /dev/null +++ b/tests/e2e/issue-comment.test.e2e.js @@ -0,0 +1,63 @@ +// @ts-check +import {expect} from '@playwright/test'; +import {test, login_user, login} from './utils_e2e.js'; + +test.beforeAll(async ({browser}, workerInfo) => { + await login_user(browser, workerInfo, 'user2'); +}); + +test('Hyperlink paste behaviour', async ({browser}, workerInfo) => { + test.skip(['Mobile Safari', 'Mobile Chrome', 'webkit'].includes(workerInfo.project.name), 'Mobile clients seem to have very weird behaviour with this test, which I cannot confirm with real usage'); + const page = await login({browser}, workerInfo); + await page.goto('/user2/repo1/issues/new'); + await page.locator('textarea').click(); + // same URL + await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some-anchor'); + await page.locator('textarea').press('Shift+Home'); + await page.locator('textarea').press('ControlOrMeta+c'); + await page.locator('textarea').press('ControlOrMeta+v'); + await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor'); + // other text + await page.locator('textarea').fill('Some other text'); + await page.locator('textarea').press('ControlOrMeta+a'); + await page.locator('textarea').press('ControlOrMeta+v'); + await expect(page.locator('textarea')).toHaveValue('[Some other text](https://codeberg.org/forgejo/forgejo#some-anchor)'); + // subset of URL + await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some'); + await page.locator('textarea').press('ControlOrMeta+a'); + await page.locator('textarea').press('ControlOrMeta+v'); + await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor'); + // superset of URL + await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some-anchor-on-the-page'); + await page.locator('textarea').press('ControlOrMeta+a'); + await page.locator('textarea').press('ControlOrMeta+v'); + await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor'); + // completely separate URL + await page.locator('textarea').fill('http://example.com'); + await page.locator('textarea').press('ControlOrMeta+a'); + await page.locator('textarea').press('ControlOrMeta+v'); + await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor'); +}); + +test('Always focus edit tab first on edit', async ({browser}, workerInfo) => { + const page = await login({browser}, workerInfo); + const response = await page.goto('/user2/repo1/issues/1'); + await expect(response?.status()).toBe(200); + + // Switch to preview tab and save + await page.click('#issue-1 .comment-container .context-menu'); + await page.click('#issue-1 .comment-container .menu>.edit-content'); + await page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]').click(); + await page.click('#issue-1 .comment-container .save'); + + await page.waitForLoadState('networkidle'); + + // Edit again and assert that edit tab should be active (and not preview tab) + await page.click('#issue-1 .comment-container .context-menu'); + await page.click('#issue-1 .comment-container .menu>.edit-content'); + const editTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-writer]'); + const previewTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]'); + + await expect(editTab).toHaveClass(/active/); + await expect(previewTab).not.toHaveClass(/active/); +});