Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make feature discovery lazy #1538

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions packages/@glimmer/runtime/lib/dom/sanitized-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,62 @@ interface NodeUrlModule {
parse(url: string): NodeUrlParseResult;
}

let protocolForUrl: (url: string) => string;

if (
typeof URL === 'object' &&
URL !== null &&
// this is super annoying, TS thinks that URL **must** be a function so `URL.parse` check
// thinks it is `never` without this `as unknown as any`
typeof (URL as unknown as any).parse === 'function'
) {
// In Ember-land the `fastboot` package sets the `URL` global to `require('url')`
// ultimately, this should be changed (so that we can either rely on the natural `URL` global
// that exists) but for now we have to detect the specific `FastBoot` case first
//
// a future version of `fastboot` will detect if this legacy URL setup is required (by
// inspecting Ember version) and if new enough, it will avoid shadowing the `URL` global
// constructor with `require('url')`.
let nodeURL = URL as NodeUrlModule;

protocolForUrl = (url: string) => {
let protocol = null;

if (typeof url === 'string') {
protocol = nodeURL.parse(url).protocol;
}
function findProtocolForURL() {
if (
typeof URL === 'object' &&
URL !== null &&
// this is super annoying, TS thinks that URL **must** be a function so `URL.parse` check
// thinks it is `never` without this `as unknown as any`
typeof (URL as unknown as any).parse === 'function'
) {
// In Ember-land the `fastboot` package sets the `URL` global to `require('url')`
// ultimately, this should be changed (so that we can either rely on the natural `URL` global
// that exists) but for now we have to detect the specific `FastBoot` case first
//
// a future version of `fastboot` will detect if this legacy URL setup is required (by
// inspecting Ember version) and if new enough, it will avoid shadowing the `URL` global
// constructor with `require('url')`.
let nodeURL = URL as NodeUrlModule;

return (url: string) => {
let protocol = null;

if (typeof url === 'string') {
protocol = nodeURL.parse(url).protocol;
}

return protocol === null ? ':' : protocol;
};
} else if (typeof URL === 'function') {
return (_url: string) => {
try {
let url = new URL(_url);

return url.protocol;
} catch (error) {
// any non-fully qualified url string will trigger an error (because there is no
// baseURI that we can provide; in that case we **know** that the protocol is
// "safe" because it isn't specifically one of the `badProtocols` listed above
// (and those protocols can never be the default baseURI)
return ':';
}
};
} else {
// fallback for IE11 support
let parsingNode = document.createElement('a');
return (url: string) => {
parsingNode.href = url;
return parsingNode.protocol;
};
}
}

return protocol === null ? ':' : protocol;
};
} else if (typeof URL === 'function') {
protocolForUrl = (_url: string) => {
try {
let url = new URL(_url);

return url.protocol;
} catch (error) {
// any non-fully qualified url string will trigger an error (because there is no
// baseURI that we can provide; in that case we **know** that the protocol is
// "safe" because it isn't specifically one of the `badProtocols` listed above
// (and those protocols can never be the default baseURI)
return ':';
}
};
} else {
// fallback for IE11 support
let parsingNode = document.createElement('a');

protocolForUrl = (url: string) => {
parsingNode.href = url;
return parsingNode.protocol;
};
let _protocolForUrlImplementation: typeof protocolForUrl | undefined;
function protocolForUrl(url: string): string {
if (!_protocolForUrlImplementation) {
_protocolForUrlImplementation = findProtocolForURL();
}
return _protocolForUrlImplementation(url);
}

export function sanitizeAttributeValue(
Expand Down
Loading