Skip to content

Commit 17d452d

Browse files
hoonjicopybara-github
authored andcommitted
ADK changes
PiperOrigin-RevId: 843140933
1 parent ffd5e20 commit 17d452d

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

src/app/components/view-image-dialog/view-image-dialog.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
1919
import {MAT_DIALOG_DATA, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
2020
import {By} from '@angular/platform-browser';
2121

22-
22+
import {SAFE_VALUES_SERVICE} from '../../core/services/interfaces/safevalues';
23+
import {MockSafeValuesService} from '../../core/services/testing/mock-safevalues.service';
2324
import {ViewImageDialogComponent, ViewImageDialogData} from './view-image-dialog.component';
2425

2526
describe('ViewImageDialogComponent', () => {
@@ -37,6 +38,7 @@ describe('ViewImageDialogComponent', () => {
3738
providers: [
3839
{provide: MatDialogRef, useValue: mockDialogRef},
3940
{provide: MAT_DIALOG_DATA, useValue: mockDialogData},
41+
{provide: SAFE_VALUES_SERVICE, useClass: MockSafeValuesService},
4042
],
4143
}).compileComponents();
4244
});

src/app/components/view-image-dialog/view-image-dialog.component.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
19-
import {Component, Inject, OnInit} from '@angular/core';
18+
import {SAFE_VALUES_SERVICE} from '../../core/services/interfaces/safevalues';
19+
import {Component, inject, OnInit} from '@angular/core';
2020
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
21-
import {DomSanitizer, SafeHtml, SafeUrl} from '@angular/platform-browser';
21+
import {SafeHtml, SafeUrl} from '@angular/platform-browser';
2222

2323
export interface ViewImageDialogData {
2424
imageData: string|null;
@@ -35,10 +35,9 @@ export class ViewImageDialogComponent implements OnInit {
3535
// Flag to determine if the content is SVG
3636
isSvgContent: boolean = false;
3737

38-
constructor(
39-
public dialogRef: MatDialogRef<ViewImageDialogComponent>,
40-
@Inject(MAT_DIALOG_DATA) public data: ViewImageDialogData,
41-
private sanitizer: DomSanitizer) {}
38+
readonly dialogRef = inject(MatDialogRef<ViewImageDialogComponent>);
39+
readonly data = inject<ViewImageDialogData>(MAT_DIALOG_DATA);
40+
private readonly safeValuesService = inject(SAFE_VALUES_SERVICE);
4241

4342
/**
4443
* Lifecycle hook to initialize the component.
@@ -64,15 +63,15 @@ export class ViewImageDialogComponent implements OnInit {
6463
// Check if the data looks like SVG
6564
if (imageData.trim().includes('<svg')) {
6665
this.isSvgContent = true;
67-
this.displayContent = this.sanitizer.bypassSecurityTrustHtml(imageData);
66+
this.displayContent = this.safeValuesService.bypassSecurityTrustHtml(imageData);
6867
} else {
6968
// Assume it's base64 data if not SVG.
7069
// Ensure it has the correct data URI prefix.
7170
const prefix =
7271
imageData.startsWith('data:image/') ? '' : 'data:image/png;base64,';
7372
this.isSvgContent = false;
7473
this.displayContent =
75-
this.sanitizer.bypassSecurityTrustUrl(prefix + imageData);
74+
this.safeValuesService.bypassSecurityTrustUrl(prefix + imageData);
7675
}
7776
}
7877

@@ -82,4 +81,4 @@ export class ViewImageDialogComponent implements OnInit {
8281
close(): void {
8382
this.dialogRef.close();
8483
}
85-
}
84+
}

src/app/core/services/interfaces/safevalues.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717

1818
import {InjectionToken} from '@angular/core';
19-
import {SafeHtml} from '@angular/platform-browser';
20-
// 1p-ONLY-IMPORT
19+
import {SafeHtml, SafeUrl} from '@angular/platform-browser';
2120

2221
export const SAFE_VALUES_SERVICE = new InjectionToken<SafeValuesService>(
2322
'SafeValuesService',
@@ -37,6 +36,7 @@ declare interface SafeValuesServiceInterface {
3736
openBlobUrl(blob: Blob): Window | null;
3837
setAnchorHref(a: HTMLAnchorElement, url: string): void;
3938
bypassSecurityTrustHtml(value: string): SafeHtml;
39+
bypassSecurityTrustUrl(url: string): SafeUrl;
4040
openBase64InNewTab(dataUrl: string, mimeType: string): void;
4141
}
4242

@@ -58,6 +58,8 @@ export abstract class SafeValuesService implements SafeValuesServiceInterface {
5858

5959
abstract bypassSecurityTrustHtml(value: string): SafeHtml;
6060

61+
abstract bypassSecurityTrustUrl(url: string): SafeUrl;
62+
6163
openBase64InNewTab(dataUrl: string, mimeType: string) {
6264
try {
6365
if (!dataUrl) {

src/app/core/services/safevalues.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import {inject, Injectable} from '@angular/core';
19-
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
19+
import {DomSanitizer, SafeHtml, SafeUrl} from '@angular/platform-browser';
2020

2121
import {SafeValuesService} from './interfaces/safevalues';
2222

@@ -54,4 +54,8 @@ export class SafeValuesServiceImpl extends SafeValuesService {
5454
bypassSecurityTrustHtml(content: string): SafeHtml {
5555
return this.sanitizer.bypassSecurityTrustHtml(content);
5656
}
57+
58+
bypassSecurityTrustUrl(url: string): SafeUrl {
59+
return this.sanitizer.bypassSecurityTrustUrl(url);
60+
}
5761
}

src/app/core/services/testing/mock-safevalues.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import {SafeValuesService} from '../interfaces/safevalues';
2020

2121
/** Mock implementation of SafeValuesService. */
2222
@Injectable()
23-
export class MockSafeValuesService implements Partial<SafeValuesService> {
23+
export class MockSafeValuesService implements SafeValuesService {
2424
windowOpen = jasmine.createSpy('windowOpen');
2525
createObjectUrl = jasmine.createSpy('createObjectUrl');
2626
openBlobUrl = jasmine.createSpy('openBlobUrl');
2727
setAnchorHref = jasmine.createSpy('setAnchorHref');
2828
openBase64InNewTab = jasmine.createSpy('openBase64InNewTab');
2929
bypassSecurityTrustHtml = jasmine.createSpy('bypassSecurityTrustHtml');
30+
bypassSecurityTrustUrl = jasmine.createSpy('bypassSecurityTrustUrl');
3031
}

0 commit comments

Comments
 (0)