Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions webviews/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ function Title({ title, titleHTML, number, url, inEditMode, setEditMode, setCurr
title={url}
data-vscode-context={JSON.stringify(context)}
onClick={event => {
// The webview host opens any anchor with an href and ignores defaultPrevented,
// so the click must not reach it or a second browser tab is opened.
event.preventDefault();
event.stopPropagation();
void openOnGitHub();
}}
>
Expand Down
3 changes: 3 additions & 0 deletions webviews/components/stickyHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export function StickyHeader({ pr, visible }: { pr: PullRequest; visible: boolea
'github:copyMenu': true,
})}
onClick={event => {
// The webview host opens any anchor with an href and ignores defaultPrevented,
// so the click must not reach it or a second browser tab is opened.
event.preventDefault();
event.stopPropagation();
void openOnGitHub();
}}
>
Expand Down
39 changes: 39 additions & 0 deletions webviews/editorWebview/test/overview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,45 @@ describe('Overview', function () {
assert.strictEqual(openOnGitHub.callCount, 2);
});

it('opens a PR number link exactly once', function () {
const pr = new PullRequestBuilder().build();
const context = new PRContext(pr);
const openOnGitHub = sinon.stub(context, 'openOnGitHub');

// Stands in for the webview host, which opens any anchor with an href that a click
// reaches, and does not check defaultPrevented.
const hostOpenedLinks: string[] = [];
const hostLinkHandler = (event: Event) => {
const anchor = (event.target as HTMLElement).closest('a[href]');
if (anchor) {
hostOpenedLinks.push(anchor.getAttribute('href')!);
}
};
window.addEventListener('click', hostLinkHandler);

try {
const out = render(
<PullRequestContext.Provider value={context}>
<Overview {...pr} />
</PullRequestContext.Provider>,
);

const numberLinks = out.container.querySelectorAll('.overview-title a, .sticky-header-number');
assert.strictEqual(numberLinks.length, 2);
numberLinks.forEach(link => {
openOnGitHub.resetHistory();
hostOpenedLinks.length = 0;

fireEvent.click(link);

assert.strictEqual(openOnGitHub.callCount, 1);
assert.deepStrictEqual(hostOpenedLinks, []);
});
} finally {
window.removeEventListener('click', hostLinkHandler);
}
});

it('shows view changes in both headers', function () {
const pr = new PullRequestBuilder().isAgentSessionsWorkspace(true).build();
const context = new PRContext(pr);
Expand Down