@@ -37,55 +37,62 @@ interface NodeUrlModule {
3737 parse ( url : string ) : NodeUrlParseResult ;
3838}
3939
40- let protocolForUrl : ( url : string ) => string ;
41-
42- if (
43- typeof URL === 'object' &&
44- URL !== null &&
45- // this is super annoying, TS thinks that URL **must** be a function so `URL.parse` check
46- // thinks it is `never` without this `as unknown as any`
47- typeof ( URL as unknown as any ) . parse === 'function'
48- ) {
49- // In Ember-land the `fastboot` package sets the `URL` global to `require('url')`
50- // ultimately, this should be changed (so that we can either rely on the natural `URL` global
51- // that exists) but for now we have to detect the specific `FastBoot` case first
52- //
53- // a future version of `fastboot` will detect if this legacy URL setup is required (by
54- // inspecting Ember version) and if new enough, it will avoid shadowing the `URL` global
55- // constructor with `require('url')`.
56- let nodeURL = URL as NodeUrlModule ;
57-
58- protocolForUrl = ( url : string ) => {
59- let protocol = null ;
60-
61- if ( typeof url === 'string' ) {
62- protocol = nodeURL . parse ( url ) . protocol ;
63- }
40+ function findProtocolForURL ( ) {
41+ if (
42+ typeof URL === 'object' &&
43+ URL !== null &&
44+ // this is super annoying, TS thinks that URL **must** be a function so `URL.parse` check
45+ // thinks it is `never` without this `as unknown as any`
46+ typeof ( ( URL as unknown ) as any ) . parse === 'function'
47+ ) {
48+ // In Ember-land the `fastboot` package sets the `URL` global to `require('url')`
49+ // ultimately, this should be changed (so that we can either rely on the natural `URL` global
50+ // that exists) but for now we have to detect the specific `FastBoot` case first
51+ //
52+ // a future version of `fastboot` will detect if this legacy URL setup is required (by
53+ // inspecting Ember version) and if new enough, it will avoid shadowing the `URL` global
54+ // constructor with `require('url')`.
55+ let nodeURL = URL as NodeUrlModule ;
56+
57+ return ( url : string ) => {
58+ let protocol = null ;
59+
60+ if ( typeof url === 'string' ) {
61+ protocol = nodeURL . parse ( url ) . protocol ;
62+ }
63+
64+ return protocol === null ? ':' : protocol ;
65+ } ;
66+ } else if ( typeof URL === 'function' ) {
67+ return ( _url : string ) => {
68+ try {
69+ let url = new URL ( _url ) ;
70+
71+ return url . protocol ;
72+ } catch ( error ) {
73+ // any non-fully qualified url string will trigger an error (because there is no
74+ // baseURI that we can provide; in that case we **know** that the protocol is
75+ // "safe" because it isn't specifically one of the `badProtocols` listed above
76+ // (and those protocols can never be the default baseURI)
77+ return ':' ;
78+ }
79+ } ;
80+ } else {
81+ // fallback for IE11 support
82+ let parsingNode = document . createElement ( 'a' ) ;
83+ return ( url : string ) => {
84+ parsingNode . href = url ;
85+ return parsingNode . protocol ;
86+ } ;
87+ }
88+ }
6489
65- return protocol === null ? ':' : protocol ;
66- } ;
67- } else if ( typeof URL === 'function' ) {
68- protocolForUrl = ( _url : string ) => {
69- try {
70- let url = new URL ( _url ) ;
71-
72- return url . protocol ;
73- } catch ( error ) {
74- // any non-fully qualified url string will trigger an error (because there is no
75- // baseURI that we can provide; in that case we **know** that the protocol is
76- // "safe" because it isn't specifically one of the `badProtocols` listed above
77- // (and those protocols can never be the default baseURI)
78- return ':' ;
79- }
80- } ;
81- } else {
82- // fallback for IE11 support
83- let parsingNode = document . createElement ( 'a' ) ;
84-
85- protocolForUrl = ( url : string ) => {
86- parsingNode . href = url ;
87- return parsingNode . protocol ;
88- } ;
90+ let _protocolForUrlImplementation : typeof protocolForUrl | undefined ;
91+ function protocolForUrl ( url : string ) : string {
92+ if ( ! _protocolForUrlImplementation ) {
93+ _protocolForUrlImplementation = findProtocolForURL ( ) ;
94+ }
95+ return _protocolForUrlImplementation ( url ) ;
8996}
9097
9198export function sanitizeAttributeValue (
0 commit comments