@@ -2712,6 +2712,7 @@ function pushStyle(
2712
2712
}
2713
2713
const precedence = props . precedence ;
2714
2714
const href = props . href ;
2715
+ const nonce = props . nonce ;
2715
2716
2716
2717
if (
2717
2718
insertionMode === SVG_MODE ||
@@ -2759,9 +2760,19 @@ function pushStyle(
2759
2760
rules : ( [ ] : Array < Chunk | PrecomputedChunk > ) ,
2760
2761
hrefs : [ stringToChunk ( escapeTextForBrowser ( href ) ) ] ,
2761
2762
sheets : ( new Map ( ) : Map < string , StylesheetResource > ) ,
2763
+ nonce ,
2762
2764
} ;
2763
2765
renderState . styles . set ( precedence , styleQueue ) ;
2764
2766
} else {
2767
+ if ( __DEV__ ) {
2768
+ if ( nonce !== styleQueue . nonce ) {
2769
+ console . error (
2770
+ 'React encountered a hoistable style tag with "%s" nonce. It doesn\'t match the previously encountered nonce "%s". They have to be the same' ,
2771
+ nonce ,
2772
+ styleQueue . nonce ,
2773
+ ) ;
2774
+ }
2775
+ }
2765
2776
// We have seen this precedence before and need to track this href
2766
2777
styleQueue . hrefs . push ( stringToChunk ( escapeTextForBrowser ( href ) ) ) ;
2767
2778
}
@@ -4684,8 +4695,9 @@ function escapeJSObjectForInstructionScripts(input: Object): string {
4684
4695
const lateStyleTagResourceOpen1 = stringToPrecomputedChunk (
4685
4696
'<style media="not all" data-precedence="' ,
4686
4697
) ;
4687
- const lateStyleTagResourceOpen2 = stringToPrecomputedChunk ( '" data-href="' ) ;
4688
- const lateStyleTagResourceOpen3 = stringToPrecomputedChunk ( '">' ) ;
4698
+ const lateStyleTagResourceOpen2 = stringToPrecomputedChunk ( '" nonce="' ) ;
4699
+ const lateStyleTagResourceOpen3 = stringToPrecomputedChunk ( '" data-href="' ) ;
4700
+ const lateStyleTagResourceOpen4 = stringToPrecomputedChunk ( '">' ) ;
4689
4701
const lateStyleTagTemplateClose = stringToPrecomputedChunk ( '</style>' ) ;
4690
4702
4691
4703
// Tracks whether the boundary currently flushing is flushign style tags or has any
@@ -4701,6 +4713,7 @@ function flushStyleTagsLateForBoundary(
4701
4713
) {
4702
4714
const rules = styleQueue . rules ;
4703
4715
const hrefs = styleQueue . hrefs ;
4716
+ const nonce = styleQueue . nonce ;
4704
4717
if ( __DEV__ ) {
4705
4718
if ( rules . length > 0 && hrefs . length === 0 ) {
4706
4719
console . error (
@@ -4712,13 +4725,17 @@ function flushStyleTagsLateForBoundary(
4712
4725
if ( hrefs . length ) {
4713
4726
writeChunk ( this , lateStyleTagResourceOpen1 ) ;
4714
4727
writeChunk ( this , styleQueue . precedence ) ;
4715
- writeChunk ( this , lateStyleTagResourceOpen2 ) ;
4728
+ if ( nonce ) {
4729
+ writeChunk ( this , lateStyleTagResourceOpen2 ) ;
4730
+ writeChunk ( this , nonce ) ;
4731
+ }
4732
+ writeChunk ( this , lateStyleTagResourceOpen3 ) ;
4716
4733
for ( ; i < hrefs . length - 1 ; i ++ ) {
4717
4734
writeChunk ( this , hrefs [ i ] ) ;
4718
4735
writeChunk ( this , spaceSeparator ) ;
4719
4736
}
4720
4737
writeChunk ( this , hrefs [ i ] ) ;
4721
- writeChunk ( this , lateStyleTagResourceOpen3 ) ;
4738
+ writeChunk ( this , lateStyleTagResourceOpen4 ) ;
4722
4739
for ( i = 0 ; i < rules . length ; i ++ ) {
4723
4740
writeChunk ( this , rules [ i ] ) ;
4724
4741
}
@@ -4805,9 +4822,10 @@ function flushStyleInPreamble(
4805
4822
const styleTagResourceOpen1 = stringToPrecomputedChunk (
4806
4823
'<style data-precedence="' ,
4807
4824
) ;
4808
- const styleTagResourceOpen2 = stringToPrecomputedChunk ( '" data-href="' ) ;
4825
+ const styleTagResourceOpen2 = stringToPrecomputedChunk ( '" nonce="' ) ;
4826
+ const styleTagResourceOpen3 = stringToPrecomputedChunk ( '" data-href="' ) ;
4809
4827
const spaceSeparator = stringToPrecomputedChunk ( ' ' ) ;
4810
- const styleTagResourceOpen3 = stringToPrecomputedChunk ( '">' ) ;
4828
+ const styleTagResourceOpen4 = stringToPrecomputedChunk ( '">' ) ;
4811
4829
4812
4830
const styleTagResourceClose = stringToPrecomputedChunk ( '</style>' ) ;
4813
4831
@@ -4822,22 +4840,27 @@ function flushStylesInPreamble(
4822
4840
4823
4841
const rules = styleQueue . rules ;
4824
4842
const hrefs = styleQueue . hrefs ;
4843
+ const nonce = styleQueue . nonce ;
4825
4844
// If we don't emit any stylesheets at this precedence we still need to maintain the precedence
4826
4845
// order so even if there are no rules for style tags at this precedence we emit an empty style
4827
4846
// tag with the data-precedence attribute
4828
4847
if ( ! hasStylesheets || hrefs . length ) {
4829
4848
writeChunk ( this , styleTagResourceOpen1 ) ;
4830
4849
writeChunk ( this , styleQueue . precedence ) ;
4850
+ if ( nonce ) {
4851
+ writeChunk ( this , styleTagResourceOpen2 ) ;
4852
+ writeChunk ( this , nonce ) ;
4853
+ }
4831
4854
let i = 0 ;
4832
4855
if ( hrefs . length ) {
4833
- writeChunk ( this , styleTagResourceOpen2 ) ;
4856
+ writeChunk ( this , styleTagResourceOpen3 ) ;
4834
4857
for ( ; i < hrefs . length - 1 ; i ++ ) {
4835
4858
writeChunk ( this , hrefs [ i ] ) ;
4836
4859
writeChunk ( this , spaceSeparator ) ;
4837
4860
}
4838
4861
writeChunk ( this , hrefs [ i ] ) ;
4839
4862
}
4840
- writeChunk ( this , styleTagResourceOpen3 ) ;
4863
+ writeChunk ( this , styleTagResourceOpen4 ) ;
4841
4864
for ( i = 0 ; i < rules . length ; i ++ ) {
4842
4865
writeChunk ( this , rules [ i ] ) ;
4843
4866
}
@@ -5534,6 +5557,7 @@ export type StyleQueue = {
5534
5557
rules : Array < Chunk | PrecomputedChunk > ,
5535
5558
hrefs : Array < Chunk | PrecomputedChunk > ,
5536
5559
sheets : Map < string , StylesheetResource> ,
5560
+ nonce : ?string ,
5537
5561
} ;
5538
5562
5539
5563
export function createHoistableState ( ) : HoistableState {
0 commit comments