@@ -12,6 +12,13 @@ angular.module('material.core.theming', ['material.core.theming.palette'])
1212 * @description Provider to configure the `$mdTheming` service.
1313 */
1414
15+ /**
16+ * @ngdoc method
17+ * @name $mdThemingProvider#setNonce
18+ * @param {string } nonceValue The nonce to be added as an attribute to the theme style tags.
19+ * Setting a value allows the use CSP policy without using the unsafe-inline directive.
20+ */
21+
1522/**
1623 * @ngdoc method
1724 * @name $mdThemingProvider#setDefaultTheme
@@ -125,6 +132,9 @@ var VALID_HUE_VALUES = [
125132// Whether or not themes are to be generated on-demand (vs. eagerly).
126133var generateOnDemand = false ;
127134
135+ // Nonce to be added as an attribute to the generated themes style tags.
136+ var nonce = null ;
137+
128138function ThemingProvider ( $mdColorPalette ) {
129139 PALETTES = { } ;
130140 THEMES = { } ;
@@ -143,6 +153,9 @@ function ThemingProvider($mdColorPalette) {
143153 extendPalette : extendPalette ,
144154 theme : registerTheme ,
145155
156+ setNonce : function ( nonceValue ) {
157+ nonce = nonceValue ;
158+ } ,
146159 setDefaultTheme : function ( theme ) {
147160 defaultTheme = theme ;
148161 } ,
@@ -355,7 +368,7 @@ function ThemingProvider($mdColorPalette) {
355368 applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
356369 applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
357370 applyTheme . registered = registered ;
358- applyTheme . generateTheme = generateTheme ;
371+ applyTheme . generateTheme = function ( name ) { generateTheme ( name , nonce ) ; } ;
359372
360373 return applyTheme ;
361374
@@ -516,7 +529,7 @@ function generateAllThemes($injector) {
516529
517530 angular . forEach ( THEMES , function ( theme ) {
518531 if ( ! GENERATED [ theme . name ] ) {
519- generateTheme ( theme . name ) ;
532+ generateTheme ( theme . name , nonce ) ;
520533 }
521534 } ) ;
522535
@@ -581,7 +594,7 @@ function generateAllThemes($injector) {
581594 }
582595}
583596
584- function generateTheme ( name ) {
597+ function generateTheme ( name , nonce ) {
585598 var theme = THEMES [ name ] ;
586599 var head = document . head ;
587600 var firstChild = head ? head . firstElementChild : null ;
@@ -596,6 +609,9 @@ function generateTheme(name) {
596609 if ( styleContent ) {
597610 var style = document . createElement ( 'style' ) ;
598611 style . setAttribute ( 'md-theme-style' , '' ) ;
612+ if ( nonce ) {
613+ style . setAttribute ( 'nonce' , nonce ) ;
614+ }
599615 style . appendChild ( document . createTextNode ( styleContent ) ) ;
600616 head . insertBefore ( style , firstChild ) ;
601617 }
0 commit comments