RED-5546: update sanitize

This commit is contained in:
Dan Percic 2022-11-21 16:26:08 +02:00
parent c925503fc6
commit 2f10819460
2 changed files with 16 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { Injectable, SecurityContext } from '@angular/core';
import { Injectable } from '@angular/core';
import { ActiveToast, ToastrService } from 'ngx-toastr';
import { IndividualConfig } from 'ngx-toastr/toastr/toastr-config';
import { NavigationStart, Router } from '@angular/router';
@ -6,8 +6,8 @@ import { TranslateService } from '@ngx-translate/core';
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { filter } from 'rxjs/operators';
import { ErrorMessageService } from './error-message.service';
import { DomSanitizer } from '@angular/platform-browser';
import { stripHtml } from 'string-strip-html';
import { DomSanitizer } from '@angular/platform-browser';
const enum NotificationType {
SUCCESS = 'SUCCESS',
@ -25,14 +25,13 @@ export interface ToasterOptions extends IndividualConfig {
/**
* These params are used as interpolateParams for translate service
*/
// eslint-disable-next-line @typescript-eslint/ban-types
readonly params?: object;
readonly params?: Record<string, string | number>;
readonly actions?: ToasterActions[];
}
export interface ErrorToasterOptions extends ToasterOptions {
/**
* Pass an http error that will be processed by error message service and shown in toast
* Pass a http error that will be processed by error message service and shown in toast
*/
readonly error?: HttpErrorResponse;
}
@ -44,9 +43,9 @@ export class Toaster {
constructor(
private readonly _toastr: ToastrService,
private readonly _router: Router,
private readonly _domSanitize: DomSanitizer,
private readonly _translateService: TranslateService,
private readonly _errorMessageService: ErrorMessageService,
private readonly _sanitizer: DomSanitizer,
) {
_router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => {
_toastr.clear();
@ -81,19 +80,9 @@ export class Toaster {
notificationType = NotificationType.INFO,
options?: Partial<ToasterOptions>,
): ActiveToast<unknown> {
const sanitized: any = {};
const params = options?.params ? this._sanitizeParams(options.params) : undefined;
if (options?.params) {
const params: any = options?.params;
for (let key of Object.keys(params)) {
const value = params[key];
sanitized[key] = stripHtml(value).result;
}
}
console.log(sanitized);
const translatedMsg = this._translateService.instant(message, sanitized) as string;
const translatedMsg = this._translateService.instant(message, params) as string;
switch (notificationType) {
case NotificationType.SUCCESS:
@ -105,4 +94,11 @@ export class Toaster {
return this._toastr.info(translatedMsg, options?.title, options);
}
}
private _sanitizeParams(params: Record<string, string | number>): Record<string, string | null> {
return Object.entries(params).reduce((acc, [key, value]) => {
acc[key] = stripHtml(value.toString()).result;
return acc;
}, {} as Record<string, string | null>);
}
}

View File

@ -2,7 +2,9 @@
<div *ngIf="title" [attr.aria-label]="title" [class]="options.titleClass">
{{ title }}
</div>
<div *ngIf="message && options.enableHtml" [class]="options.messageClass" [innerHTML]="message" aria-live="polite" role="alert"></div>
<div *ngIf="message && !options.enableHtml" [attr.aria-label]="message" [class]="options.messageClass" aria-live="polite" role="alert">
{{ message }}
</div>