Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Stop opening links in a new tab (#4446)
Olga Bulat280dc574599f
diff --git a/frontend/docker-compose.playwright.yml b/frontend/docker-compose.playwright.ymlindex 93ad9383a..e26780e12 100644--- a/frontend/docker-compose.playwright.yml+++ b/frontend/docker-compose.playwright.yml@@ -1,4 +1,3 @@-version: "3"services:playwright:build:diff --git a/frontend/src/components/VLink.vue b/frontend/src/components/VLink.vueindex 0c811ea84..89b8b9b09 100644--- a/frontend/src/components/VLink.vue+++ b/frontend/src/components/VLink.vue@@ -40,20 +40,17 @@* Links with `href` starting with `/` are treated as internal links.** Internal links use `NuxtLink` component with `to` attribute set to `localePath(href)`- * External links use `a` element. If `href` does not start with `#`, they are set to- * open in a new tab.+ * External links use `a` element, and open in the same tab.*/import { computed, defineComponent } from "vue"import { useContext } from "@nuxtjs/composition-api"-import { useAnalytics } from "~/composables/use-analytics"-import { defineEvent } from "~/types/emits"import VIcon from "~/components/VIcon/VIcon.vue"type InternalLinkProps = { to: string }-type ExternalLinkProps = { target: string; rel: string }+type ExternalLinkProps = { rel: string }type DisabledLinkProps = { role: string }type LinkProps =| InternalLinkProps@@ -128,20 +125,20 @@ export default defineComponent({return null} else {// External link should open in a new tab- return { target: "_blank", rel: "noopener noreferrer" }+ return { rel: "noopener noreferrer" }}}// if href is undefined, return props that make the link disabledreturn { role: "link" }})- const { sendCustomEvent } = useAnalytics()+ const { $sendCustomEvent } = useContext()const handleExternalClick = () => {if (!checkHref(props) || !props.sendExternalLinkClickEvent) {return}- sendCustomEvent("EXTERNAL_LINK_CLICK", {+ $sendCustomEvent("EXTERNAL_LINK_CLICK", {url: props.href,})}diff --git a/frontend/src/utils/attribution-html.ts b/frontend/src/utils/attribution-html.tsindex 106d3b67b..566e01d7f 100644--- a/frontend/src/utils/attribution-html.ts+++ b/frontend/src/utils/attribution-html.ts@@ -122,7 +122,7 @@ const licenseElementImg = (licenseElement: LicenseElement): string => {* @returns the HTML markup of the `<a>` tag*/const extLink = (href: string, text: string) =>- h("a", { target: "_blank", rel: "noopener noreferrer", href }, [text])+ h("a", { rel: "noopener noreferrer", href }, [text])/* Interfaces */diff --git a/frontend/test/playwright/e2e/audio-detail.spec.ts b/frontend/test/playwright/e2e/audio-detail.spec.tsindex 0ccf1b237..54e94a0cc 100644--- a/frontend/test/playwright/e2e/audio-detail.spec.ts+++ b/frontend/test/playwright/e2e/audio-detail.spec.ts@@ -4,7 +4,10 @@ import {collectAnalyticsEvents,expectEventPayloadToMatch,} from "~~/test/playwright/utils/analytics"-import { preparePageForTests } from "~~/test/playwright/utils/navigation"+import {+ openAndCloseExternalLink,+ preparePageForTests,+} from "~~/test/playwright/utils/navigation"import { t } from "~~/test/playwright/utils/i18n"import { getH1 } from "~~/test/playwright/utils/components"@@ -42,12 +45,9 @@ test("sends GET_MEDIA event on CTA button click", async ({ context, page }) => {const analyticsEvents = collectAnalyticsEvents(context)await goToCustomAudioPage(page)-- const pagePromise = context.waitForEvent("page")- await page.getByRole("link", { name: /get this audio/i }).click()-- const newPage = await pagePromise- newPage.close()+ await openAndCloseExternalLink(page, {+ name: new RegExp(t("audioDetails.weblink"), "i"),+ })const getMediaEvent = analyticsEvents.find((event) => event.n === "GET_MEDIA")diff --git a/frontend/test/playwright/e2e/external-sources.spec.ts b/frontend/test/playwright/e2e/external-sources.spec.tsindex 7ef1c8a69..cfd8adccf 100644--- a/frontend/test/playwright/e2e/external-sources.spec.ts+++ b/frontend/test/playwright/e2e/external-sources.spec.ts@@ -2,6 +2,7 @@ import { expect, test } from "@playwright/test"import {goToSearchTerm,+ openAndCloseExternalLink,preparePageForTests,} from "~~/test/playwright/utils/navigation"@@ -44,8 +45,6 @@ test.describe("analytics", () => {page,context,}) => {- const pagePromise = page.context().waitForEvent("page")-const events = collectAnalyticsEvents(context)await goToSearchTerm(page, "cat", { searchType: "image", mode: "SSR" })@@ -55,10 +54,7 @@ test.describe("analytics", () => {name: new RegExp(t("externalSources.form.supportedTitleSm"), "i"),}).click()- await page.getByRole("link", { name: "Centre for Ageing Better" }).click()-- const newPage = await pagePromise- await newPage.close()+ await openAndCloseExternalLink(page, { name: "Centre for Ageing Better" })const selectEvent = events.find((event) => event.n === "SELECT_EXTERNAL_SOURCE"diff --git a/frontend/test/playwright/e2e/header-internal.spec.ts b/frontend/test/playwright/e2e/header-internal.spec.tsindex 83671d0e1..e5f11e3e5 100644--- a/frontend/test/playwright/e2e/header-internal.spec.ts+++ b/frontend/test/playwright/e2e/header-internal.spec.ts@@ -2,6 +2,7 @@ import { expect, Page, test } from "@playwright/test"import {isDialogOpen,+ openAndCloseExternalLink,preparePageForTests,scrollToBottom,} from "~~/test/playwright/utils/navigation"@@ -63,21 +64,17 @@ test.describe("Header internal", () => {expect(scrollPosition).toBeGreaterThan(100)})- test("the modal opens an external link in a new window and it closes the modal", async ({+ test("the modal opens an external link in the same window and the modal is closed on back click", async ({page,}) => {await page.goto("/about")await scrollToBottom(page)await clickMenuButton(page)- // Open the external link in a new tab, close the tab- const [popup] = await Promise.all([- page.waitForEvent("popup"),- page.locator('div[role="dialog"] >> text=API').click(),- ])- await popup.close()- // If we want the modal to stay open, we'll need to change this to `true`,- // and implement the change+ await openAndCloseExternalLink(page, {+ locator: page.locator('div[role="dialog"] >> text=API'),+ })+expect(await isDialogOpen(page)).toBe(false)})diff --git a/frontend/test/playwright/e2e/report-media.spec.ts b/frontend/test/playwright/e2e/report-media.spec.tsindex a51f22446..5139cdd0a 100644--- a/frontend/test/playwright/e2e/report-media.spec.ts+++ b/frontend/test/playwright/e2e/report-media.spec.ts@@ -13,7 +13,7 @@ import {} from "~~/test/playwright/utils/analytics"import { supportedMediaTypes } from "~/constants/media"-import { ReportReason } from "~/constants/content-report"+import type { ReportReason } from "~/constants/content-report"test.describe.configure({ mode: "parallel" })@@ -57,13 +57,12 @@ const submitDmcaReport = async (page: Page, context: BrowserContext) => {})})await page.click('text="Infringes copyright"')- const [newPage] = await Promise.all([- context.waitForEvent("page"),- await page.click('text="Open form"'), // Opens a new tab- ])- await newPage.waitForLoadState()++ await page.click('text="Open form"')+ await page.waitForURL(/forms/)+// Return the beginning of the url, without parameters- return newPage.url().split("/forms/")[0] + "/forms/"+ return page.url().split("/forms/")[0] + "/forms/"}// todo: Test a sensitive report with the optional description fielddiff --git a/frontend/test/playwright/e2e/translation-banner.spec.ts b/frontend/test/playwright/e2e/translation-banner.spec.tsindex bc794d27a..853fa85bc 100644--- a/frontend/test/playwright/e2e/translation-banner.spec.ts+++ b/frontend/test/playwright/e2e/translation-banner.spec.ts@@ -15,11 +15,8 @@ test.describe("translation banner", () => {await page.goto(localeSearchPath)await expect(page.locator("text= 100")).toBeVisible({ timeout: 500 })- const [page1] = await Promise.all([- page.waitForEvent("popup"),- page.click("text=contributing a translation"),- ])- await expect(page1).toHaveURL(+ await page.click("text=contributing a translation")+ await expect(page).toHaveURL(`https://translate.wordpress.org/projects/meta/openverse/${locale}/default/`)})diff --git a/frontend/test/playwright/utils/navigation.ts b/frontend/test/playwright/utils/navigation.tsindex 95aa095ff..792b1d05d 100644--- a/frontend/test/playwright/utils/navigation.ts+++ b/frontend/test/playwright/utils/navigation.ts@@ -448,3 +448,20 @@ export const skipToContent = async (page: Page) => {// Skip to contentawait page.keyboard.press(keycodes.Enter)}++export const openAndCloseExternalLink = async (+ page: Page,+ { name, locator }: { name?: string | RegExp | undefined; locator?: Locator }+) => {+ if (!name && !locator) {+ throw new Error("Either name or locator must be provided")+ }+ const externalLink: Locator = locator ?? page.getByRole("link", { name })+ const externalLinkUrl = await externalLink.getAttribute("href")+ if (!externalLinkUrl) {+ throw new Error("External link URL not found")+ }+ await externalLink.click()++ await page.goBack()+}diff --git a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-2xl-linux.png b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-2xl-linux.pngindex 96e0fc72f..2fde17d15 100644Binary files a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-2xl-linux.png and b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-2xl-linux.png differdiff --git a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-sm-linux.png b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-sm-linux.pngindex 9bbf7b47b..7a423e8e0 100644Binary files a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-sm-linux.png and b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-sm-linux.png differdiff --git a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-xs-linux.png b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-xs-linux.pngindex 140b6faf6..b84bc9fcb 100644Binary files a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-xs-linux.png and b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-ltr-html-tab-xs-linux.png differdiff --git a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-sm-linux.png b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-sm-linux.pngindex c531683e2..49ae793c5 100644Binary files a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-sm-linux.png and b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-sm-linux.png differdiff --git a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-xs-linux.png b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-xs-linux.pngindex 482fb424f..e9de89675 100644Binary files a/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-xs-linux.png and b/frontend/test/storybook/visual-regression/v-media-reuse.spec.ts-snapshots/media-reuse-rtl-html-tab-xs-linux.png differdiff --git a/frontend/test/unit/specs/components/v-link.spec.js b/frontend/test/unit/specs/components/v-link.spec.jsindex 071f7be9e..6592a4ea0 100644--- a/frontend/test/unit/specs/components/v-link.spec.js+++ b/frontend/test/unit/specs/components/v-link.spec.js@@ -3,22 +3,19 @@ import Vue from "vue"import { render } from "~~/test/unit/test-utils/render"-import { useAnalytics } from "~/composables/use-analytics"-import VLink from "~/components/VLink.vue"-jest.mock("~/composables/use-analytics", () => ({- useAnalytics: jest.fn(),+jest.mock("@nuxtjs/composition-api", () => ({+ useContext: () => ({+ $sendCustomEvent: jest.fn(),+ }),}))describe("VLink", () => {- useAnalytics.mockImplementation(() => ({- sendCustomEvent: jest.fn(),- }))it.each`- href | target | rel- ${"/about"} | ${null} | ${null}- ${"http://localhost:8443/"} | ${"_blank"} | ${"noopener noreferrer"}+ href | target | rel+ ${"/about"} | ${null} | ${null}+ ${"http://localhost:8443/"} | ${null} | ${"noopener noreferrer"}`("Creates a correct link component based on href",({ href, target, rel }) => {