@@ -98,11 +98,15 @@ export function startClientSideIsDarkLogic(params: {
9898 registerEffectAction : ( action : ( ) => void ) => void ;
9999 doPersistDarkModePreferenceWithCookie : boolean ;
100100 colorSchemeExplicitlyProvidedAsParameter : ColorScheme | "system" ;
101+ nonce ?: string ;
102+ trustedTypesPolicyName ?: string ;
101103} ) {
102104 const {
103105 doPersistDarkModePreferenceWithCookie,
104106 registerEffectAction,
105- colorSchemeExplicitlyProvidedAsParameter
107+ colorSchemeExplicitlyProvidedAsParameter,
108+ nonce,
109+ trustedTypesPolicyName = "react-dsfr"
106110 } = params ;
107111
108112 const { clientSideIsDark, ssrWasPerformedWithIsDark : ssrWasPerformedWithIsDark_ } = ( ( ) : {
@@ -174,6 +178,14 @@ export function startClientSideIsDarkLogic(params: {
174178
175179 ssrWasPerformedWithIsDark = ssrWasPerformedWithIsDark_ ;
176180
181+ const trustedTypes = ( window as any ) . trustedTypes ;
182+ const sanitizer =
183+ typeof trustedTypes !== "undefined"
184+ ? trustedTypes . createPolicy ( trustedTypesPolicyName , { createHTML : ( s : string ) => s } )
185+ : {
186+ createHTML : ( s : string ) => s
187+ } ;
188+
177189 $clientSideIsDark . current = clientSideIsDark ;
178190
179191 [ data_fr_scheme , data_fr_theme ] . forEach ( attr =>
@@ -228,7 +240,13 @@ export function startClientSideIsDarkLogic(params: {
228240
229241 element . id = rootColorSchemeStyleTagId ;
230242
231- element . innerHTML = `:root { color-scheme: ${ isDark ? "dark" : "light" } ; }` ;
243+ if ( nonce !== undefined ) {
244+ element . setAttribute ( "nonce" , nonce ) ;
245+ }
246+
247+ element . innerHTML = sanitizer . createHTML (
248+ `:root { color-scheme: ${ isDark ? "dark" : "light" } ; }`
249+ ) ;
232250
233251 document . head . appendChild ( element ) ;
234252 } ;
0 commit comments