@a11y-pulse/browser-adaptor 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ import {
2
+ nextPaintScript
3
+ } from "../chunk-QUDMCLZ7.js";
4
+
1
5
  // src/adaptors/playwright.ts
2
6
  var cdpSessions = /* @__PURE__ */ new WeakMap();
3
7
  var focusEmulatedPages = /* @__PURE__ */ new WeakSet();
@@ -56,6 +60,7 @@ var PlaywrightAdaptor = class {
56
60
  await this.page.keyboard.press("Enter");
57
61
  }
58
62
  async screenshotClip(clip, scale = 1) {
63
+ await this.page.evaluate(nextPaintScript);
59
64
  const session = await cdpSession(this.page);
60
65
  if (session) {
61
66
  const { data } = await session.send("Page.captureScreenshot", {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adaptors/playwright.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"playwright-core\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\n\n// The CDP session for each page, shared by focus emulation and clipped screenshots. A fresh adaptor\n// is created per audit run, but a page often outlives one run, so without this each run would open\n// another session that is never detached. The session is kept attached for the page's lifetime:\n// Chrome clears focus emulation when the CDP client disconnects. Resolves to null on Firefox and\n// WebKit, where Playwright offers no CDP.\nconst cdpSessions = new WeakMap<Page, Promise<CDPSession | null>>();\n\n// Pages that have had focus emulation enabled at least once.\nconst focusEmulatedPages = new WeakSet<Page>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nconst EVAL_BLOCKED = /EvalError|unsafe-eval|Content Security Policy/i;\n\nfunction cdpSession(page: Page): Promise<CDPSession | null> {\n\tconst existing = cdpSessions.get(page);\n\n\tif (existing) {\n\t\treturn existing;\n\t}\n\n\tconst session = page\n\t\t.context()\n\t\t.newCDPSession(page)\n\t\t.catch(() => null);\n\tcdpSessions.set(page, session);\n\n\treturn session;\n}\n\nfunction isChromium(page: Page): boolean {\n\ttry {\n\t\treturn page.context().browser()?.browserType().name() === \"chromium\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nexport type PlaywrightAdaptorOptions = {\n\t/**\n\t * Device scale factor for evidence screenshots. Defaults to 2. On Chromium any value is honoured\n\t * per capture. Elsewhere this must match the `deviceScaleFactor` the browser context was created\n\t * with, because Playwright's public screenshot API cannot rescale a single capture.\n\t */\n\tscreenshotClipScale?: number;\n};\n\n/** A BrowserAdaptor backed by a Playwright Page. */\nexport class PlaywrightAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PlaywrightAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t}\n\n\tasync evaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\ttry {\n\t\t\t// Playwright takes a single argument, so the function source and its args travel packed\n\t\t\t// into one tuple. Handles nested in that tuple are still resolved to live elements.\n\t\t\treturn (await this.page.evaluate(\n\t\t\t\t([source, packed]) => new Function(`return (${source})`)()(...(packed as unknown[])),\n\t\t\t\t[fn.toString(), args] as [string, unknown[]],\n\t\t\t)) as T;\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\n\t\t\tif (EVAL_BLOCKED.test(message)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"The Playwright adaptor rebuilds page functions with new Function(), which this page's Content-Security-Policy blocks. Allow 'unsafe-eval' in script-src, or use the Puppeteer adaptor.\",\n\t\t\t\t\t{ cause: error },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (session) {\n\t\t\t// The public API cannot scale one capture, and its `clip` is only honoured inside the\n\t\t\t// viewport. CDP is what Puppeteer drives too, so evidence images stay pixel-comparable.\n\t\t\tconst { data } = await session.send(\"Page.captureScreenshot\", {\n\t\t\t\tformat: \"png\",\n\t\t\t\tcaptureBeyondViewport: true,\n\t\t\t\tclip: { ...clip, scale },\n\t\t\t});\n\n\t\t\treturn Buffer.from(data, \"base64\");\n\t\t}\n\n\t\t// Clips are document coordinates, and Playwright renders anything outside the viewport blank\n\t\t// unless the capture is full-page.\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\tfullPage: true,\n\t\t\tclip,\n\t\t\tscale: \"device\",\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (!session) {\n\t\t\t// Playwright emulates focus on Firefox and WebKit itself, so there is nothing to assert\n\t\t\t// and nothing worth warning about.\n\t\t\tif (isChromium(this.page)) {\n\t\t\t\tconsole.warn(\"Could not open a CDP session for the browser adaptor\");\n\t\t\t}\n\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tif (focusEmulatedPages.has(this.page)) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulatedPages.add(this.page);\n\t\t} catch (error) {\n\t\t\tconsole.warn(\"Could not enable focus emulation for the browser adaptor\", {\n\t\t\t\terror,\n\t\t\t});\n\t\t}\n\t}\n}\n"],"mappings":";AAQA,IAAM,cAAc,oBAAI,QAA0C;AAGlE,IAAM,qBAAqB,oBAAI,QAAc;AAE7C,IAAM,gCAAgC;AAEtC,IAAM,eAAe;AAErB,SAAS,WAAW,MAAwC;AAC3D,QAAM,WAAW,YAAY,IAAI,IAAI;AAErC,MAAI,UAAU;AACb,WAAO;AAAA,EACR;AAEA,QAAM,UAAU,KACd,QAAQ,EACR,cAAc,IAAI,EAClB,MAAM,MAAM,IAAI;AAClB,cAAY,IAAI,MAAM,OAAO;AAE7B,SAAO;AACR;AAEA,SAAS,WAAW,MAAqB;AACxC,MAAI;AACH,WAAO,KAAK,QAAQ,EAAE,QAAQ,GAAG,YAAY,EAAE,KAAK,MAAM;AAAA,EAC3D,QAAQ;AACP,WAAO;AAAA,EACR;AACD;AAYO,IAAM,oBAAN,MAAkD;AAAA,EAGxD,YACkB,MACjB,UAAoC,CAAC,GACpC;AAFgB;AAGjB,SAAK,sBAAsB,QAAQ,uBAAuB;AAAA,EAC3D;AAAA,EAJkB;AAAA,EAHT;AAAA,EAST,MAAM,SAEL,OACG,MACU;AACb,QAAI;AAGH,aAAQ,MAAM,KAAK,KAAK;AAAA,QACvB,CAAC,CAAC,QAAQ,MAAM,MAAM,IAAI,SAAS,WAAW,MAAM,GAAG,EAAE,EAAE,GAAI,MAAoB;AAAA,QACnF,CAAC,GAAG,SAAS,GAAG,IAAI;AAAA,MACrB;AAAA,IACD,SAAS,OAAO;AACf,YAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAErE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AQ,KAAK,sCAAsC;AAAA,QACxD,SAAS;AAAA,MACV,CAAC;AACD,yBAAmB,IAAI,KAAK,IAAI;AAAA,IACjC,SAAS,OAAO;AACf,cAAQ,KAAK,4DAA4D;AAAA,QACxE;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/adaptors/playwright.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"playwright-core\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\nimport { nextPaintScript } from \"../paint\";\n\n// The CDP session for each page, shared by focus emulation and clipped screenshots. A fresh adaptor\n// is created per audit run, but a page often outlives one run, so without this each run would open\n// another session that is never detached. The session is kept attached for the page's lifetime:\n// Chrome clears focus emulation when the CDP client disconnects. Resolves to null on Firefox and\n// WebKit, where Playwright offers no CDP.\nconst cdpSessions = new WeakMap<Page, Promise<CDPSession | null>>();\n\n// Pages that have had focus emulation enabled at least once.\nconst focusEmulatedPages = new WeakSet<Page>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nconst EVAL_BLOCKED = /EvalError|unsafe-eval|Content Security Policy/i;\n\nfunction cdpSession(page: Page): Promise<CDPSession | null> {\n\tconst existing = cdpSessions.get(page);\n\n\tif (existing) {\n\t\treturn existing;\n\t}\n\n\tconst session = page\n\t\t.context()\n\t\t.newCDPSession(page)\n\t\t.catch(() => null);\n\tcdpSessions.set(page, session);\n\n\treturn session;\n}\n\nfunction isChromium(page: Page): boolean {\n\ttry {\n\t\treturn page.context().browser()?.browserType().name() === \"chromium\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nexport type PlaywrightAdaptorOptions = {\n\t/**\n\t * Device scale factor for evidence screenshots. Defaults to 2. On Chromium any value is honoured\n\t * per capture. Elsewhere this must match the `deviceScaleFactor` the browser context was created\n\t * with, because Playwright's public screenshot API cannot rescale a single capture.\n\t */\n\tscreenshotClipScale?: number;\n};\n\n/** A BrowserAdaptor backed by a Playwright Page. */\nexport class PlaywrightAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PlaywrightAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t}\n\n\tasync evaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\ttry {\n\t\t\t// Playwright takes a single argument, so the function source and its args travel packed\n\t\t\t// into one tuple. Handles nested in that tuple are still resolved to live elements.\n\t\t\treturn (await this.page.evaluate(\n\t\t\t\t([source, packed]) => new Function(`return (${source})`)()(...(packed as unknown[])),\n\t\t\t\t[fn.toString(), args] as [string, unknown[]],\n\t\t\t)) as T;\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\n\t\t\tif (EVAL_BLOCKED.test(message)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"The Playwright adaptor rebuilds page functions with new Function(), which this page's Content-Security-Policy blocks. Allow 'unsafe-eval' in script-src, or use the Puppeteer adaptor.\",\n\t\t\t\t\t{ cause: error },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\tawait this.page.evaluate(nextPaintScript);\n\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (session) {\n\t\t\t// The public API cannot scale one capture, and its `clip` is only honoured inside the\n\t\t\t// viewport. CDP is what Puppeteer drives too, so evidence images stay pixel-comparable.\n\t\t\tconst { data } = await session.send(\"Page.captureScreenshot\", {\n\t\t\t\tformat: \"png\",\n\t\t\t\tcaptureBeyondViewport: true,\n\t\t\t\tclip: { ...clip, scale },\n\t\t\t});\n\n\t\t\treturn Buffer.from(data, \"base64\");\n\t\t}\n\n\t\t// Clips are document coordinates, and Playwright renders anything outside the viewport blank\n\t\t// unless the capture is full-page.\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\tfullPage: true,\n\t\t\tclip,\n\t\t\tscale: \"device\",\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (!session) {\n\t\t\t// Playwright emulates focus on Firefox and WebKit itself, so there is nothing to assert\n\t\t\t// and nothing worth warning about.\n\t\t\tif (isChromium(this.page)) {\n\t\t\t\tconsole.warn(\"Could not open a CDP session for the browser adaptor\");\n\t\t\t}\n\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tif (focusEmulatedPages.has(this.page)) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulatedPages.add(this.page);\n\t\t} catch (error) {\n\t\t\tconsole.warn(\"Could not enable focus emulation for the browser adaptor\", {\n\t\t\t\terror,\n\t\t\t});\n\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1
+ import {
2
+ nextPaintScript
3
+ } from "../chunk-QUDMCLZ7.js";
4
+
1
5
  // src/adaptors/puppeteer.ts
2
6
  var focusEmulationSessions = /* @__PURE__ */ new WeakMap();
3
7
  var DEFAULT_SCREENSHOT_CLIP_SCALE = 2;
@@ -26,6 +30,7 @@ var PuppeteerAdaptor = class {
26
30
  await this.page.keyboard.press("Enter");
27
31
  }
28
32
  async screenshotClip(clip, scale = 1) {
33
+ await this.page.evaluate(nextPaintScript);
29
34
  return await this.page.screenshot({
30
35
  type: "png",
31
36
  optimizeForSpeed: this.optimizeForSpeed,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adaptors/puppeteer.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"puppeteer\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\n\n// The CDP session holding focus emulation for each page. A fresh adaptor is created per audit run,\n// but a page often outlives one run, so without this each run would open another CDP session that\n// is never detached. The session is kept attached for the page's lifetime: Chrome clears the\n// emulation when the CDP client disconnects.\nconst focusEmulationSessions = new WeakMap<Page, CDPSession>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nexport type PuppeteerAdaptorOptions = {\n\t/** Device scale factor for evidence screenshots. Defaults to 2, applied per capture. */\n\tscreenshotClipScale?: number;\n\n\t/**\n\t * Trade PNG compression for capture speed. Defaults to true, which roughly doubles the encoded\n\t * size of each capture without changing a single pixel.\n\t */\n\toptimizeForSpeed?: boolean;\n};\n\n/** A BrowserAdaptor backed by a Puppeteer Page. */\nexport class PuppeteerAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tprivate readonly optimizeForSpeed: boolean;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PuppeteerAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t\tthis.optimizeForSpeed = options.optimizeForSpeed ?? true;\n\t}\n\n\tevaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\t// Puppeteer resolves any JSHandle passed as an arg to the live element.\n\t\treturn this.page.evaluate(fn as never, ...(args as never[])) as Promise<T>;\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\toptimizeForSpeed: this.optimizeForSpeed,\n\t\t\tclip: { ...clip, scale },\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\ttry {\n\t\t\tconst existing = focusEmulationSessions.get(this.page);\n\n\t\t\tif (existing) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: true,\n\t\t\t\t});\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst client = await this.page.createCDPSession();\n\t\t\tawait client.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulationSessions.set(this.page, client);\n\t\t} catch (error) {\n\t\t\tconsole.warn(\"Could not enable focus emulation for the browser adaptor\", {\n\t\t\t\terror,\n\t\t\t});\n\t\t}\n\t}\n}\n"],"mappings":";AAOA,IAAM,yBAAyB,oBAAI,QAA0B;AAE7D,IAAM,gCAAgC;AAc/B,IAAM,mBAAN,MAAiD;AAAA,EAKvD,YACkB,MACjB,UAAmC,CAAC,GACnC;AAFgB;AAGjB,SAAK,sBAAsB,QAAQ,uBAAuB;AAC1D,SAAK,mBAAmB,QAAQ,oBAAoB;AAAA,EACrD;AAAA,EALkB;AAAA,EALT;AAAA,EAEQ;AAAA,EAUjB,SAEC,OACG,MACU;AAEb,WAAO,KAAK,KAAK,SAAS,IAAa,GAAI,IAAgB;AAAA,EAC5D;AAAA,EAEA,eAAe,IAA+C;AAC7D,WAAO,KAAK,KAAK,eAAe,EAAE;AAAA,EACnC;AAAA,EAEA,MAAM,WAAW,KAAgC;AAChD,UAAO,IAAiB,QAAQ;AAAA,EACjC;AAAA,EAEA,MAAM,WAA0B;AAC/B,UAAM,KAAK,KAAK,SAAS,MAAM,KAAK;AAAA,EACrC;AAAA,EAEA,MAAM,aAA4B;AACjC,UAAM,KAAK,KAAK,SAAS,MAAM,OAAO;AAAA,EACvC;AAAA,EAEA,MAAM,eAAe,MAAY,QAAQ,GAAwB;AAChE,WAAQ,MAAM,KAAK,KAAK,WAAW;AAAA,MAClC,MAAM;AAAA,MACN,kBAAkB,KAAK;AAAA,MACvB,MAAM,EAAE,GAAG,MAAM,MAAM;AAAA,IACxB,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,uBAAsC;AAK3C,QAAI;AACH,YAAM,WAAW,uBAAuB,IAAI,KAAK,IAAI;AAErD,UAAI,UAAU;AAEb,cAAM,SAAS,KAAK,sCAAsC;AAAA,UACzD,SAAS;AAAA,QACV,CAAC;AACD,cAAM,SAAS,KAAK,sCAAsC;AAAA,UACzD,SAAS;AAAA,QACV,CAAC;AAED;AAAA,MACD;AAEA,YAAM,SAAS,MAAM,KAAK,KAAK,iBAAiB;AAChD,YAAM,OAAO,KAAK,sCAAsC;AAAA,QACvD,SAAS;AAAA,MACV,CAAC;AACD,6BAAuB,IAAI,KAAK,MAAM,MAAM;AAAA,IAC7C,SAAS,OAAO;AACf,cAAQ,KAAK,4DAA4D;AAAA,QACxE;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/adaptors/puppeteer.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"puppeteer\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\nimport { nextPaintScript } from \"../paint\";\n\n// The CDP session holding focus emulation for each page. A fresh adaptor is created per audit run,\n// but a page often outlives one run, so without this each run would open another CDP session that\n// is never detached. The session is kept attached for the page's lifetime: Chrome clears the\n// emulation when the CDP client disconnects.\nconst focusEmulationSessions = new WeakMap<Page, CDPSession>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nexport type PuppeteerAdaptorOptions = {\n\t/** Device scale factor for evidence screenshots. Defaults to 2, applied per capture. */\n\tscreenshotClipScale?: number;\n\n\t/**\n\t * Trade PNG compression for capture speed. Defaults to true, which roughly doubles the encoded\n\t * size of each capture without changing a single pixel.\n\t */\n\toptimizeForSpeed?: boolean;\n};\n\n/** A BrowserAdaptor backed by a Puppeteer Page. */\nexport class PuppeteerAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tprivate readonly optimizeForSpeed: boolean;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PuppeteerAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t\tthis.optimizeForSpeed = options.optimizeForSpeed ?? true;\n\t}\n\n\tevaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\t// Puppeteer resolves any JSHandle passed as an arg to the live element.\n\t\treturn this.page.evaluate(fn as never, ...(args as never[])) as Promise<T>;\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\tawait this.page.evaluate(nextPaintScript);\n\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\toptimizeForSpeed: this.optimizeForSpeed,\n\t\t\tclip: { ...clip, scale },\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\ttry {\n\t\t\tconst existing = focusEmulationSessions.get(this.page);\n\n\t\t\tif (existing) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: true,\n\t\t\t\t});\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst client = await this.page.createCDPSession();\n\t\t\tawait client.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulationSessions.set(this.page, client);\n\t\t} catch (error) {\n\t\t\tconsole.warn(\"Could not enable focus emulation for the browser adaptor\", {\n\t\t\t\terror,\n\t\t\t});\n\t\t}\n\t}\n}\n"],"mappings":";;;;;AAQA,IAAM,yBAAyB,oBAAI,QAA0B;AAE7D,IAAM,gCAAgC;AAc/B,IAAM,mBAAN,MAAiD;AAAA,EAKvD,YACkB,MACjB,UAAmC,CAAC,GACnC;AAFgB;AAGjB,SAAK,sBAAsB,QAAQ,uBAAuB;AAC1D,SAAK,mBAAmB,QAAQ,oBAAoB;AAAA,EACrD;AAAA,EALkB;AAAA,EALT;AAAA,EAEQ;AAAA,EAUjB,SAEC,OACG,MACU;AAEb,WAAO,KAAK,KAAK,SAAS,IAAa,GAAI,IAAgB;AAAA,EAC5D;AAAA,EAEA,eAAe,IAA+C;AAC7D,WAAO,KAAK,KAAK,eAAe,EAAE;AAAA,EACnC;AAAA,EAEA,MAAM,WAAW,KAAgC;AAChD,UAAO,IAAiB,QAAQ;AAAA,EACjC;AAAA,EAEA,MAAM,WAA0B;AAC/B,UAAM,KAAK,KAAK,SAAS,MAAM,KAAK;AAAA,EACrC;AAAA,EAEA,MAAM,aAA4B;AACjC,UAAM,KAAK,KAAK,SAAS,MAAM,OAAO;AAAA,EACvC;AAAA,EAEA,MAAM,eAAe,MAAY,QAAQ,GAAwB;AAChE,UAAM,KAAK,KAAK,SAAS,eAAe;AAExC,WAAQ,MAAM,KAAK,KAAK,WAAW;AAAA,MAClC,MAAM;AAAA,MACN,kBAAkB,KAAK;AAAA,MACvB,MAAM,EAAE,GAAG,MAAM,MAAM;AAAA,IACxB,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,uBAAsC;AAK3C,QAAI;AACH,YAAM,WAAW,uBAAuB,IAAI,KAAK,IAAI;AAErD,UAAI,UAAU;AAEb,cAAM,SAAS,KAAK,sCAAsC;AAAA,UACzD,SAAS;AAAA,QACV,CAAC;AACD,cAAM,SAAS,KAAK,sCAAsC;AAAA,UACzD,SAAS;AAAA,QACV,CAAC;AAED;AAAA,MACD;AAEA,YAAM,SAAS,MAAM,KAAK,KAAK,iBAAiB;AAChD,YAAM,OAAO,KAAK,sCAAsC;AAAA,QACvD,SAAS;AAAA,MACV,CAAC;AACD,6BAAuB,IAAI,KAAK,MAAM,MAAM;AAAA,IAC7C,SAAS,OAAO;AACf,cAAQ,KAAK,4DAA4D;AAAA,QACxE;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;","names":[]}
@@ -0,0 +1,13 @@
1
+ // src/paint.ts
2
+ function nextPaintScript() {
3
+ return new Promise((resolve) => {
4
+ requestAnimationFrame(() => {
5
+ requestAnimationFrame(() => resolve());
6
+ });
7
+ });
8
+ }
9
+
10
+ export {
11
+ nextPaintScript
12
+ };
13
+ //# sourceMappingURL=chunk-QUDMCLZ7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/paint.ts"],"sourcesContent":["/**\n * A doubleRAF that aims to wait until the page has painted the next frame.\n */\nexport function nextPaintScript(): Promise<void> {\n\treturn new Promise((resolve) => {\n\t\trequestAnimationFrame(() => {\n\t\t\trequestAnimationFrame(() => resolve());\n\t\t});\n\t});\n}\n"],"mappings":";AAGO,SAAS,kBAAiC;AAChD,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC/B,0BAAsB,MAAM;AAC3B,4BAAsB,MAAM,QAAQ,CAAC;AAAA,IACtC,CAAC;AAAA,EACF,CAAC;AACF;","names":[]}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * A doubleRAF that aims to wait until the page has painted the next frame.
3
+ */
4
+ export declare function nextPaintScript(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a11y-pulse/browser-adaptor",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Shared browser adaptor primitives and DOM helpers for A11y Pulse accessibility audits.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -77,7 +77,7 @@
77
77
  }
78
78
  },
79
79
  "devDependencies": {
80
- "happy-dom": "^20.12.0",
80
+ "happy-dom": "^20.14.5",
81
81
  "playwright-core": "^1.63.0",
82
82
  "puppeteer": "^25.10.0"
83
83
  }