From 2759972df4d3fa2a9e7e297dae3be5287169b4bc Mon Sep 17 00:00:00 2001 From: Chris Umphress Date: Wed, 28 Feb 2024 22:17:54 -0600 Subject: [PATCH 1/2] Added support for Content-Security-Policy through Nonce injection. --- src/lib/ngx-print.base.ts | 9 ++++++--- src/lib/ngx-print.service.spec.ts | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/ngx-print.base.ts b/src/lib/ngx-print.base.ts index ec9706d..e071280 100644 --- a/src/lib/ngx-print.base.ts +++ b/src/lib/ngx-print.base.ts @@ -1,4 +1,4 @@ -import { Injectable } from "@angular/core"; +import { CSP_NONCE, Inject, Injectable, Optional } from "@angular/core"; import { PrintOptions } from "./print-options"; @Injectable({ @@ -9,6 +9,8 @@ export class PrintBase { private _printStyle: string[] = []; private _styleSheetFile: string = ''; + constructor(@Inject(CSP_NONCE) @Optional() private nonce?: string | null) {} + //#region Getters and Setters /** * Sets the print styles based on the provided values. @@ -34,7 +36,8 @@ export class PrintBase { * -join/replace to transform an array objects to css-styled string */ public returnStyleValues() { - return ``; + const styleNonce = this.nonce ? ` nonce="${this.nonce}"` : ''; + return ` ${this._printStyle.join(' ').replace(/,/g, ';')} `; } /** @@ -196,7 +199,7 @@ export class PrintBase { links = this.getElementTag('link'); } - // If the openNewTab option is set to true, then set the popOut option to an empty string. + // If the openNewTab option is set to true, then set the popOut option to an empty string. // This will cause the print dialog to open in a new tab. if (printOptions.openNewTab) { popOut = ''; diff --git a/src/lib/ngx-print.service.spec.ts b/src/lib/ngx-print.service.spec.ts index 3b2a667..a52d5e6 100644 --- a/src/lib/ngx-print.service.spec.ts +++ b/src/lib/ngx-print.service.spec.ts @@ -1,8 +1,11 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { NgxPrintService } from './ngx-print.service'; -import { Component } from '@angular/core'; +import { Component, CSP_NONCE } from '@angular/core'; import { PrintOptions } from './print-options'; + +const testNonce = 'dummy-nonce-value'; + @Component({ template: `