@@ -105,7 +105,7 @@ describe("HTML render selection bridge", () => {
105105
106106 it ( "reveals a quote through nested scroll containers before returning its target" , ( ) => {
107107 const p = page (
108- '<div id="outer" style="overflow-x: auto; overflow-y: auto"><div id="inner" style="overflow-x: auto; overflow-y: auto"><p>quoted text</p></div></div>' ,
108+ '<div id="outer" style="overflow-x: auto; overflow-y: auto; scroll-behavior: smooth "><div id="inner" style="overflow-x: auto; overflow-y: auto; scroll-behavior: smooth "><p>quoted text</p></div></div>' ,
109109 ) ;
110110 const outer = p . view . document . querySelector < HTMLElement > ( "#outer" ) ! ;
111111 const inner = p . view . document . querySelector < HTMLElement > ( "#inner" ) ! ;
@@ -121,6 +121,20 @@ describe("HTML render selection bridge", () => {
121121 scrollHeight : { value : 1000 } ,
122122 scrollWidth : { value : 1000 } ,
123123 } ) ;
124+ for ( const element of [ inner , outer ] ) {
125+ let top = 0 ;
126+ let left = 0 ;
127+ Object . defineProperties ( element , {
128+ // Auto scrolling follows the page's smooth behavior and does not finish synchronously.
129+ scrollTop : { get : ( ) => top , set : ( ) => { } } ,
130+ scrollLeft : { get : ( ) => left , set : ( ) => { } } ,
131+ } ) ;
132+ element . scrollBy = ( options ?: ScrollToOptions | number ) => {
133+ if ( typeof options !== "object" || options . behavior !== "instant" ) return ;
134+ top += options . top ?? 0 ;
135+ left += options . left ?? 0 ;
136+ } ;
137+ }
124138 const bounds = ( left : number , top : number , width : number , height : number ) =>
125139 new p . view . DOMRect ( left , top , width , height ) ;
126140 inner . getBoundingClientRect = ( ) =>
@@ -147,8 +161,16 @@ describe("HTML render selection bridge", () => {
147161 expect ( target ) . toEqual ( { left : 80 , top : 145 , width : 80 , height : 15 } ) ;
148162 expect ( scroll ) . not . toHaveBeenCalled ( ) ;
149163
150- inner . scrollTop = inner . scrollLeft = 500 ;
151- outer . scrollTop = outer . scrollLeft = 300 ;
164+ inner . scrollBy ( {
165+ top : 500 - inner . scrollTop ,
166+ left : 500 - inner . scrollLeft ,
167+ behavior : "instant" ,
168+ } ) ;
169+ outer . scrollBy ( {
170+ top : 300 - outer . scrollTop ,
171+ left : 300 - outer . scrollLeft ,
172+ behavior : "instant" ,
173+ } ) ;
152174 p . command ( "target" , { text : "quoted text" , start : 0 , end : 11 , prefix : "" , suffix : "" } ) ;
153175 expect ( htmlSelectionParams ( p . messages ( ) . at ( - 1 ) ) ?. target ) . toEqual ( {
154176 left : 10 ,
@@ -158,6 +180,49 @@ describe("HTML render selection bridge", () => {
158180 } ) ;
159181 } ) ;
160182
183+ it . each ( [
184+ { left : 700 , top : 20 , expectedLeft : 160 , expectedTop : 20 } ,
185+ { left : - 200 , top : 20 , expectedLeft : 80 , expectedTop : 20 } ,
186+ { left : 10 , top : 700 , expectedLeft : 10 , expectedTop : 80 } ,
187+ { left : 10 , top : - 200 , expectedLeft : 10 , expectedTop : 80 } ,
188+ { left : 700 , top : 700 , expectedLeft : 160 , expectedTop : 80 } ,
189+ ] ) (
190+ "reveals a quote at ($left, $top) before replying on a smooth-scrolling page" ,
191+ ( { left, top, expectedLeft, expectedTop } ) => {
192+ const p = page ( "<style>html { scroll-behavior: smooth }</style><p>quoted text</p>" ) ;
193+ Object . defineProperties ( p . view , {
194+ innerWidth : { value : 320 } ,
195+ innerHeight : { value : 240 } ,
196+ } ) ;
197+ let dx = 0 ;
198+ let dy = 0 ;
199+ const bounds = ( ) => new p . view . DOMRect ( left - dx , top - dy , 80 , 15 ) ;
200+ Object . assign ( p . view . Range . prototype , {
201+ getBoundingClientRect : bounds ,
202+ getClientRects : ( ) => [ bounds ( ) ] ,
203+ } ) ;
204+ const scroll = vi
205+ . spyOn ( p . view , "scrollBy" )
206+ . mockImplementation ( ( options ?: ScrollToOptions | number ) => {
207+ // The reply must use coordinates after scrolling, without waiting for an animation.
208+ if ( typeof options !== "object" || options . behavior !== "instant" ) return ;
209+ dx += options . left ?? 0 ;
210+ dy += options . top ?? 0 ;
211+ } ) ;
212+ const quote = { text : "quoted text" , start : 0 , end : 11 , prefix : "" , suffix : "" } ;
213+ p . command ( "mark" , quote ) ;
214+ expect ( scroll ) . not . toHaveBeenCalled ( ) ;
215+ p . command ( "target" , quote ) ;
216+ expect ( htmlSelectionParams ( p . messages ( ) . at ( - 1 ) ) ?. target ) . toEqual ( {
217+ left : expectedLeft ,
218+ top : expectedTop ,
219+ width : 80 ,
220+ height : 15 ,
221+ } ) ;
222+ expect ( scroll ) . toHaveBeenCalledTimes ( 1 ) ;
223+ } ,
224+ ) ;
225+
161226 it ( "keeps native Tab navigation for oversized selections" , async ( ) => {
162227 const p = page ( `<p>${ "x" . repeat ( 8001 ) } </p><button>Next</button>` ) ;
163228 const paragraph = p . view . document . querySelector ( "p" ) ! ;
0 commit comments