@@ -17,7 +17,7 @@ import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG, platformDynamicServer, PlatformSt
17
17
import { Observable } from 'rxjs' ;
18
18
import { first } from 'rxjs/operators' ;
19
19
20
- import { renderApplication } from '../src/utils' ;
20
+ import { renderApplication , SERVER_CONTEXT } from '../src/utils' ;
21
21
22
22
function createMyServerApp ( standalone : boolean ) {
23
23
@Component ( {
@@ -696,7 +696,7 @@ describe('platform-server integration', () => {
696
696
let doc : string ;
697
697
let called : boolean ;
698
698
let expectedOutput =
699
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!<h1 textcontent="fine">fine</h1></app></body></html>' ;
699
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!<h1 textcontent="fine">fine</h1></app></body></html>' ;
700
700
701
701
beforeEach ( ( ) => {
702
702
// PlatformConfig takes in a parsed document so that it can be cached across requests.
@@ -713,11 +713,17 @@ describe('platform-server integration', () => {
713
713
714
714
platform . bootstrapModule ( AsyncServerModule )
715
715
. then ( ( moduleRef ) => {
716
- const applicationRef : ApplicationRef = moduleRef . injector . get ( ApplicationRef ) ;
716
+ const applicationRef = moduleRef . injector . get ( ApplicationRef ) ;
717
717
return applicationRef . isStable . pipe ( first ( ( isStable : boolean ) => isStable ) )
718
718
. toPromise ( ) ;
719
719
} )
720
720
. then ( ( b ) => {
721
+ // Note: the `ng-server-context` is not present in this output, since
722
+ // `renderModule` or `renderApplication` functions are not used here.
723
+ const expectedOutput =
724
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
725
+ 'Works!<h1 textcontent="fine">fine</h1></app></body></html>' ;
726
+
721
727
expect ( platform . injector . get ( PlatformState ) . renderToString ( ) ) . toBe ( expectedOutput ) ;
722
728
platform . destroy ( ) ;
723
729
called = true ;
@@ -772,7 +778,8 @@ describe('platform-server integration', () => {
772
778
. then ( output => {
773
779
expect ( output ) . toBe (
774
780
'<html><head><title>fakeTitle</title></head>' +
775
- '<body><app ng-version="0.0.0-PLACEHOLDER">Works!<h1 textcontent="fine">fine</h1></app>' +
781
+ '<body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other">' +
782
+ 'Works!<h1 textcontent="fine">fine</h1></app>' +
776
783
'<!--test marker--></body></html>' ) ;
777
784
called = true ;
778
785
} )
@@ -789,7 +796,7 @@ describe('platform-server integration', () => {
789
796
renderModule ( SVGServerModule , options ) ;
790
797
bootstrap . then ( output => {
791
798
expect ( output ) . toBe (
792
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
799
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >' +
793
800
'<svg><use xlink:href="#clear"></use></svg></app></body></html>' ) ;
794
801
called = true ;
795
802
} ) ;
@@ -830,7 +837,69 @@ describe('platform-server integration', () => {
830
837
renderModule ( ExampleStylesModule , options ) ;
831
838
bootstrap . then ( output => {
832
839
expect ( output ) . toMatch (
833
- / < h t m l > < h e a d > < s t y l e n g - t r a n s i t i o n = " e x a m p l e - s t y l e s " > d i v \[ _ n g c o n t e n t - s c \d + \] { color: b l u e ; } \[ _ n g h o s t - s c \d + \] { c o l o r : r e d ; } < \/ s t y l e > < \/ h e a d > < b o d y > < a p p _ n g h o s t - s c \d + = " " n g - v e r s i o n = " 0 .0 .0 - P L A C E H O L D E R " > < d i v _ n g c o n t e n t - s c \d + = " " > W o r k s ! < \/ d i v > < \/ a p p > < \/ b o d y > < \/ h t m l > / ) ;
840
+ / < h t m l > < h e a d > < s t y l e n g - t r a n s i t i o n = " e x a m p l e - s t y l e s " > d i v \[ _ n g c o n t e n t - s c \d + \] { color: b l u e ; } \[ _ n g h o s t - s c \d + \] { c o l o r : r e d ; } < \/ s t y l e > < \/ h e a d > < b o d y > < a p p _ n g h o s t - s c \d + = " " n g - v e r s i o n = " 0 .0 .0 - P L A C E H O L D E R " n g - s e r v e r - c o n t e x t = " o t h e r " > < d i v _ n g c o n t e n t - s c \d + = " " > W o r k s ! < \/ d i v > < \/ a p p > < \/ b o d y > < \/ h t m l > / ) ;
841
+ called = true ;
842
+ } ) ;
843
+ } ) ) ;
844
+
845
+ it ( 'adds the `ng-server-context` attribute to host elements' , waitForAsync ( ( ) => {
846
+ const options = {
847
+ document : doc ,
848
+ } ;
849
+ const providers = [ {
850
+ provide : SERVER_CONTEXT ,
851
+ useValue : 'ssg' ,
852
+ } ] ;
853
+ const bootstrap = isStandalone ?
854
+ renderApplication (
855
+ MyStylesAppStandalone ,
856
+ { ...options , platformProviders : providers , appId : 'example-styles' } ) :
857
+ renderModule ( ExampleStylesModule , { ...options , extraProviders : providers } ) ;
858
+ bootstrap . then ( output => {
859
+ expect ( output ) . toMatch (
860
+ / < h t m l > < h e a d > < s t y l e n g - t r a n s i t i o n = " e x a m p l e - s t y l e s " > d i v \[ _ n g c o n t e n t - s c \d + \] { color: b l u e ; } \[ _ n g h o s t - s c \d + \] { c o l o r : r e d ; } < \/ s t y l e > < \/ h e a d > < b o d y > < a p p _ n g h o s t - s c \d + = " " n g - v e r s i o n = " 0 .0 .0 - P L A C E H O L D E R " n g - s e r v e r - c o n t e x t = " s s g " > < d i v _ n g c o n t e n t - s c \d + = " " > W o r k s ! < \/ d i v > < \/ a p p > < \/ b o d y > < \/ h t m l > / ) ;
861
+ called = true ;
862
+ } ) ;
863
+ } ) ) ;
864
+
865
+ it ( 'sanitizes the `serverContext` value' , waitForAsync ( ( ) => {
866
+ const options = {
867
+ document : doc ,
868
+ } ;
869
+ const providers = [ {
870
+ provide : SERVER_CONTEXT ,
871
+ useValue : '!!!Some extra chars&& --><!--' ,
872
+ } ] ;
873
+ const bootstrap = isStandalone ?
874
+ renderApplication (
875
+ MyStylesAppStandalone ,
876
+ { ...options , platformProviders : providers , appId : 'example-styles' } ) :
877
+ renderModule ( ExampleStylesModule , { ...options , extraProviders : providers } ) ;
878
+ bootstrap . then ( output => {
879
+ // All symbols other than [a-zA-Z0-9\-] are removed
880
+ expect ( output ) . toMatch ( / n g - s e r v e r - c o n t e x t = " S o m e e x t r a c h a r s - - - - " / ) ;
881
+ called = true ;
882
+ } ) ;
883
+ } ) ) ;
884
+
885
+ it ( 'uses `other` as the `serverContext` value when all symbols are removed after sanitization' ,
886
+ waitForAsync ( ( ) => {
887
+ const options = {
888
+ document : doc ,
889
+ } ;
890
+ const providers = [ {
891
+ provide : SERVER_CONTEXT ,
892
+ useValue : '!!! &&<>' ,
893
+ } ] ;
894
+ const bootstrap = isStandalone ?
895
+ renderApplication (
896
+ MyStylesAppStandalone ,
897
+ { ...options , platformProviders : providers , appId : 'example-styles' } ) :
898
+ renderModule ( ExampleStylesModule , { ...options , extraProviders : providers } ) ;
899
+ bootstrap . then ( output => {
900
+ // All symbols other than [a-zA-Z0-9\-] are removed,
901
+ // the `other` is used as the default.
902
+ expect ( output ) . toMatch ( / n g - s e r v e r - c o n t e x t = " o t h e r " / ) ;
834
903
called = true ;
835
904
} ) ;
836
905
} ) ) ;
@@ -842,7 +911,7 @@ describe('platform-server integration', () => {
842
911
renderModule ( FalseAttributesModule , options ) ;
843
912
bootstrap . then ( output => {
844
913
expect ( output ) . toBe (
845
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
914
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >' +
846
915
'<my-child ng-reflect-attr="false">Works!</my-child></app></body></html>' ) ;
847
916
called = true ;
848
917
} ) ;
@@ -855,7 +924,7 @@ describe('platform-server integration', () => {
855
924
renderModule ( NameModule , options ) ;
856
925
bootstrap . then ( output => {
857
926
expect ( output ) . toBe (
858
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
927
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >' +
859
928
'<input name=""></app></body></html>' ) ;
860
929
called = true ;
861
930
} ) ;
@@ -872,7 +941,7 @@ describe('platform-server integration', () => {
872
941
renderModule ( HTMLTypesModule , options ) ;
873
942
bootstrap . then ( output => {
874
943
expect ( output ) . toBe (
875
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
944
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >' +
876
945
'<div><b>foo</b> bar</div></app></body></html>' ) ;
877
946
called = true ;
878
947
} ) ;
@@ -885,7 +954,7 @@ describe('platform-server integration', () => {
885
954
renderModule ( HiddenModule , options ) ;
886
955
bootstrap . then ( output => {
887
956
expect ( output ) . toBe (
888
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">' +
957
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >' +
889
958
'<input hidden=""><input></app></body></html>' ) ;
890
959
called = true ;
891
960
} ) ;
@@ -902,7 +971,7 @@ describe('platform-server integration', () => {
902
971
// title should be added by the render hook.
903
972
expect ( output ) . toBe (
904
973
'<html><head><title>RenderHook</title></head><body>' +
905
- '<app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>' ) ;
974
+ '<app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</app></body></html>' ) ;
906
975
called = true ;
907
976
} ) ;
908
977
} ) ) ;
@@ -919,7 +988,7 @@ describe('platform-server integration', () => {
919
988
// title should be added by the render hook.
920
989
expect ( output ) . toBe (
921
990
'<html><head><title>RenderHook</title><meta name="description"></head>' +
922
- '<body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>' ) ;
991
+ '<body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</app></body></html>' ) ;
923
992
expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
924
993
called = true ;
925
994
} ) ;
@@ -936,7 +1005,7 @@ describe('platform-server integration', () => {
936
1005
// title should be added by the render hook.
937
1006
expect ( output ) . toBe (
938
1007
'<html><head><title>AsyncRenderHook</title></head><body>' +
939
- '<app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>' ) ;
1008
+ '<app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</app></body></html>' ) ;
940
1009
called = true ;
941
1010
} ) ;
942
1011
} ) ) ;
@@ -954,7 +1023,7 @@ describe('platform-server integration', () => {
954
1023
// title should be added by the render hook.
955
1024
expect ( output ) . toBe (
956
1025
'<html><head><meta name="description"><title>AsyncRenderHook</title></head>' +
957
- '<body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>' ) ;
1026
+ '<body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</app></body></html>' ) ;
958
1027
expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
959
1028
called = true ;
960
1029
} ) ;
@@ -1245,7 +1314,7 @@ describe('platform-server integration', () => {
1245
1314
describe ( 'ServerTransferStoreModule' , ( ) => {
1246
1315
let called = false ;
1247
1316
const defaultExpectedOutput =
1248
- '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app><script id="transfer-state" type="application/json">{&q;test&q;:10}</script></body></html>' ;
1317
+ '<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</app><script id="transfer-state" type="application/json">{&q;test&q;:10}</script></body></html>' ;
1249
1318
1250
1319
beforeEach ( ( ) => {
1251
1320
called = false ;
@@ -1278,7 +1347,7 @@ describe('platform-server integration', () => {
1278
1347
document : '<esc-app></esc-app>'
1279
1348
} ) . then ( output => {
1280
1349
expect ( output ) . toBe (
1281
- '<html><head></head><body><esc-app ng-version="0.0.0-PLACEHOLDER">Works!</esc-app>' +
1350
+ '<html><head></head><body><esc-app ng-version="0.0.0-PLACEHOLDER" ng-server-context="other" >Works!</esc-app>' +
1282
1351
'<script id="transfer-state" type="application/json">' +
1283
1352
'{&q;testString&q;:&q;&l;/script&g;&l;script&g;' +
1284
1353
'alert(&s;Hello&a;&s; + \\&q;World\\&q;);&q;}</script></body></html>' ) ;
0 commit comments