GeezerVison is showing github.com — fix(common): Prefer to use pageXOffset / pageYOffset instance of scro… · angular/angular@5692607
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: aio/src/app/shared/scroll.service.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,7 @@ export class ScrollService implements OnDestroy { | 24 | 24 | poppedStateScrollPosition: ScrollPosition|null = null; | 25 | 25 | // Whether the browser supports the necessary features for manual scroll restoration. | 26 | 26 | supportManualScrollRestoration: boolean = !!window && ('scrollTo' in window) && | 27 | | - ('scrollX' in window) && ('scrollY' in window) && isScrollRestorationWritable(); | | 27 | + ('pageXOffset' in window) && isScrollRestorationWritable(); | 28 | 28 |
| 29 | 29 | // Offset from the top of the document to bottom of any static elements | 30 | 30 | // at the top (e.g. toolbar) + some margin | |
Collapse file Copy file name to clipboardExpand all lines: packages/common/src/viewport_scroller.ts +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,7 +89,7 @@ export class BrowserViewportScroller implements ViewportScroller { | 89 | 89 | */ | 90 | 90 | getScrollPosition(): [number, number] { | 91 | 91 | if (this.supportsScrolling()) { | 92 | | - return [this.window.scrollX, this.window.scrollY]; | | 92 | + return [this.window.pageXOffset, this.window.pageYOffset]; | 93 | 93 | } else { | 94 | 94 | return [0, 0]; | 95 | 95 | } | @@ -149,7 +149,7 @@ export class BrowserViewportScroller implements ViewportScroller { | 149 | 149 | */ | 150 | 150 | private supportScrollRestoration(): boolean { | 151 | 151 | try { | 152 | | - if (!this.window || !this.window.scrollTo) { | | 152 | + if (!this.supportsScrolling()) { | 153 | 153 | return false; | 154 | 154 | } | 155 | 155 | // The `scrollRestoration` property could be on the `history` instance or its prototype. | @@ -166,7 +166,7 @@ export class BrowserViewportScroller implements ViewportScroller { | 166 | 166 |
| 167 | 167 | private supportsScrolling(): boolean { | 168 | 168 | try { | 169 | | - return !!this.window.scrollTo; | | 169 | + return !!this.window && !!this.window.scrollTo && 'pageXOffset' in this.window; | 170 | 170 | } catch { | 171 | 171 | return false; | 172 | 172 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/common/test/viewport_scroller_spec.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,8 @@ describe('BrowserViewportScroller', () => { | 15 | 15 | let windowSpy: any; | 16 | 16 |
| 17 | 17 | beforeEach(() => { | 18 | | - windowSpy = jasmine.createSpyObj('window', ['history', 'scrollTo']); | | 18 | + windowSpy = | | 19 | + jasmine.createSpyObj('window', ['history', 'scrollTo', 'pageXOffset', 'pageYOffset']); | 19 | 20 | windowSpy.history.scrollRestoration = 'auto'; | 20 | 21 | documentSpy = jasmine.createSpyObj('document', ['getElementById', 'getElementsByName']); | 21 | 22 | scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!); | |
Collapse file Copy file name to clipboardExpand all lines: packages/router/test/bootstrap.spec.ts +7-10Lines changed: 7 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -348,27 +348,24 @@ describe('bootstrap', () => { | 348 | 348 | await router.navigateByUrl('/aa'); | 349 | 349 | window.scrollTo(0, 5000); | 350 | 350 |
| 351 | | - // IE 11 uses non-standard pageYOffset instead of scrollY | 352 | | - const getScrollY = () => window.scrollY !== undefined ? window.scrollY : window.pageYOffset; | 353 | | - | 354 | 351 | await router.navigateByUrl('/fail'); | 355 | | - expect(getScrollY()).toEqual(5000); | | 352 | + expect(window.pageYOffset).toEqual(5000); | 356 | 353 |
| 357 | 354 | await router.navigateByUrl('/bb'); | 358 | 355 | window.scrollTo(0, 3000); | 359 | 356 |
| 360 | | - expect(getScrollY()).toEqual(3000); | | 357 | + expect(window.pageYOffset).toEqual(3000); | 361 | 358 |
| 362 | 359 | await router.navigateByUrl('/cc'); | 363 | | - expect(getScrollY()).toEqual(0); | | 360 | + expect(window.pageYOffset).toEqual(0); | 364 | 361 |
| 365 | 362 | await router.navigateByUrl('/aa#marker2'); | 366 | | - expect(getScrollY()).toBeGreaterThanOrEqual(5900); | 367 | | - expect(getScrollY()).toBeLessThan(6000); // offset | | 363 | + expect(window.pageYOffset).toBeGreaterThanOrEqual(5900); | | 364 | + expect(window.pageYOffset).toBeLessThan(6000); // offset | 368 | 365 |
| 369 | 366 | await router.navigateByUrl('/aa#marker3'); | 370 | | - expect(getScrollY()).toBeGreaterThanOrEqual(8900); | 371 | | - expect(getScrollY()).toBeLessThan(9000); | | 367 | + expect(window.pageYOffset).toBeGreaterThanOrEqual(8900); | | 368 | + expect(window.pageYOffset).toBeLessThan(9000); | 372 | 369 | done(); | 373 | 370 | }); | 374 | 371 |
| |
|