diff --git a/e2e/horizontalScrollHold.spec.ts b/e2e/horizontalScrollHold.spec.ts new file mode 100644 index 0000000000..87fdd53ed8 --- /dev/null +++ b/e2e/horizontalScrollHold.spec.ts @@ -0,0 +1,105 @@ +import type { Page } from "@playwright/test"; +import { expect, test } from "./fixtures"; + +// Regression tests for #7270 (spacer must never extend scrollWidth on right-edge overscroll or +// fractional scrollLeft) and #6624 / #6865 (spacer must still hold scrollLeft when a line-end hint +// temporarily disappears). + +async function openLongFile(page: Page, text: string) { + const path = "/dev/acme-api/src/LongLine.ts"; + await page.evaluate( + async ({ path, text }) => { + const backend = "/src/editor/core/backend.ts"; + (await import(/* @vite-ignore */ backend)).docBackend().files.set(path, text); + const service = "/src/workbench/contributions.ts"; + await (await import(/* @vite-ignore */ service)).editorService().openFile(path); + }, + { path, text }, + ); + const scroller = page.locator(".cm-scroller").first(); + await expect(scroller).toBeVisible(); + return scroller; +} + +test("right-edge elastic overscroll does not grow scrollWidth (#7270)", async ({ workbench: page }) => { + const scroller = await openLongFile(page, `${"x".repeat(300)}\n`); + await expect(page.locator(".cm-content")).toContainText("xxxxxxxxxx"); + + const result = await scroller.evaluate((el) => { + const startWidth = el.scrollWidth; + const cw = el.clientWidth; + const widths: number[] = []; + for (let i = 0; i < 5; i++) { + const overscrolled = el.scrollWidth - cw + 20; + Object.defineProperty(el, "scrollLeft", { configurable: true, get: () => overscrolled, set: () => {} }); + el.dispatchEvent(new Event("scroll")); + widths.push(el.scrollWidth); + } + delete (el as unknown as Record).scrollLeft; + return { startWidth, cw, widths }; + }); + + expect(result.startWidth).toBeGreaterThan(result.cw); + expect(result.widths).toEqual(Array(5).fill(result.startWidth)); +}); + +test("fractional scrollLeft at the right edge does not ratchet scrollWidth (#7270)", async ({ workbench: page }) => { + const scroller = await openLongFile(page, `${"x".repeat(300)}\n`); + await expect(page.locator(".cm-content")).toContainText("xxxxxxxxxx"); + + const result = await scroller.evaluate((el) => { + const startWidth = el.scrollWidth; + const cw = el.clientWidth; + const widths: number[] = []; + for (let i = 0; i < 5; i++) { + const fractional = el.scrollWidth - cw + 0.5; + Object.defineProperty(el, "scrollLeft", { configurable: true, get: () => fractional, set: () => {} }); + el.dispatchEvent(new Event("scroll")); + widths.push(el.scrollWidth); + } + delete (el as unknown as Record).scrollLeft; + return { startWidth, widths }; + }); + + expect(result.widths).toEqual(Array(5).fill(result.startWidth)); +}); + +test("still holds horizontal scroll position when a line-end hint disappears (#6624)", async ({ workbench: page }) => { + const scroller = await openLongFile(page, `${"x".repeat(200)}\n`); + await expect(page.locator(".cm-content")).toContainText("xxxxxxxxxx"); + + const result = await scroller.evaluate((el) => { + const baseWidth = el.scrollWidth; + // Simulate a line-end hint widening the scroller's overflow range without changing .cm-content. + const hint = document.createElement("div"); + hint.style.position = "absolute"; + hint.style.top = "0"; + hint.style.left = `${baseWidth}px`; + hint.style.width = "300px"; + hint.style.height = "1px"; + hint.style.pointerEvents = "none"; + el.appendChild(hint); + + const widthWithHint = el.scrollWidth; + // Scroll into the region that only exists because of the line-end hint. + el.scrollLeft = widthWithHint - el.clientWidth - 40; + el.dispatchEvent(new Event("scroll")); + const scrollBefore = el.scrollLeft; + + // Remove the line-end hint, as happens on an edit: scrollLeft must stay put. + hint.remove(); + const scrollWhileAway = el.scrollLeft; + + // Scrolling back to 0 releases the hold so the scroll range shrinks back to baseWidth. + el.scrollLeft = 0; + el.dispatchEvent(new Event("scroll")); + const widthAfterReturn = el.scrollWidth; + + return { baseWidth, clientWidth: el.clientWidth, widthWithHint, scrollBefore, scrollWhileAway, widthAfterReturn }; + }); + + expect(result.widthWithHint).toBeGreaterThan(result.baseWidth); + expect(result.scrollBefore).toBeGreaterThan(result.baseWidth - result.clientWidth); + expect(result.scrollWhileAway).toBe(result.scrollBefore); + expect(result.widthAfterReturn).toBe(result.baseWidth); +}); diff --git a/src/editor/core/cm/horizontalScrollHold.test.ts b/src/editor/core/cm/horizontalScrollHold.test.ts index 1db9fc0f3b..5ac33568eb 100644 --- a/src/editor/core/cm/horizontalScrollHold.test.ts +++ b/src/editor/core/cm/horizontalScrollHold.test.ts @@ -18,20 +18,87 @@ function mount(): EditorView { return view; } +function setMetrics(scroller: HTMLElement, clientWidth: number, scrollWidth: number) { + Object.defineProperty(scroller, "clientWidth", { configurable: true, value: clientWidth }); + Object.defineProperty(scroller, "scrollWidth", { configurable: true, value: scrollWidth }); +} + // A line-end hint that leaves on an edit shrinks the scroll range; the spacer at the visible right // edge keeps a horizontally scrolled view where it is instead of letting the browser pull it left. -it("keeps a spacer at the visible right edge as the view scrolls", () => { +it("keeps a spacer at the visible right edge as the view scrolls and resets at scrollLeft 0", () => { const view = mount(); const scroller = view.scrollDOM; const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; expect(spacer.getAttribute("aria-hidden")).toBe("true"); - Object.defineProperty(scroller, "clientWidth", { configurable: true, value: 500 }); + setMetrics(scroller, 500, 800); scroller.scrollLeft = 40; scroller.dispatchEvent(new Event("scroll")); expect(spacer.style.left).toBe("539px"); scroller.scrollLeft = 0; scroller.dispatchEvent(new Event("scroll")); - expect(spacer.style.left).toBe("499px"); + expect(spacer.style.left).toBe("0px"); +}); + +it("caps the spacer at scrollWidth - 1 during right-edge overscroll (#7270)", () => { + const view = mount(); + const scroller = view.scrollDOM; + const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; + setMetrics(scroller, 500, 800); + // Elastic overscroll pushes scrollLeft past scrollWidth - clientWidth (300). + scroller.scrollLeft = 340; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("799px"); +}); + +it("never places the spacer past scrollWidth on fractional scrollLeft (#7270)", () => { + const view = mount(); + const scroller = view.scrollDOM; + const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; + setMetrics(scroller, 500, 800); + // Mid-range fractional scroll keeps the full visible edge inside scrollWidth. + scroller.scrollLeft = 40.5; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("540px"); + // Right-edge fractional scroll stays capped so spacer right edge <= scrollWidth. + scroller.scrollLeft = 300.5; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("799px"); +}); + +it("leaves the spacer at 0 when content fits or on negative overscroll", () => { + const view = mount(); + const scroller = view.scrollDOM; + const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; + setMetrics(scroller, 500, 500); + scroller.scrollLeft = 10; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("0px"); + + setMetrics(scroller, 500, 800); + scroller.scrollLeft = -20; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("0px"); +}); + +it("tracks a growing scrollWidth when lines become longer and recedes when scrolling left", () => { + const view = mount(); + const scroller = view.scrollDOM; + const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; + setMetrics(scroller, 500, 800); + scroller.scrollLeft = 300; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("799px"); + + // Content grows wider: scrolling further right can reach the new end. + setMetrics(scroller, 500, 1100); + scroller.scrollLeft = 600; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("1099px"); + + // Scrolling back left moves the spacer left so the range can shrink. + scroller.scrollLeft = 100; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("599px"); }); it("removes the spacer with the editor", () => { diff --git a/src/editor/core/cm/horizontalScrollHold.ts b/src/editor/core/cm/horizontalScrollHold.ts index 4d224b371f..0c11ad7c21 100644 --- a/src/editor/core/cm/horizontalScrollHold.ts +++ b/src/editor/core/cm/horizontalScrollHold.ts @@ -29,8 +29,11 @@ export const horizontalScrollHold = [ } private edge(): number { - const { scrollLeft, clientWidth } = this.view.scrollDOM; - return Math.max(0, scrollLeft + clientWidth - 1); + const { scrollLeft, clientWidth, scrollWidth } = this.view.scrollDOM; + if (scrollLeft <= 0 || scrollWidth <= clientWidth) return 0; + // Clamp to the current scrollWidth so elastic overscroll or fractional scrollLeft + // cannot push the 1px spacer past the end and grow the scroll range (#7270). + return Math.max(0, Math.min(Math.ceil(scrollLeft + clientWidth), scrollWidth) - 1); } private put(x: number) {