Files
wekan/tests/playwright/specs/viewer-copy-code.e2e.js
T
Lauri Ojansivu 0495b8729d Copying a code block in a card description no longer opens a new tab.
The copy button of a code block is an <a href="#"> inside the card viewer.
Its own listener copied the text and prevented the navigation, but the click
went on to the viewer's 'click a' handler, which opens every link it sees in
a new tab - here this page plus "#". The button now stops the click, and the
viewer handler leaves the copy button alone and no longer opens a link to a
place on the page ("#", "#section") in a new tab; it moves to the target in
place when there is one.

tests/copyCodeBlock.test.cjs pins both and their order before window.open;
tests/playwright/specs/viewer-copy-code.e2e.js clicks the button and checks
that no tab opens, the page stays and the block's text is copied.

Thanks to carl-unique and xet7 !

Fixes #6753.
2026-10-10 01:20:53 +03:00

42 lines
2.0 KiB
JavaScript

'use strict';
// #6753: the copy button of a code block in a card description copies the
// block and stays on the page. It is an <a href="#"> inside the viewer, and the
// viewer's link handler opened that href - the board again - in a new tab.
const { test, expect } = require('../fixtures');
const db = require('../helpers/db');
const { openBoard } = require('../helpers/auth');
test('copying a code block opens no tab and copies its text (#6753)', async ({ boardPage: page, board, context }) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']).catch(() => {});
const card = db.findOne('cards', { boardId: board.boardId });
db.updateOne('cards', { _id: card._id }, { $set: { description: [
'Commands', '', '```', 'sudo snap refresh wekan', '```', '', '[jump](#nowhere)',
].join('\n') } });
await openBoard(page, board.boardId, board.slug);
await page.locator('.minicard', { hasText: card.title }).first().locator('.minicard-title').click();
const viewer = page.locator('.card-details .viewer', { hasText: 'Commands' }).first();
const copy = viewer.locator('a.js-copy-code');
await expect(copy).toHaveCount(1);
const pagesBefore = context.pages().length;
const url = page.url();
await copy.click();
// Negative: no new tab, no navigation, and the description did not open
// for editing.
await page.waitForTimeout(500);
expect(context.pages().length).toBe(pagesBefore);
expect(page.url()).toBe(url);
await expect(page.locator('.card-details form.inlined-form')).toHaveCount(0);
const copied = await page.evaluate(() => navigator.clipboard.readText().catch(() => null));
if (copied !== null) expect(copied.trim()).toBe('sudo snap refresh wekan');
// A link to a place on the page does not open a tab either (when the
// sanitizer keeps it as a link at all).
const jump = viewer.locator('a[href^="#"]', { hasText: 'jump' });
if (await jump.count()) {
await jump.click();
await page.waitForTimeout(500);
expect(context.pages().length).toBe(pagesBefore);
}
});