diff --git a/README.md b/README.md index 15e5d53..1da10e7 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,9 @@ this.printService.printStyle = styleSheet; this.printService.styleSheetFile = fileLocation; ``` +## Content-Security-Policy (CSP) Support +If Angular is configured to use a [CSP Nonce](https://angular.io/api/core/CSP_NONCE), ngx-print will automatically inject the `[printStyle]` CSS rules with this Nonce authorization. + ## Contributors :1st_place_medal: Huge thanks to: [deeplotia](https://github.com/deeplotia) , [Ben L](https://github.com/broem) , [Gavyn McKenzie](https://github.com/gavmck) , [silenceway](https://github.com/silenceway), [Muhammad Ahsan Ayaz](https://github.com/AhsanAyaz), [Core121](https://github.com/Core121) and to all `ngx-print` users 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 ``; } /** @@ -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: `