Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Fix the skip-to-content link reloading the results (#3942)

Olga Bulat3ed38fc4b138
diff --git a/frontend/src/components/VButton.vue b/frontend/src/components/VButton.vue
index e04311964..2d113a89e 100644
--- a/frontend/src/components/VButton.vue
+++ b/frontend/src/components/VButton.vue
@@ -44,6 +44,8 @@ import {
} from "~/types/button"
import type { ButtonForm } from "~/types/button"
+import { skipToContentTargetId } from "~/constants/window"
+
import VLink from "~/components/VLink.vue"
export type ButtonProps = ProperlyExtractPropTypes<
@@ -68,7 +70,9 @@ const VButton = defineComponent({
props: {
/**
* Passed to `component :is` to allow the button to *appear* as a button but
- * work like another element (like an `anchor`). May only be either `button` or `a`.
+ * work like another element (like an `anchor`). May only be either `button` or `VLink`.
+ * `anchor` is only supported for the `VSkipToContentButton` component that uses
+ * a hash URL (`skipToContentTargetId`).
*
* We do not support other elements because their use cases are marginal, and they
* add complexity that we can avoid otherwise.
@@ -186,7 +190,7 @@ const VButton = defineComponent({
},
setup(props, { attrs }) {
const typeAttribute = computed<ButtonType | undefined>(() =>
- props.as === "VLink" ? undefined : props.type
+ ["VLink", "a"].includes(props.as) ? undefined : props.type
)
const connectionStyles = computed(() =>
@@ -240,7 +244,10 @@ const VButton = defineComponent({
watch(
() => props.as,
(as) => {
- if (["a", "NuxtLink"].includes(as)) {
+ if (
+ ["a", "NuxtLink"].includes(as) &&
+ attrs.href !== `#${skipToContentTargetId}`
+ ) {
warn(
`Please use \`VLink\` with an \`href\` prop instead of ${as} for the button component`
)
diff --git a/frontend/src/components/VSkipToContentButton.vue b/frontend/src/components/VSkipToContentButton.vue
index 45ee9b14e..63d01935c 100644
--- a/frontend/src/components/VSkipToContentButton.vue
+++ b/frontend/src/components/VSkipToContentButton.vue
@@ -1,7 +1,7 @@
<template>
<VButton
:href="`#${skipToContentTargetId}`"
- as="VLink"
+ as="a"
variant="filled-pink"
size="medium"
class="skip-button z-50 focus:fixed focus:absolute focus:ms-2 focus:mt-2"
diff --git a/frontend/src/middleware/search.ts b/frontend/src/middleware/search.ts
index 4a100a678..eb5c4ec2f 100644
--- a/frontend/src/middleware/search.ts
+++ b/frontend/src/middleware/search.ts
@@ -1,6 +1,8 @@
import { useSearchStore } from "~/stores/search"
import { useMediaStore } from "~/stores/media"
+import { skipToContentTargetId } from "~/constants/window"
+
import { handledClientSide } from "~/utils/errors"
import type { Middleware } from "@nuxt/types"
@@ -21,17 +23,16 @@ export const searchMiddleware: Middleware = async ({
* This is meant to block direct access to /search and all sub-routes.
*
* The `q` parameter is required for searches on the frontend.
- * The API also allows for calls without the `q` parameter when searching within
- * `creator` field, but the frontend query still uses `q` for it.
- * The `?q=cat&searchBy=creator` frontend query is converted to
- * `?creator=cat&searchBy=creator` API request query in
- * `prepare-search-query-params`.
- * Note that the search by creator is not displayed in the UI.
*/
if (!q) {
return redirect("/")
}
+ // Don't do anything when clicking on the skip-to-content link.
+ if (route.hash === `#${skipToContentTargetId}`) {
+ return
+ }
+
const searchStore = useSearchStore($pinia)
await searchStore.initProviderFilters()
diff --git a/frontend/src/pages/image/collection.vue b/frontend/src/pages/image/collection.vue
index dc58c0584..c4afeddae 100644
--- a/frontend/src/pages/image/collection.vue
+++ b/frontend/src/pages/image/collection.vue
@@ -1,5 +1,9 @@
<template>
- <div class="p-6 pt-0 lg:p-10 lg:pt-2">
+ <div
+ :id="skipToContentTargetId"
+ tabindex="-1"
+ class="p-6 pt-0 lg:p-10 lg:pt-2"
+ >
<VCollectionResults
v-if="collectionParams"
search-term=""
@@ -24,8 +28,9 @@ import { computed, ref, watch } from "vue"
import { collectionMiddleware } from "~/middleware/collection"
import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
-import type { ImageDetail } from "~/types/media"
+import { skipToContentTargetId } from "~/constants/window"
import { useI18n } from "~/composables/use-i18n"
+import type { ImageDetail } from "~/types/media"
import VCollectionResults from "~/components/VSearchResultsGrid/VCollectionResults.vue"
@@ -93,6 +98,7 @@ export default defineComponent({
collectionLabel,
loadMore,
media,
+ skipToContentTargetId,
}
},
head: {},
diff --git a/frontend/src/types/button.ts b/frontend/src/types/button.ts
index e21e7e7d5..0500c6aff 100644
--- a/frontend/src/types/button.ts
+++ b/frontend/src/types/button.ts
@@ -1,4 +1,4 @@
-export const buttonForms = ["VLink", "button"] as const
+export const buttonForms = ["VLink", "button", "a"] as const
export type ButtonForm = (typeof buttonForms)[number]
diff --git a/frontend/test/playwright/e2e/all-results-keyboard.spec.ts b/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
index 15cda938c..7e95cd62d 100644
--- a/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
+++ b/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
@@ -4,50 +4,56 @@ import audio from "~~/test/playwright/utils/audio"
import {
goToSearchTerm,
preparePageForTests,
+ skipToContent,
} from "~~/test/playwright/utils/navigation"
import {
+ getContentLink,
locateFocusedResult,
walkToNextOfType,
walkToType,
} from "~~/test/playwright/utils/search-results"
import { keycodes } from "~/constants/key-codes"
+import { SupportedMediaType } from "~/constants/media"
test.describe.configure({ mode: "parallel" })
+const singleResultRegex = (
+ mediaType: SupportedMediaType,
+ searchTerm?: string
+) => {
+ const query = searchTerm ? `\\?q=${searchTerm}` : ""
+ return new RegExp(
+ `/${mediaType}/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}${query}$`,
+ "i"
+ )
+}
+
test.describe("all results grid keyboard accessibility test", () => {
+ const searchTerm = "birds"
test.beforeEach(async ({ page }) => {
await preparePageForTests(page, "xl")
- await goToSearchTerm(page, "birds")
+ await goToSearchTerm(page, searchTerm)
})
test("should open image results as links", async ({ page }) => {
- await walkToType("image", page)
+ const mediaType = "image"
+ await walkToType(mediaType, page)
await page.keyboard.press("Enter")
- const urlRegex = new RegExp(
- `/image/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\\?q=birds$`,
- "i"
- )
+ const urlRegex = singleResultRegex(mediaType, searchTerm)
+
await page.waitForURL(urlRegex)
expect(page.url()).toMatch(urlRegex)
})
test("should open audio results as links", async ({ page }) => {
- await walkToType("audio", page)
+ const mediaType = "audio"
+ await walkToType(mediaType, page)
await page.keyboard.press("Enter")
- await page.waitForURL(
- new RegExp(
- `/audio/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\\?q=birds$`,
- "i"
- )
- )
- expect(page.url()).toMatch(
- new RegExp(
- `/audio/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\\?q=birds$`,
- "i"
- )
- )
+ const urlRegex = singleResultRegex(mediaType, searchTerm)
+ await page.waitForURL(urlRegex)
+ expect(page.url()).toMatch(urlRegex)
})
test("should show instructions snackbar when focusing first audio", async ({
@@ -121,4 +127,19 @@ test.describe("all results grid keyboard accessibility test", () => {
await expect(playButton).toBeVisible()
await expect(pauseButton).toBeHidden()
})
+
+ // Test for https://github.com/WordPress/openverse/issues/3940
+ test("clicking on skip-to-content should not navigate", async ({ page }) => {
+ const getResultsLabel = async (type: SupportedMediaType) => {
+ const link = await getContentLink(page, type)
+ return link.textContent()
+ }
+ const imageResultsLabel = await getResultsLabel("image")
+ const audioResultsLabel = await getResultsLabel("audio")
+
+ await skipToContent(page)
+
+ expect(await getResultsLabel("image")).toEqual(imageResultsLabel)
+ expect(await getResultsLabel("audio")).toEqual(audioResultsLabel)
+ })
})
diff --git a/frontend/test/playwright/e2e/search-navigation.spec.ts b/frontend/test/playwright/e2e/search-navigation.spec.ts
index 806db8b44..9561226eb 100644
--- a/frontend/test/playwright/e2e/search-navigation.spec.ts
+++ b/frontend/test/playwright/e2e/search-navigation.spec.ts
@@ -1,25 +1,22 @@
-import { expect, Page, test } from "@playwright/test"
+import { expect, test } from "@playwright/test"
import {
- goToSearchTerm,
filters,
- searchFromHeader,
+ goToSearchTerm,
openFirstResult,
preparePageForTests,
+ searchFromHeader,
} from "~~/test/playwright/utils/navigation"
import { mockProviderApis } from "~~/test/playwright/utils/route"
import breakpoints from "~~/test/playwright/utils/breakpoints"
import { getBackToSearchLink } from "~~/test/playwright/utils/components"
-import { AUDIO, IMAGE, SupportedMediaType } from "~/constants/media"
+import { getContentLink } from "~~/test/playwright/utils/search-results"
-test.describe.configure({ mode: "parallel" })
+import { AUDIO, IMAGE } from "~/constants/media"
-const getContentLink = async (page: Page, mediaType: SupportedMediaType) => {
- const linkName = new RegExp(`See .+${mediaType}.+found for`)
- return page.getByRole("link", { name: linkName })
-}
+test.describe.configure({ mode: "parallel" })
test.describe("search history navigation", () => {
breakpoints.describeMobileAndDesktop(({ breakpoint }) => {
diff --git a/frontend/test/playwright/utils/navigation.ts b/frontend/test/playwright/utils/navigation.ts
index 6bf3acafd..d61f946b1 100644
--- a/frontend/test/playwright/utils/navigation.ts
+++ b/frontend/test/playwright/utils/navigation.ts
@@ -2,18 +2,18 @@ import { expect } from "@playwright/test"
import { LanguageDirection, t } from "~~/test/playwright/utils/i18n"
+import type { MediaType, SupportedSearchType } from "~/constants/media"
import {
ALL_MEDIA,
AUDIO,
IMAGE,
- MediaType,
MODEL_3D,
searchPath,
- SupportedSearchType,
VIDEO,
} from "~/constants/media"
import type { Breakpoint } from "~/constants/screens"
+import { keycodes } from "~/constants/key-codes"
import type { BrowserContext, Locator, Page } from "@playwright/test"
@@ -423,3 +423,10 @@ export const setCookies = async (
})
await context.addCookies(cookiesToSet)
}
+
+export const skipToContent = async (page: Page) => {
+ // Go to skip to content button
+ await page.keyboard.press(keycodes.Tab)
+ // Skip to content
+ await page.keyboard.press(keycodes.Enter)
+}
diff --git a/frontend/test/playwright/utils/search-results.ts b/frontend/test/playwright/utils/search-results.ts
index ccfefeaa0..db975bfdb 100644
--- a/frontend/test/playwright/utils/search-results.ts
+++ b/frontend/test/playwright/utils/search-results.ts
@@ -1,6 +1,9 @@
import { expect, Page } from "@playwright/test"
+import { skipToContent } from "~~/test/playwright/utils/navigation"
+
import { keycodes } from "~/constants/key-codes"
+import type { SupportedMediaType } from "~/constants/media"
export const walkToNextOfType = async (type: "image" | "audio", page: Page) => {
const isActiveElementOfType = () => {
@@ -19,11 +22,9 @@ export const walkToNextOfType = async (type: "image" | "audio", page: Page) => {
await page.keyboard.press(keycodes.Tab)
}
}
+
export const walkToType = async (type: "image" | "audio", page: Page) => {
- // Go to skip to content button
- await page.keyboard.press(keycodes.Tab)
- // Skip to content
- await page.keyboard.press(keycodes.Enter)
+ await skipToContent(page)
await walkToNextOfType(type, page)
}
@@ -36,3 +37,15 @@ export const locateFocusedResult = async (page: Page) => {
return page.locator(`[href="${url.pathname}?q=birds"]`)
}
+
+/**
+ * Returns the content link from the all content search results page
+ * for the given media type.
+ */
+export const getContentLink = async (
+ page: Page,
+ mediaType: SupportedMediaType
+) => {
+ const linkName = new RegExp(`See .+${mediaType}.+found for`)
+ return page.getByRole("link", { name: linkName })
+}