MCPcopy Create free account
hub / github.com/testing-library/user-event / patchFocus

Function patchFocus

src/document/patchFocus.ts:12–85  ·  view source on GitHub ↗
(HTMLElement: typeof globalThis['HTMLElement'])

Source from the content-addressed store, hash-verified

10}
11
12export function patchFocus(HTMLElement: typeof globalThis['HTMLElement']) {
13 if (HTMLElement.prototype[patched]) {
14 return
15 }
16
17 // eslint-disable-next-line @typescript-eslint/unbound-method
18 const {focus, blur} = HTMLElement.prototype
19
20 Object.defineProperties(HTMLElement.prototype, {
21 focus: {
22 configurable: true,
23 value: patchedFocus,
24 writable: true,
25 },
26 blur: {
27 configurable: true,
28 value: patchedBlur,
29 writable: true,
30 },
31 [patched]: {
32 configurable: true,
33 get: () => ({focus, blur}),
34 },
35 })
36
37 let activeCall: symbol
38
39 function patchedFocus(this: HTMLElement, options: FocusOptions) {
40 if (this.ownerDocument.visibilityState !== 'hidden') {
41 return focus.call(this, options)
42 }
43
44 const blurred = getActiveTarget(this.ownerDocument)
45 if (blurred === this) {
46 return
47 }
48
49 const thisCall = Symbol('focus call')
50 activeCall = thisCall
51
52 if (blurred) {
53 blur.call(blurred)
54 dispatchDOMEvent(blurred, 'blur', {relatedTarget: this})
55 dispatchDOMEvent(blurred, 'focusout', {
56 relatedTarget: activeCall === thisCall ? this : null,
57 })
58 }
59 if (activeCall === thisCall) {
60 focus.call(this, options)
61 dispatchDOMEvent(this, 'focus', {relatedTarget: blurred})
62 }
63 if (activeCall === thisCall) {
64 dispatchDOMEvent(this, 'focusin', {relatedTarget: blurred})
65 }
66 }
67
68 function patchedBlur(this: HTMLElement) {
69 if (this.ownerDocument.visibilityState !== 'hidden') {

Callers 5

setupMainFunction · 0.90
setupDirectFunction · 0.90
prepareFunction · 0.90
patchFocus.tsFile · 0.90
focus.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected