Skip to content

Commit ead7deb

Browse files
mmalerbajosephperrott
authored andcommitted
virtual-scroll: Avoid using bypassSecurityTrustStyle which is banned in google3 (#12181)
1 parent 739d25e commit ead7deb

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

src/cdk-experimental/scrolling/virtual-scroll-viewport.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Wrap the rendered content in an element that will be used to offset it based on the scroll
33
position.
44
-->
5-
<div #contentWrapper class="cdk-virtual-scroll-content-wrapper"
6-
[style.transform]="_renderedContentTransform">
5+
<div #contentWrapper class="cdk-virtual-scroll-content-wrapper">
76
<ng-content></ng-content>
87
</div>
98
<!--

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ describe('CdkVirtualScrollViewport', () => {
2323
viewport = testComponent.viewport;
2424
});
2525

26-
it('should sanitize transform inputs', fakeAsync(() => {
27-
finishInit(fixture);
28-
viewport.orientation = 'arbitrary string as orientation' as any;
29-
viewport.setRenderedContentOffset(
30-
'arbitrary string as offset' as any, 'arbitrary string as to' as any);
31-
fixture.detectChanges();
32-
flush();
33-
34-
expect((viewport._renderedContentTransform as any).changingThisBreaksApplicationSecurity)
35-
.toBe('translateY(NaNpx)');
36-
}));
37-
3826
it('should render initial state', fakeAsync(() => {
3927
finishInit(fixture);
4028

src/cdk-experimental/scrolling/virtual-scroll-viewport.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
ViewChild,
2121
ViewEncapsulation,
2222
} from '@angular/core';
23-
import {DomSanitizer, SafeStyle} from '@angular/platform-browser';
2423
import {animationFrameScheduler, fromEvent, Observable, Subject} from 'rxjs';
2524
import {sampleTime, take, takeUntil} from 'rxjs/operators';
2625
import {CdkVirtualForOf} from './virtual-for-of';
@@ -68,11 +67,8 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
6867
*/
6968
_totalContentSize = 0;
7069

71-
/** The transform used to offset the rendered content wrapper element. */
72-
_renderedContentTransform: SafeStyle;
73-
74-
/** The raw string version of the rendered content transform. */
75-
private _rawRenderedContentTransform: string;
70+
/** The the rendered content transform. */
71+
private _renderedContentTransform: string;
7672

7773
/** The currently rendered range of indices. */
7874
private _renderedRange: ListRange = {start: 0, end: 0};
@@ -107,8 +103,9 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
107103
/** A list of functions to run after the next change detection cycle. */
108104
private _runAfterChangeDetection: Function[] = [];
109105

110-
constructor(public elementRef: ElementRef, private _changeDetectorRef: ChangeDetectorRef,
111-
private _ngZone: NgZone, private _sanitizer: DomSanitizer,
106+
constructor(public elementRef: ElementRef,
107+
private _changeDetectorRef: ChangeDetectorRef,
108+
private _ngZone: NgZone,
112109
@Inject(VIRTUAL_SCROLL_STRATEGY) private _scrollStrategy: VirtualScrollStrategy) {}
113110

114111
ngOnInit() {
@@ -223,17 +220,16 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
223220
let transform = `translate${axis}(${Number(offset)}px)`;
224221
this._renderedContentOffset = offset;
225222
if (to === 'to-end') {
226-
// TODO(mmalerba): The viewport should rewrite this as a `to-start` offset on the next render
227-
// cycle. Otherwise elements will appear to expand in the wrong direction (e.g.
228-
// `mat-expansion-panel` would expand upward).
229223
transform += ` translate${axis}(-100%)`;
224+
// The viewport should rewrite this as a `to-start` offset on the next render cycle. Otherwise
225+
// elements will appear to expand in the wrong direction (e.g. `mat-expansion-panel` would
226+
// expand upward).
230227
this._renderedContentOffsetNeedsRewrite = true;
231228
}
232-
if (this._rawRenderedContentTransform != transform) {
229+
if (this._renderedContentTransform != transform) {
233230
// We know this value is safe because we parse `offset` with `Number()` before passing it
234231
// into the string.
235-
this._rawRenderedContentTransform = transform;
236-
this._renderedContentTransform = this._sanitizer.bypassSecurityTrustStyle(transform);
232+
this._renderedContentTransform = transform;
237233
this._markChangeDetectionNeeded(() => {
238234
if (this._renderedContentOffsetNeedsRewrite) {
239235
this._renderedContentOffset -= this.measureRenderedContentSize();
@@ -317,6 +313,11 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
317313

318314
// Apply changes to Angular bindings.
319315
this._ngZone.run(() => this._changeDetectorRef.detectChanges());
316+
// Apply the content transform. The transform can't be set via an Angular binding because
317+
// bypassSecurityTrustStyle is banned in Google. However the value is safe, it's composed of
318+
// string literals, a variable that can only be 'X' or 'Y', and user input that is run through
319+
// the `Number` function first to coerce it to a numeric value.
320+
this._contentWrapper.nativeElement.style.transform = this._renderedContentTransform;
320321
// Apply the pending scroll offset separately, since it can't be set up as an Angular binding.
321322
if (this._pendingScrollOffset != null) {
322323
if (this.orientation === 'horizontal') {

0 commit comments

Comments
 (0)