Skip to content

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

Refactor templates to use main instead of div for semantic structure (#5673)

Ashish Vermaf363867b7eb7
diff --git a/frontend/src/pages/audio/collection.vue b/frontend/src/pages/audio/collection.vue
index 2ba87efca..4a56e2c48 100644
--- a/frontend/src/pages/audio/collection.vue
+++ b/frontend/src/pages/audio/collection.vue
@@ -53,7 +53,7 @@ await useAsyncData(
</script>
<template>
- <div
+ <main
:id="skipToContentTargetId"
tabindex="-1"
class="p-6 pt-0 lg:p-10 lg:pt-2"
@@ -69,5 +69,5 @@ await useAsyncData(
:can-load-more="canLoadMore"
@load-more="loadMore"
/>
- </div>
+ </main>
</template>
diff --git a/frontend/src/pages/image/collection.vue b/frontend/src/pages/image/collection.vue
index 73f6402ef..e458fccbb 100644
--- a/frontend/src/pages/image/collection.vue
+++ b/frontend/src/pages/image/collection.vue
@@ -53,7 +53,7 @@ await useAsyncData(
</script>
<template>
- <div
+ <main
:id="skipToContentTargetId"
tabindex="-1"
class="p-6 pt-0 lg:p-10 lg:pt-2"
@@ -69,5 +69,5 @@ await useAsyncData(
:can-load-more="canLoadMore"
@load-more="loadMore"
/>
- </div>
+ </main>
</template>
diff --git a/frontend/src/pages/search.vue b/frontend/src/pages/search.vue
index d94690633..695c4ffa9 100644
--- a/frontend/src/pages/search.vue
+++ b/frontend/src/pages/search.vue
@@ -147,7 +147,7 @@ useLazyAsyncData(
</script>
<template>
- <div
+ <main
:id="skipToContentTargetId"
tabindex="-1"
class="browse-page flex w-full flex-col px-6 lg:px-10"
@@ -167,5 +167,5 @@ useLazyAsyncData(
data-testid="search-results"
/>
</div>
- </div>
+ </main>
</template>
diff --git a/frontend/test/playwright/e2e/skip-to-content.spec.ts b/frontend/test/playwright/e2e/skip-to-content.spec.ts
index f1fc50cab..3dcdf1388 100644
--- a/frontend/test/playwright/e2e/skip-to-content.spec.ts
+++ b/frontend/test/playwright/e2e/skip-to-content.spec.ts
@@ -1,7 +1,10 @@
import { expect } from "@playwright/test"
import { test } from "~~/test/playwright/utils/test"
import breakpoints from "~~/test/playwright/utils/breakpoints"
-import { preparePageForTests } from "~~/test/playwright/utils/navigation"
+import {
+ goToSearchTerm,
+ preparePageForTests,
+} from "~~/test/playwright/utils/navigation"
import { skipToContentLink } from "~~/test/playwright/utils/components"
import { skipToContentTargetId } from "#shared/constants/window"
@@ -14,17 +17,19 @@ test.describe.configure({ mode: "parallel" })
* - single result page
* - content page
* - home page
+ * - collection page (image)
*/
const pages = [
- "/search/image?q=galah",
- "/search/?q=galah",
- "/",
- "/image/e9d97a98-621b-4ec2-bf70-f47a74380452",
- "/about",
- "/image/collection?collection=tag&tag=cat",
-]
-
-for (const pageUrl of pages) {
+ ["/search/image?q=galah", "MAIN"],
+ ["/search/audio?q=galah", "MAIN"],
+ ["/search/?q=galah", "MAIN"],
+ ["/", "DIV"],
+ ["/image/e9d97a98-621b-4ec2-bf70-f47a74380452", "MAIN"],
+ ["/about", "MAIN"],
+ ["/image/collection?collection=tag&tag=cat", "MAIN"],
+] as const
+
+for (const [pageUrl, contentTag] of pages) {
breakpoints.describeMobileAndDesktop(async ({ breakpoint }) => {
test(`can skip to content on ${pageUrl}`, async ({ page }) => {
await preparePageForTests(page, breakpoint, {
@@ -33,6 +38,17 @@ for (const pageUrl of pages) {
await page.goto(pageUrl)
+ const main = page.getByRole("main")
+ const content = page.locator(`#${skipToContentTargetId}`)
+ await expect(main).toHaveCount(1)
+ await expect(content).toHaveJSProperty("tagName", contentTag)
+ await expect(content).toHaveAttribute("tabindex", "-1")
+ await expect(
+ page.locator(
+ `main#${skipToContentTargetId}, main #${skipToContentTargetId}`
+ )
+ ).toHaveCount(1)
+
// Tab to the skip to content button
await page.keyboard.press(keycodes.Tab)
@@ -43,7 +59,24 @@ for (const pageUrl of pages) {
// skip-to-content button, body becomes the active element.
// This is why we simply check that there's a visible element with
// id="content" on the page.
- await expect(page.locator(`#${skipToContentTargetId}`)).toBeVisible()
+ await expect(content).toBeVisible()
})
})
}
+
+test("search main landmark is the skip target after client-side navigation", async ({
+ page,
+}) => {
+ await preparePageForTests(page, "xl")
+ await goToSearchTerm(page, "galah", { mode: "CSR" })
+
+ const main = page.getByRole("main")
+ await expect(main).toHaveCount(1)
+ await expect(main).toHaveAttribute("id", skipToContentTargetId)
+ await expect(main).toHaveAttribute("tabindex", "-1")
+ await expect(main.getByTestId("search-results")).toBeVisible()
+ await expect(skipToContentLink(page)).toHaveAttribute(
+ "href",
+ `#${skipToContentTargetId}`
+ )
+})