|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file reset.until-found.a11y.browser.spec.ts |
| 5 | + * @input packages/core/src/reset.css, alone on a blank page |
| 6 | + * @output Browser evidence that `hidden="until-found"` content stays findable |
| 7 | + * @position reset.css integration proof. The browser hides until-found content |
| 8 | + * with `content-visibility: hidden`, which find in page and text fragments |
| 9 | + * can search and reveal; `display: none` takes the content out of both, and |
| 10 | + * only a shipping engine shows which one applies. |
| 11 | + */ |
| 12 | + |
| 13 | +import * as fs from 'node:fs'; |
| 14 | +import {fileURLToPath} from 'node:url'; |
| 15 | +import {expect, test} from '@playwright/test'; |
| 16 | + |
| 17 | +const RESET_CSS = fs.readFileSync( |
| 18 | + fileURLToPath(new URL('./reset.css', import.meta.url)), |
| 19 | + 'utf8', |
| 20 | +); |
| 21 | + |
| 22 | +// Served at a real URL so a text fragment navigation can run against it. |
| 23 | +const ORIGIN = 'http://reset.test'; |
| 24 | +const PAGE = `<!doctype html> |
| 25 | +<style>${RESET_CSS}</style> |
| 26 | +<div style="height: 3000px">Above the fold</div> |
| 27 | +<div id="answer" hidden="until-found">The hidden answer</div> |
| 28 | +<div id="closed" hidden>Plain hidden text</div> |
| 29 | +<script> |
| 30 | + document.getElementById('answer').addEventListener('beforematch', () => { |
| 31 | + document.body.dataset.beforematch = 'fired'; |
| 32 | + }); |
| 33 | +</script>`; |
| 34 | + |
| 35 | +test.beforeEach(async ({page}) => { |
| 36 | + await page.route(`${ORIGIN}/**`, async route => |
| 37 | + route.fulfill({contentType: 'text/html', body: PAGE}), |
| 38 | + ); |
| 39 | +}); |
| 40 | + |
| 41 | +test('until-found content keeps the browser rendering', async ({page}) => { |
| 42 | + await page.goto(`${ORIGIN}/`); |
| 43 | + const styles = await page.evaluate(() => |
| 44 | + ['answer', 'closed'].map(id => { |
| 45 | + const element = document.getElementById(id); |
| 46 | + if (!element) { |
| 47 | + throw new Error(`missing #${id}`); |
| 48 | + } |
| 49 | + const style = getComputedStyle(element); |
| 50 | + return { |
| 51 | + display: style.display, |
| 52 | + contentVisibility: style.contentVisibility, |
| 53 | + }; |
| 54 | + }), |
| 55 | + ); |
| 56 | + expect(styles).toEqual([ |
| 57 | + {display: 'block', contentVisibility: 'hidden'}, |
| 58 | + {display: 'none', contentVisibility: 'visible'}, |
| 59 | + ]); |
| 60 | +}); |
| 61 | + |
| 62 | +test('a text fragment finds until-found content and reveals it', async ({ |
| 63 | + page, |
| 64 | +}) => { |
| 65 | + await page.goto(`${ORIGIN}/#:~:text=hidden%20answer`); |
| 66 | + // The browser removes the attribute and fires beforematch when it reveals. |
| 67 | + await expect(page.locator('#answer')).not.toHaveAttribute('hidden'); |
| 68 | + await expect(page.locator('#answer')).toBeInViewport(); |
| 69 | + await expect(page.locator('body')).toHaveAttribute( |
| 70 | + 'data-beforematch', |
| 71 | + 'fired', |
| 72 | + ); |
| 73 | + // Plain hidden content stays out of reach. |
| 74 | + await expect(page.locator('#closed')).toBeHidden(); |
| 75 | +}); |
0 commit comments