5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { Injectable } from '@angular/core' ;
8
+ import { Injectable , CSP_NONCE , Optional , Inject } from '@angular/core' ;
9
9
import { Platform } from '@angular/cdk/platform' ;
10
10
11
11
/** Global registry for all dynamically-created, injected media queries. */
@@ -20,7 +20,10 @@ export class MediaMatcher {
20
20
/** The internal matchMedia method to return back a MediaQueryList like object. */
21
21
private _matchMedia : ( query : string ) => MediaQueryList ;
22
22
23
- constructor ( private _platform : Platform ) {
23
+ constructor (
24
+ private _platform : Platform ,
25
+ @Optional ( ) @Inject ( CSP_NONCE ) private _nonce ?: string | null ,
26
+ ) {
24
27
this . _matchMedia =
25
28
this . _platform . isBrowser && window . matchMedia
26
29
? // matchMedia is bound to the window scope intentionally as it is an illegal invocation to
@@ -37,7 +40,7 @@ export class MediaMatcher {
37
40
*/
38
41
matchMedia ( query : string ) : MediaQueryList {
39
42
if ( this . _platform . WEBKIT || this . _platform . BLINK ) {
40
- createEmptyStyleRule ( query ) ;
43
+ createEmptyStyleRule ( query , this . _nonce ) ;
41
44
}
42
45
return this . _matchMedia ( query ) ;
43
46
}
@@ -52,14 +55,19 @@ export class MediaMatcher {
52
55
* inside the `@media` match existing elements on the page. We work around it by having one rule
53
56
* targeting the `body`. See https://github.com/angular/components/issues/23546.
54
57
*/
55
- function createEmptyStyleRule ( query : string ) {
58
+ function createEmptyStyleRule ( query : string , nonce : string | undefined | null ) {
56
59
if ( mediaQueriesForWebkitCompatibility . has ( query ) ) {
57
60
return ;
58
61
}
59
62
60
63
try {
61
64
if ( ! mediaQueryStyleNode ) {
62
65
mediaQueryStyleNode = document . createElement ( 'style' ) ;
66
+
67
+ if ( nonce ) {
68
+ mediaQueryStyleNode . nonce = nonce ;
69
+ }
70
+
63
71
mediaQueryStyleNode . setAttribute ( 'type' , 'text/css' ) ;
64
72
document . head ! . appendChild ( mediaQueryStyleNode ) ;
65
73
}
0 commit comments