@@ -229,6 +229,13 @@ const INTERACTIVE = [
229229 '[role="switch"]' ,
230230 '[role="tab"]' ,
231231 '[role="menuitem"]' ,
232+ '[role="menuitemcheckbox"]' ,
233+ '[role="menuitemradio"]' ,
234+ '[role="treeitem"]' ,
235+ '[role="combobox"]' ,
236+ '[role="textbox"]' ,
237+ '[role="searchbox"]' ,
238+ '[role="spinbutton"]' ,
232239 '[role="option"]' ,
233240 '[role="slider"]' ,
234241 '[tabindex]:not([tabindex="-1"])' ,
@@ -330,19 +337,43 @@ interface Candidate {
330337 readonly rect : HitRect ;
331338}
332339
333- /** The page's interactive elements and their rects: one read. */
340+ /** The watched element `element` belongs to, if any (itself included). */
341+ function watchedOwner ( element : Element ) : Element | null {
342+ for (
343+ let node : Element | null = element ;
344+ node != null ;
345+ node = node . parentElement
346+ ) {
347+ if ( watched . has ( node ) ) {
348+ return node ;
349+ }
350+ }
351+ return null ;
352+ }
353+
354+ /**
355+ * The page's interactive elements and their rects: one read. A watched
356+ * control counts by its own box, never by a part inside it (its invisible
357+ * input is grown by its hit area, which must not shrink a neighbour's).
358+ */
334359function readCandidates ( doc : Document ) : Candidate [ ] {
335- return [ ...doc . querySelectorAll ( INTERACTIVE ) ] . map ( element => ( {
336- element,
337- rect : plainRect ( element ) ,
338- } ) ) ;
360+ const seen = new Set < Element > ( ) ;
361+ const candidates : Candidate [ ] = [ ] ;
362+ for ( const found of doc . querySelectorAll ( INTERACTIVE ) ) {
363+ const element = watchedOwner ( found ) ?? found ;
364+ if ( ! seen . has ( element ) ) {
365+ seen . add ( element ) ;
366+ candidates . push ( { element, rect : plainRect ( element ) } ) ;
367+ }
368+ }
369+ return candidates ;
339370}
340371
341372/** A control's rect and clamped insets, from candidates already read. */
342373function measureOne (
343374 watch : Watch ,
344375 candidates : ReadonlyArray < Candidate > ,
345- ) : { target : HitRect ; insets : HitInsets } {
376+ ) : { target : HitRect ; insets : HitInsets ; neighbours : HitRect [ ] } {
346377 const target = plainRect ( watch . element ) ;
347378 // Two targets each growing up to half the minimum reach each other from a
348379 // minimum apart.
@@ -362,7 +393,11 @@ function measureOne(
362393 ! isOwn ( watch , element ) ,
363394 )
364395 . map ( ( { rect} ) => rect ) ;
365- return { target, insets : clampedHitInsets ( target , neighbours , watch . minSize ) } ;
396+ return {
397+ target,
398+ insets : clampedHitInsets ( target , neighbours , watch . minSize ) ,
399+ neighbours,
400+ } ;
366401}
367402
368403/** One pass: every pending control's reads, then every write. */
@@ -424,25 +459,48 @@ function scheduleAll(): void {
424459 * neighbour, and the next tap uses the fresh insets.
425460 */
426461function onPress ( event : Event ) : void {
427- const element = event . currentTarget as Element ;
462+ // One listener for every watched control, on the document: a control, a
463+ // row or a handle carries none of its own (a list's listeners stay the
464+ // list's, spec:AST-103 FR5). The innermost watched element the press
465+ // lands in is the one it is for.
466+ for (
467+ let node : Element | null =
468+ event . target instanceof Element ? event . target : null ;
469+ node != null ;
470+ node = node . parentElement
471+ ) {
472+ if ( watched . has ( node ) ) {
473+ checkPress ( node , event as PointerEvent ) ;
474+ return ;
475+ }
476+ }
477+ }
478+
479+ function checkPress ( element : Element , event : PointerEvent ) : void {
428480 const entry = watched . get ( element ) ;
429481 if ( entry == null ) {
430482 return ;
431483 }
432484 // A swallower left from a touch that sent no click goes with the next one.
433485 entry . disarm ?.( ) ;
434- const press = event as PointerEvent ;
486+ const press = event ;
435487 if ( ! isCoarse || press . pointerType !== 'touch' ) {
436488 return ;
437489 }
438490 const candidates = readCandidates ( element . ownerDocument ) ;
439- let isInside = false ;
491+ const x = press . clientX ;
492+ const y = press . clientY ;
493+ // The touch is the control's unless a neighbour is nearer it: inside the
494+ // fresh area too (a corner beside an offset or diagonal neighbour), and
495+ // a few px past it, where a browser's own touch adjustment reaches.
496+ let isNearerNeighbour = false ;
440497 for ( const watch of entry . watches ) {
441- const { target, insets} = measureOne ( watch , candidates ) ;
498+ const { target, insets, neighbours } = measureOne ( watch , candidates ) ;
442499 deliver ( watch , insets ) ;
443- isInside ||= contains ( grown ( target , insets ) , press . clientX , press . clientY ) ;
500+ const own = distanceTo ( target , x , y ) ;
501+ isNearerNeighbour ||= neighbours . some ( rect => distanceTo ( rect , x , y ) < own ) ;
444502 }
445- if ( ! isInside ) {
503+ if ( isNearerNeighbour ) {
446504 const swallow = ( click : Event ) => {
447505 disarm ( ) ;
448506 // A keyboard or assistive-technology activation (detail 0) is never
@@ -501,7 +559,6 @@ function observe(element: Element, entry: Watched): void {
501559 }
502560 const stopScope =
503561 parent == null ? null : observeContent ( parent , ( ) => onScopeChange ( parent ) ) ;
504- element . addEventListener ( 'pointerdown' , onPress , PASSIVE_CAPTURE ) ;
505562 entry . release = ( ) => {
506563 visibility ?. unobserve ( element ) ;
507564 resize ?. unobserve ( element ) ;
@@ -517,7 +574,6 @@ function observe(element: Element, entry: Watched): void {
517574 }
518575 }
519576 stopScope ?.( ) ;
520- element . removeEventListener ( 'pointerdown' , onPress , PASSIVE_CAPTURE ) ;
521577 entry . disarm ?.( ) ;
522578 } ;
523579}
@@ -532,6 +588,7 @@ function startObserving(): void {
532588 return ;
533589 }
534590 view . addEventListener ( 'resize' , scheduleAll ) ;
591+ view . document . addEventListener ( 'pointerdown' , onPress , PASSIVE_CAPTURE ) ;
535592 if ( typeof IntersectionObserver === 'function' ) {
536593 visibility = new IntersectionObserver ( entries => {
537594 for ( const entry of entries ) {
@@ -587,6 +644,7 @@ function stopObserving(): void {
587644 entry . release = null ;
588645 }
589646 view ?. removeEventListener ( 'resize' , scheduleAll ) ;
647+ view ?. document . removeEventListener ( 'pointerdown' , onPress , PASSIVE_CAPTURE ) ;
590648 visibility ?. disconnect ( ) ;
591649 resize ?. disconnect ( ) ;
592650 visibility = null ;
@@ -616,6 +674,29 @@ function onPointerChange(): void {
616674 }
617675}
618676
677+ /** Options of `watchHitArea`. */
678+ export interface WatchHitAreaOptions {
679+ /** The size to grow toward, px. @default 44 */
680+ readonly minSize ?: number ;
681+ }
682+
683+ /**
684+ * Stops every watch and forgets them, for tests: no observer, listener or
685+ * answer outlives a test that left a watch behind.
686+ */
687+ export function resetHitAreaForTests ( ) : void {
688+ stopObserving ( ) ;
689+ for ( const entry of watched . values ( ) ) {
690+ entry . disarm ?.( ) ;
691+ }
692+ watched . clear ( ) ;
693+ pending . clear ( ) ;
694+ pointer ?. removeEventListener ( 'change' , onPointerChange ) ;
695+ pointer = null ;
696+ view = null ;
697+ isCoarse = false ;
698+ }
699+
619700/**
620701 * Keeps `element`'s hit insets live until the returned function runs:
621702 * `onInsets` hears each change (typically `writeHitInsets` on the control).
@@ -628,19 +709,23 @@ function onPointerChange(): void {
628709export function watchHitArea (
629710 element : Element ,
630711 onInsets : ( insets : HitInsets ) => void ,
631- minSize : number = MIN_TOUCH_TARGET ,
712+ { minSize = MIN_TOUCH_TARGET } : WatchHitAreaOptions = { } ,
632713) : ( ) => void {
633714 const owner = element . ownerDocument . defaultView ;
634715 if ( owner == null ) {
635716 return ( ) => { } ;
636717 }
637- if ( watched . size === 0 ) {
638- view = owner ;
639- pointer = owner . matchMedia ?.( TOUCH_QUERY ) ?? null ;
718+ view = owner ;
719+ // Read the pointer query at every watch, not only the first: an
720+ // environment that replaces matchMedia (a test) must not leave a stale
721+ // answer behind from a watch that never stopped.
722+ const query = owner . matchMedia ?.( TOUCH_QUERY ) ?? null ;
723+ if ( query !== pointer ) {
724+ pointer ?. removeEventListener ( 'change' , onPointerChange ) ;
725+ pointer = query ;
640726 pointer ?. addEventListener ( 'change' , onPointerChange ) ;
641- isCoarse = false ;
642- onPointerChange ( ) ;
643727 }
728+ onPointerChange ( ) ;
644729 const watch : Watch = {
645730 element,
646731 minSize,
0 commit comments